Added WidgetText. Fixed some things. Updated README

This commit is contained in:
2025-12-04 17:23:53 +02:00
parent 58abb91c64
commit 56e07e1080
8 changed files with 473 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ using namespace std;
using nlohmann::json;
map<pair<string, int>, TTF_Font*> font_map;
string default_font_name = "font.ttf";
bool init_sdl()
{
@@ -75,6 +76,49 @@ TTF_Font* get_font(int size, const string& filename)
}
}
SDL_Rect surface_align(const SDL_Surface* base, const SDL_Surface* applied,
HorizontalAlign halign, VerticalAlign valign)
{
SDL_Rect align = {.x = 0, .y = 0, .w = 0, .h = 0};
if ((nullptr == base) || (nullptr == applied))
{
return align;
}
switch (halign)
{
case HALIGN_LEFT:
align.x = 0;
break;
case HALIGN_CENTER:
align.x = base->w / 2 - applied->w / 2;
break;
case HALIGN_RIGHT:
align.x = base->w - applied->w;
break;
}
switch (valign)
{
case VALIGN_TOP:
align.y = 0;
break;
case VALIGN_CENTER:
align.y = base->h / 2 - applied->h / 2;
break;
case VALIGN_BOTTOM:
align.y = base->h - applied->h;
break;
}
return align;
}
bool read_config_json(json& cfg, const string& filename, ostream* log)
{
ifstream cfg_file(filename);
@@ -120,3 +164,74 @@ void json_extract(const nlohmann::json& j, const string& key, int& out)
out = j[key];
}
}
void json_extract(const nlohmann::json& j, const string& key, bool& out)
{
if (j.contains(key) && j[key].is_boolean())
{
out = j[key];
}
}
void json_extract(const json& j, const string& key, HorizontalAlign& out)
{
if (j.contains(key))
{
try
{
out = j[key].get<HorizontalAlign>();
}
catch(const std::exception& e)
{
}
}
}
void json_extract(const json& j, const string& key, VerticalAlign& out)
{
if (j.contains(key))
{
try
{
out = j[key].get<VerticalAlign>();
}
catch(const std::exception& e)
{
}
}
}
void json_extract(const json& j, const string& key, TextFit& out)
{
if (j.contains(key))
{
try
{
out = j[key].get<TextFit>();
}
catch(const std::exception& e)
{
}
}
}
void json_extract(const json& j, const string& key, SDL_Color& out)
{
if (j.contains(key) && j[key].is_object())
{
const json& j2 = j[key];
json_extract(j2, "r", out.r);
json_extract(j2, "g", out.g);
json_extract(j2, "b", out.b);
json_extract(j2, "a", out.a);
}
}
void json_extract(const nlohmann::json& j, const string& key, Uint8& out)
{
if (j.contains(key) && j[key].is_number_unsigned())
{
out = j[key];
}
}