From ec7ef40aea9ac7ea237edf9a687d469f3eec92b4 Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 23 Oct 2025 16:35:40 +0200 Subject: [PATCH] feat: input class for abstracting SDL_Input functions --- engine/CMakeLists.txt | 2 ++ engine/include/engine/input/input.h | 29 ++++++++++++++++++++++ engine/src/input/input.cpp | 24 +++++++++++++++++++ engine/src/renderer/core.cpp | 2 ++ sandbox/src/main.cpp | 37 +++++++++++------------------ 5 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 engine/include/engine/input/input.h create mode 100644 engine/src/input/input.cpp diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 7d01b6c..548a698 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -5,6 +5,8 @@ set(SOURCES src/IO/file_manager.cpp src/renderer/debug.cpp + src/input/input.cpp + src/scene/scene.cpp src/window/window.cpp diff --git a/engine/include/engine/input/input.h b/engine/include/engine/input/input.h new file mode 100644 index 0000000..43e0e9b --- /dev/null +++ b/engine/include/engine/input/input.h @@ -0,0 +1,29 @@ +#ifndef ENGINE_INPUT_H_ +#define ENGINE_INPUT_H_ + +#include +#include + +#include "engine/time/timestep.h" + +#include "engine/export.h" + +namespace Core { + +class ENGINE_API Input { +public: + Input() = delete; +private: + friend class Engine; + static void Update(Timestep dt); +public: + [[nodiscard]] static glm::vec2 GetRelativeMouse(); + [[nodiscard]] static bool IsKeyPressed(SDL_Scancode keyCode); +private: + static bool* s_keys_state; + static int s_keys_state_size; +}; + +} + +#endif // ENGINE_INPUT_H_ \ No newline at end of file diff --git a/engine/src/input/input.cpp b/engine/src/input/input.cpp new file mode 100644 index 0000000..3050e6b --- /dev/null +++ b/engine/src/input/input.cpp @@ -0,0 +1,24 @@ +#include "engine/input/input.h" + +namespace Core { + +bool* Input::s_keys_state = nullptr; +int Input::s_keys_state_size = 0; + +// Engine only function +void Input::Update(Timestep dt) { + s_keys_state = (bool*)SDL_GetKeyboardState(&s_keys_state_size); +} + +glm::vec2 Input::GetRelativeMouse() { + glm::vec2 mouse; + SDL_GetRelativeMouseState(&mouse.x, &mouse.y); + return mouse; +} + +bool Input::IsKeyPressed(SDL_Scancode keyCode) { + assert(keyCode < s_keys_state_size && "Key is out of bounds of the key input state"); + return s_keys_state[keyCode]; +} + +} diff --git a/engine/src/renderer/core.cpp b/engine/src/renderer/core.cpp index e46e4d6..61df694 100644 --- a/engine/src/renderer/core.cpp +++ b/engine/src/renderer/core.cpp @@ -6,6 +6,7 @@ #include "engine/renderer/wavefront.h" #include "engine/time/timestep.h" +#include "engine/input/input.h" namespace Core { @@ -34,6 +35,7 @@ void Engine::Run(std::unique_ptr app) { m_window->ProcessEvents(); + Input::Update(dt); m_app->OnUpdate(dt); m_renderer->Render(); diff --git a/sandbox/src/main.cpp b/sandbox/src/main.cpp index aa8e816..1bda3bc 100644 --- a/sandbox/src/main.cpp +++ b/sandbox/src/main.cpp @@ -20,6 +20,7 @@ #include "engine/components/batch.h" #include "engine/scene/scene.h" +#include "engine/input/input.h" #include "engine/api.h" @@ -84,19 +85,16 @@ public: m_angle = 3.45f; - m_paused = false; - m_yaw = -90.0f; // looking along -Z initially m_pitch = 0.0f; // no vertical tilt } void OnUpdate(Timestep dt) override { - float mouseXRel, mouseYRel; - SDL_GetRelativeMouseState(&mouseXRel, &mouseYRel); + glm::vec2 mouseRel = Input::GetRelativeMouse(); float sensitivity = 0.1f; // tweak as needed - m_yaw += mouseXRel * sensitivity; - m_pitch -= mouseYRel * sensitivity; // invert Y for typical FPS control + m_yaw += mouseRel.x * sensitivity; + m_pitch -= mouseRel.y * sensitivity; // invert Y for typical FPS control // clamp pitch to avoid flipping // if (pitch > 89.0f) pitch = 89.0f; @@ -112,30 +110,24 @@ public: glm::vec3 velocity(0.f); - const bool* state = SDL_GetKeyboardState(nullptr); - - if (state[SDL_SCANCODE_P]) m_paused = !m_paused; - glm::vec3 front = glm::normalize(glm::vec3(cameraViewDirection.x, 0.f, cameraViewDirection.z)); glm::vec3 right = glm::normalize(glm::cross(front, glm::vec3(0.f, 1.f, 0.f))); - if (state[SDL_SCANCODE_W]) velocity += front; - if (state[SDL_SCANCODE_S]) velocity -= front; - if (state[SDL_SCANCODE_A]) velocity -= right; - if (state[SDL_SCANCODE_D]) velocity += right; - if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f; - if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f; + if (Input::IsKeyPressed(SDL_SCANCODE_W)) velocity += front; + if (Input::IsKeyPressed(SDL_SCANCODE_S)) velocity -= front; + if (Input::IsKeyPressed(SDL_SCANCODE_A)) velocity -= right; + if (Input::IsKeyPressed(SDL_SCANCODE_D)) velocity += right; + if (Input::IsKeyPressed(SDL_SCANCODE_SPACE)) velocity.y += 1.f; + if (Input::IsKeyPressed(SDL_SCANCODE_LSHIFT)) velocity.y -= 1.f; auto& camTransform = cameraEntity.GetComponent(); camTransform.position += velocity * (float)dt * 2.5f; // speed is e.g. 2.5f camTransform.rotation = cameraViewDirection; // update rotation - if (!m_paused) { - m_angle += glm::radians(45.0f) * dt; // 72° per second - if (m_angle > glm::two_pi()) { - m_angle -= glm::two_pi(); // keep value small - } + m_angle += glm::radians(45.0f) * dt; // 72° per second + if (m_angle > glm::two_pi()) { + m_angle -= glm::two_pi(); // keep value small } // ---- Day-night simulation ---- @@ -190,6 +182,7 @@ public: } } private: + // for internal 1-second timer int m_elapsed; std::shared_ptr m_scene; @@ -203,8 +196,6 @@ private: float m_dayTime = 0.0f; // accumulates time for day-night cycle float m_dayLength = 60.0f; // seconds per full day cycle - bool m_paused = false; - float m_yaw = -90.0f; // looking along -Z initially float m_pitch = 0.0f; // no vertical tilt };