WE BE RENDERING BABY

This commit is contained in:
2026-01-07 03:04:20 +01:00
parent c83c423b42
commit e306c5e23f
13 changed files with 377 additions and 37 deletions
+148
View File
@@ -0,0 +1,148 @@
#include <destrum/Input/InputManager.h>
void InputManager::Init() {
// Nothing mandatory here. You can also enable relative mouse mode, etc.
// SDL_SetRelativeMouseMode(SDL_TRUE);
}
void InputManager::BeginFrame() {
m_keysPressed.clear();
m_keysReleased.clear();
m_mousePressed.clear();
m_mouseReleased.clear();
m_wheelX = 0;
m_wheelY = 0;
m_textInput.clear();
// Update mouse delta based on last known position
m_mouseDX = m_mouseX - m_prevMouseX;
m_mouseDY = m_mouseY - m_prevMouseY;
m_prevMouseX = m_mouseX;
m_prevMouseY = m_mouseY;
}
void InputManager::ProcessEvent(const SDL_Event& e) {
switch (e.type) {
case SDL_KEYDOWN: {
if (e.key.repeat) break; // avoid repeat spam for "Pressed"
SDL_Scancode sc = e.key.keysym.scancode;
m_keysDown.insert(sc);
m_keysPressed.insert(sc);
} break;
case SDL_KEYUP: {
SDL_Scancode sc = e.key.keysym.scancode;
m_keysDown.erase(sc);
m_keysReleased.insert(sc);
} break;
case SDL_MOUSEBUTTONDOWN: {
Uint8 b = e.button.button;
m_mouseDown.insert(b);
m_mousePressed.insert(b);
} break;
case SDL_MOUSEBUTTONUP: {
Uint8 b = e.button.button;
m_mouseDown.erase(b);
m_mouseReleased.insert(b);
} break;
case SDL_MOUSEMOTION: {
m_mouseX = e.motion.x;
m_mouseY = e.motion.y;
} break;
case SDL_MOUSEWHEEL: {
// Accumulate wheel per frame
m_wheelX += e.wheel.x;
m_wheelY += e.wheel.y;
} break;
case SDL_TEXTINPUT: {
// text typed this frame
m_textInput += e.text.text;
} break;
default: break;
}
}
bool InputManager::IsKeyDown(SDL_Scancode sc) const {
return m_keysDown.find(sc) != m_keysDown.end();
}
bool InputManager::WasKeyPressed(SDL_Scancode sc) const {
return m_keysPressed.find(sc) != m_keysPressed.end();
}
bool InputManager::WasKeyReleased(SDL_Scancode sc) const {
return m_keysReleased.find(sc) != m_keysReleased.end();
}
bool InputManager::IsMouseDown(Uint8 button) const {
return m_mouseDown.find(button) != m_mouseDown.end();
}
bool InputManager::WasMousePressed(Uint8 button) const {
return m_mousePressed.find(button) != m_mousePressed.end();
}
bool InputManager::WasMouseReleased(Uint8 button) const {
return m_mouseReleased.find(button) != m_mouseReleased.end();
}
void InputManager::StartTextInput() {
if (!m_textInputActive) {
SDL_StartTextInput();
m_textInputActive = true;
}
}
void InputManager::StopTextInput() {
if (m_textInputActive) {
SDL_StopTextInput();
m_textInputActive = false;
}
}
void InputManager::BindAction(const std::string& action, const Binding& binding) {
m_bindings[action].push_back(binding);
}
bool InputManager::QueryBinding(const Binding& b, ButtonState state) const {
switch (b.device) {
case Device::Keyboard: {
auto sc = static_cast<SDL_Scancode>(b.code);
if (state == ButtonState::Down) return IsKeyDown(sc);
if (state == ButtonState::Pressed) return WasKeyPressed(sc);
if (state == ButtonState::Released) return WasKeyReleased(sc);
} break;
case Device::MouseButton: {
auto mb = static_cast<Uint8>(b.code);
if (state == ButtonState::Down) return IsMouseDown(mb);
if (state == ButtonState::Pressed) return WasMousePressed(mb);
if (state == ButtonState::Released) return WasMouseReleased(mb);
} break;
case Device::MouseWheel: {
// Encoding:
// +1/-1 => wheel Y (up/down), +2/-2 => wheel X (right/left)
const int c = b.code;
if (state != ButtonState::Pressed) return false; // wheel is "impulse"
if (c == 1) return m_wheelY > 0;
if (c == -1) return m_wheelY < 0;
if (c == 2) return m_wheelX > 0;
if (c == -2) return m_wheelX < 0;
} break;
}
return false;
}
bool InputManager::GetAction(const std::string& action, ButtonState state) const {
auto it = m_bindings.find(action);
if (it == m_bindings.end()) return false;
for (const auto& b : it->second) {
if (QueryBinding(b, state)) return true;
}
return false;
}