aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/input.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/input.cpp')
-rw-r--r--src/engine/input.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/engine/input.cpp b/src/engine/input.cpp
new file mode 100644
index 0000000..ffa8076
--- /dev/null
+++ b/src/engine/input.cpp
@@ -0,0 +1,101 @@
+#include "engine/input.hpp"
+#include "imgui_impl_sdl3.h"
+
+void Input::begin_frame() {
+ // Save previous state
+ for (int i = 0; i < SDL_SCANCODE_COUNT; i++) {
+ key_prev_[i] = key_state_[i];
+ key_just_pressed_[i] = false;
+ key_just_released_[i] = false;
+ }
+ mouse_prev_ = mouse_pos_;
+ mouse_delta_ = glm::vec2(0.0f);
+ scroll_ = 0.0f;
+ for (int i = 0; i < 8; i++) {
+ mouse_prev_state_[i] = mouse_state_[i];
+ mouse_pressed_[i] = false;
+ mouse_released_[i] = false;
+ }
+
+ // Pump SDL events
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ ImGui_ImplSDL3_ProcessEvent(&event); // let the dev UI see input too
+ switch (event.type) {
+ case SDL_EVENT_QUIT:
+ quit_ = true;
+ break;
+
+ case SDL_EVENT_KEY_DOWN: {
+ auto sc = event.key.scancode;
+ if (sc < SDL_SCANCODE_COUNT) {
+ if (!key_state_[sc]) key_just_pressed_[sc] = true;
+ key_state_[sc] = true;
+ }
+ break;
+ }
+ case SDL_EVENT_KEY_UP: {
+ auto sc = event.key.scancode;
+ if (sc < SDL_SCANCODE_COUNT) {
+ key_just_released_[sc] = true;
+ key_state_[sc] = false;
+ }
+ break;
+ }
+
+ case SDL_EVENT_MOUSE_MOTION:
+ mouse_pos_ = glm::vec2(event.motion.x, event.motion.y);
+ mouse_delta_ = glm::vec2(event.motion.xrel, event.motion.yrel);
+ break;
+
+ case SDL_EVENT_MOUSE_BUTTON_DOWN:
+ if (event.button.button < 8) {
+ if (!mouse_state_[event.button.button])
+ mouse_pressed_[event.button.button] = true;
+ mouse_state_[event.button.button] = true;
+ }
+ break;
+ case SDL_EVENT_MOUSE_BUTTON_UP:
+ if (event.button.button < 8) {
+ mouse_released_[event.button.button] = true;
+ mouse_state_[event.button.button] = false;
+ }
+ break;
+
+ case SDL_EVENT_MOUSE_WHEEL:
+ scroll_ = event.wheel.y;
+ break;
+
+ default:
+ break;
+ }
+ }
+}
+
+bool Input::key_down(SDL_Keycode key) const {
+ return key_state_[SDL_GetScancodeFromKey(key, nullptr)];
+}
+
+bool Input::key_pressed(SDL_Keycode key) const {
+ return key_just_pressed_[SDL_GetScancodeFromKey(key, nullptr)];
+}
+
+bool Input::key_released(SDL_Keycode key) const {
+ return key_just_released_[SDL_GetScancodeFromKey(key, nullptr)];
+}
+
+glm::vec2 Input::mouse_pos() const { return mouse_pos_; }
+glm::vec2 Input::mouse_delta() const { return mouse_delta_; }
+float Input::scroll_delta() const { return scroll_; }
+
+bool Input::mouse_down(int button) const {
+ return button < 8 && mouse_state_[button];
+}
+
+bool Input::mouse_pressed(int button) const {
+ return button < 8 && mouse_pressed_[button];
+}
+
+bool Input::mouse_released(int button) const {
+ return button < 8 && mouse_released_[button];
+}