diff --git a/include/InputManager.h b/include/InputManager.h index ce4a4f4..6aed753 100644 --- a/include/InputManager.h +++ b/include/InputManager.h @@ -96,27 +96,23 @@ public: GRAVE }; + struct InputAction { + std::string name; + std::vector bindings; + std::function callback; + }; + InputManager(); ~InputManager(); void init(); // see if necessary void processEvents(); - void registerAction(const std::string& actionName, EventType eventType, Key key, std::function callback); - bool isKeyPressed(Key key); + void registerAction(const std::string& actionName, const std::vector& keys, std::function callback); private: + std::vector actions; std::map keyMap; - std::map eventMap; - - struct ActionBinding { - std::string actionName; // not strictly necessary, but good for debugging purposes - EventType eventType; - Key key; - std::function callback; - }; - std::vector actionBindings; void initKeyMap(); - void initEventMap(); void handleEvent(const SDL_Event& event); }; \ No newline at end of file diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 2f5cdad..4b4e0f1 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -3,7 +3,6 @@ InputManager::InputManager() { initKeyMap(); - initEventMap(); } InputManager::~InputManager() { @@ -101,34 +100,27 @@ void InputManager::initKeyMap() { }; } -void InputManager::initEventMap() { - eventMap = { - {EventType::KeyDown, SDL_EVENT_KEY_DOWN}, - {EventType::KeyUp, SDL_EVENT_KEY_UP}, - }; -} - -void InputManager::registerAction(const std::string& actionName, EventType eventType, Key key, std::function callback) { - actionBindings.push_back({actionName, eventType, key, callback}); -} - -bool InputManager::isKeyPressed(Key key) { - const bool* state = SDL_GetKeyboardState(nullptr); - auto sdlKey = keyMap[key]; - return state[sdlKey]; +void InputManager::registerAction(const std::string& actionName, const std::vector& keys, std::function callback) { + actions.push_back({actionName, keys, callback}); } void InputManager::processEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { + std::cout << "Event detected: " << event.type << std::endl; handleEvent(event); } } void InputManager::handleEvent(const SDL_Event& event) { - for (const auto& binding : actionBindings) { - if (event.type == eventMap[binding.eventType] && event.key.scancode == keyMap[binding.key]) { - binding.callback(); + if (event.type != SDL_EVENT_KEY_DOWN) return; // TODO: add other events if necessary + + for (const auto& action : actions) { + for (const auto& binding : action.bindings) { + if (event.key.scancode == keyMap[binding]) { + std::cout << "Action triggered: " << action.name << std::endl; + action.callback(); + } } } } \ No newline at end of file diff --git a/src/_Init.cpp b/src/_Init.cpp index cb82136..bab9cd3 100644 --- a/src/_Init.cpp +++ b/src/_Init.cpp @@ -27,7 +27,7 @@ SDL_AppResult SDL_AppIterate(void *appstate) { return SDL_APP_SUCCESS; } - vego::game->handleEvents(); // bad + //vego::game->handleEvents(); // bad Uint64 frameStart = SDL_GetTicks();