-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
228 lines (149 loc) · 4.33 KB
/
Game.cpp
File metadata and controls
228 lines (149 loc) · 4.33 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "Game.h"
#include "Input.h"
#include "CollisionManager.h"
/*#include "PlayScene.h"
#include "HUDScene.h"
#include "SkyScene.h"
#include "MenuGUIScene.h"
#include "Menu3DScene.h"*/
#include "MenuState.h"
#include "PlayState.h"
#include "Profiler.h"
#include "Compressor.h"
#include "AudioManager.h"
Game* Game::s_xInstance = nullptr;
Game::Game() {
if (s_xInstance != nullptr)
throw std::exception("Singleton class Game has already been instantiated");
s_xInstance = this;
m_fElapsedTime = 0.0f;
m_fTimeScale = 1.0f;
m_iState = (State)-1;
m_xCurrentState = nullptr;
//Compressor::instance()->compress("data\\*", "Data.data");
Compressor::instance()->extract("Data.data", ".");
}
Game::~Game() {
glfwTerminate();
delete m_xRenderer;
/*for (unsigned int i = 0; i < m_xScenes.size(); ++i) {
delete m_xScenes[i];
}*/
if (m_xCurrentState != nullptr)
delete m_xCurrentState;
std::cout << "Memory allocated for GameObjects: " << GameObject::getAllocatedMemorySize() << std::endl;
std::cout << "Memory allocated for Components: " << Component::getAllocatedMemorySize() << std::endl;
if (GameObject::getAllocatedMemorySize() > 0) {
Log::Warn("All GameObjects have not been deallocated");
}
if (Component::getAllocatedMemorySize() > 0) {
Log::Warn("All Components have not been deallocated");
}
Profiler::instance()->logData();
}
Game* Game::instance() {
return s_xInstance;
}
int Game::init() {
glewExperimental = true;
if (!glfwInit()) {
Log::Err("Failed to initialize GLFW");
return -1;
}
glfwWindowHint(GLFW_RESIZABLE, 0);
glfwWindowHint(GLFW_SAMPLES, 4);
/* *
int monitor_count;
GLFWmonitor** monitors = glfwGetMonitors(&monitor_count);
m_xWindow = glfwCreateWindow(1920, 1080, "OpenGL Window", monitor_count > 1 ? monitors[1] : monitors[0], nullptr);
/* */
m_xWindow = glfwCreateWindow(1280, 720, "OpenGL Window", nullptr, nullptr);
glfwMakeContextCurrent(m_xWindow);
glfwGetWindowSize(m_xWindow, &m_vWindowSize.x, &m_vWindowSize.y);
int MajorVersion;
int MinorVersion;
glGetIntegerv(GL_MAJOR_VERSION, &MajorVersion);
glGetIntegerv(GL_MINOR_VERSION, &MinorVersion);
std::cout << "OpenGL v" << MajorVersion << "." << MinorVersion << std::endl;
if(glewInit() != GLEW_OK) {
Log::Err("Failed to initialize GLEW");
return -1;
}
std::cout << "GLEW version " << glewGetString(GLEW_VERSION) << "\n";
m_xRenderer = new Renderer(m_xWindow);
Input::instance()->init(m_xWindow);
_setState(MENU_STATE);
AudioManager::instance()->playMusic("music01.mp3");
return 0;
}
void Game::loop() {
m_iLastTime = m_iStartTime = clock();
m_fElapsedTime = 0.0f;
float delta = 0.0f;
dispatchEvent(Event(Event::GAME_START));
while(!glfwWindowShouldClose(m_xRenderer->getWindow())) {
glfwPollEvents();
update(delta, m_fElapsedTime);
render();
if (m_iNextState != m_iState)
_setState(m_iNextState);
clock_t now = clock();
delta = ((float)(now - m_iLastTime) / (float)CLOCKS_PER_SEC) * m_fTimeScale;
m_fElapsedTime += delta;
m_iLastTime = now;
}
}
void Game::quit() {
glfwSetWindowShouldClose(m_xWindow, true);
}
/*void Game::pause() {
if (m_iState != PAUSE_STATE)
setState(PAUSE_STATE);
else
setState(PLAY_STATE);
}*/
void Game::setState(State state) {
m_iNextState = state;
}
void Game::_setState(State state) {
m_iState = state;
m_iNextState = state;
if (m_xCurrentState != nullptr)
delete m_xCurrentState;
switch (state) {
case MENU_STATE:
m_xCurrentState = new MenuState();
break;
case PLAY_STATE:
m_xCurrentState = new PlayState();
break;
}
m_xCurrentState->start();
std::cout << "Memory allocated for GameObjects: " << GameObject::getAllocatedMemorySize() << std::endl;
std::cout << "Memory allocated for Components: " << Component::getAllocatedMemorySize() << std::endl;
}
Game::State Game::getState() const {
return m_iState;
}
void Game::update(float delta, float elapsedTime) {
PROFILE_START
Input::instance()->update();
m_xCurrentState->update(delta, elapsedTime);
PROFILE_END
}
void Game::render() {
PROFILE_START
m_xCurrentState->render(m_xRenderer);
glfwSwapBuffers(m_xWindow);
PROFILE_END
}
Event Game::makeGameEvent(Event::Type type) {
Event e(type);
e.game.delta = 0.0f;
e.game.elapsedTime = m_fElapsedTime;
e.game.state = m_iState;
return e;
}
const glm::ivec2& Game::getWindowSize() const {
return m_vWindowSize;
}