0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

function to read dialoglines from json file

This commit is contained in:
ineslelin 2025-01-25 13:00:18 +01:00
parent f5bbabab8d
commit dbfffa7448
2 changed files with 14 additions and 14 deletions

View File

@ -41,18 +41,8 @@ public:
TTF_Font* loadFont(const char* filepath);
/*
* font - to search for font
* text - text to display
* displayoptions - theres like 4 goddamn funcs, ill wrap them all in one
* fg + bg - solid only takes fg, rest also bg colors
* wraplength - manually set wraplength, set to 0 to wrap on newline chars, -1 to disable (again. 4 more funcs)
* */
// TODO: change the sdl surface, i just dont know what i gotta change it to lol
// TODO: probably gotta change sdl surface since this is a wrapper func for the dev
SDL_Surface* RenderText(GameInternal* game, std::string font, std::string text, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth);
/*
* takes everything + a filepath to a dialogfile and the id for the dialog
* */
SDL_Surface* RenderTextFromFile(GameInternal* game, std::string font, std::string filepath, int id, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth);
std::map<const char*, TTF_Font*> font_cache;

View File

@ -68,11 +68,21 @@ SDL_Surface* TextManager::RenderText(GameInternal* game, std::string font, std::
SDL_Surface* TextManager::RenderTextFromFile(GameInternal* game, std::string font, std::string filepath, int id, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth)
{
std::ifstream f(filepath);
json data = json::parse(f);
json data = json::parse(f);;
// logic to parse dialogline
if(!json::accept(data))
{
std::cerr << "JSON is invalid!" << std::endl;
}
std::string text = "placeholder so i dont get compile errors :3";
auto it = std::find_if(data.begin(), data.end(), [id](const nlohmann::json& obj){
return obj.contains("id") && obj["id"] == id;
});
if(it == data.end() || !it->contains("line"))
sdt::cerr << "Object with id " << id << " not found or 'line' not present!" << std::endl;
std::string text = (*it)["line"];
return RenderText(game, font, text, displayOptions, fg, bg, wrapWidth);
}