|
ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
|
事件总线,缓冲队列模式 更多...
#include <EventBus.h>
Public 类型 | |
| using | HandlerID = uint64_t |
Public 成员函数 | |
| EventBus (const EventBus &)=delete | |
| EventBus & | operator= (const EventBus &)=delete |
| EventBus (EventBus &&)=delete | |
| EventBus & | operator= (EventBus &&)=delete |
静态 Public 成员函数 | |
| template<typename EventType> | |
| static HandlerID | Subscribe (std::function< void(const EventType &)> callback) |
| template<typename EventType> | |
| static void | Unsubscribe (HandlerID id) |
| template<typename EventType> | |
| static void | Emit (const EventType &event) |
| static void | ProcessEvents () |
| template<typename EventType> | |
| static void | Clear () |
| static void | ClearAll () |
| static EventBus & | GetInstance () |
事件总线,缓冲队列模式
所有事件先入队,在游戏循环的统一时刻(ProcessEvents)派发。 避免递归触发、迭代器失效等问题。
使用示例: struct CollisionEvent : public Shit::Event { class GameObject* a; class GameObject* b; };
uint64_t id = Shit::EventBus::Subscribe<CollisionEvent>( [](const CollisionEvent& e) { /* handle *\/ });
Shit::EventBus::Emit(CollisionEvent{nullptr, nullptr}); // 游戏循环结束时: Shit::EventBus::ProcessEvents(); Shit::EventBus::Unsubscribe<CollisionEvent>(id);
| using HandlerID = uint64_t |
|
delete |
|
delete |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
static |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |