aboutsummaryrefslogtreecommitdiff
path: root/src/core/application.cpp
blob: fd04e629119978274891d690d51e83be80e8c7f6 (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
#include "core/application.h"

#include "core/assert.h"
#include "core/log.h"

#include <chrono>

namespace Hsdbg
{
    namespace
    {
        using Clock = std::chrono::steady_clock;

        constexpr float MAX_DELTA_TIME = 0.25f;

        // the run loop has nothing to block on until the window owns it, so it is
        // time boxed for now to keep the app from spinning forever
        constexpr float PLACEHOLDER_RUN_TIME = 1.0f;
    }

    Application* Application::s_instance = nullptr;

    Application::Application(const ApplicationSpec& spec)
        : m_spec(spec)
    {
        HSDBG_ASSERT(s_instance == nullptr, "an Application instance already exists");
        s_instance = this;

        Log::info("{}: starting up", m_spec.name);
    }

    Application::~Application()
    {
        Log::info("{}: shutting down after {} frames", m_spec.name, m_frame_count);

        s_instance = nullptr;
    }

    auto Application::get() -> Application&
    {
        HSDBG_ASSERT(s_instance != nullptr, "no Application instance has been created");
        return *s_instance;
    }

    auto Application::run() -> void
    {
        m_running = true;

        auto last_frame_time = Clock::now();

        while (m_running)
        {
            const auto now = Clock::now();
            const float elapsed = std::chrono::duration<float>(now - last_frame_time).count();
            last_frame_time = now;

            m_delta_time = elapsed < MAX_DELTA_TIME ? elapsed : MAX_DELTA_TIME;
            m_time += m_delta_time;
            m_frame_count += 1;

            update();
        }
    }

    auto Application::close() -> void
    {
        m_running = false;
    }

    auto Application::update() -> void
    {
        if (m_time >= PLACEHOLDER_RUN_TIME)
            close();
    }
}