hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/src/core/application.cpp
blob: d98ae4f79357418a920c0f3be3f81f5c3b626db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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()
    {
    }
}