-
Notifications
You must be signed in to change notification settings - Fork 0
/
game-engine.cpp
45 lines (37 loc) · 1.03 KB
/
game-engine.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
#include "game-engine.hpp"
#include <cstdlib>
#include <ctime>
#include "main-menu-state.hpp"
GameEngine::GameEngine(int width, int height, const std::string& title):
data_(std::make_shared<GameData>())
{
srand(time(nullptr));
data_->window_.create(sf::VideoMode(width, height), title, sf::Style::Close | sf::Style::Titlebar);
data_->states_.pushState(statePtr(std::make_unique<MainMenuState>(data_)));
run();
}
void GameEngine::run()
{
sf::Time newTime;
sf::Time frameTime;
sf::Time accumulator;
sf::Time currentTime = clock_.getElapsedTime();
while (data_->window_.isOpen())
{
newTime = clock_.getElapsedTime();
frameTime = newTime - currentTime;
if(frameTime > sf::seconds(0.25))
{
frameTime = sf::seconds(0.25);
}
currentTime = newTime;
accumulator += frameTime;
while(accumulator >= dt)
{
data_->states_.getCurrentState()->handleInput();
data_->states_.getCurrentState()->update(dt);
accumulator -= dt;
}
data_->states_.getCurrentState()->render();
}
}