-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.cpp
138 lines (98 loc) · 3.06 KB
/
GameManager.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
//---------------------------------------------------------------------//
// GameManager.cpp //
// Used to intialize and release all other manager //
// Contains the game loop as well as the Update and Render functions //
// Used to make sure all functions are called in the correct order //
// //
// By: Ather Omar //
//---------------------------------------------------------------------//
#include "GameManager.h"
#include <time.h>
//-----------------------------------------------------------
// QuickSDL
//-----------------------------------------------------------
namespace QuickSDL {
//Initializing to NULL
GameManager* GameManager::sInstance = NULL;
GameManager* GameManager::Instance() {
//Create a new instance if no instance was created before
if(sInstance == NULL)
sInstance = new GameManager();
return sInstance;
}
void GameManager::Release() {
delete sInstance;
sInstance = NULL;
}
GameManager::GameManager() {
srand(time(0));
mQuit = false;
//Initialize SDL
mGraphics = Graphics::Instance();
// Quits the game if SDL fails to initialize
if(!Graphics::Initialized())
mQuit = true;
//Initialize AssetManager
mAssetMgr = AssetManager::Instance();
//Initialize InputManager
mInputMgr = InputManager::Instance();
//Initialize AudioManager
mAudioMgr = AudioManager::Instance();
//Initialize Timer
mTimer = Timer::Instance();
mScreenMgr = ScreenManager::Instance();
}
GameManager::~GameManager() {
ScreenManager::Release();
mScreenMgr = NULL;
AudioManager::Release();
mAudioMgr = NULL;
AssetManager::Release();
mAssetMgr = NULL;
Graphics::Release();
mGraphics = NULL;
InputManager::Release();
mInputMgr = NULL;
Timer::Release();
mTimer = NULL;
}
void GameManager::EarlyUpdate() {
//Updating the input state before any other updates are run to make sure the Input check is accurate
mInputMgr->Update();
}
void GameManager::Update() {
//GameEntity Updates should happen here
mScreenMgr->Update();
}
void GameManager::LateUpdate() {
//Any collision detection should happen here
mInputMgr->UpdatePrevInput();
mTimer->Reset();
}
void GameManager::Render() {
//Clears the last frame's render from the back buffer
mGraphics->ClearBackBuffer();
//All other rendering is to happen here
mScreenMgr->Render();
//Renders the current frame
mGraphics->Render();
}
void GameManager::Run() {
while(!mQuit) {
mTimer->Update();
while(SDL_PollEvent(&mEvents) != 0) {
//Checks if the user quit the game
if(mEvents.type == SDL_QUIT) {
mQuit = true;
}
}
//Limits the frame rate to FRAME_RATE
if(mTimer->DeltaTime() >= (1.0f / FRAME_RATE)) {
EarlyUpdate();
Update();
LateUpdate();
Render();
}
}
}
}