From 873f85d1d2b6faa1c2c76cf14f18cbe7534f90c7 Mon Sep 17 00:00:00 2001 From: ezveee Date: Thu, 16 Jan 2025 12:04:38 +0100 Subject: [PATCH] feat: start on InputManager --- include/InputManager.h | 22 ++++++++++++++++++++++ src/InputManager.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 include/InputManager.h create mode 100644 src/InputManager.cpp diff --git a/include/InputManager.h b/include/InputManager.h new file mode 100644 index 0000000..3e33b0f --- /dev/null +++ b/include/InputManager.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include +#include +#include + +class InputManager { +public: + InputManager(); + ~InputManager(); + + void init(); + void processEvents(); + void registerAction(const std::string& actionName, std::function callback); + +private: + std::map> actionBindings; + std::map> actionCallbacks; + void handleEvent(const SDL_Event& event); +}; \ No newline at end of file diff --git a/src/InputManager.cpp b/src/InputManager.cpp new file mode 100644 index 0000000..e0223b4 --- /dev/null +++ b/src/InputManager.cpp @@ -0,0 +1,39 @@ +#include "InputManager.h" +#include + +InputManager::InputManager() {} + +InputManager::~InputManager() { + SDL_Quit(); +} + +void InputManager::init() { + if (SDL_Init(SDL_INIT_EVENTS) != 0) { + std::cerr << "Failed to initialize SDL: " << SDL_GetError() << std::endl; + return; + } +} + +void InputManager::processEvents() { + SDL_Event event; + + while (SDL_PollEvent(&event)) { + handleEvent(event); + } +} + +void InputManager::registerAction(const std::string& actionName, std::function callback) { + actionCallbacks[actionName] = callback; +} + +void InputManager::handleEvent(const SDL_Event& event) { + for (const auto& [actionName, bindings] : actionBindings) { + for (const auto& binding : bindings) { + if (event.type == binding) { + if (actionCallbacks.find(actionName) != actionCallbacks.end()) { + actionCallbacks[actionName](); + } + } + } + } +} \ No newline at end of file