-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameEngine.cpp
298 lines (250 loc) · 8.59 KB
/
gameEngine.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "gameEngine.h"
GameEngine::GameEngine(GLuint width, GLuint height)
: WIDTH{ width }, HEIGHT{ height } {
center.x = WIDTH / 2;
center.y = HEIGHT / 2;
// build window
sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 2;
window = new sf::RenderWindow(sf::VideoMode(WIDTH, HEIGHT), "DOWNTOWN BAZOOKA", sf::Style::Default, settings);
window->setFramerateLimit(60);
// main systems
graphics = new Graphics(*window);
Resources::get(); // makes sure to load resources
physics = new Physics();
terrain = new Terrain();
physics->terrainGen = terrain;
// init camera
player = new Player(&cam);
cam.transform->setPos(glm::vec3(0.0f, 1.8f, 0.0f));
player->transform->parentAll(cam.transform);
player->spawn(glm::vec3(0.0f, 150.0f, 0.0f), false);
entityManager = new EntityManager(player);
menu = new Menu(player);
window->resetGLStates();
audio = new Audio();
Resources::get().mainTrack.play();
// mouse and window focusing variables
sf::Mouse::setPosition(center, *window);
window->setMouseCursorVisible(false);
window->setKeyRepeatEnabled(false); // so sf::Event::KeyPressed will be more accurate
// initiate game helper class
game = new Game(player->transform);
}
void GameEngine::start() {
frameTime.restart();
while (running) {
GLfloat delta = frameTime.restart().asSeconds();
pollEvents();
getInput();
toggleOptions();
update(delta);
render();
}
}
void GameEngine::pollEvents() {
mouseScroll = 0.0f;
sf::Event e;
while (window->pollEvent(e)) {
switch (e.type) {
case sf::Event::Closed:
running = false;
break;
case sf::Event::Resized:
{ // need these brackets cuz c++ switches are wacky
float w = static_cast<float>(e.size.width);
float h = static_cast<float>(e.size.height);
window->setView(sf::View(sf::FloatRect(0.0f, 0.0f, w, h)));
graphics->resize(e.size.width, e.size.height);
center.x = e.size.width / 2;
center.y = e.size.height / 2;
break;
}
case sf::Event::MouseWheelScrolled:
if (e.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) {
mouseScroll -= e.mouseWheelScroll.delta;
}
break;
default:
break;
}
}
}
void GameEngine::getInput() {
// check and track game focusing
lastFocused = gameFocused;
gameFocused = window->hasFocus() && clickedInside;
// update static input bool arrays
if (gameFocused || lastFocused) {
input.update();
}
if (gameFocused && !lastFocused) {
input.forgetLeftClickThisFrame();
}
// check if clicked inside window this frame
if (!gameFocused && lastFocused) {
clickedInside = false;
}
if (!clickedInside && sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
sf::Vector2i mp = sf::Mouse::getPosition(*window);
clickedInside = mp.x >= 0 && mp.y >= 0;
}
// handle mouse visibility
lastMouseVisible = mouseVisible;
mouseVisible = !gameFocused;
if (mouseVisible != lastMouseVisible) {
// if you call this every frame it messes up mouse icon
window->setMouseCursorVisible(mouseVisible);
}
// get mouse movement and update based on window focus
mouseMove = sf::Vector2i(0, 0);
if (gameFocused) {
mouseMove = input.getMouseMovement(*window, !lastFocused, center);
}
}
void GameEngine::toggleOptions() {
// toggle blur
if (Input::justPressed(sf::Keyboard::Num1)) {
blurring = !blurring;
std::cout << "BLUR " << (blurring ? "ON" : "OFF") << std::endl;
}
// toggle mipmaps
if (Input::justPressed(sf::Keyboard::Num2)) {
mipmapping = !mipmapping;
Resources::get().loadTextures(mipmapping);
std::cout << "MIPMAPS " << (mipmapping ? "ON" : "OFF") << std::endl;
}
// toggle collider wireframe view
if (Input::justPressed(sf::Keyboard::Num3)) {
wireframe = !wireframe;
blurring = !wireframe;
mipmapping = !wireframe;
Resources::get().loadTextures(mipmapping);
std::cout << "PHYSICS DEBUG " << (wireframe ? "ON" : "OFF") << std::endl;
}
// toggle terrain color debug
if (Input::justPressed(sf::Keyboard::Num4)) {
regenerateWorld();
std::cout << "TERRAIN DEBUG " << (terrain->toggleDebugColors() ? "ON" : "OFF") << std::endl;
}
// randomize world seed
if (Input::justPressed(sf::Keyboard::Num5)) {
regenerateWorld();
terrain->setSeed(glm::vec2(Mth::randRange(-10000.0f, 10000.0f), Mth::randRange(-10000.0f, 10000.0f)));
std::cout << "RANDOMIZED WORLD SEED" << std::endl;
}
// recompile shaders if prompted
if (Input::justPressed(sf::Keyboard::Num0)) {
Resources::get().buildShaders();
}
if (Input::justPressed(sf::Keyboard::Tab) && gameFocused) { // && gameFocused cuz alt tab
paused = !paused;
}
if (Input::justPressed(sf::Keyboard::F1)) {
showFPS = !showFPS;
}
}
void GameEngine::update(GLfloat delta) {
// plus and minus keys are on shift layer so use equals and dash for convenience
if (Input::pressed(sf::Keyboard::Equal)) {
mouseScroll += 5.0f * delta;
}
if (Input::pressed(sf::Keyboard::Dash)) {
mouseScroll -= 5.0f * delta;
}
// update menu
running = menu->update(delta);
if (Menu::justClosed()) {
player->spawn(glm::vec3(0.0f, 5.0f, 0.0f), true);
}
if (Menu::justOpened()) {
game->reset();
regenerateWorld();
player->spawn(glm::vec3(0.0f, 150.0f, 0.0f), false);
}
// update camera
cam.behavior = player->isDead() ? CameraMode::DEATH :
menu->getVisible() ? CameraMode::AUTOSPIN : CameraMode::NORMAL;
cam.updateCameraDistance(mouseScroll);
cam.update(mouseMove.x, mouseMove.y, delta);
if (paused) { // dont update main game stuff if paused
return;
}
// update game first because it stores delta time and other useful variables
game->update(delta);
if (Game::requiresWorldRegen()) {
regenerateWorld();
Game::setRequiresWorldRegen(false);
}
// update audio
audio->update(delta);
// update all game entities
entityManager->update(delta);
terrain->update(delta);
// detect and resolve collisions
physics->update(delta);
}
void GameEngine::render() {
if (wireframe) { // maybe later make it only add in colliders near player
Graphics::DEBUG = true;
physics->streamColliderModels();
} else {
Graphics::DEBUG = false;
}
// render main game scene
graphics->renderScene(cam, *terrain, blurring);
// reset states to begin SFML 2D rendering
window->resetGLStates();
// draw UI
menu->draw(*window, showFPS);
// do final post processing of image (draws skybox here as well)
graphics->finalProcessing(cam, blurring);
// swap buffers
window->display();
}
void GameEngine::regenerateWorld() {
terrain->deleteChunks();
physics->rebuildCollisionTree(0.0f);
entityManager->returnAllObjects();
}
// test math utils by drawing a bunch of boxes using each function
void GameEngine::testMathUtils() {
int numTestBoxes = 500000;
float size = 10.0f;
float curTime = fmod(Game::timeSinceStart(), 20.0f);
curTime = 0.0f;
glm::vec3 scale = glm::vec3(1.0f);
for (int i = 0; i < numTestBoxes; ++i) {
glm::vec3 p;
if (curTime < 5.0f) {
p = Mth::randInsideUnitCube() * size;
} else if (curTime < 10.0f) {
p = Mth::randInsideSphere(size);
} else if (curTime < 15.0f) {
p = glm::vec3(Mth::randRange(-size, size), Mth::randRange(-size, size), Mth::randRange(-size, size));
} else {
p = glm::vec3(Mth::rand01()*size*2.0f - size, Mth::rand01()*size*2.0f - size, Mth::rand01()*size*2.0f - size);
}
p.y += size;
glm::mat4 model;
model = glm::translate(model, p);
model = glm::scale(model, scale);
HSBColor color(Mth::rand01(), 1.0f, 1.0f);
Graphics::addToStream(Shape::CUBE_SOLID, model, color.toRGB());
//Graphics::addToStream(true, model, glm::vec3(1.0f));
}
}
// delete all our dumb pointers lol
GameEngine::~GameEngine() {
delete window;
delete graphics;
delete physics;
delete terrain;
delete player;
delete entityManager;
delete menu;
delete audio;
delete game;
}