feat: event emitter

This commit is contained in:
2025-10-22 11:31:42 +02:00
parent 31fbecbb47
commit 71f1b2c6d2
9 changed files with 147 additions and 123 deletions

View File

@ -12,6 +12,7 @@
extern IApplication* CreateApplication();
int main() {
Engine::Run(std::unique_ptr<IApplication>(CreateApplication()));
auto engine = Engine::GetInstance();
engine->Run(std::unique_ptr<IApplication>(CreateApplication()));
return 0;
}

View File

@ -2,7 +2,7 @@
#define APPLICATION_H_
#include "engine/scene/scene.h"
#include "engine/window/event.h"
#include "engine/window/event.hpp"
#include "engine/export.h"
class ENGINE_API IApplication {

View File

@ -13,15 +13,22 @@
#include "engine/app/app.h"
#include "engine/export.h"
class ENGINE_API Engine {
class ENGINE_API Engine : public EventHandler {
public:
static void Run(std::unique_ptr<IApplication> app);
static Engine* GetInstance();
void Run(std::unique_ptr<IApplication> app);
private:
static std::unique_ptr<IApplication> s_app;
static std::shared_ptr<Window> s_window;
static std::unique_ptr<Renderer> s_renderer;
static std::shared_ptr<Scene> s_scene;
static bool s_running;
Engine() {}
static Engine* s_instance;
void OnEvent(const Event& event) override;
private:
std::unique_ptr<IApplication> m_app;
std::shared_ptr<Window> m_window;
std::unique_ptr<Renderer> m_renderer;
std::shared_ptr<Scene> m_scene;
bool m_running;
};

View File

@ -1,81 +0,0 @@
#ifndef EVENT_H_
#define EVENT_H_
#include <functional>
#include <algorithm>
#include <typeindex>
#include <unordered_map>
#include <vector>
enum class EventType {
WINDOW_RESIZE,
WINDOW_CLOSE,
};
class Event {
public:
enum EventCategory {
WINDOW,
// KEYBOARD ...
};
Event(EventCategory category) : m_category(category) {}
virtual ~Event() {}
Event(const Event& event) = default;
inline const EventCategory GetCategory() const { return m_category; }
inline const virtual EventType GetType() const = 0;
private:
EventCategory m_category;
};
class EventDispatcher {
using Type = std::type_index;
using RawFn = std::function<void(const void*)>;
struct Slot { std::size_t id; RawFn fn; };
std::unordered_map<Type, std::vector<Slot>> subs_;
std::size_t next_id_ = 1;
public:
struct Handle {
std::type_index type{typeid(void)};
std::size_t id{0};
explicit operator bool() const { return id != 0; }
};
template<class E, class F>
Handle Subscribe(F&& f) {
auto& vec = subs_[Type(typeid(E))];
Handle h{ Type(typeid(E)), next_id_++ };
// Wrap strongly typed callback into type-erased RawFn
RawFn wrapper = [fn = std::function<void(const E&)>(std::forward<F>(f))](const void* p){
fn(*static_cast<const E*>(p));
};
vec.push_back(Slot{h.id, std::move(wrapper)});
return h;
}
// Unsubscribe with handle
void Unsubscribe(const Handle& h) {
auto it = subs_.find(h.type);
if (it == subs_.end()) return;
auto& vec = it->second;
vec.erase(std::remove_if(vec.begin(), vec.end(),
[&](const Slot& s){ return s.id == h.id; }),
vec.end());
}
// Publish immediately
template<class E>
void Dispatch(const E& e) const {
auto it = subs_.find(Type(typeid(E)));
if (it == subs_.end()) return;
for (auto& slot : it->second) slot.fn(&e);
}
};
#endif // EVENT_H_

View File

@ -0,0 +1,74 @@
#ifndef EVENT_H_
#define EVENT_H_
#include <functional>
#include <algorithm>
#include <typeindex>
#include <unordered_map>
#include <vector>
enum class EventType {
WINDOW_RESIZE,
WINDOW_CLOSE,
};
class Event {
public:
enum EventCategory {
WINDOW,
// KEYBOARD ...
};
Event(EventCategory category) : m_category(category) {}
virtual ~Event() {}
Event(const Event& event) = default;
inline const EventCategory GetCategory() const { return m_category; }
inline const virtual EventType GetType() const = 0;
private:
EventCategory m_category;
};
class EventHandler {
public:
EventHandler() = default;
virtual void OnEvent(const Event& event) = 0;
};
class EventEmitter {
public:
struct Handle {
std::size_t id{0};
explicit operator bool() const { return id != 0; }
};
EventEmitter() = default;
Handle Subscribe2(EventHandler* handler) {
auto slot = Slot{ m_next_id++, handler };
m_subs.push_back(slot);
return Handle{ slot.id };
}
void Unsubscribe2(const Handle& h) {
m_subs.erase(std::remove_if(m_subs.begin(), m_subs.end(),
[&](const Slot& s){ return s.id == h.id; }),
m_subs.end());
}
protected:
void EmitEvent(const Event& event) {
for (auto &sub : m_subs) {
sub.handler->OnEvent(event);
}
}
private:
struct Slot { std::size_t id; EventHandler* handler; };
std::vector<Slot> m_subs;
std::size_t m_next_id = 1;
};
#endif // EVENT_H_

View File

@ -1,7 +1,7 @@
#ifndef WINDOW_EVENTS_H_
#define WINDOW_EVENTS_H_
#include "engine/window/event.h"
#include "engine/window/event.hpp"
class WindowEvent : public Event {
public:

View File

@ -3,7 +3,7 @@
#include <SDL3/SDL.h>
#include <memory>
#include "engine/window/event.h"
#include "engine/window/event.hpp"
#define ENGINE_GL_MAJOR_VERSION 4
#define ENGINE_GL_MINOR_VERSION 6
@ -13,7 +13,7 @@
#define DEFAULT_WIDTH 1024
#define DEFAULT_HEIGHT 768
class Window : public EventDispatcher {
class Window : public EventEmitter {
friend class Engine;
private:
Window();