diff --git a/src/Game.cpp b/src/Game.cpp index a840c15..97602cd 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "GameRenderer.hpp" @@ -17,37 +18,37 @@ Game::~Game() void Game::update(std::stop_token stop_token) { while (!stop_token.stop_requested()) { - game_mutex.lock(); + { + auto lock = std::lock_guard(game_mutex); - ticks++; + ticks++; - player.update(); + player.update(); - for (Arrow *arrow : arrows) { - arrow->update(); + for (Arrow *arrow : arrows) { + arrow->update(); + } + + for (Zombo *zombo : zombos) { + zombo->update(player.x, player.y); + } + + if (ticks % 1000 == 0) { + zombos.push_back(new Zombo(&renderer, 20.0, 20.0)); // doesn't work :(((( + } + + renderer.redraw(); + + map.check_bounds(player.x, player.y); } - for (Zombo *zombo : zombos) { - zombo->update(player.x, player.y); - } - - if (ticks % 1000 == 0) { - zombos.push_back(new Zombo(&renderer, 20.0, 20.0)); // doesn't work :(((( - } - - renderer.redraw(); - - map.check_bounds(player.x, player.y); - - game_mutex.unlock(); - std::this_thread::sleep_for(16666us); } } void Game::draw() { - const std::lock_guard lock(game_mutex); + std::lock_guard lock(game_mutex); renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF); @@ -64,61 +65,65 @@ void Game::draw() void Game::run() { - update_thread = std::jthread(std::bind_front(&Game::update, this)); + auto update_thread = std::jthread(std::bind_front(&Game::update, this)); while (true) { SDL_Event e; SDL_WaitEvent(&e); - if (e.type == SDL_QUIT) { - break; - } + { + auto lock = std::lock_guard(game_mutex); - if (e.type == SDL_KEYDOWN) { - switch (e.key.keysym.sym) { - case SDLK_RIGHT: - case SDLK_d: - player.x_vel = player.speed; - break; - case SDLK_LEFT: - case SDLK_a: - player.x_vel = -player.speed; - break; - case SDLK_DOWN: - case SDLK_s: - player.y_vel = player.speed; - break; - case SDLK_UP: - case SDLK_w: - player.y_vel = -player.speed; - break; + if (e.type == SDL_QUIT) { + break; } - } - if (e.type == SDL_KEYUP) { - switch (e.key.keysym.sym) { - case SDLK_RIGHT: - case SDLK_d: - case SDLK_LEFT: - case SDLK_a: - player.x_vel = 0; - break; - case SDLK_DOWN: - case SDLK_s: - case SDLK_UP: - case SDLK_w: - player.y_vel = 0; - break; + if (e.type == SDL_KEYDOWN) { + switch (e.key.keysym.sym) { + case SDLK_RIGHT: + case SDLK_d: + player.x_vel = player.speed; + break; + case SDLK_LEFT: + case SDLK_a: + player.x_vel = -player.speed; + break; + case SDLK_DOWN: + case SDLK_s: + player.y_vel = player.speed; + break; + case SDLK_UP: + case SDLK_w: + player.y_vel = -player.speed; + break; + } } - } - if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1) { - arrows.push_back(new Arrow(&renderer, player.x, player.y, player.angle)); - } + if (e.type == SDL_KEYUP) { + switch (e.key.keysym.sym) { + case SDLK_RIGHT: + case SDLK_d: + case SDLK_LEFT: + case SDLK_a: + player.x_vel = 0; + break; + case SDLK_DOWN: + case SDLK_s: + case SDLK_UP: + case SDLK_w: + player.y_vel = 0; + break; + } + } - int mouse_x, mouse_y; - SDL_GetMouseState(&mouse_x, &mouse_y); - player.angle = std::atan2(mouse_y - renderer.screen_height / 2, mouse_x - renderer.screen_width / 2); + if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1) { + arrows.push_back(new Arrow(&renderer, player.x, player.y, player.angle)); + } + + int mouse_x, mouse_y; + SDL_GetMouseState(&mouse_x, &mouse_y); + player.angle = std::atan2(mouse_y - renderer.screen_height / 2, mouse_x - renderer.screen_width / 2); + } draw(); } diff --git a/src/Game.hpp b/src/Game.hpp index b8ac967..c4060d9 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -19,7 +19,6 @@ private: std::vector arrows; std::vector zombos; - std::jthread update_thread; std::mutex game_mutex; unsigned int ticks = 0;