From 639c3bb38a3f688ac3da3d9665ecc9abbb47a39d Mon Sep 17 00:00:00 2001 From: ineslelin Date: Tue, 28 Jan 2025 17:49:51 +0100 Subject: [PATCH] drawing to screen now works --- include/TextManager.h | 17 +++++++------ src/GameInternal.cpp | 8 ++++++ src/TextManager.cpp | 59 ++++++++++++++++++++++++++++--------------- 3 files changed, 56 insertions(+), 28 deletions(-) diff --git a/include/TextManager.h b/include/TextManager.h index 29cd98b..bbd1ba7 100644 --- a/include/TextManager.h +++ b/include/TextManager.h @@ -5,8 +5,7 @@ #include #include #include - -#include +#include enum DisplayOptions { @@ -18,10 +17,10 @@ enum DisplayOptions struct Color { - int r; - int g; - int b; - int a; + Uint8 r; + Uint8 g; + Uint8 b; + Uint8 a; }; struct Rect @@ -37,7 +36,7 @@ class GameInternal; class TextManager { public: - TextManager() {} + TextManager() : font_cache() { } ~TextManager() { for (auto& it : this->font_cache) { TTF_CloseFont(it.second); @@ -49,6 +48,8 @@ public: TextManager(TextManager const&) = delete; void operator=(TextManager const&) = delete; + bool isTextRendered = false; + TTF_Font* loadFont(const char* filepath); // TODO: probably gotta change sdl surface since this is a wrapper func for the dev @@ -58,7 +59,7 @@ public: std::map font_cache; private: - SDL_Texture* CreateRenderedTexture(TTF_Font* font, std::string text, DisplayOptions displayOptions, SDL_Color fg, SDL_Color bg, int wrapWidth); + 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); SDL_Surface* RenderShaded(TTF_Font* font, std::string text, SDL_Color fg, SDL_Color bg, int wrapWidth); SDL_Surface* RenderBlended(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth); diff --git a/src/GameInternal.cpp b/src/GameInternal.cpp index 1e4eac3..f968738 100644 --- a/src/GameInternal.cpp +++ b/src/GameInternal.cpp @@ -4,6 +4,7 @@ #include "AssetManager.h" #include "RenderManager.h" #include +#include #include "SDL3/SDL_init.h" #include "SoundManager.h" #include "Entity.h" @@ -88,6 +89,13 @@ SDL_AppResult GameInternal::init(const char* title, int xpos, int ypos, int widt Mix_Volume(-1, MIX_MAX_VOLUME); Mix_AllocateChannels(16); + if(!TTF_Init()) + { + std::cout << "ERROR: SDL_TTF couldnt be initialized! " << SDL_GetError() << std::endl; + SDL_ClearError(); + return SDL_APP_FAILURE; + } + // loading sounds // assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav"); // assets->addSoundEffect("steps", "assets/sound/steps.wav"); diff --git a/src/TextManager.cpp b/src/TextManager.cpp index f8e7a0c..81fe2fc 100644 --- a/src/TextManager.cpp +++ b/src/TextManager.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "GameInternal.h" #include "AssetManager.h" @@ -11,21 +12,18 @@ using json = nlohmann::json; TTF_Font* TextManager::loadFont(const char *filepath) { - if(!TTF_Init()) - std::cerr << "Failed to initialize SDL_TTF'" << std::endl; - - auto it = this->font_cache.find(filepath); - - if (it != this->font_cache.end()) { - return it->second; - } +// auto it = this->font_cache.find(filepath); +// +// if (it != this->font_cache.end()) { +// return it->second; +// } auto font = TTF_OpenFont(filepath, 12); // setting fontsize to 12 for now if (font == NULL) std::cerr << "Couldn't load font '" << filepath << "'" << std::endl; - this->font_cache.emplace(filepath, font); +// this->font_cache.emplace(filepath, font); std::cout << "Loaded font at " << filepath << std::endl; @@ -41,7 +39,8 @@ void TextManager::RenderText(GameInternal* game, std::string font, std::string t SDL_FRect sdlSrcRect = { src.x, src.y, src.w, src.h }; SDL_FRect sdlDstRect = { dst.x, dst.y, dst.w, dst.h }; - SDL_RenderTexture(VEGO_Game().renderer, CreateRenderedTexture(ttfFont, text, displayOptions, sdlFg, sdlBg, wrapWidth), &sdlSrcRect, &sdlDstRect); + SDL_RenderTexture(game->renderer, CreateRenderedTexture(game, ttfFont, text, displayOptions, sdlFg, sdlBg, wrapWidth), &sdlSrcRect, &sdlDstRect); + SDL_RenderPresent(game->renderer); } void TextManager::RenderTextFromFile(GameInternal* game, std::string font, std::string filepath, int id, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth, Rect src, Rect dst) @@ -54,21 +53,41 @@ void TextManager::RenderTextFromFile(GameInternal* game, std::string font, std:: SDL_FRect sdlDstRect = { dst.x, dst.y, dst.w, dst.h }; std::ifstream f(filepath); - json data = json::parse(f);; + if (!f.is_open()) { + std::cerr << "Failed to open file: " << filepath << std::endl; + return; + } - auto it = std::find_if(data.begin(), data.end(), [id](const nlohmann::json& obj){ - return obj.contains("id") && obj["id"] == id; - }); + json data; + try { + data = json::parse(f); + } catch (const json::parse_error& e) { + std::cerr << "JSON parse error: " << e.what() << std::endl; + return; + } - if(it == data.end() || !it->contains("line")) - std::cerr << "Object with id " << id << " not found or 'line' not present!" << std::endl; + 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; + break; + } + } - std::string text = (*it)["line"]; + if(!found) + { + std::cerr << "Object with id " << id << " not found!" << std::endl; + return; + } - SDL_RenderTexture(VEGO_Game().renderer, CreateRenderedTexture(ttfFont, text, displayOptions, sdlFg, sdlBg, wrapWidth), &sdlSrcRect, &sdlDstRect); + SDL_RenderTexture(game->renderer, CreateRenderedTexture(game, ttfFont, text, displayOptions, sdlFg, sdlBg, wrapWidth), &sdlSrcRect, &sdlDstRect); + SDL_RenderPresent(game->renderer); } -SDL_Texture* TextManager::CreateRenderedTexture(TTF_Font* font, std::string text, DisplayOptions displayOptions, SDL_Color fg, SDL_Color bg, int wrapWidth) +SDL_Texture* TextManager::CreateRenderedTexture(GameInternal* game, TTF_Font* font, std::string text, DisplayOptions displayOptions, SDL_Color fg, SDL_Color bg, int wrapWidth) { SDL_Surface* surface = nullptr; @@ -94,7 +113,7 @@ SDL_Texture* TextManager::CreateRenderedTexture(TTF_Font* font, std::string text if(!surface) std::cerr << "Error when rendering text!" << std::endl; - return SDL_CreateTextureFromSurface(VEGO_Game().renderer, surface); + return SDL_CreateTextureFromSurface(game->renderer, surface); } SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth)