feat: input class for abstracting SDL_Input functions
This commit is contained in:
@ -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
|
||||
|
||||
|
||||
29
engine/include/engine/input/input.h
Normal file
29
engine/include/engine/input/input.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef ENGINE_INPUT_H_
|
||||
#define ENGINE_INPUT_H_
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#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_
|
||||
24
engine/src/input/input.cpp
Normal file
24
engine/src/input/input.cpp
Normal file
@ -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];
|
||||
}
|
||||
|
||||
}
|
||||
@ -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<IApplication> app) {
|
||||
|
||||
m_window->ProcessEvents();
|
||||
|
||||
Input::Update(dt);
|
||||
m_app->OnUpdate(dt);
|
||||
|
||||
m_renderer->Render();
|
||||
|
||||
Reference in New Issue
Block a user