From 7f7aa9e72fbef1932ee020b359cd721dd7e2e93e Mon Sep 17 00:00:00 2001 From: Reimar Date: Thu, 23 Oct 2025 14:37:46 +0200 Subject: [PATCH] Implement player movement --- src/Game.cpp | 72 ++++++++++++++++++++++++++++++++++++-------- src/Game.hpp | 8 +++++ src/GameRenderer.cpp | 9 ++++++ src/GameRenderer.hpp | 2 ++ src/Map.cpp | 5 ++- src/Player.hpp | 10 +++--- 6 files changed, 87 insertions(+), 19 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index 40dce87..fc9ba93 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,15 +1,44 @@ +#include #include "GameRenderer.hpp" #include "Player.hpp" #include "Game.hpp" -#include +using namespace std::literals::chrono_literals; Game::Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {} -Game::~Game() = default; +Game::~Game() +{ + stopping = true; + update_thread.join(); +} + +void Game::update() +{ + while (!stopping) { + player.update(); + + renderer.redraw(); + + std::this_thread::sleep_for(16666us); + } +} + +void Game::draw() const +{ + renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF); + + map.draw(player.x, player.y); + + player.draw(); + + renderer.flush(); +} void Game::run() { + update_thread = std::thread(&Game::update, this); + while (true) { SDL_Event e; SDL_WaitEvent(&e); @@ -22,21 +51,40 @@ void Game::run() switch (e.key.keysym.sym) { case SDLK_RIGHT: case SDLK_d: - player.x_vel = 5; + player.x_vel = player.speed; + break; case SDLK_LEFT: case SDLK_a: - player.x_vel = -5; + 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; } } - player.update(); + 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; + } + } - renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF); - - map.draw(player.x, player.y); - - player.draw(); - - renderer.flush(); + draw(); } } diff --git a/src/Game.hpp b/src/Game.hpp index 1c25113..6e0e34f 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -1,6 +1,7 @@ #ifndef GAME_HPP #define GAME_HPP +#include #include "GameRenderer.hpp" #include "Player.hpp" #include "Map.hpp" @@ -12,12 +13,19 @@ private: Player player; Map map; + std::thread update_thread; + bool stopping = false; + + void update(); + public: Game(); ~Game(); void run(); + + void draw() const; }; #endif diff --git a/src/GameRenderer.cpp b/src/GameRenderer.cpp index a7f7167..e33c2ec 100644 --- a/src/GameRenderer.cpp +++ b/src/GameRenderer.cpp @@ -64,3 +64,12 @@ void GameRenderer::flush() const { SDL_RenderPresent(renderer); } + +void GameRenderer::redraw() const +{ + SDL_Event event; + event.type = SDL_WINDOWEVENT; + event.window.event = SDL_WINDOWEVENT_EXPOSED; + event.window.windowID = SDL_GetWindowID(window); + SDL_PushEvent(&event); +} diff --git a/src/GameRenderer.hpp b/src/GameRenderer.hpp index 86a79c3..2d0891d 100644 --- a/src/GameRenderer.hpp +++ b/src/GameRenderer.hpp @@ -28,6 +28,8 @@ public: void clear_screen(int r, int g, int b, int a) const; void flush() const; + + void redraw() const; }; #endif \ No newline at end of file diff --git a/src/Map.cpp b/src/Map.cpp index 0ea33db..11258fa 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -1,5 +1,4 @@ #include -#include #include "Map.hpp" Map::Map(GameRenderer *renderer, int tile_size) : @@ -27,8 +26,8 @@ void Map::draw(int player_x, int player_y) const { for (int tile_x = 0; tile_x < tiles.size(); tile_x++) { for (int tile_y = 0; tile_y < tiles[tile_x].size(); tile_y++) { - int screen_x = player_x + tile_x * tile_size + tile_offset_x * tile_size + renderer->screen_width / 2; - int screen_y = player_y + tile_y * tile_size + tile_offset_y * tile_size + renderer->screen_height / 2; + int screen_x = tile_x * tile_size + tile_offset_x * tile_size + renderer->screen_width / 2 - player_x; + int screen_y = tile_y * tile_size + tile_offset_y * tile_size + renderer->screen_height / 2 - player_y; if ( screen_x < -tile_size || screen_y < -tile_size || diff --git a/src/Player.hpp b/src/Player.hpp index 3954b62..72f5fd0 100644 --- a/src/Player.hpp +++ b/src/Player.hpp @@ -10,11 +10,13 @@ private: Sprite hero_sprite; public: - int x = 0; - int y = 0; + double x = 0; + double y = 0; - int x_vel = 0; - int y_vel = 0; + double x_vel = 0; + double y_vel = 0; + + const double speed = 0.9; Player(GameRenderer *renderer);