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

technically a text_cache but maps throw segfaults for some reason

This commit is contained in:
ineslelin 2025-01-28 19:09:58 +01:00
parent 639c3bb38a
commit dc2e54eea3
2 changed files with 14 additions and 5 deletions

View File

@ -48,16 +48,16 @@ public:
TextManager(TextManager const&) = delete;
void operator=(TextManager const&) = delete;
std::map<const char*, TTF_Font*> font_cache;
std::map<std::string, SDL_Texture*> 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<const char*, TTF_Font*> 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);

View File

@ -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)