I am making a C++ sdl based game engine, currently doing the EventManager and the event…
- User
I am making a C++ sdl based game engine, currently doing the EventManager and the event system, this is what I have now:
Here’s a broad overview of how the classes and their methods could be structured within your event-driven system. This structure is designed to maintain clarity, flexibility, and separation of concerns while leveraging C++ features for efficient and effective event management.
EventManager
class EventManager { public: void Subscribe(EventType type, std::function<void(std::shared_ptr)> handler); void Publish(std::shared_ptr event); void Update(); // Processes the event queue and dispatches events
private: std::mutex queueMutex; std::queue<std::shared_ptr> eventQueue; std::map<EventType, std::vector<std::function<void(std::shared_ptr)>>> subscribers; };
In this setup:
- Subscribe allows different parts of the game engine (e.g., InputManager) to subscribe to specific event types with a callback function.
- Publish adds events to the queue. In a real-time game, you’d likely call this method in response to SDL events or other input sources.
- Update is called every game loop iteration to dispatch and clear processed events. It ensures thread-safe access to the event queue.
Event Base Class and Subclasses
class Event { public: virtual ~Event() = default; virtual EventType GetType() const = 0; // Common event data and methods protected: int priority; };
class MouseButtonEvent : public Event { public: EventType GetType() const override { return EventType::MouseButton; } // Mouse button event-specific data and methods };
class KeyboardEvent : public Event { public: EventType GetType() const override { return EventType::Keyboard; } // Keyboard event-specific data and methods };
// Additional Event subclasses follow the same pattern.
In this structure:
- Each Event subclass implements GetType(), returning its specific EventType. It allows EventManager to correctly dispatch the event to subscribers interested in that type.
- You can extend the hierarchy with more event types as needed, following the same pattern.
Usage with InputManager (Consumer Example)
class InputManager { public: InputManager(EventManager& eventManager) { // Subscribe to specific event types eventManager.Subscribe(EventType::Keyboard, [this](std::shared_ptr event){ this->HandleKeyboardEvent(std::static_pointer_cast(event)); }); }
void HandleKeyboardEvent(std::shared_ptr<KeyboardEvent> event) { // Process the keyboard event } // Other event handling methods};
In this example:
- InputManager subscribes to events by providing a lambda that wraps its handling method. For events it is interested in, it casts the Event pointer to the correct type using std::static_pointer_cast.
- Assistant
- User
I like this but I found it to be complex, can you explain to be the subscriber snippet and how do you make sure only Event (and its subclasses) are going to be subscribed for?
- Assistant
- User
I understand, is this assert really needed or could be just bloating the already working good code? If there is an specific useful case to keep it, Where do I add this static_assert?
- Assistant
- User
I see it is a compilation only check with I don't mind about it, since my compilation time is already less than 1 second, since it won't affect runtime then I think it wouldn't be a problem to add it, but remember this is going to be compiled into an .so or .dll, will this also lead to a compilation error when a user in their own project using my library as base try to subscribe with a wrong T type or this will only work inside my own code?
- Assistant
Adding the
static_assertto enforce inheritance fromEventfor the template parameterTin yourSubscribemethod is indeed only a compile-time check and has no runtime overhead. Yes, this will also lead to a compilation error for users of your library if they try to subscribe with a type that does not inherit from . This occurs because the check is performed during the compilation of the client code that utilizes your library. - User
I really liked this approach, thanks for sharing it with me, now moving on, I plan on adding a way to import the events from the SDL_PollEvent method, so I can fill the Queue with those, but I also want to add a way for users to add their own events to the Queue maybe after filling it with the SDL_PollEvent, I plan to fill the Queue in the Update method before the locking and dispatching of the events. How can I do this?
- Assistant
- User
Sorry I forgot to add, that I don't want to expose the SDL underlying my own library, I want the user to be abstract of what I use, also for a future or possible change in my own backend to be for example SFML or other, So what I planned to do is Create each sub class for each SDL event, then I guess I could do an SDLConverter to handle with a switch the passage of each SDL_Event to the Event, right?
- Assistant
- User
Why this was made with shared_ptr or a pointer instead of the actual Event object? Is there a reason to take this route in the pub/sub pattern or was it coincidentally?
- Assistant
- User
OK I understood, now why not using normal pointers "*" instead of shared_ptr to deal with this overhead? I haven't use them yet as I only used the normal pointers from C in other parts of the code, will this be a problem doing it with C pointer style?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.