Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / src/systems/input_system.cpp

sdl_runtime_compiler git · main

SDL3 game for running and compiling code at runtime

sdl3 c++ compiler dlopen cmake · first commit 2026-04-19 · last commit 2026-07-03 (3 months ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-runtime-compiler

C++ 72.3% C 26.2%
git clone https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler.gitwget https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler/archive/sdl_runtime_compiler.tar.gz
src/systems/input_system.cpp 6.1 KB · 148 lines raw
#include "core/game.hpp"
#include "entities/player.hpp"
#include "entities/world.hpp"
#include "states/game_state.hpp"
#include "states/menu_state.hpp"
#include "systems/input_system.hpp"

namespace
Nimbin
{

void
InputSystem::handle_Event(Game& game, const SDL_Event& e, FrameInput& input)
{
    switch (e.type) {
        case SDL_EVENT_MOUSE_MOTION:
            input.look_x += (int)e.motion.xrel;
            input.look_y += (int)e.motion.yrel;
            break;

        case SDL_EVENT_MOUSE_WHEEL:
            input.scroll_x += (double)e.wheel.x;
            input.scroll_y += (double)e.wheel.y;
            break;

        case SDL_EVENT_MOUSE_BUTTON_DOWN: {
                if (e.button.button == SDL_BUTTON_LEFT)
                    input.shoot = true;
                if (e.button.button == SDL_BUTTON_RIGHT)
                    input.commands.push_back(InputCommand::Click);
                break;
        }
        case SDL_EVENT_MOUSE_BUTTON_UP: {
                if (e.button.button == SDL_BUTTON_LEFT)
                    input.shoot = false;
                break;
        }
        case SDL_EVENT_KEY_DOWN: {
            SDL_Keycode sym = e.key.key;
            const bool ctrl = (e.key.mod & SDL_KMOD_CTRL);

            if (input.capture_vim && !input.vim_normal_keys) {
                if (ctrl) { input.vim_normal_keys = true; break; }

                     if (sym == SDLK_RETURN)    input.vim_keys.push_back(-1);
                else if (sym == SDLK_ESCAPE)    input.vim_keys.push_back(-2);
                else if (sym == SDLK_BACKSPACE) input.vim_keys.push_back(-3);
                else if (sym == SDLK_TAB)       input.vim_keys.push_back(-4);
                break;
            }
            if (input.capture_typing) {
                if (ctrl) {
                    switch (sym) {
                        case SDLK_W: input.forward  = true; break;
                        case SDLK_A: input.left     = true; break;
                        case SDLK_S: input.backward = true; break;
                        case SDLK_D: input.right    = true; break;
                    }
                }
                else if (sym == SDLK_RETURN) {
                    input.commands.push_back(InputCommand::ConnectPultSubmit);
                }
                else if (sym == SDLK_ESCAPE) {
                    input.commands.push_back(InputCommand::ConnectPultExit);
                }
                else if (sym == SDLK_BACKSPACE && !input.typing_buffer.empty()) {
                    input.typing_buffer.pop_back();
                }
                break;
            }

            if (input.typing_mode) {
                if (sym == SDLK_RETURN)
                    input.commands.push_back(InputCommand::TypingCommit);
                else if (sym == SDLK_ESCAPE)
                    input.commands.push_back(InputCommand::TypingCancel);
                else if (sym == SDLK_BACKSPACE && !input.typing_buffer.empty())
                    input.typing_buffer.pop_back();
                break;
            }


            switch (sym) {
                case SDLK_W: input.forward  = true; break;
                case SDLK_A: input.left     = true; break;
                case SDLK_S: input.backward = true; break;
                case SDLK_D: input.right    = true; break;
                case SDLK_E: input.pick     = true; break;
                case SDLK_X: if (!e.key.repeat)
                                 input.commands.push_back(InputCommand::FocusToggle);
                                                    break;

                case SDLK_F1:     game.change_State(cpputils::make_unique<GameState>()); break;
                case SDLK_F2:     input.commands.push_back(InputCommand::SandboxRestore);break;
                case SDLK_SPACE:  input.commands.push_back(InputCommand::Jump);          break;
                case SDLK_Q:      input.commands.push_back(InputCommand::Drop);          break;
                case SDLK_F:      input.commands.push_back(InputCommand::ShootAll);      break;
                case SDLK_C:      input.commands.push_back(InputCommand::Compile);       break;
                case SDLK_R:      input.commands.push_back(InputCommand::Run);           break;
                case SDLK_P:      input.commands.push_back(InputCommand::DebugPrint);    break;
                case SDLK_T:      input.commands.push_back(InputCommand::TypingStart);   break;
                case SDLK_LSHIFT:
                case SDLK_RSHIFT: input.commands.push_back(InputCommand::FreelookStart); break;
                case SDLK_ESCAPE: if (ctrl) game.change_State(cpputils::make_unique<MenuState>()); break;
                case SDLK_PLUS:
                    if (e.key.mod & SDL_KMOD_CTRL)
                                { input.commands.push_back(InputCommand::NumberInc); }   break;
                case SDLK_MINUS:
                    if (e.key.mod & SDL_KMOD_CTRL)
                                { input.commands.push_back(InputCommand::NumberDec); }   break;
            }
            break;
        }

        case SDL_EVENT_KEY_UP: {
            SDL_Keycode sym = e.key.key;

            switch (sym) {
                case SDLK_W: input.forward  = false; break;
                case SDLK_A: input.left     = false; break;
                case SDLK_S: input.backward = false; break;
                case SDLK_D: input.right    = false; break;
                case SDLK_E: input.pick     = false; break;

                case SDLK_LCTRL: input.vim_normal_keys = false; break;

                case SDLK_LSHIFT:
                case SDLK_RSHIFT:
                     if (!input.capture_typing && !input.capture_vim)
                        input.commands.push_back(InputCommand::FreelookEnd);
                    break;
            }
            break;
        }

        case SDL_EVENT_TEXT_INPUT:
            if (input.capture_vim && !input.vim_normal_keys) {
                for (const char* p = e.text.text; *p; ++p)
                    input.vim_keys.push_back((unsigned char)*p);
            }
            else if (input.capture_typing || input.typing_mode) {
                input.typing_buffer += e.text.text;
            }
            break;
    }
}

} // namespace Nimbin