Compare commits
No commits in common. "5ab7b40154e208bf1c78a0f8ecd181883f43e260" and "3c4dc154b1749dad6057653eff245a5e1761dce5" have entirely different histories.
5ab7b40154
...
3c4dc154b1
29
src/Game.cpp
29
src/Game.cpp
@ -45,27 +45,22 @@ void Game::Run() {
|
|||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
{
|
|
||||||
Game game;
|
Game game;
|
||||||
|
bool quit = false;
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (true) {
|
while (!quit) {
|
||||||
game.draw(renderer);
|
game.draw(renderer);
|
||||||
while (SDL_WaitEvent(&event)) {
|
while (SDL_WaitEvent(&event)) {
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
goto quit;
|
quit = true;
|
||||||
|
break;
|
||||||
case SDL_KEYDOWN :
|
case SDL_KEYDOWN :
|
||||||
game.handle_key(event.key.keysym.sym);
|
game.handle_key(event.key.keysym.sym);
|
||||||
game.draw(renderer);
|
game.draw(renderer);
|
||||||
break;
|
|
||||||
case SDL_WINDOWEVENT:
|
|
||||||
if (event.window.event == SDL_WINDOWEVENT_CLOSE)
|
|
||||||
goto quit;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
quit:;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
@ -74,10 +69,7 @@ void Game::Run() {
|
|||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::Game() :
|
Game::Game() : m_wrong_guesses(0), m_game_state(State::PLAY) {
|
||||||
m_wrong_guesses(0),
|
|
||||||
m_game_state(State::PLAY),
|
|
||||||
m_hills(get_resized(get_hills(), 400, 400)) {
|
|
||||||
const char *defaultFontPath = getDefaultFontPath();
|
const char *defaultFontPath = getDefaultFontPath();
|
||||||
if (defaultFontPath == nullptr) {
|
if (defaultFontPath == nullptr) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@ -133,10 +125,6 @@ void Game::draw(SDL_Renderer *renderer) {
|
|||||||
draw_guesses(renderer);
|
draw_guesses(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::draw_hang_man(SDL_Renderer *renderer) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void Game::draw_guesses(SDL_Renderer *renderer) {
|
void Game::draw_guesses(SDL_Renderer *renderer) {
|
||||||
size_t len = strlen(word);
|
size_t len = strlen(word);
|
||||||
int total_width = (len - 1) * STEP_SIZE + CHAR_SIZE;
|
int total_width = (len - 1) * STEP_SIZE + CHAR_SIZE;
|
||||||
@ -179,8 +167,3 @@ void Game::draw_guesses(SDL_Renderer *renderer) {
|
|||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::~Game() {
|
|
||||||
for (SDL_Surface *surface: m_hills)
|
|
||||||
SDL_FreeSurface(surface);
|
|
||||||
}
|
|
||||||
|
@ -19,13 +19,11 @@ private:
|
|||||||
_TTF_Font *font;
|
_TTF_Font *font;
|
||||||
int m_wrong_guesses;
|
int m_wrong_guesses;
|
||||||
State m_game_state;
|
State m_game_state;
|
||||||
std::vector<SDL_Surface *> m_hills;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void Run();
|
static void Run();
|
||||||
|
|
||||||
Game();
|
Game();
|
||||||
~Game();
|
|
||||||
|
|
||||||
void handle_key(SDL_Keycode event);
|
void handle_key(SDL_Keycode event);
|
||||||
|
|
||||||
@ -34,6 +32,5 @@ public:
|
|||||||
private:
|
private:
|
||||||
void draw_guesses(SDL_Renderer *renderer);
|
void draw_guesses(SDL_Renderer *renderer);
|
||||||
|
|
||||||
void draw_hang_man(SDL_Renderer *renderer);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -23,56 +23,3 @@ std::vector<SDL_Surface *> get_hills() {
|
|||||||
|
|
||||||
return surfaces;
|
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,8 +11,4 @@ constexpr size_t array_len(T *array[]) {
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
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