diff options
| author | hachem <im@hachem.wtf> | 2025-08-12 16:35:42 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-08-12 16:35:42 +0200 |
| commit | 16dff443789c7f1461b46ce52966972ae14c042b (patch) | |
| tree | afbaf901703c05cbb5eb18f3333898eb498d2e54 /src/Core/Application.cpp | |
| parent | ed6de8793f267fb1fc5c9af2e68cd4e573ee1d25 (diff) | |
[add]: Application manager and EventDispatcher
Diffstat (limited to 'src/Core/Application.cpp')
| -rw-r--r-- | src/Core/Application.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/Core/Application.cpp b/src/Core/Application.cpp new file mode 100644 index 0000000..9ca060c --- /dev/null +++ b/src/Core/Application.cpp @@ -0,0 +1,86 @@ +#include "Application.h" +#include <iostream> + +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<Window>(name, width, height); + m_Window->SetEventCallback([this](Event& event) + { + OnEvent(event); + }); + + OnInit(); + } + + Application::~Application() + { + OnShutdown(); + s_Instance = nullptr; + } + + void Application::Run() + { + while (m_Running) + { + if (!m_Minimized) + { + OnUpdate(); + OnRender(); + } + + m_Window->OnUpdate(); + } + } + + void Application::Close() + { + m_Running = false; + } + + void Application::OnEvent(Event& event) + { + EventDispatcher dispatcher(event); + dispatcher.Dispatch<WindowCloseEvent>([this, &event](WindowCloseEvent& e) + { + m_Running = false; + event.Handled = true; + return true; + }); + + dispatcher.Dispatch<WindowResizeEvent>([this, &event](WindowResizeEvent& e) + { + if (e.GetWidth() == 0 || e.GetHeight() == 0) + m_Minimized = true; + else + m_Minimized = false; + + event.Handled = true; + return true; + }); + + dispatcher.Dispatch<KeyPressedEvent>([this, &event](KeyPressedEvent& e) + { + if (e.GetKeyCode() == 256) + Close(); + return true; + }); + } + + void Application::OnInit() { } + void Application::OnShutdown() { } + + void Application::OnUpdate() + { + } + + void Application::OnRender() + { + } +} |
