diff --git a/CMakeLists.txt b/CMakeLists.txt index 7040175..da2d4c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 4.2) project(Text-Adventure) -set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ui/window.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Common/fileCommon.cpp) +set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ui/window.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Common/fileCommon.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ui/Text.cpp) find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared) find_package(SDL3_ttf REQUIRED) @@ -16,4 +16,4 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Editor) #add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/) target_link_libraries(Text-Adventure PUBLIC Engine SDL3::SDL3 SDL3_ttf::SDL3_ttf) -target_include_directories(Text-Adventure PUBLIC ${CMAKE_SOURCE_DIR}/Extern ${CMAKE_SOURCE_DIR}/ui ${CMAKE_SOURCE_DIR}/Common) +target_include_directories(Text-Adventure PUBLIC ${CMAKE_SOURCE_DIR}/Extern ${CMAKE_SOURCE_DIR}/ui ${CMAKE_SOURCE_DIR}/Common) diff --git a/Common/fileCommon.h b/Common/fileCommon.h index b801893..d01c409 100644 --- a/Common/fileCommon.h +++ b/Common/fileCommon.h @@ -1,3 +1,5 @@ +#pragma once + #include #include #include "json.hpp" diff --git a/main.cpp b/main.cpp index c486146..a3dad46 100644 --- a/main.cpp +++ b/main.cpp @@ -10,17 +10,15 @@ #include #include #include +#include "Text.h" #include "fileCommon.h" #include "game.h" #include "window.h" Game game; Window window; -TTF_Font *font = nullptr; -SDL_Texture *font_texture = nullptr; - -fs::path fontPath("Resources/Pixelzone-E4MV4.ttf"); +Text text; void EditRooms() { @@ -62,8 +60,6 @@ void EditRooms() { SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { - SDL_Surface *text; - SDL_Color color = {255, 255, 255, SDL_ALPHA_OPAQUE}; if (SDL_Init(SDL_INIT_VIDEO) == false) { SDL_Log("SDL could not initialize! SDL error: %s\n", SDL_GetError()); @@ -75,20 +71,10 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { return result; } - if (!TTF_Init()) { - SDL_Log("Couldn't initialize SDL_ttf: %s\n", SDL_GetError()); - return SDL_APP_FAILURE; - } + result = text.OpenFont("Resources/Pixelzone-E4MV4.ttf"); - font = TTF_OpenFont(fs::absolute(fontPath).c_str(), 12.0f); - text = TTF_RenderText_Blended(font, "Hello World!", 0, color); - if (text) { - font_texture = SDL_CreateTextureFromSurface(window.GetRenderer(), text); - SDL_DestroySurface(text); - } - if (!font_texture) { - SDL_Log("Couldn't create text: %s\n", SDL_GetError()); - return SDL_APP_FAILURE; + if (result != SDL_APP_CONTINUE) { + return result; } if (!game) { @@ -101,6 +87,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { return SDL_APP_FAILURE; } + text.SetText("Hello There", &window); + return SDL_APP_CONTINUE; } @@ -132,15 +120,7 @@ SDL_AppResult SDL_AppIterate(void *appstate) { SDL_SetRenderDrawColor(window.GetRenderer(), 0, 0, 0, 255); // Set render draw color to black SDL_RenderClear(window.GetRenderer()); // Clear the renderer - int w = 0, h = 0; - SDL_FRect dst; - const float scale = 4.0f; - - /* Center the text and scale it up */ - SDL_GetRenderOutputSize(window.GetRenderer(), &w, &h); - SDL_SetRenderScale(window.GetRenderer(), scale, scale); - SDL_GetTextureSize(font_texture, &dst.w, &dst.h); - SDL_RenderTexture(window.GetRenderer(), font_texture, NULL, &dst); + text.Render(); SDL_RenderPresent(window.GetRenderer()); // Render the screen // game.Run(); @@ -152,7 +132,7 @@ SDL_AppResult SDL_AppIterate(void *appstate) { void SDL_AppQuit(void *appstate, SDL_AppResult result) { if (result == SDL_APP_SUCCESS) { window.Close(); - TTF_CloseFont(font); + text.CloseFont(); SDL_Quit(); } } diff --git a/ui/Text.cpp b/ui/Text.cpp new file mode 100644 index 0000000..88d05f2 --- /dev/null +++ b/ui/Text.cpp @@ -0,0 +1,53 @@ +#include "Text.h" +#include +#include + + +SDL_AppResult Text::OpenFont(fs::path fontPath) { + + if (!TTF_Init()) { + SDL_Log("Couldn't initialize SDL_ttf: %s\n", SDL_GetError()); + return SDL_APP_FAILURE; + } + + loadedFont = TTF_OpenFont(fs::absolute(fontPath).c_str(), 12.0f); + if (!loadedFont) { + printf("Font not found! \n"); + return SDL_APP_FAILURE; + } + + color = WHITE; + return SDL_APP_CONTINUE; +} + +bool Text::SetText(std::string text, Window *window) { + buffer += text; + + SDL_DestroySurface(textRenderingSurface); + textRenderingSurface = TTF_RenderText_Blended(loadedFont, buffer.c_str(), 0, color); + + if (textRenderingSurface) { + font_texture = SDL_CreateTextureFromSurface(window->GetRenderer(), textRenderingSurface); + SDL_DestroySurface(textRenderingSurface); + } + if (!font_texture) { + SDL_Log("Couldn't create text: %s\n", SDL_GetError()); + return false; + } + + return true; +} + +void Text::Render() { + int w = 0, h = 0; + SDL_FRect dst; + const float scale = 4.0f; + + /* Center the text and scale it up */ + SDL_GetRenderOutputSize(window.GetRenderer(), &w, &h); + SDL_SetRenderScale(window.GetRenderer(), scale, scale); + SDL_GetTextureSize(font_texture, &dst.w, &dst.h); + dst.x = ((w / scale) - dst.w) / 2; + dst.y = ((h / scale) - dst.h) / 2; + SDL_RenderTexture(window.GetRenderer(), font_texture, NULL, &dst); +} diff --git a/ui/Text.h b/ui/Text.h new file mode 100644 index 0000000..ebf1f4e --- /dev/null +++ b/ui/Text.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include "fileCommon.h" +#include "window.h" + +#define WHITE {255, 255, 255, SDL_ALPHA_OPAQUE} + + +class Text { + +private: + std::string buffer; + TTF_Font *loadedFont = nullptr; + + SDL_Surface *textRenderingSurface = nullptr; + SDL_Color color; + + SDL_Texture *font_texture = nullptr; + + /// ToDo: Replace with dynamic settings / loaded font + fs::path fontPath; + + +public: + SDL_AppResult OpenFont(fs::path fontPath); + bool SetText(std::string text, Window *window); + void Render(); + void CloseFont() { TTF_CloseFont(loadedFont); } +}; diff --git a/ui/window.h b/ui/window.h index 2fcc62a..3d7b918 100644 --- a/ui/window.h +++ b/ui/window.h @@ -1,3 +1,5 @@ +#pragma once + #include #include #include @@ -14,8 +16,8 @@ public: void Resize(int w, int h); void Close(); - SDL_Window *GetWindow() { return window; } - SDL_Renderer *GetRenderer() { return renderer; } + SDL_Window *GetWindow() const { return window; } + SDL_Renderer *GetRenderer() const { return renderer; } };