diff --git a/src/Game.cpp b/src/Game.cpp index c4c4f01..37581ab 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -7,6 +7,9 @@ #include "words.hpp" #include "utils.hpp" +const int CHAR_SIZE = 30; +const int STEP_SIZE = CHAR_SIZE + CHAR_SIZE / 2; +const int UNDERSCORE_DY = 10; void Game::Run() { if (SDL_Init(SDL_INIT_VIDEO) < 0) { @@ -41,6 +44,7 @@ void Game::Run() { bool quit = false; SDL_Event event; while (!quit) { + game.draw(renderer); while (SDL_WaitEvent(&event)) { switch (event.type) { case SDL_QUIT: @@ -48,6 +52,7 @@ void Game::Run() { break; case SDL_KEYDOWN : game.handle_key(event.key.keysym.sym); + game.draw(renderer); } } @@ -72,8 +77,32 @@ Game::Game() { word = all_words.back(); all_words.pop_back(); guess_corrector = std::make_unique(word); + std::cout << "Word: " << word << std::endl; } void Game::handle_key(SDL_Keycode event) { } + +void Game::draw(SDL_Renderer *renderer) { + SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderClear(renderer); + + draw_guesses(renderer); +} + +void Game::draw_guesses(SDL_Renderer *renderer) { + size_t len = strlen(word); + int total_width = (len - 1) * STEP_SIZE + CHAR_SIZE; + int start_x = (SCREEN_SIZE.x - total_width) / 2; + int char_y = (SCREEN_SIZE.y / 2) - CHAR_SIZE / 2; + + for (int i = 0; i < len; i++) { + int here = start_x + i * STEP_SIZE; + 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); + } + + SDL_RenderPresent(renderer); +} diff --git a/src/Game.hpp b/src/Game.hpp index edfa6cf..cc6918c 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "GuessCorrector.hpp" const SDL_Point SCREEN_SIZE{800, 800}; @@ -20,5 +21,10 @@ public: Game(); void handle_key(SDL_Keycode event); + + void draw(SDL_Renderer *renderer); + +private: + void draw_guesses(SDL_Renderer *renderer); };