trmnl_sdl/sdl_helpers.h

91 lines
2.4 KiB
C++

#ifndef SDL_HELPERS_H_
#define SDL_HELPERS_H_
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include <string>
#include "json.hpp"
extern std::string default_font_name;
enum HorizontalAlign
{
HALIGN_LEFT,
HALIGN_CENTER,
HALIGN_RIGHT
};
NLOHMANN_JSON_SERIALIZE_ENUM(HorizontalAlign,
{
{HALIGN_LEFT, "left"},
{HALIGN_CENTER, "center"},
{HALIGN_RIGHT, "right"},
})
enum VerticalAlign
{
VALIGN_TOP,
VALIGN_CENTER,
VALIGN_BOTTOM
};
NLOHMANN_JSON_SERIALIZE_ENUM(VerticalAlign,
{
{VALIGN_TOP, "top"},
{VALIGN_CENTER, "center"},
{VALIGN_BOTTOM, "bottom"},
})
enum TextFit
{
FIT_NONE,
FIT_SHRINK,
FIT_AUTO
};
NLOHMANN_JSON_SERIALIZE_ENUM(TextFit,
{
{FIT_NONE, "none"},
{FIT_SHRINK, "shrink"},
{FIT_AUTO, "auto"},
})
// Call this before everything
// Prints its messages
bool init_sdl();
// Call this at the end only if init has passed
void clean_sdl();
// A simple way to get a font pointer to use
// Can return NULL
TTF_Font* get_font(int size, const std::string& filename = default_font_name);
// Returns a rect to use during bliting of 2 surfaces
// base - surface on which the other is applied to
// applied - the surface which will be applied to the other
// hint - rect to hint visible area from applied - can be NULL
SDL_Rect surface_align(const SDL_Surface* base, const SDL_Surface* applied,
HorizontalAlign halign, VerticalAlign valign, const SDL_Rect* hint = nullptr);
// Reads the file and tries to parse a JSON file with comments
// cfg - output json struct
// filename - filepath to open and read
// log - output errors to this ostream, silent if NULL
bool read_config_json(nlohmann::json& cfg, const std::string& filename, std::ostream* log = &std::cout);
// JSON Extractors - They do not override already set values if key is not present
void json_extract(const nlohmann::json& j, const std::string& key, std::string& out);
void json_extract(const nlohmann::json& j, const std::string& key, int& out);
void json_extract(const nlohmann::json& j, const std::string& key, bool& out);
void json_extract(const nlohmann::json& j, const std::string& key, HorizontalAlign& out);
void json_extract(const nlohmann::json& j, const std::string& key, VerticalAlign& out);
void json_extract(const nlohmann::json& j, const std::string& key, TextFit& out);
void json_extract(const nlohmann::json& j, const std::string& key, SDL_Color& out);
void json_extract(const nlohmann::json& j, const std::string& key, Uint8& out);
#endif // SDL_HELPERS_H_