From 7c62375761368fb5943d9f7ca51b1205a1e7c33f Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Sat, 3 Aug 2024 11:12:30 +0200 Subject: [PATCH] font loading and drawing chars --- src/Game.cpp | 28 ++++++++++++++++++++++++++++ src/Game.hpp | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Game.cpp b/src/Game.cpp index d934347..829e6c5 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -12,6 +12,7 @@ const int CHAR_SIZE = 30; const int STEP_SIZE = CHAR_SIZE + CHAR_SIZE / 2; const int UNDERSCORE_DY = 10; +const SDL_Color TEXT_COLOR = {255, 255, 255}; void Game::Run() { if (SDL_Init(SDL_INIT_VIDEO) < 0) { @@ -121,6 +122,33 @@ void Game::draw_guesses(SDL_Renderer *renderer) { const SDL_Rect rect = {here, char_y + UNDERSCORE_DY, CHAR_SIZE, 5}; SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); SDL_RenderFillRect(renderer, &rect); + + std::optional current_char = guess_corrector->guessed().lock()[i]; + if (current_char) { + const char *text_to_write = &*current_char; + + SDL_Surface *surface = TTF_RenderText_Solid(font, text_to_write, TEXT_COLOR); + if (surface == nullptr) { + std::cerr << "Failed to create surface: " << TTF_GetError() << std::endl; + continue; + } + + SDL_Texture *txt = SDL_CreateTextureFromSurface(renderer, surface); + if (txt == nullptr) { + std::cerr << "Failed to create texture: " << SDL_GetError() << std::endl; + SDL_FreeSurface(surface); + continue; + } + + int text_width = surface->w; + int text_height = surface->h; + SDL_Rect text_rect = {here + (CHAR_SIZE - text_width) / 2, char_y - text_height, text_width, text_height}; + + SDL_RenderCopy(renderer, txt, nullptr, &text_rect); + + SDL_DestroyTexture(txt); + SDL_FreeSurface(surface); + } } SDL_RenderPresent(renderer); diff --git a/src/Game.hpp b/src/Game.hpp index 4f47190..9def6a9 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -15,7 +15,7 @@ private: std::vector all_words; std::unique_ptr guess_corrector; const char *word; - const _TTF_Font *font; + _TTF_Font *font; public: static void Run();