-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
74 lines (61 loc) · 1.44 KB
/
Game.cpp
File metadata and controls
74 lines (61 loc) · 1.44 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
#include "Game.h"
#include "SplashScreen.h"
sf::Font Game::_font;
std::shared_ptr<Screen> Game::screen = std::make_shared<SplashScreen>();
Game::Game()
{
game_over = false;
sprintf_s(ver,"0.0.1");
sprintf_s(title,"Game %s", ver);
_font.loadFromFile("fonts/chango.ttf");
}
void Game::run()
{
sf::Clock clock1;
sf::Clock clock2;
sf::RenderWindow window(sf::VideoMode(window_x, window_y), title);
printf("Running Game\n");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
break;
}
}
if (game_over) {
window.close();
}
elapsed = clock1.getElapsedTime();
// Render section of the game loop
if (elapsed.asSeconds() >= (0.02)) {
//Lock game speed.
//Take input here
Game::screen->getInput(window);
//Update logic here.
Game::screen->update();
clock1.restart();
}
elapsed = clock2.getElapsedTime();
if (elapsed.asSeconds() >= (/*1/fpsMax*/ 0/02)) {
//Set up FPS counter.
//Lock render speed.
fps = float(1)/elapsed.asSeconds();
clock2.restart();
render(window);
}
else {
sf::Time sleeptime = sf::Time(sf::seconds(((1/fpsMax) - elapsed.asSeconds())*0.5));
sf::sleep(sleeptime);
}
}
}
void Game::render(sf::RenderWindow &window) {
window.clear();
Game::screen->render(window);
window.display();
}
Game::~Game()
{
}