From 3fd7b383b945d90f1612002285a35f354bbd1e8b Mon Sep 17 00:00:00 2001 From: ineslelin Date: Fri, 24 Jan 2025 21:45:01 +0100 Subject: [PATCH] add function to render text --- include/TextManager.h | 36 +++++++++++++++++++++++++ src/TextManager.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/include/TextManager.h b/include/TextManager.h index a2b607f..c8a8c96 100644 --- a/include/TextManager.h +++ b/include/TextManager.h @@ -2,8 +2,27 @@ #define CHICKENGAME_TEXTMANAGER_H #include +#include #include +enum DisplayOptions +{ + SOLID, + SHADED, + BLENDED, + LCD, +}; + +struct Color +{ + int r; + int g; + int b; + int a; +}; + +class GameInternal; + class TextManager { public: @@ -12,6 +31,8 @@ public: for (auto& it : this->font_cache) { TTF_CloseFont(it.second); } + + TTF_Quit(); } TextManager(TextManager const&) = delete; @@ -19,8 +40,23 @@ 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 + SDL_Surface* RenderText(GameInternal* game, std::string font, std::string text, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth); + std::map font_cache; + private: + SDL_Surface* RenderSolid(TTF_Font* font, std::string text, Color fg, int wrapWidth); + SDL_Surface* RenderShaded(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth); + SDL_Surface* RenderBlended(TTF_Font* font, std::string text, Color fg, int wrapWidth); + SDL_Surface* RenderLCD(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth); }; #endif //CHICKENGAME_TEXTMANAGER_H diff --git a/src/TextManager.cpp b/src/TextManager.cpp index cbb1c47..13aa15a 100644 --- a/src/TextManager.cpp +++ b/src/TextManager.cpp @@ -2,8 +2,14 @@ #include +#include "GameInternal.h" +#include "AssetManager.h" + 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()) { @@ -20,4 +26,60 @@ TTF_Font* TextManager::loadFont(const char *filepath) std::cout << "Loaded font at " << filepath << std::endl; return font; +} + +SDL_Surface* TextManager::RenderText(GameInternal* game, std::string font, std::string text, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth) +{ + TTF_Font* ttfFont = game->assets->getFont(font); + + switch(displayOptions) + { + case SOLID: + RenderSolid(font, text, fg, wrapWidth); + return; + case SHADED: + RenderShaded(font, text, fg, bg, wrapWidth); + return; + case BLENDED: + RenderBlended(font, text, fg, wrapWidth) + return; + case LCD: + RenderLCD(font, text, fg, bg, wrapWidth); + return; + default: + std::cerr << "Invalid display option!" << std::endl; + return; + } +} + +SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, Color fg, int wrapWidth) +{ + if(wrapWidth == -1) + return TTF_RenderText_Solid(font, text.c_str(), text.length(), fg); + + return TTF_RenderText_Solid_Wrapped(font, text.c_str(), text.length(), fg, wrapWidth); +} + +SDL_Surface* TextManager::RenderShaded(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth) +{ + if(wrapWidth == -1) + return TTF_RenderText_Shaded(font, text.c_str(), text.length(), fg, bg); + + return TTF_RenderText_Shaded_Wrapped(font, text.c_str(), text.length(), fg, bg, wrapWidth); +} + +SDL_Surface* TextManager::RenderBlended(TTF_Font* font, std::string text, Color fg, int wrapWidth) +{ + if(wrapWidth == -1) + return TTF_RenderText_Blended(font, text.c_str(), text.length(), fg); + + return TTF_RenderText_Blended_Wrapped(font, text.c_str(), text.length(), fg, wrapWidth); +} + +SDL_Surface* TextManager::RenderLCD(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth) +{ + if(wrapWidth == -1) + return TTF_RenderText_LCD(font, text.c_str(), text.length(), fg, bg); + + return TTF_RenderText_LCD_Wrapped(font, text.c_str(), text.length(), fg, bg, wrapWidth); } \ No newline at end of file