hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/src/core/application.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/application.cpp')
-rw-r--r--src/core/application.cpp86
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..d98ae4f
--- /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->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<WindowCloseEvent>([this, &event](WindowCloseEvent& e)
+ {
+ m_running = false;
+ event.handled = true;
+ return true;
+ });
+
+ dispatcher.dispatch<WindowResizeEvent>([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<KeyPressedEvent>([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()
+ {
+ }
+}