From cd0c08dc7ce754473178ad3f32f7740a1dddc1eb Mon Sep 17 00:00:00 2001 From: Vaino Kauppila Date: Thu, 2 Jul 2026 22:58:25 +0300 Subject: Initial import: LibreForts (M0-M5 in progress) Open-source Forts clone: custom C++20 engine + data-driven (Lua) 2D physics artillery RTS. - Building: node/strut graph on a stiff mass-spring solver (canon Forts model), triangulation rigidity, axial + 30-degree angle-stress breaking, cascading collapse, fire (DoT + spread), ground destroys debris. - Weapons (M4): cannon (ballistic) + laser (beam/ignite) with select-and-aim-in- arc UX; splash / beam damage; bg-brace passthrough. - Devices + economy (M5): reactors + win/loss + restart, mines/turbines/battery, metal deposits, storage caps, per-shot energy cost. - Data-driven via Lua/sol2 with a layered mod loader; enemy-fort scenario mod. - Renderer: SDL3 + textures + ImGui dev UI. stb_image + ImGui vendored. - Assets: CC0 placeholders only; real game art loaded at runtime from the user's own install via forts: paths (bring-your-own; nothing copyrighted committed). See README.md / CLAUDE.md and research/ + requirements/ for detail. --- src/engine/input.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/engine/input.cpp (limited to 'src/engine/input.cpp') 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]; +} -- cgit v1.3