-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
48 lines (37 loc) · 1.03 KB
/
Game.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
#include "Game.h"
#include "GameState.h"
namespace CoreAidan
{
Game::Game(int width, int height, std::string title)
{
_data->window.create(sf::VideoMode(width, height), title, sf::Style::Close | sf::Style::Titlebar);
_data->machine.AddState(StateRef(new GameState(this->_data)));
this->Run();
}
void Game::Run()
{
float newTime, frameTime, interpolation;
float currentTime = this->_clock.getElapsedTime().asSeconds();
float accumulator = 0.0f;
while (this->_data->window.isOpen())
{
this->_data->machine.ProcessStateChanges();
newTime = this->_clock.getElapsedTime().asSeconds();
frameTime = newTime - currentTime;
if (frameTime > 0.25f)
{
frameTime = 0.25f;
}
currentTime = newTime;
accumulator += frameTime;
while (accumulator >= dt)
{
this->_data->machine.GetActiveState()->HandleInput();
this->_data->machine.GetActiveState()->Update(dt);
accumulator -= dt;
}
interpolation = accumulator / dt;
this->_data->machine.GetActiveState()->Draw(interpolation);
}
}
}