From 7eab5371ce5d867784472fafb061345fc9eaab69 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Fri, 2 Aug 2024 16:12:56 +0200 Subject: [PATCH] words and so on --- CMakeLists.txt | 1 + src/Game.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/Game.hpp | 14 +++++++++++++- src/utils.hpp | 12 ++++++++++++ src/words.hpp | 2 +- 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/utils.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index de3e610..9ec1280 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable(hang_man src/main.cpp src/State.hpp src/words.hpp src/words.cpp + src/utils.hpp ) target_link_libraries(hang_man PRIVATE diff --git a/src/Game.cpp b/src/Game.cpp index 5b644ec..27b1d90 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,7 +1,12 @@ #include +#include +#include #include "Game.hpp" #include "SDL.h" #include "SDL_ttf.h" +#include "words.hpp" +#include "utils.hpp" + void Game::Run() { if (SDL_Init(SDL_INIT_VIDEO) < 0) { @@ -32,4 +37,44 @@ void Game::Run() { SDL_RenderClear(renderer); SDL_RenderPresent(renderer); + Game game; + bool quit = false; + SDL_Event event; + while (!quit) { + while (SDL_WaitEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + quit = true; + break; + case SDL_KEYDOWN : + game.handle_key(event.key.keysym.sym); + } + } + + } + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_QuitSubSystem(SDL_INIT_VIDEO); + SDL_Quit(); +} + +Game::Game() : guessed() { + std::random_device random_device{}; + std::mt19937 rng(random_device()); + + size_t words_len = array_len(words); + this->all_words = std::vector(); + this->all_words.reserve(words_len); + for (int i = 0; i < words_len; i++) + all_words.push_back(words[i]); + std::shuffle(all_words.begin(), all_words.end(), rng); +} + +void Game::handle_key(SDL_Keycode event) { + +} + +void Game::select_word() { + } diff --git a/src/Game.hpp b/src/Game.hpp index 4a6bc8e..b280049 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -1,13 +1,25 @@ #pragma once #include +#include +#include const SDL_Point SCREEN_SIZE{800, 800}; class Game { - +private: + std::vector all_words; + std::vector guessed; + std::string word; public: static void Run(); + + Game(); + + void handle_key(SDL_Keycode event); + +private: + void select_word(); }; diff --git a/src/utils.hpp b/src/utils.hpp new file mode 100644 index 0000000..f7501cc --- /dev/null +++ b/src/utils.hpp @@ -0,0 +1,12 @@ +#pragma once + + +#include + +template +constexpr size_t array_len(T *array[]) { + size_t i = 0; + while (array[i] != nullptr) + i++; + return i; +} diff --git a/src/words.hpp b/src/words.hpp index fbfc4bf..3d206d0 100644 --- a/src/words.hpp +++ b/src/words.hpp @@ -1,3 +1,3 @@ #pragma once -const char* words[]; \ No newline at end of file +extern const char *words[]; \ No newline at end of file