-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.cpp
More file actions
141 lines (112 loc) · 3.55 KB
/
GameManager.cpp
File metadata and controls
141 lines (112 loc) · 3.55 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
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
// system includes
#include <windows.h>
// game engine includes
#include "GameManager.h"
#include "LogManager.h"
#include "WorldManager.h"
#include "GraphicsManager.h"
#include "InputManager.h"
#include "ResourceManager.h"
#include "ObjectList.h"
#include "ObjectListIterator.h"
#include "Clock.h"
#include "EventStep.h"
// constructor, set type, gameover, and gameloop number
df::GameManager::GameManager(){
setType("GameManager");
game_over = false;
game_loop = 0;
}
// overloaded constructor
df::GameManager::GameManager(GameManager const&){
setType("GameManager");
game_over = false;
game_loop = 0;
}
// Get the singleton instance of the GameManager.
df::GameManager &df::GameManager::getInstance(){
static GameManager game_manager;
return game_manager;
}
// Startup all GameManager services.
int df::GameManager::startUp(){
// startup managers
df::LogManager::getInstance().startUp();
df::WorldManager::getInstance().startUp();
df::GraphicsManager::getInstance().startUp();
df::InputManager::getInstance().startUp();
df::ResourceManager::getInstance().startUp();
df::Manager::startUp();
// set up world boundary and view
df::WorldManager::getInstance().setBoundary(df::Box(df::Position(0, 0),
df::GraphicsManager::getInstance().getHorizontal(),
df::GraphicsManager::getInstance().getVertical()));
df::WorldManager::getInstance().setView(df::Box(df::Position(0, 0),
df::GraphicsManager::getInstance().getHorizontal(),
df::GraphicsManager::getInstance().getVertical()));
// write to log
df::LogManager::getInstance().writeLog("GameManager::startUp Managers have been started\n");
return 0;
}
// Shut down GameManager services.
void df::GameManager::shutDown(){
// write to log
df::LogManager::getInstance().writeLog("GameManager::shutDown Managers are being shutdown\n");
df::InputManager::getInstance().shutDown();
df::GraphicsManager::getInstance().shutDown();
df::WorldManager::getInstance().shutDown();
df::LogManager::getInstance().shutDown();
df::Manager::shutDown();
}
// Run game loop.
void df::GameManager::run(){
df::LogManager::getInstance().writeLog("GameManager::run GameManager is running the game\n");
// get clock
game_loop = 0;
df::WorldManager &wm = df::WorldManager::getInstance();
df::GraphicsManager &gm = df::GraphicsManager::getInstance();
Clock cl;
int i = 0;
// run game loops
while (!getGameOver()){
// send step event to all objects
EventStep es = EventStep(i);
df::Manager::onEvent(&es);
// get input
df::InputManager::getInstance().getInput();
// update world
wm.update();
// draw world to screen
wm.draw();
// swap buffers and show player new screen
gm.swapBuffers();
// add one to loop
i++;
// get split and sleep extra time if finished in less than default frame time
int delta = cl.split();
if (delta > 0 && delta < FRAME_TIME_DEFAULT)
Sleep(FRAME_TIME_DEFAULT - delta);
frame_time = cl.delta();
// write to lock how long game loop took
df::LogManager::getInstance().writeLog("GameManager::run Frame took %d milliseconds\n", frame_time);
game_loop++;
}
}
// Set game over status to indicated value.
// If true (the default), will stop game loop.
void df::GameManager::setGameOver(bool game_over){
this->game_over = game_over;
}
// Get game over status.
bool df::GameManager::getGameOver() const{
return game_over;
}
// Return frame time.
// Frame time is target time for game loop, in milliseconds.
int df::GameManager::getFrameTime() const{
return frame_time;
}
// returns the game loop that the game is on
int df::GameManager::getGameLoop() const{
return game_loop;
}