From ed25ef2ecf4ae9797bbe4a849cfe68af24adc4eb Mon Sep 17 00:00:00 2001 From: wwmsmiff Date: Tue, 13 Jun 2023 11:22:43 +0530 Subject: [PATCH 1/2] New input system --- src/main.cpp | 126 +++++++++++++++++++++++++++------------------------ 1 file changed, 68 insertions(+), 58 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 81879b3..90bdbff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,19 @@ constexpr float player_velocity_v{0.5f}; bool playerUp{}, playerDown{}, playerLeft{}, playerRight{}; Vec2 playerDirection{}; +struct InputSystem +{ + std::set pressedKeys; + std::set releasedKeys; + std::set heldKeys; + + void reset() + { + this->pressedKeys.clear(); + this->releasedKeys.clear(); + } +} gInput{}; + struct SdlDeleter { void operator()(SDL_Renderer *renderer) const @@ -86,6 +100,43 @@ Entity target{Vec2{randomFloat(padding_v, window_width_v - padding_v), void playerUpdate(float delta) { + if (gInput.heldKeys.contains(SDLK_w)) + { + playerDirection.y = -1; + playerDown = false; + playerUp = true; + } + + if (gInput.heldKeys.contains(SDLK_s)) + { + playerDirection.y = 1; + playerUp = false; + playerDown = true; + } + + if (gInput.heldKeys.contains(SDLK_a)) + { + playerDirection.x = -1; + playerRight = false; + playerLeft = true; + } + if (gInput.heldKeys.contains(SDLK_d)) + { + playerDirection.x = 1; + playerLeft = false; + playerRight = true; + } + + if (!gInput.heldKeys.contains(SDLK_d) && !gInput.heldKeys.contains(SDLK_a)) + { + playerDirection.x = 0; + playerLeft = playerRight = false; + } + if (!gInput.heldKeys.contains(SDLK_w) && !gInput.heldKeys.contains(SDLK_s)) + { + playerDirection.y = 0; + playerUp = playerDown = false; + } Vec2 playerDirectionNormalized = playerDirection.normalized(); if (playerRight) @@ -143,7 +194,7 @@ int main(int argc, char *argv[]) SDL_SetRenderDrawBlendMode(gRenderer.get(), SDL_BLENDMODE_BLEND); bool running{true}; - SDL_Event gEvent; + SDL_Event event{}; target.setColor(SDL_Color{78, 255, 45, 255}); @@ -151,6 +202,8 @@ int main(int argc, char *argv[]) while (running) { + gInput.reset(); + auto distance = std::sqrt(static_cast( std::pow((player.getPosition().x - target.getPosition().x), 2) + std::pow((player.getPosition().y - target.getPosition().y), 2))); @@ -165,65 +218,22 @@ int main(int argc, char *argv[]) auto delta{current - start}; SDL_SetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE, "letterbox"); SDL_RenderSetLogicalSize(gRenderer.get(), window_width_v, window_height_v); - while (SDL_PollEvent(&gEvent)) - { - if (gEvent.type == SDL_QUIT) - running = false; - if (gEvent.type == SDL_KEYDOWN) - { - switch (gEvent.key.keysym.sym) - { - case SDLK_RIGHT: - case SDLK_d: - playerDirection.x = 1; - playerLeft = false; - playerRight = true; - break; - case SDLK_LEFT: - case SDLK_a: - playerDirection.x = -1; - playerRight = false; - playerLeft = true; - break; - case SDLK_UP: - case SDLK_w: - playerDirection.y = -1; - playerDown = false; - playerUp = true; - break; - case SDLK_DOWN: - case SDLK_s: - playerDirection.y = 1; - playerUp = false; - playerDown = true; - } - } - if (gEvent.type == SDL_KEYUP) + while (SDL_PollEvent(&event)) + { + switch (event.type) { - switch (gEvent.key.keysym.sym) - { - case SDLK_RIGHT: - case SDLK_d: - playerDirection.x = 0; - playerRight = false; - break; - case SDLK_LEFT: - case SDLK_a: - playerDirection.x = 0; - playerLeft = false; - break; - case SDLK_UP: - case SDLK_w: - playerDirection.y = 0; - playerUp = false; - break; - case SDLK_DOWN: - case SDLK_s: - playerDirection.y = 0; - playerDown = false; - break; - } + case SDL_QUIT: + running = false; + break; + case SDL_KEYDOWN: + gInput.pressedKeys.insert(event.key.keysym.sym); + gInput.heldKeys.insert(event.key.keysym.sym); + break; + case SDL_KEYUP: + gInput.releasedKeys.insert(event.key.keysym.sym); + gInput.heldKeys.erase(gInput.heldKeys.erase(event.key.keysym.sym)); + break; } } From 6ad634f9b7147614b10bfc06fd62041709838c6c Mon Sep 17 00:00:00 2001 From: wwmsmiff Date: Wed, 14 Jun 2023 17:57:50 +0530 Subject: [PATCH 2/2] Add suggested changes --- include/vec2.hpp | 5 +++-- src/main.cpp | 13 ++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/vec2.hpp b/include/vec2.hpp index b169cf1..1e25725 100644 --- a/include/vec2.hpp +++ b/include/vec2.hpp @@ -36,6 +36,8 @@ template struct Vec2 std::sqrt((this->x * this->x) + (this->y * this->y))); } + bool zero() const { return (!this->x && !this->y); } + void operator+=(const Vec2 &other) { this->x += other.x; @@ -141,8 +143,7 @@ template Vec2 operator-(const Vec2 &vec2) return Vec2{-vec2.x, -vec2.y}; } -template -std::ostream &operator<<(std::ostream &out, Vec2 vec2) +template std::ostream &operator<<(std::ostream &out, Vec2 vec2) { out << "Vec2(" << vec2.x << ',' << vec2.y << ')'; return out; diff --git a/src/main.cpp b/src/main.cpp index 90bdbff..fe19c74 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,10 +4,10 @@ #include #include #include -#include #include #include #include +#include #include "entity.hpp" #include "vec2.hpp" @@ -25,9 +25,9 @@ Vec2 playerDirection{}; struct InputSystem { - std::set pressedKeys; - std::set releasedKeys; - std::set heldKeys; + std::unordered_set pressedKeys; + std::unordered_set releasedKeys; + std::unordered_set heldKeys; void reset() { @@ -137,7 +137,10 @@ void playerUpdate(float delta) playerDirection.y = 0; playerUp = playerDown = false; } - Vec2 playerDirectionNormalized = playerDirection.normalized(); + + Vec2 playerDirectionNormalized{}; + if (!playerDirection.zero()) + playerDirectionNormalized = playerDirection.normalized(); if (playerRight) {