forked from Barnold1953/GraphicsTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainGame.cpp
More file actions
304 lines (252 loc) · 10.8 KB
/
Copy pathMainGame.cpp
File metadata and controls
304 lines (252 loc) · 10.8 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
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
299
300
301
302
303
304
#define _CRT_SECURE_NO_WARNINGS // To shut up the compiler about sprintf...
#include "MainGame.h"
#include <Bengine/Bengine.h>
#include <Bengine/ResourceManager.h>
#include <SDL/SDL.h>
#include <random>
#include <ctime>
#include <algorithm>
#include <cmath>
// Some helpful constants.
const float DESIRED_FPS = 60.0f; // FPS the game is designed to run at
const int MAX_PHYSICS_STEPS = 6; // Max number of physics steps per frame
const float MS_PER_SECOND = 1000; // Number of milliseconds in a second
const float DESIRED_FRAMETIME = MS_PER_SECOND / DESIRED_FPS; // The desired frame time per frame
const float MAX_DELTA_TIME = 1.0f; // Maximum size of deltaTime
MainGame::~MainGame() {
// Empty
}
void MainGame::run() {
init();
initBalls();
// Start our previousTicks variable
Uint32 previousTicks = SDL_GetTicks();
// Game loop
while (m_gameState == GameState::RUNNING) {
m_fpsLimiter.begin();
processInput();
// Calculate the frameTime in milliseconds
Uint32 newTicks = SDL_GetTicks();
Uint32 frameTime = newTicks - previousTicks;
previousTicks = newTicks; // Store newTicks in previousTicks so we can use it next frame
// Get the total delta time
float totalDeltaTime = (float)frameTime / DESIRED_FRAMETIME;
int i = 0; // This counter makes sure we don't spiral to death!
// Loop while we still have steps to process.
while (totalDeltaTime > 0.0f && i < MAX_PHYSICS_STEPS) {
// The deltaTime should be the the smaller of the totalDeltaTime and MAX_DELTA_TIME
float deltaTime = std::min(totalDeltaTime, MAX_DELTA_TIME);
// Update all physics here and pass in deltaTime
update(deltaTime);
// Since we just took a step that is length deltaTime, subtract from totalDeltaTime
totalDeltaTime -= deltaTime;
// Increment our frame counter so we can limit steps to MAX_PHYSICS_STEPS
i++;
}
m_camera.update();
draw();
m_fps = m_fpsLimiter.end();
}
}
void MainGame::init() {
Bengine::init();
m_screenWidth = 1920;
m_screenHeight = 1080;
m_window.create("Ball Game", m_screenWidth, m_screenHeight, 0);
glClearColor(0.0, 0.0, 0.0, 1.0);
m_camera.init(m_screenWidth, m_screenHeight);
// Point the camera to the center of the screen
m_camera.setPosition(glm::vec2(m_screenWidth / 2.0f, m_screenHeight / 2.0f));
m_spriteBatch.init();
// Initialize sprite font
m_spriteFont = std::make_unique<Bengine::SpriteFont>("Fonts/chintzy.ttf", 40);
// Compile our texture shader
m_textureProgram.compileShaders("Shaders/textureShading.vert", "Shaders/textureShading.frag");
m_textureProgram.addAttribute("vertexPosition");
m_textureProgram.addAttribute("vertexColor");
m_textureProgram.addAttribute("vertexUV");
m_textureProgram.linkShaders();
m_fpsLimiter.setMaxFPS(60.0f);
initRenderers();
}
void MainGame::initRenderers() {
m_ballRenderers.push_back(std::make_unique<BallRenderer>());
m_ballRenderers.push_back(std::make_unique<MomentumBallRenderer>());
m_ballRenderers.push_back(std::make_unique<VelocityBallRenderer>(m_screenWidth, m_screenHeight));
m_ballRenderers.push_back(std::make_unique<TrippyBallRenderer>(m_screenWidth, m_screenHeight));
}
struct BallSpawn {
BallSpawn(const Bengine::ColorRGBA8& colr,
float rad, float m, float minSpeed,
float maxSpeed, float prob) :
color(colr),
radius(rad),
mass(m),
randSpeed(minSpeed, maxSpeed),
probability(prob) {
// Empty
}
Bengine::ColorRGBA8 color;
float radius;
float mass;
float probability;
std::uniform_real_distribution<float> randSpeed;
};
#include <iostream>
void MainGame::initBalls() {
// Initialize the grid
m_grid = std::make_unique<Grid>(m_screenWidth, m_screenHeight, CELL_SIZE);
#define ADD_BALL(p, ...) \
totalProbability += p; \
possibleBalls.emplace_back(__VA_ARGS__);
// Number of balls to spawn
const int NUM_BALLS = 20000;
// Random engine stuff
std::mt19937 randomEngine((unsigned int)time(nullptr));
std::uniform_real_distribution<float> randX(0.0f, (float)m_screenWidth);
std::uniform_real_distribution<float> randY(0.0f, (float)m_screenHeight);
std::uniform_real_distribution<float> randDir(-1.0f, 1.0f);
// Add all possible balls
std::vector <BallSpawn> possibleBalls;
float totalProbability = 0.0f;
/// Random values for ball types
std::uniform_real_distribution<float> r1(2.0f, 6.0f);
std::uniform_int_distribution<int> r2(0, 255);
// Adds the balls using a macro
ADD_BALL(1.0f, Bengine::ColorRGBA8(255, 255, 255, 255),
2.0f, 1.0f, 0.1f, 7.0f, totalProbability);
ADD_BALL(1.0f, Bengine::ColorRGBA8(1, 254, 145, 255),
2.0f, 2.0f, 0.1f, 3.0f, totalProbability);
ADD_BALL(1.0f, Bengine::ColorRGBA8(177, 0, 254, 255),
3.0f, 4.0f, 0.0f, 0.0f, totalProbability)
ADD_BALL(1.0f, Bengine::ColorRGBA8(254, 0, 0, 255),
3.0f, 4.0f, 0.0f, 0.0f, totalProbability);
ADD_BALL(1.0f, Bengine::ColorRGBA8(0, 255, 255, 255),
3.0f, 4.0f, 0.0f, 0.0f, totalProbability);
ADD_BALL(1.0f, Bengine::ColorRGBA8(255, 255, 0, 255),
3.0f, 4.0f, 0.0f, 0.0f, totalProbability);
// Make a bunch of random ball types
for (int i = 0; i < 10000; i++) {
ADD_BALL(1.0f, Bengine::ColorRGBA8(r2(randomEngine), r2(randomEngine), r2(randomEngine), 255),
r1(randomEngine), r1(randomEngine), 0.0f, 0.0f, totalProbability);
}
// Random probability for ball spawn
std::uniform_real_distribution<float> spawn(0.0f, totalProbability);
// Small optimization that sets the size of the internal array to prevent
// extra allocations.
m_balls.reserve(NUM_BALLS);
// Set up ball to spawn with default value
BallSpawn* ballToSpawn = &possibleBalls[0];
for (int i = 0; i < NUM_BALLS; i++) {
// Get the ball spawn roll
float spawnVal = spawn(randomEngine);
// Figure out which ball we picked
for (size_t j = 0; j < possibleBalls.size(); j++) {
if (spawnVal <= possibleBalls[j].probability) {
ballToSpawn = &possibleBalls[j];
break;
}
}
// Get random starting position
glm::vec2 pos(randX(randomEngine), randY(randomEngine));
// Hacky way to get a random direction
glm::vec2 direction(randDir(randomEngine), randDir(randomEngine));
if (direction.x != 0.0f || direction.y != 0.0f) { // The chances of direction == 0 are astronomically low
direction = glm::normalize(direction);
} else {
direction = glm::vec2(1.0f, 0.0f); // default direction
}
// Add ball
m_balls.emplace_back(ballToSpawn->radius, ballToSpawn->mass, pos, direction * ballToSpawn->randSpeed(randomEngine),
Bengine::ResourceManager::getTexture("Textures/circle.png").id,
ballToSpawn->color);
// Add the ball do the grid. IF YOU EVER CALL EMPLACE BACK AFTER INIT BALLS, m_grid will have DANGLING POINTERS!
m_grid->addBall(&m_balls.back());
}
}
void MainGame::update(float deltaTime) {
m_ballController.updateBalls(m_balls, m_grid.get(), deltaTime, m_screenWidth, m_screenHeight);
}
void MainGame::draw() {
// Set the base depth to 1.0
glClearDepth(1.0);
// Clear the color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
// Grab the camera matrix
glm::mat4 projectionMatrix = m_camera.getCameraMatrix();
m_ballRenderers[m_currentRenderer]->renderBalls(m_spriteBatch, m_balls, projectionMatrix);
m_textureProgram.use();
// Make sure the shader uses texture 0
GLint textureUniform = m_textureProgram.getUniformLocation("mySampler");
glUniform1i(textureUniform, 0);
GLint pUniform = m_textureProgram.getUniformLocation("P");
glUniformMatrix4fv(pUniform, 1, GL_FALSE, &projectionMatrix[0][0]);
drawHud();
m_textureProgram.unuse();
m_window.swapBuffer();
}
void MainGame::drawHud() {
const Bengine::ColorRGBA8 fontColor(255, 0, 0, 255);
// Convert float to char *
char buffer[64];
sprintf(buffer, "%.1f", m_fps);
m_spriteBatch.begin();
m_spriteFont->draw(m_spriteBatch, buffer, glm::vec2(0.0f, m_screenHeight - 32.0f),
glm::vec2(1.0f), 0.0f, fontColor);
m_spriteBatch.end();
m_spriteBatch.renderBatch();
}
void MainGame::processInput() {
// Update input manager
m_inputManager.update();
SDL_Event evnt;
//Will keep looping until there are no more events to process
while (SDL_PollEvent(&evnt)) {
switch (evnt.type) {
case SDL_QUIT:
m_gameState = GameState::EXIT;
break;
case SDL_MOUSEMOTION:
m_ballController.onMouseMove(m_balls, (float)evnt.motion.x, (float)m_screenHeight - (float)evnt.motion.y);
m_inputManager.setMouseCoords((float)evnt.motion.x, (float)evnt.motion.y);
break;
case SDL_KEYDOWN:
m_inputManager.pressKey(evnt.key.keysym.sym);
break;
case SDL_KEYUP:
m_inputManager.releaseKey(evnt.key.keysym.sym);
break;
case SDL_MOUSEBUTTONDOWN:
m_ballController.onMouseDown(m_balls, (float)evnt.button.x, (float)m_screenHeight - (float)evnt.button.y);
m_inputManager.pressKey(evnt.button.button);
break;
case SDL_MOUSEBUTTONUP:
m_ballController.onMouseUp(m_balls);
m_inputManager.releaseKey(evnt.button.button);
break;
}
}
if (m_inputManager.isKeyPressed(SDLK_ESCAPE)) {
m_gameState = GameState::EXIT;
}
// Handle gravity changes
if (m_inputManager.isKeyPressed(SDLK_LEFT)) {
m_ballController.setGravityDirection(GravityDirection::LEFT);
} else if (m_inputManager.isKeyPressed(SDLK_RIGHT)) {
m_ballController.setGravityDirection(GravityDirection::RIGHT);
} else if (m_inputManager.isKeyPressed(SDLK_UP)) {
m_ballController.setGravityDirection(GravityDirection::UP);
} else if (m_inputManager.isKeyPressed(SDLK_DOWN)) {
m_ballController.setGravityDirection(GravityDirection::DOWN);
} else if (m_inputManager.isKeyPressed(SDLK_SPACE)) {
m_ballController.setGravityDirection(GravityDirection::NONE);
}
// Switch renderers
if (m_inputManager.isKeyPressed(SDLK_1)) {
m_currentRenderer++;
if (m_currentRenderer >= (int)m_ballRenderers.size()) {
m_currentRenderer = 0;
}
}
}