words and so on
This commit is contained in:
		
							
								
								
									
										45
									
								
								src/Game.cpp
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								src/Game.cpp
									
									
									
									
									
								
							@@ -1,7 +1,12 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <random>
 | 
			
		||||
#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<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() {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										14
									
								
								src/Game.hpp
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								src/Game.hpp
									
									
									
									
									
								
							@@ -1,13 +1,25 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <SDL_rect.h>
 | 
			
		||||
#include <SDL_events.h>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
const SDL_Point SCREEN_SIZE{800, 800};
 | 
			
		||||
 | 
			
		||||
class Game {
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    std::vector<const char*> all_words;
 | 
			
		||||
    std::vector<std::string> guessed;
 | 
			
		||||
    std::string word;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    static void Run();
 | 
			
		||||
 | 
			
		||||
    Game();
 | 
			
		||||
 | 
			
		||||
    void handle_key(SDL_Keycode event);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    void select_word();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										12
									
								
								src/utils.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/utils.hpp
									
									
									
									
									
										Normal 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;
 | 
			
		||||
}
 | 
			
		||||
@@ -1,3 +1,3 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
const char* words[];
 | 
			
		||||
extern const char *words[];
 | 
			
		||||
		Reference in New Issue
	
	Block a user