0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 10:13:42 +00:00

feat: start on InputManager

This commit is contained in:
ezveee 2025-01-16 12:04:38 +01:00
parent 2b06077cd8
commit 873f85d1d2
2 changed files with 61 additions and 0 deletions

22
include/InputManager.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <SDL3/SDL.h>
#include <map>
#include <string>
#include <functional>
#include <vector>
class InputManager {
public:
InputManager();
~InputManager();
void init();
void processEvents();
void registerAction(const std::string& actionName, std::function<void()> callback);
private:
std::map<std::string, std::vector<SDL_EventType>> actionBindings;
std::map<std::string, std::function<void()>> actionCallbacks;
void handleEvent(const SDL_Event& event);
};

39
src/InputManager.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "InputManager.h"
#include <iostream>
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<void()> 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]();
}
}
}
}
}