feat: event emitter
This commit is contained in:
@ -12,6 +12,7 @@
|
|||||||
extern IApplication* CreateApplication();
|
extern IApplication* CreateApplication();
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Engine::Run(std::unique_ptr<IApplication>(CreateApplication()));
|
auto engine = Engine::GetInstance();
|
||||||
|
engine->Run(std::unique_ptr<IApplication>(CreateApplication()));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -2,7 +2,7 @@
|
|||||||
#define APPLICATION_H_
|
#define APPLICATION_H_
|
||||||
|
|
||||||
#include "engine/scene/scene.h"
|
#include "engine/scene/scene.h"
|
||||||
#include "engine/window/event.h"
|
#include "engine/window/event.hpp"
|
||||||
#include "engine/export.h"
|
#include "engine/export.h"
|
||||||
|
|
||||||
class ENGINE_API IApplication {
|
class ENGINE_API IApplication {
|
||||||
|
|||||||
@ -13,15 +13,22 @@
|
|||||||
#include "engine/app/app.h"
|
#include "engine/app/app.h"
|
||||||
#include "engine/export.h"
|
#include "engine/export.h"
|
||||||
|
|
||||||
class ENGINE_API Engine {
|
class ENGINE_API Engine : public EventHandler {
|
||||||
public:
|
public:
|
||||||
static void Run(std::unique_ptr<IApplication> app);
|
static Engine* GetInstance();
|
||||||
|
|
||||||
|
void Run(std::unique_ptr<IApplication> app);
|
||||||
private:
|
private:
|
||||||
static std::unique_ptr<IApplication> s_app;
|
Engine() {}
|
||||||
static std::shared_ptr<Window> s_window;
|
static Engine* s_instance;
|
||||||
static std::unique_ptr<Renderer> s_renderer;
|
|
||||||
static std::shared_ptr<Scene> s_scene;
|
void OnEvent(const Event& event) override;
|
||||||
static bool s_running;
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -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_
|
|
||||||
74
engine/include/engine/window/event.hpp
Normal file
74
engine/include/engine/window/event.hpp
Normal 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_
|
||||||
@ -1,7 +1,7 @@
|
|||||||
#ifndef WINDOW_EVENTS_H_
|
#ifndef WINDOW_EVENTS_H_
|
||||||
#define WINDOW_EVENTS_H_
|
#define WINDOW_EVENTS_H_
|
||||||
|
|
||||||
#include "engine/window/event.h"
|
#include "engine/window/event.hpp"
|
||||||
|
|
||||||
class WindowEvent : public Event {
|
class WindowEvent : public Event {
|
||||||
public:
|
public:
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "engine/window/event.h"
|
#include "engine/window/event.hpp"
|
||||||
|
|
||||||
#define ENGINE_GL_MAJOR_VERSION 4
|
#define ENGINE_GL_MAJOR_VERSION 4
|
||||||
#define ENGINE_GL_MINOR_VERSION 6
|
#define ENGINE_GL_MINOR_VERSION 6
|
||||||
@ -13,7 +13,7 @@
|
|||||||
#define DEFAULT_WIDTH 1024
|
#define DEFAULT_WIDTH 1024
|
||||||
#define DEFAULT_HEIGHT 768
|
#define DEFAULT_HEIGHT 768
|
||||||
|
|
||||||
class Window : public EventDispatcher {
|
class Window : public EventEmitter {
|
||||||
friend class Engine;
|
friend class Engine;
|
||||||
private:
|
private:
|
||||||
Window();
|
Window();
|
||||||
|
|||||||
@ -2,48 +2,67 @@
|
|||||||
|
|
||||||
#include "engine/renderer/core.h"
|
#include "engine/renderer/core.h"
|
||||||
|
|
||||||
#include "engine/window/event.h"
|
#include "engine/window/event.hpp"
|
||||||
#include "engine/renderer/wavefront.h"
|
#include "engine/renderer/wavefront.h"
|
||||||
|
|
||||||
std::unique_ptr<IApplication> Engine::s_app = nullptr;
|
Engine* Engine::s_instance = nullptr;
|
||||||
std::shared_ptr<Window> Engine::s_window = nullptr;
|
|
||||||
std::unique_ptr<Renderer> Engine::s_renderer = nullptr;
|
|
||||||
std::shared_ptr<Scene> Engine::s_scene = nullptr;
|
|
||||||
bool Engine::s_running = false;
|
|
||||||
|
|
||||||
void Engine::Run(std::unique_ptr<IApplication> app) {
|
void Engine::Run(std::unique_ptr<IApplication> app) {
|
||||||
s_scene = std::make_shared<Scene>();
|
m_scene = std::make_shared<Scene>();
|
||||||
s_renderer = std::make_unique<Renderer>(s_scene);
|
m_renderer = std::make_unique<Renderer>(m_scene);
|
||||||
s_window = Window::GetInstance();
|
m_window = Window::GetInstance();
|
||||||
s_app = std::move(app);
|
m_app = std::move(app);
|
||||||
s_running = true;
|
m_running = true;
|
||||||
|
|
||||||
s_app->OnInit(s_scene);
|
m_app->OnInit(m_scene);
|
||||||
s_renderer->Init();
|
m_renderer->Init();
|
||||||
|
|
||||||
s_window->Subscribe<WindowCloseEvent>([&](const WindowCloseEvent& e) {
|
// m_window->Subscribe<WindowCloseEvent>([&](const WindowCloseEvent& e) {
|
||||||
s_running = false;
|
|
||||||
s_app->OnEvent(e);
|
// m_app->OnEvent(e);
|
||||||
});
|
// });
|
||||||
|
|
||||||
s_window->Subscribe<WindowResizeEvent>([&](const WindowResizeEvent& e) {
|
// m_window->Subscribe<WindowResizeEvent>([&](const WindowResizeEvent& e) {
|
||||||
s_renderer->OnWindowResized(e.GetWidth(), e.GetHeight());
|
// m_renderer->OnWindowResized(e.GetWidth(), e.GetHeight());
|
||||||
s_app->OnEvent(e);
|
// m_app->OnEvent(e);
|
||||||
});
|
// });
|
||||||
|
|
||||||
while (s_running) {
|
m_window->Subscribe2(this);
|
||||||
s_window->ProcessEvents();
|
|
||||||
|
|
||||||
s_app->OnUpdate();
|
while (m_running) {
|
||||||
|
m_window->ProcessEvents();
|
||||||
|
|
||||||
s_renderer->Render();
|
m_app->OnUpdate();
|
||||||
|
|
||||||
s_window->SwapBuffers();
|
m_renderer->Render();
|
||||||
|
|
||||||
|
m_window->SwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
s_app->OnShutdown();
|
m_app->OnShutdown();
|
||||||
|
|
||||||
s_window->Destroy();
|
m_window->Destroy();
|
||||||
s_app.reset();
|
m_app.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::OnEvent(const Event& event) {
|
||||||
|
m_app->OnEvent(event);
|
||||||
|
if (event.GetCategory() == Event::EventCategory::WINDOW) {
|
||||||
|
if (event.GetType() == EventType::WINDOW_RESIZE) {
|
||||||
|
auto e = static_cast<const WindowResizeEvent&>(event);
|
||||||
|
m_renderer->OnWindowResized(e.GetWidth(), e.GetHeight());
|
||||||
|
}
|
||||||
|
if (event.GetType() == EventType::WINDOW_CLOSE) {
|
||||||
|
m_running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Engine* Engine::GetInstance() {
|
||||||
|
if (!s_instance) {
|
||||||
|
s_instance = new Engine();
|
||||||
|
}
|
||||||
|
|
||||||
|
return s_instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -101,10 +101,12 @@ void Window::ProcessEvents() {
|
|||||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
||||||
case SDL_EVENT_QUIT:
|
case SDL_EVENT_QUIT:
|
||||||
Dispatch(WindowCloseEvent());
|
Dispatch(WindowCloseEvent());
|
||||||
|
EmitEvent(WindowCloseEvent{});
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_KEY_DOWN:
|
case SDL_EVENT_KEY_DOWN:
|
||||||
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
||||||
Dispatch(WindowCloseEvent());
|
Dispatch(WindowCloseEvent());
|
||||||
|
EmitEvent(WindowCloseEvent{});
|
||||||
}
|
}
|
||||||
if (event.key.scancode == SDL_SCANCODE_F11) {
|
if (event.key.scancode == SDL_SCANCODE_F11) {
|
||||||
bool isFullscreen = SDL_GetWindowFlags(m_handle) & SDL_WINDOW_FULLSCREEN;
|
bool isFullscreen = SDL_GetWindowFlags(m_handle) & SDL_WINDOW_FULLSCREEN;
|
||||||
@ -122,7 +124,9 @@ void Window::ProcessEvents() {
|
|||||||
0,
|
0,
|
||||||
width,
|
width,
|
||||||
height);
|
height);
|
||||||
Dispatch(WindowResizeEvent(static_cast<unsigned int>(m_width), static_cast<unsigned int>(m_height)));
|
auto event = WindowResizeEvent(static_cast<unsigned int>(m_width), static_cast<unsigned int>(m_height));
|
||||||
|
Dispatch(event);
|
||||||
|
EmitEvent(event);
|
||||||
SDL_SetWindowRelativeMouseMode(m_handle, true);
|
SDL_SetWindowRelativeMouseMode(m_handle, true);
|
||||||
SDL_Rect boundaries = {0, 0, m_width, m_height};
|
SDL_Rect boundaries = {0, 0, m_width, m_height};
|
||||||
SDL_SetWindowMouseRect(m_handle, &boundaries);
|
SDL_SetWindowMouseRect(m_handle, &boundaries);
|
||||||
|
|||||||
Reference in New Issue
Block a user