mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 04:43:41 +00:00
Implemented event manager
This commit is contained in:
parent
e0c35aa690
commit
d66d860cdc
2
extern/SDL
vendored
2
extern/SDL
vendored
@ -1 +1 @@
|
||||
Subproject commit e027b85cc457556071cbb2f3f1bcf8803c1bc001
|
||||
Subproject commit f6864924f76e1a0b4abaefc76ae2ed22b1a8916e
|
||||
11
include/EventListener.h
Normal file
11
include/EventListener.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL_init.h>
|
||||
#include <SDL3/SDL_events.h>
|
||||
|
||||
class EventListener {
|
||||
public:
|
||||
virtual SDL_AppResult handleEvent(SDL_EventType type, SDL_Event* event) = 0;
|
||||
EventListener() {};
|
||||
virtual ~EventListener() {};
|
||||
};
|
||||
18
include/EventManager.h
Normal file
18
include/EventManager.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "EventListener.h"
|
||||
|
||||
#include <initializer_list>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "SDL3/SDL_events.h"
|
||||
#include "SDL3/SDL_init.h"
|
||||
|
||||
class EventManager {
|
||||
public:
|
||||
void registerListener(EventListener* listener, std::initializer_list<SDL_EventType> eventTypes);
|
||||
SDL_AppResult handleEvent(SDL_Event* event);
|
||||
private:
|
||||
std::map<SDL_EventType, std::vector<EventListener*>> eventListeners = std::map<SDL_EventType, std::vector<EventListener*>>();
|
||||
};
|
||||
@ -7,7 +7,10 @@
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#include "EventManager.h"
|
||||
#include "Manager.h"
|
||||
#include "SDL3/SDL_events.h"
|
||||
#include "SDL3/SDL_init.h"
|
||||
#include "Vector2D.h"
|
||||
#include "Entity.h"
|
||||
#include "RenderManager.h"
|
||||
@ -31,6 +34,7 @@ public:
|
||||
SDL_AppResult init();
|
||||
|
||||
void handleEvents();
|
||||
SDL_AppResult handleEvent(SDL_Event* event);
|
||||
void update(Uint64 frameTime);
|
||||
void render();
|
||||
void clean();
|
||||
@ -47,6 +51,7 @@ public:
|
||||
|
||||
Manager manager;
|
||||
RenderManager renderManager;
|
||||
EventManager eventManager;
|
||||
Map* map; // game specific, might not be needed for all types of games
|
||||
|
||||
ConfigLoader* config;
|
||||
|
||||
5
include/VEGO_Event.h
Normal file
5
include/VEGO_Event.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
enum VEGO_Event {
|
||||
VEGO_Event_Interaction = SDL_EVENT_USER
|
||||
};
|
||||
33
src/EventManager.cpp
Normal file
33
src/EventManager.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "EventManager.h"
|
||||
#include "EventListener.h"
|
||||
#include "SDL3/SDL_events.h"
|
||||
#include "SDL3/SDL_init.h"
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
|
||||
void EventManager::registerListener(EventListener* listener, std::initializer_list<SDL_EventType> eventTypes)
|
||||
{
|
||||
std::ranges::for_each(eventTypes.begin(), eventTypes.end(), [this, &listener](const SDL_EventType& eventType) {
|
||||
if (!this->eventListeners.contains(eventType)) {
|
||||
this->eventListeners.insert({eventType, std::vector<EventListener*>()});
|
||||
}
|
||||
this->eventListeners.at(eventType).emplace_back(listener);
|
||||
});
|
||||
}
|
||||
|
||||
SDL_AppResult EventManager::handleEvent(SDL_Event* event)
|
||||
{
|
||||
SDL_EventType type = (SDL_EventType) event->type;
|
||||
if (this->eventListeners.contains(type)) {
|
||||
auto results = this->eventListeners.at(type) | std::views::transform(
|
||||
[&event](EventListener* listener) {
|
||||
return listener->handleEvent((SDL_EventType) event->type, event);
|
||||
});
|
||||
if (std::ranges::contains(results, SDL_APP_FAILURE))
|
||||
return SDL_APP_FAILURE;
|
||||
if (std::ranges::contains(results, SDL_APP_SUCCESS))
|
||||
return SDL_APP_SUCCESS;
|
||||
}
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
@ -4,6 +4,7 @@
|
||||
#include "AssetManager.h"
|
||||
#include "RenderManager.h"
|
||||
#include <SDL3_mixer/SDL_mixer.h>
|
||||
#include "SDL3/SDL_events.h"
|
||||
#include "SDL3/SDL_init.h"
|
||||
#include "SoundManager.h"
|
||||
#include "Entity.h"
|
||||
@ -14,6 +15,7 @@
|
||||
#include "GameFactory.h"
|
||||
|
||||
#include <VEGO.h>
|
||||
#include <VEGO_Event.h>
|
||||
|
||||
#include "ConfigLoader.h"
|
||||
|
||||
@ -44,6 +46,9 @@ SDL_AppResult GameInternal::init()
|
||||
GameInternal::soundManager = new SoundManager();
|
||||
GameInternal::collisionHandler = new CollisionHandler(manager); // why does this use a referrence, but AssetManager a pointer?
|
||||
|
||||
/// \TODO: from c++26 you (should be able to) can loop through all values of an enum
|
||||
SDL_RegisterEvents(VEGO_Event_Interaction);
|
||||
|
||||
int flags = 0;
|
||||
if (finalConfig.at("fullscreen"))
|
||||
{
|
||||
@ -126,6 +131,10 @@ void GameInternal::handleEvents()
|
||||
}
|
||||
}
|
||||
|
||||
SDL_AppResult GameInternal::handleEvent(SDL_Event* event) {
|
||||
return this->eventManager.handleEvent(event);
|
||||
}
|
||||
|
||||
void GameInternal::update(Uint64 frameTime)
|
||||
{
|
||||
manager.refresh();
|
||||
|
||||
@ -40,7 +40,7 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
||||
return SDL_APP_CONTINUE;
|
||||
return vego::game->handleEvent(event);
|
||||
}
|
||||
|
||||
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user