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