-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cpp
105 lines (78 loc) · 1.92 KB
/
App.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "App.h"
#include "Config.h"
#include "States/MainMenuState.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <numeric>
#include <chrono>
App::App()
: m_appData(new AppData(*this))
{
m_outOfFocus = false;
m_settings.antialiasingLevel = 8;
m_appData->window.create(
sf::VideoMode(Config::ScreenWidth, Config::ScreenHeight),
Config::AppTitle,
Config::WindowStyle
);
m_appData->stateManager.AddState(StateRef(new MainMenuState(m_appData)));
}
void App::ProcessArguments(const std::vector<std::string>& args)
{
}
void App::Init()
{
m_appData->resources.LoadFont("chewy", Config::ChewyFont);
}
void App::Exit()
{
m_appData->window.close();
}
void App::HandleEvents()
{
static sf::Event event;
while (m_appData->window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::GainedFocus:
m_outOfFocus = false;
m_appData->stateManager.GetCurrentState()->Resume();
break;
case sf::Event::LostFocus:
m_outOfFocus = true;
m_appData->stateManager.GetCurrentState()->Pause();
break;
case sf::Event::Closed:
Exit();
break;
}
m_appData->stateManager.GetCurrentState()->HandleEvent(event);
}
}
int App::Run()
{
using std::chrono::steady_clock;
const float FPS = 1.f / Config::FRAME_RATE;
auto prev = steady_clock::now();
while (m_appData->window.isOpen())
{
auto now = steady_clock::now();
std::chrono::duration<float> deltaTime = now - prev;
if (deltaTime.count() < FPS)
{
continue;
}
prev = now;
m_appData->stateManager.Update();
HandleEvents();
if (!m_outOfFocus)
{
m_appData->stateManager.GetCurrentState()->HandleInput();
m_appData->stateManager.GetCurrentState()->FixedUpdate(FPS);
m_appData->stateManager.GetCurrentState()->Update(deltaTime.count());
m_appData->stateManager.GetCurrentState()->Draw();
}
}
return 0;
}