diff --git a/.gitmodules b/.gitmodules index 6dd82fa..2cea83e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -17,3 +17,6 @@ path = docs/doxygen-awesome-css url = https://github.com/jothepro/doxygen-awesome-css.git +[submodule "extern/json"] + path = extern/json + url = https://github.com/nlohmann/json.git diff --git a/CMakeLists.txt b/CMakeLists.txt index d38fa27..e1a36b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,11 +25,13 @@ add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL) add_subdirectory(extern/tmxlite/tmxlite EXCLUDE_FROM_ALL) - file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp) add_library(${PROJECT_NAME} ${SOURCES}) -target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR}) +target_include_directories(${PROJECT_NAME} PUBLIC + ${ENGINE_INCLUDE_DIR} + ${ENGINE_SOURCE_DIR}/extern/json/include +) target_link_libraries(${PROJECT_NAME} PUBLIC # should be private when all SDL functionality has a wrapper SDL3::SDL3-static diff --git a/extern/json b/extern/json new file mode 160000 index 0000000..d0789e3 --- /dev/null +++ b/extern/json @@ -0,0 +1 @@ +Subproject commit d0789e365da6e410f52387f19eb218dc5b6ccbf2 diff --git a/include/TextManager.h b/include/TextManager.h index c8a8c96..3e33fb0 100644 --- a/include/TextManager.h +++ b/include/TextManager.h @@ -4,6 +4,7 @@ #include #include #include +#include enum DisplayOptions { @@ -49,14 +50,18 @@ public: * */ // 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); + /* + * 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; 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); + 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); + SDL_Surface* RenderLCD(TTF_Font* font, std::string text, SDL_Color fg, SDL_Color bg, int wrapWidth); }; #endif //CHICKENGAME_TEXTMANAGER_H diff --git a/src/TextManager.cpp b/src/TextManager.cpp index 13aa15a..fe3d275 100644 --- a/src/TextManager.cpp +++ b/src/TextManager.cpp @@ -1,10 +1,14 @@ #include "TextManager.h" #include +#include +#include #include "GameInternal.h" #include "AssetManager.h" +using json = nlohmann::json; + TTF_Font* TextManager::loadFont(const char *filepath) { if(!TTF_Init()) @@ -31,28 +35,49 @@ TTF_Font* TextManager::loadFont(const char *filepath) 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); + SDL_Surface* surface = nullptr; + + SDL_Color sdlFg = {fg.r, fg.g, fg.b, fg.a}; + SDL_Color sdlBg = {bg.r, bg.g, bg.b, bg.a}; switch(displayOptions) { case SOLID: - RenderSolid(font, text, fg, wrapWidth); - return; + surface = RenderSolid(ttfFont, text, sdlFg, wrapWidth); + break; case SHADED: - RenderShaded(font, text, fg, bg, wrapWidth); - return; + surface = RenderShaded(ttfFont, text, sdlFg, sdlBg, wrapWidth); + break; case BLENDED: - RenderBlended(font, text, fg, wrapWidth) - return; + surface = RenderBlended(ttfFont, text, sdlFg, wrapWidth); + break; case LCD: - RenderLCD(font, text, fg, bg, wrapWidth); - return; + surface = RenderLCD(ttfFont, text, sdlFg, sdlBg, wrapWidth); + break; default: std::cerr << "Invalid display option!" << std::endl; - return; + break; } + + if(!surface) + std::cerr << "Error when rendering text!" << std::endl; + + return surface; } -SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, Color fg, int wrapWidth) +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); + + // logic to parse dialogline + + std::string text = "placeholder so i dont get compile errors :3"; + + return RenderText(game, font, text, displayOptions, fg, bg, wrapWidth); +} + +SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth) { if(wrapWidth == -1) return TTF_RenderText_Solid(font, text.c_str(), text.length(), fg); @@ -60,7 +85,7 @@ SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, Color 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) +SDL_Surface* TextManager::RenderShaded(TTF_Font* font, std::string text, SDL_Color fg, SDL_Color bg, int wrapWidth) { if(wrapWidth == -1) return TTF_RenderText_Shaded(font, text.c_str(), text.length(), fg, bg); @@ -68,7 +93,7 @@ SDL_Surface* TextManager::RenderShaded(TTF_Font* font, std::string text, Color f 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) +SDL_Surface* TextManager::RenderBlended(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth) { if(wrapWidth == -1) return TTF_RenderText_Blended(font, text.c_str(), text.length(), fg); @@ -76,7 +101,7 @@ SDL_Surface* TextManager::RenderBlended(TTF_Font* font, std::string text, Color 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) +SDL_Surface* TextManager::RenderLCD(TTF_Font* font, std::string text, SDL_Color fg, SDL_Color bg, int wrapWidth) { if(wrapWidth == -1) return TTF_RenderText_LCD(font, text.c_str(), text.length(), fg, bg);