-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
77 lines (61 loc) · 2.4 KB
/
Copy pathGame.cpp
File metadata and controls
77 lines (61 loc) · 2.4 KB
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
#include "Game.hpp"
#include "SplashState.hpp"
#include <SFML/Window.hpp>
#include <fstream>
namespace engine{
//Constructor of the Game class
Game::Game(unsigned int width, unsigned int height, std::string title, const bool& fullscreen){
if(fullscreen){
_data->renderWindow.create(sf::VideoMode{width,height}, title, sf::Style::Close | sf::Style::Titlebar | sf::Style::Fullscreen);
}else{
_data->renderWindow.create(sf::VideoMode{width,height}, title, sf::Style::Close | sf::Style::Titlebar);
}
_data->machine.AddState(StateRef(new SplashState(_data)));
_data->renderWindow.setFramerateLimit(FPS);
_data->renderWindow.setVerticalSyncEnabled(true);
//place in middle of screen
try {
_data->renderWindow.setPosition(sf::Vector2i{sf::Vector2u{
sf::VideoMode::getDesktopMode().width / 2 - SCREEN_WIDTH / 2,
sf::VideoMode::getDesktopMode().height / 2 - SCREEN_HEIGHT / 2}});
}catch (const std::exception& e){
// nothing
}
//game highscore files
for(int i = 1; i <= LEVEL_AMOUNT; i++){
std::string file = HIGHSCORE_FILENAME;
file += std::to_string(i);
file += ".txt";
std::ifstream readFile(file);
if(!readFile.good()){
std::ofstream writeFile(file);
writeFile << std::to_string(10000000000.0);
writeFile.close();
}readFile.close();
}
run();
}
//While loop for running the states
void Game::run() {
float newTime, frameTime, interpolation;
float currentTime = _clock.getElapsedTime().asSeconds();
float accumulator = 0.0f;
while(_data->renderWindow.isOpen()){
_data->machine.ProcessStateChanges();
newTime = _clock.getElapsedTime().asSeconds();
frameTime = newTime - currentTime;
if(frameTime > 0.25f){
frameTime = 0.25f;
}
currentTime = newTime;
accumulator += frameTime;
while(accumulator >= dt60){
_data->machine.GetActiveState()->HandleInput();
_data->machine.GetActiveState()->Update(dt60);
accumulator -= dt60;
}
interpolation = accumulator / dt;
_data->machine.GetActiveState()->Draw(interpolation);
}
}
}