From dc2e54eea3b4c5beb2a00fc2f5084f7ddcc359a4 Mon Sep 17 00:00:00 2001 From: ineslelin Date: Tue, 28 Jan 2025 19:09:58 +0100 Subject: [PATCH] technically a text_cache but maps throw segfaults for some reason --- include/TextManager.h | 6 +++--- src/TextManager.cpp | 13 +++++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/TextManager.h b/include/TextManager.h index bbd1ba7..235ac26 100644 --- a/include/TextManager.h +++ b/include/TextManager.h @@ -48,16 +48,16 @@ public: TextManager(TextManager const&) = delete; void operator=(TextManager const&) = delete; + std::map font_cache; + std::map text_cache; + bool isTextRendered = false; TTF_Font* loadFont(const char* filepath); - // TODO: probably gotta change sdl surface since this is a wrapper func for the dev void RenderText(GameInternal* game, std::string font, std::string text, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth, Rect src, Rect dst); void RenderTextFromFile(GameInternal* game, std::string font, std::string filepath, int id, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth, Rect src, Rect dst); - std::map font_cache; - private: SDL_Texture* CreateRenderedTexture(GameInternal* game, TTF_Font* font, std::string text, DisplayOptions displayOptions, SDL_Color fg, SDL_Color bg, int wrapWidth); SDL_Surface* RenderSolid(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth); diff --git a/src/TextManager.cpp b/src/TextManager.cpp index 81fe2fc..fdc549f 100644 --- a/src/TextManager.cpp +++ b/src/TextManager.cpp @@ -69,7 +69,6 @@ void TextManager::RenderTextFromFile(GameInternal* game, std::string font, std:: std::string text; bool found = false; for (auto it = data.begin(); it != data.end(); ++it) { - std::cout << "Key: " << it.key() << ", Value: " << it.value() << std::endl; if (it.key() == std::to_string(id)) { text = it.value(); found = true; @@ -89,6 +88,12 @@ void TextManager::RenderTextFromFile(GameInternal* game, std::string font, std:: SDL_Texture* TextManager::CreateRenderedTexture(GameInternal* game, TTF_Font* font, std::string text, DisplayOptions displayOptions, SDL_Color fg, SDL_Color bg, int wrapWidth) { +// auto it = this->text_cache.find(text); +// +// if (it != this->text_cache.end()) { +// return it->second; +// } + SDL_Surface* surface = nullptr; switch(displayOptions) @@ -113,7 +118,11 @@ SDL_Texture* TextManager::CreateRenderedTexture(GameInternal* game, TTF_Font* fo if(!surface) std::cerr << "Error when rendering text!" << std::endl; - return SDL_CreateTextureFromSurface(game->renderer, surface); + auto texture = SDL_CreateTextureFromSurface(game->renderer, surface); + +// text_cache[text] = texture; + + return texture; } SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth)