mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 13:43:41 +00:00
feat: start on InputManager
This commit is contained in:
parent
2b06077cd8
commit
873f85d1d2
22
include/InputManager.h
Normal file
22
include/InputManager.h
Normal 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
39
src/InputManager.cpp
Normal 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]();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user