diff --git a/include/TextManager.h b/include/TextManager.h index 3e33fb0..a145e20 100644 --- a/include/TextManager.h +++ b/include/TextManager.h @@ -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 font_cache; diff --git a/src/TextManager.cpp b/src/TextManager.cpp index fe3d275..5ebe67e 100644 --- a/src/TextManager.cpp +++ b/src/TextManager.cpp @@ -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); }