-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameLoop.cpp
142 lines (125 loc) · 2.73 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "GameLoop.h"
#include <iostream>
//just call gameloop
GameLoop::GameLoop(void) :
mSpriteSheet()
{
sf::SoundBuffer soundBuffer;
song.openFromFile("Assets/song.wav");
song.play();
song.setLoop(true);
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), "Japanese Ghost Detective");
//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):
if(song.getStatus() == song.Paused)
song.play();
mGame.update();
break;
case(gGAMEOVER):
break;
case(gPAUSED):
song.pause();
default:
break;
}
nextGameTick += SKIP_TICKS;
loops++;
}
//Input Here
while (mWindow.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
mGame.quit();
mWindow.close();
}
if (event.type == sf::Event::LostFocus || event.type == sf::Event::MouseLeft)
{
if(mGameState == gGAME)
mGameState = gPAUSED;
}
switch(mGameState)
{
case(gMENU):
mMenu.input(&event, &mWindow);
break;
case(gGAME):
mGame.input(&event);
break;
case(gCOMPLETE):
case(gGAMEOVER):
mGame.reset();
if(event.type == sf::Event::KeyPressed && sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
mGameState = gMENU;
break;
case(gPAUSED):
if(event.type == sf::Event::KeyPressed)
mGameState = gGAME;
break;
default:
break;
}
}
//calculate interpolation to (try to) smooth rendering between update
interpolation = float(clock.getElapsedTime().asMilliseconds() + SKIP_TICKS - nextGameTick)
/ float(SKIP_TICKS);
//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;
case(gPAUSED):
mGame.draw(&mWindow, 0);
gameOver.setTexture(*mSpriteSheet.getTexture(sPAUSED));
mWindow.draw(gameOver);
break;
case(gCOMPLETE):
gameOver.setTexture(*mSpriteSheet.getTexture(sCOMPLETE));
mWindow.draw(gameOver);
break;
default:
break;
}
mWindow.display();
}
}