From dbe642e5e26cf2bd99218f727157b58c7fed286e Mon Sep 17 00:00:00 2001 From: hachem Date: Tue, 12 Aug 2025 16:35:42 +0200 Subject: feat: application manager and EventDispatcher --- src/core/application.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/core/application.cpp (limited to 'src/core/application.cpp') diff --git a/src/core/application.cpp b/src/core/application.cpp new file mode 100644 index 0000000..d98ae4f --- /dev/null +++ b/src/core/application.cpp @@ -0,0 +1,86 @@ +#include "application.h" +#include + +namespace Donut +{ + Application* Application::s_instance = nullptr; + + Application::Application(const std::string& name, int width, int height) + : m_running(true), m_minimized(false) + { + s_instance = this; + + m_window = std::make_unique(name, width, height); + m_window->set_event_callback([this](Event& event) + { + on_event(event); + }); + + on_init(); + } + + Application::~Application() + { + on_shutdown(); + s_instance = nullptr; + } + + void Application::run() + { + while (m_running) + { + if (!m_minimized) + { + on_update(); + on_render(); + } + + m_window->on_update(); + } + } + + void Application::close() + { + m_running = false; + } + + void Application::on_event(Event& event) + { + EventDispatcher dispatcher(event); + dispatcher.dispatch([this, &event](WindowCloseEvent& e) + { + m_running = false; + event.handled = true; + return true; + }); + + dispatcher.dispatch([this, &event](WindowResizeEvent& e) + { + if (e.get_width() == 0 || e.get_height() == 0) + m_minimized = true; + else + m_minimized = false; + + event.handled = true; + return true; + }); + + dispatcher.dispatch([this, &event](KeyPressedEvent& e) + { + if (e.get_key_code() == 256) + close(); + return true; + }); + } + + void Application::on_init() { } + void Application::on_shutdown() { } + + void Application::on_update() + { + } + + void Application::on_render() + { + } +} -- cgit v1.3