From 1941c4c29a5462d6066f8c839265d77d4d05084b Mon Sep 17 00:00:00 2001 From: Jan Mrna Date: Sat, 27 Sep 2025 14:46:13 +0200 Subject: [PATCH] Move UserInput implementation to user_input.cpp --- cpp/Makefile | 2 +- cpp/src/user_input.cpp | 91 ++++++++++++++++++++++++++++++++++++ cpp/src/user_input.hpp | 102 +++++------------------------------------ 3 files changed, 104 insertions(+), 91 deletions(-) diff --git a/cpp/Makefile b/cpp/Makefile index 449f090..81c8f71 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -3,7 +3,7 @@ all: test pathfinding # TODO linter? pathfinding: - g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp src/sprite.cpp src/entities.cpp src/gameloop.cpp src/map.cpp src/pathfindingdemo.cpp src/tile.cpp + g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp src/sprite.cpp src/entities.cpp src/gameloop.cpp src/map.cpp src/pathfindingdemo.cpp src/tile.cpp src/user_input.cpp test: src/test.cpp src/array.hpp g++ -Wall -Wextra -Wpedantic -ggdb3 -std=c++23 -lgtest -o test src/test.cpp diff --git a/cpp/src/user_input.cpp b/cpp/src/user_input.cpp index e69de29..e55a94f 100644 --- a/cpp/src/user_input.cpp +++ b/cpp/src/user_input.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include + +#include "user_input.hpp" + +#include "log.hpp" +#include "math.hpp" + +UserInput::UserInput() + : // pre-alloc some space + m_Actions(10) { + LOG_DEBUG("."); +}; + +UserInput::~UserInput() { LOG_DEBUG("."); }; + +std::expected UserInput::Init() { return {}; } + +const std::vector &UserInput::GetActions() { + m_Actions.clear(); + static WorldPos move_direction = {0.0f, 0.0f}; + static bool send_move_action = false; + SDL_Event event; + + while (SDL_PollEvent(&event)) { + if (event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP) { + bool key_down = event.type == SDL_EVENT_KEY_DOWN ? true : false; + SDL_KeyboardEvent kbd_event = event.key; + if (kbd_event.repeat) { + // SDL repeats KEY_DOWN if key is held down, we ignore that + continue; + } + LOG_DEBUG("Key '", static_cast(kbd_event.key), + key_down ? "' down" : "' up"); + + switch (kbd_event.key) { + case 'q': + m_Actions.emplace_back(UserAction::Type::EXIT); + // further processing of inputs is not needed + return m_Actions; + case 'w': + case 's': + case 'a': + case 'd': + case SDLK_UP: + case SDLK_DOWN: + case SDLK_LEFT: + case SDLK_RIGHT: { + static std::map move_base{ + {'w', {0.0, 1.0}}, + {'s', {0.0, -1.0}}, + {'a', {1.0, 0.0}}, + {'d', {-1.0, 0.0}}, + {static_cast(SDLK_UP), {0.0, 1.0}}, + {static_cast(SDLK_DOWN), {0.0, -1.0}}, + {static_cast(SDLK_LEFT), {1.0, 0.0}}, + {static_cast(SDLK_RIGHT), {-1.0, 0.0}}, + }; + + float direction = key_down ? -1.0f : 1.0f; + send_move_action = true; + move_direction += move_base[kbd_event.key] * direction; + break; + } + case SDLK_SPACE: + if (key_down) + m_Actions.emplace_back(UserAction::Type::FIRE); + break; + default: + LOG_INFO("Key '", static_cast(kbd_event.key), "' not mapped"); + break; + } + } else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { + SDL_MouseButtonEvent mouse_event = event.button; + LOG_DEBUG("Mouse down: ", mouse_event.x, ", ", mouse_event.y); + m_Actions.emplace_back(UserAction::Type::MOVE_TARGET, + WorldPos{mouse_event.x, mouse_event.y}); + } else { + // TODO uncomment, for now too much noise + // LOG_WARNING("Action not processed"); + } + } + if (send_move_action) { + m_Actions.emplace_back(UserAction::Type::MOVE, move_direction.normalized()); + send_move_action = false; + } + return m_Actions; +} diff --git a/cpp/src/user_input.hpp b/cpp/src/user_input.hpp index c67aae1..8a19617 100644 --- a/cpp/src/user_input.hpp +++ b/cpp/src/user_input.hpp @@ -1,121 +1,43 @@ #pragma once -#include -#include -#include -#include #include +#include +#include -#include "math.hpp" #include "log.hpp" +#include "math.hpp" class UserAction { public: - enum class Type { NONE, EXIT, MOVE, CROUCH, STAND, FIRE, MOVE_TARGET}; + enum class Type { NONE, EXIT, MOVE, CROUCH, STAND, FIRE, MOVE_TARGET }; UserAction() = default; - UserAction(Type t) : type(t) {} - UserAction(Type t, char key) : type(t), Argument{.key = key} {} - - UserAction(Type t, Vec2D v) : type(t), Argument{.position = v} {} - + UserAction(Type t, WorldPos v) : type(t), Argument{.position = v} {} ~UserAction() = default; Type type; union { - Vec2D position; + WorldPos position; char key; } Argument; }; class UserInput { public: - UserInput() - : // pre-alloc some space - m_Actions(10) { - LOG_DEBUG("."); - }; + UserInput(); + ~UserInput(); UserInput(const UserInput &x) = delete; UserInput(UserInput &&x) = delete; - ~UserInput() { LOG_DEBUG("."); }; + UserInput &operator=(const UserInput &) = delete; + UserInput &operator=(UserInput &&) = delete; - std::expected Init() { return {}; } + std::expected Init(); - const std::vector &GetActions() { - m_Actions.clear(); - static Vec2D move_direction = {0.0f, 0.0f}; - static bool send_move_action = false; - SDL_Event event; - - while (SDL_PollEvent(&event)) { - if (event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP) { - bool key_down = event.type == SDL_EVENT_KEY_DOWN ? true : false; - SDL_KeyboardEvent kbd_event = event.key; - if (kbd_event.repeat) { - // SDL repeats KEY_DOWN if key is held down, we ignore that - continue; - } - LOG_DEBUG("Key '", static_cast(kbd_event.key), - key_down ? "' down" : "' up"); - - switch (kbd_event.key) { - case 'q': - m_Actions.emplace_back(UserAction::Type::EXIT); - // further processing of inputs is not needed - return m_Actions; - case 'w': - case 's': - case 'a': - case 'd': - case SDLK_UP: - case SDLK_DOWN: - case SDLK_LEFT: - case SDLK_RIGHT: { - static std::map> move_base{ - {'w', {0.0, 1.0}}, - {'s', {0.0, -1.0}}, - {'a', {1.0, 0.0}}, - {'d', {-1.0, 0.0}}, - {static_cast(SDLK_UP), {0.0, 1.0}}, - {static_cast(SDLK_DOWN), {0.0, -1.0}}, - {static_cast(SDLK_LEFT), {1.0, 0.0}}, - {static_cast(SDLK_RIGHT), {-1.0, 0.0}}, - }; - - float direction = key_down ? -1.0f : 1.0f; - send_move_action = true; - move_direction += move_base[kbd_event.key] * direction; - break; - } - case SDLK_SPACE: - if (key_down) - m_Actions.emplace_back(UserAction::Type::FIRE); - break; - default: - LOG_INFO("Key '", static_cast(kbd_event.key), "' not mapped"); - break; - } - } else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { - SDL_MouseButtonEvent mouse_event = event.button; - LOG_DEBUG("Mouse down: ", mouse_event.x, ", ", mouse_event.y); - m_Actions.emplace_back(UserAction::Type::MOVE_TARGET, - Vec2D{mouse_event.x, mouse_event.y}); - } else { - // TODO uncomment, for now too much noise - // LOG_WARNING("Action not processed"); - } - } - if (send_move_action) { - m_Actions.emplace_back(UserAction::Type::MOVE, - move_direction.normalized()); - send_move_action = false; - } - return m_Actions; - } + const std::vector &GetActions(); private: std::vector m_Actions;