Compare commits
4 Commits
3c4dc154b1
...
5ab7b40154
Author | SHA1 | Date | |
---|---|---|---|
5ab7b40154 | |||
549214014d | |||
472b439bed | |||
27ac1468c6 |
31
src/Game.cpp
31
src/Game.cpp
@ -45,22 +45,27 @@ void Game::Run() {
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
{
|
||||
Game game;
|
||||
bool quit = false;
|
||||
SDL_Event event;
|
||||
while (!quit) {
|
||||
while (true) {
|
||||
game.draw(renderer);
|
||||
while (SDL_WaitEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_QUIT:
|
||||
quit = true;
|
||||
break;
|
||||
case SDL_KEYDOWN :
|
||||
goto quit;
|
||||
case SDL_KEYDOWN:
|
||||
game.handle_key(event.key.keysym.sym);
|
||||
game.draw(renderer);
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
if (event.window.event == SDL_WINDOWEVENT_CLOSE)
|
||||
goto quit;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
quit:;
|
||||
}
|
||||
|
||||
SDL_DestroyRenderer(renderer);
|
||||
@ -69,7 +74,10 @@ void Game::Run() {
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
Game::Game() : m_wrong_guesses(0), m_game_state(State::PLAY) {
|
||||
Game::Game() :
|
||||
m_wrong_guesses(0),
|
||||
m_game_state(State::PLAY),
|
||||
m_hills(get_resized(get_hills(), 400, 400)) {
|
||||
const char *defaultFontPath = getDefaultFontPath();
|
||||
if (defaultFontPath == nullptr) {
|
||||
std::stringstream ss;
|
||||
@ -125,6 +133,10 @@ void Game::draw(SDL_Renderer *renderer) {
|
||||
draw_guesses(renderer);
|
||||
}
|
||||
|
||||
void Game::draw_hang_man(SDL_Renderer *renderer) {
|
||||
|
||||
}
|
||||
|
||||
void Game::draw_guesses(SDL_Renderer *renderer) {
|
||||
size_t len = strlen(word);
|
||||
int total_width = (len - 1) * STEP_SIZE + CHAR_SIZE;
|
||||
@ -167,3 +179,8 @@ void Game::draw_guesses(SDL_Renderer *renderer) {
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
Game::~Game() {
|
||||
for (SDL_Surface *surface: m_hills)
|
||||
SDL_FreeSurface(surface);
|
||||
}
|
||||
|
@ -19,11 +19,13 @@ private:
|
||||
_TTF_Font *font;
|
||||
int m_wrong_guesses;
|
||||
State m_game_state;
|
||||
std::vector<SDL_Surface *> m_hills;
|
||||
|
||||
public:
|
||||
static void Run();
|
||||
|
||||
Game();
|
||||
~Game();
|
||||
|
||||
void handle_key(SDL_Keycode event);
|
||||
|
||||
@ -32,5 +34,6 @@ public:
|
||||
private:
|
||||
void draw_guesses(SDL_Renderer *renderer);
|
||||
|
||||
void draw_hang_man(SDL_Renderer *renderer);
|
||||
};
|
||||
|
||||
|
@ -23,3 +23,56 @@ std::vector<SDL_Surface *> get_hills() {
|
||||
|
||||
return surfaces;
|
||||
}
|
||||
|
||||
SDL_Surface *resize_surface(SDL_Surface *t_surface, int t_width, int t_height) {
|
||||
if (!t_surface)
|
||||
throw std::runtime_error("Original surface is null.");
|
||||
|
||||
if (t_surface->w == t_width && t_surface->h == t_height)
|
||||
return t_surface;
|
||||
|
||||
SDL_Surface *resizedSurface = SDL_CreateRGBSurface(
|
||||
0,
|
||||
t_width,
|
||||
t_height,
|
||||
t_surface->format->BitsPerPixel,
|
||||
t_surface->format->Rmask,
|
||||
t_surface->format->Gmask,
|
||||
t_surface->format->Bmask,
|
||||
t_surface->format->Amask
|
||||
);
|
||||
|
||||
if (!resizedSurface)
|
||||
throw std::runtime_error("Failed to create resized surface: " + std::string(SDL_GetError()));
|
||||
|
||||
SDL_Rect src_rect, dest_rect;
|
||||
for (int y = 0; y < t_height; ++y) {
|
||||
for (int x = 0; x < t_width; ++x) {
|
||||
src_rect.x = x * t_surface->w / t_width;
|
||||
src_rect.y = y * t_surface->h / t_height;
|
||||
src_rect.w = 1;
|
||||
src_rect.h = 1;
|
||||
|
||||
dest_rect.x = x;
|
||||
dest_rect.y = y;
|
||||
dest_rect.w = 1;
|
||||
dest_rect.h = 1;
|
||||
|
||||
SDL_BlitSurface(t_surface, &src_rect, resizedSurface, &dest_rect);
|
||||
}
|
||||
}
|
||||
|
||||
return resizedSurface;
|
||||
}
|
||||
|
||||
|
||||
std::vector<SDL_Surface *> get_resized(const std::vector<SDL_Surface *> &t_originals, int t_width, int t_height) {
|
||||
std::vector<SDL_Surface *> ret;
|
||||
ret.reserve(t_originals.size());
|
||||
for (SDL_Surface *original: t_originals) {
|
||||
SDL_Surface *resized = resize_surface(original, t_width, t_height);
|
||||
SDL_FreeSurface(original);
|
||||
ret.push_back(resized);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -11,4 +11,8 @@ constexpr size_t array_len(T *array[]) {
|
||||
return i;
|
||||
}
|
||||
|
||||
std::vector<SDL_Surface*> get_hills();
|
||||
std::vector<SDL_Surface *> get_resized(const std::vector<SDL_Surface *>& t_original, int t_width, int t_height);
|
||||
|
||||
std::vector<SDL_Surface *> get_hills();
|
||||
|
||||
SDL_Surface *resize_surface(SDL_Surface *t_surface, int t_width, int t_height);
|
||||
|
Loading…
x
Reference in New Issue
Block a user