blob: a8cc6615f091a25c759a34cb5d064d825ae2e407 (
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
|
#include "app.h"
#include "states/ids.h"
#include "states/titleState.h"
const sf::Time App::TIME_PER_FRAME = sf::seconds(1.f/60.f);
App::App()
: textures_()
, window_(sf::VideoMode(640, 480), "Demo game" )
, stateStack_( State::Context{&window_, &textures_, &textureFonts_} )
//, player_()
{
window_.setFramerateLimit(60);
registerStates();
stateStack_.push(States::Id::Title);
}
void App::run(){
// Make the app render the current state, so it needs a state stack too
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while(window_.isOpen()){
// TODO Maybe move to a fixed time game loop (I prefer that)
sf::Time dt = clock.restart();
timeSinceLastUpdate += dt;
while (timeSinceLastUpdate > TIME_PER_FRAME)
{
timeSinceLastUpdate -= TIME_PER_FRAME;
processInput();
update(TIME_PER_FRAME);
// Check inside this loop, because stack might be empty before update() call
if (stateStack_.isEmpty()) window_.close();
}
render();
}
}
void App::render(){
window_.clear();
stateStack_.render();
window_.display();
}
void App::update(sf::Time dt){
stateStack_.update(dt);
}
void App::processInput(){
sf::Event event;
while (window_.pollEvent(event))
{
stateStack_.handleEvent(event);
if (event.type == sf::Event::Closed)
window_.close();
}
}
void App::registerStates(){
stateStack_.registerState<TitleState>(States::Id::Title);
}
|