words and so on

This commit is contained in:
Love 2024-08-02 16:12:56 +02:00
parent ca535d4a70
commit 7eab5371ce
5 changed files with 72 additions and 2 deletions

View File

@ -17,6 +17,7 @@ add_executable(hang_man src/main.cpp
src/State.hpp src/State.hpp
src/words.hpp src/words.hpp
src/words.cpp src/words.cpp
src/utils.hpp
) )
target_link_libraries(hang_man PRIVATE target_link_libraries(hang_man PRIVATE

View File

@ -1,7 +1,12 @@
#include <iostream> #include <iostream>
#include <algorithm>
#include <random>
#include "Game.hpp" #include "Game.hpp"
#include "SDL.h" #include "SDL.h"
#include "SDL_ttf.h" #include "SDL_ttf.h"
#include "words.hpp"
#include "utils.hpp"
void Game::Run() { void Game::Run() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) { if (SDL_Init(SDL_INIT_VIDEO) < 0) {
@ -32,4 +37,44 @@ void Game::Run() {
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_RenderPresent(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<const char *>();
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() {
} }

View File

@ -1,13 +1,25 @@
#pragma once #pragma once
#include <SDL_rect.h> #include <SDL_rect.h>
#include <SDL_events.h>
#include <vector>
const SDL_Point SCREEN_SIZE{800, 800}; const SDL_Point SCREEN_SIZE{800, 800};
class Game { class Game {
private:
std::vector<const char*> all_words;
std::vector<std::string> guessed;
std::string word;
public: public:
static void Run(); static void Run();
Game();
void handle_key(SDL_Keycode event);
private:
void select_word();
}; };

12
src/utils.hpp Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <cstddef>
template<typename T>
constexpr size_t array_len(T *array[]) {
size_t i = 0;
while (array[i] != nullptr)
i++;
return i;
}

View File

@ -1,3 +1,3 @@
#pragma once #pragma once
const char* words[]; extern const char *words[];