dEngine
Simple 2D C++ game engine
EventSystem.h
Go to the documentation of this file.
1 #ifndef DENGINE_EVENTSYSTEM_H
2 #define DENGINE_EVENTSYSTEM_H
3 
4 #include <SDL.h>
5 #include <vector>
6 #include <functional>
7 #include <unordered_map>
8 #include <string>
9 #include <memory>
10 #include <map>
11 #include "GameStates.h"
12 
13 namespace dengine {
17  struct CustomEventData {
18  // Define custom data fields here
19  int intValue;
20  std::string stringValue;
21  };
22 
26  class EventSystem {
27  public:
28  EventSystem();
29 
30  typedef std::function<void(SDL_Event&)> EventCallback;
31  std::map<Uint32, EventCallback> eventCallbacks;
32 
67  void RegisterEventCallback(Uint32 eventType, EventCallback callback) {
68  eventCallbacks[eventType] = callback;
69  }
70 
75  void DeregisterEventCallback(Uint32 eventType) {
76  eventCallbacks.erase(eventType);
77  }
78 
82  void processEvents(){
83  SDL_Event event;
84 
85  while (SDL_PollEvent(&event)) {
86  if (event.type == SDL_QUIT) {
88  }
89 
90  // Check if there's a registered callback for this event type
91  if (eventCallbacks.find(event.type) != eventCallbacks.end()) {
92  // Call the registered callback function passing the event as an argument
93  eventCallbacks[event.type](event);
94  }
95  }
96  }
97 
101  void cleanup();
102  };
103 }
104 #endif //DENGINE_EVENTSYSTEM_H
void RegisterEventCallback(Uint32 eventType, EventCallback callback)
Function to register an event callback.
Definition: EventSystem.h:67
A struct containing event data.
Definition: EventSystem.h:17
std::function< void(SDL_Event &)> EventCallback
Definition: EventSystem.h:30
void setGameState(int state)
Set the game state. This can be done with an int or with the GAMESTATES enum.
Definition: GameState.cpp:19
void DeregisterEventCallback(Uint32 eventType)
Function to deregister an event callback.
Definition: EventSystem.h:75
std::map< Uint32, EventCallback > eventCallbacks
Definition: EventSystem.h:31
Event System.
Definition: EventSystem.h:26
void processEvents()
Process events, including custom events.
Definition: EventSystem.h:82
User initiated quit.
Definition: GameStates.h:29
std::string stringValue
Definition: EventSystem.h:20
The namespace containing the engine&#39;s code.
Definition: Collider.h:9
static GameState & GetInstance()
Definition: GameState.cpp:23
int intValue
Definition: EventSystem.h:19