-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameLoop.cpp
111 lines (96 loc) · 1.89 KB
/
GameLoop.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "GameLoop.h"
#include <iostream>
//just call gameloop
GameLoop::GameLoop(void) :
mSpriteSheet()
{
loop();
}
GameLoop::~GameLoop(void)
{
}
void GameLoop::loop()
{
mGameState = gMENU;
//Initialize variables to regulate update speed
sf::Clock clock;
int nextGameTick = clock.getElapsedTime().asMilliseconds();
int loops;
float interpolation;
//--------------------------------------------
//create window
mWindow.create(sf::VideoMode(WindowWidth, WindowHeight), "Tiny Maze");
//create container classes
Menu mMenu(&mSpriteSheet);
Game mGame(&mSpriteSheet);
//game loop
while (mWindow.isOpen())
{
loops = 0;
mWindow.clear();
sf::Event event;
//controls update speed
while (clock.getElapsedTime().asMilliseconds() > nextGameTick && loops < MAX_FRAMESKIP)
{
//updates Here
switch(mGameState)
{
case(gMENU):
mMenu.update();
break;
case(gGAME):
mGame.update();
break;
case(gGAMEOVER):
break;
default:
break;
}
nextGameTick += SKIP_TICKS;
loops++;
}
//Input Here
while (mWindow.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
mGame.quit();
mWindow.close();
}
switch(mGameState)
{
case(gMENU):
mMenu.input(&event, &mWindow);
break;
case(gGAME):
mGame.input(&event);
break;
case(gGAMEOVER):
break;
default:
break;
}
}
//calculate interpolation to (try to) smooth rendering between update
interpolation = float(clock.getElapsedTime().asMilliseconds() + SKIP_TICKS - nextGameTick)
/ float(SKIP_TICKS);
sf::Sprite gameOver;
//draw methods here
switch(mGameState)
{
case(gMENU):
mMenu.draw(&mWindow);
break;
case(gGAME):
mGame.draw(&mWindow, interpolation);
break;
case(gGAMEOVER):
gameOver.setTexture(*mSpriteSheet.getTexture(sGAMEOVER));
mWindow.draw(gameOver);
break;
default:
break;
}
mWindow.display();
}
}