-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.cpp
More file actions
537 lines (382 loc) · 16.3 KB
/
Copy pathpong.cpp
File metadata and controls
537 lines (382 loc) · 16.3 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#define GL_SILENCE_DEPRECATION
#define STB_IMAGE_IMPLEMENTATION
#ifdef _WINDOWS
#include <GL/glew.h>
#endif
/**
* below is a convenient debug tool
*/
#define LOG(argument) std::cout << argument << '\n'
/**
File path adjusted to relative, only compatible with Debian-based linux
because the packages for glm and sdl are under "SDL2" directory
for all other OS, remove "include/" from headers
**/
#define GL_GLEXT_PROTOTYPES 1
#include "SDL2/SDL.h"
#include "SDL2/SDL_opengl.h"
#include "include/glm/mat4x4.hpp"
#include "include/glm/gtc/matrix_transform.hpp"
#include "include/ShaderProgram.h"
#include "include/stb_image.h"
// —— NEW STUFF for the pong game—— //
#include <ctime> //
#include "cmath" //
#include <random> //
// ——————————————— //
/**
* in the future we could change shaders
* note we also have two sprites used here
* they all have to be absolute paths
* same as other headers, for other OS/envrionments, remove all path components before "include" but keep "include/" itsef
*/
const char V_SHADER_PATH[] = "/home/ren/projects/myGames/include/shaders/vertex_textured.glsl",
F_SHADER_PATH[] = "/home/ren/projects/myGames/include/shaders/fragment_textured.glsl";
/**
* Above is my header setup that I use for all my projects
*/
/**
* Overall design thinking//scafolding
*
* This will be the last single-file project
* -for all future work, switch to more elegant and efficient separate compilation
*
* check y coordinate to wrap around movement of padding
* -the decision of wrap around is for more gameplay fun
* -too hard. Give up on this.
*
* Wall bounce mechanism
* -this is easy, flip y velocity value
*
* Gameover screen?
* -tentatively, stop all movements, thats it. Sprite is too much hassle
*
*
*/
/**
* below are all the new consts we need
*/
float left_side_pos = -4.5f; //where to put the left pad
float right_side_pos = 4.5f; //where to put right pad
float top_bound = 4.2f; //screen boundary
float bot_bound = -4.2f;
bool pause = false; //start and pause the game, toggle on p
bool pve = false; //switch to player vs machine, toggle on e
float delta_time;
float g_previous_ticks = 0.0f;
const float g_ai_speed = 1.3f; // ai speed
float g_left_speed = 2.2f; //player speed
float g_right_speed = 2.2f;
int rounds = 0; //calculate how many rounds have passed
float flip = 1.0f;
float left_flip = 1.0f;
float right_flip = 1.0f;
bool end_game = false;
const int WINDOW_WIDTH = 1280,
WINDOW_HEIGHT = 960;
const float BG_RED = 0.9608f,
BG_BLUE = 0.9608f,
BG_GREEN = 0.9608f,
BG_OPACITY = 1.0f;
const int VIEWPORT_X = 0,
VIEWPORT_Y = 0,
VIEWPORT_WIDTH = WINDOW_WIDTH,
VIEWPORT_HEIGHT = WINDOW_HEIGHT;
// absolute path.
const char BANZAI_SPRITE_FILEPATH[] = "/home/ren/projects/myGames/include/assets/Banzai.png";
const char PLAYER_SPRITE_FILEPATH[] = "/home/ren/projects/myGames/include/assets/Goomba.png";
const float MILLISECONDS_IN_SECOND = 1000.0;
const float MINIMUM_COLLISION_DISTANCE = 0.8f;
const int NUMBER_OF_TEXTURES = 1;
const GLint LEVEL_OF_DETAIL = 0;
const GLint TEXTURE_BORDER = 0;
SDL_Window* g_display_window;
bool g_game_is_running = true;
ShaderProgram g_shader_program;
glm::mat4 g_view_matrix,
g_banzai_matrix,
g_projection_matrix,
g_left_matrix,
g_right_matrix;
GLuint g_goomba_texture_id,
g_banzai_texture_id;
glm::vec3 g_banzai_position = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 g_banzai_movement = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 g_left_position = glm::vec3(left_side_pos, 0.0f, 0.0f); //initialize body on the left side
glm::vec3 g_left_movement = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 g_right_position = glm::vec3(right_side_pos, 0.0f, 0.0f);
glm::vec3 g_right_movement = glm::vec3(0.0f, 0.0f, 0.0f); //initialize body on the right side
float g_banzai_speed = 1.0f;
GLuint load_texture(const char* filepath)
{
int width, height, number_of_components;
unsigned char* image = stbi_load(filepath, &width, &height, &number_of_components, STBI_rgb_alpha);
if (image == NULL)
{
LOG("Unable to load image. Make sure the path is correct.");
assert(false);
}
GLuint textureID;
glGenTextures(NUMBER_OF_TEXTURES, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, LEVEL_OF_DETAIL, GL_RGBA, width, height, TEXTURE_BORDER, GL_RGBA, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
stbi_image_free(image);
return textureID;
}
void initialise()
{
SDL_Init(SDL_INIT_VIDEO);
g_display_window = SDL_CreateWindow("Hello, Collisions!",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT,
SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(g_display_window);
SDL_GL_MakeCurrent(g_display_window, context);
#ifdef _WINDOWS
glewInit();
#endif
glViewport(VIEWPORT_X, VIEWPORT_Y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
g_shader_program.load(V_SHADER_PATH, F_SHADER_PATH);
g_banzai_matrix = glm::mat4(1.0f);
g_left_matrix = glm::mat4(1.0f);
g_left_matrix = glm::translate(g_left_matrix, glm::vec3(left_side_pos, 0.0f, 0.0f)); //keep it to left
g_left_position += g_left_movement;
g_right_matrix = glm::mat4(1.0f);
g_right_matrix = glm::translate(g_left_matrix, glm::vec3(right_side_pos, 0.0f, 0.0f)); //keep it to right
g_right_position += g_right_movement;
g_view_matrix = glm::mat4(1.0f);
g_projection_matrix = glm::ortho(-5.0f, 5.0f, -3.75f, 3.75f, -1.0f, 1.0f);
g_goomba_texture_id = load_texture(BANZAI_SPRITE_FILEPATH);
g_banzai_texture_id = load_texture(PLAYER_SPRITE_FILEPATH);
g_shader_program.set_projection_matrix(g_projection_matrix);
g_shader_program.set_view_matrix(g_view_matrix);
glUseProgram(g_shader_program.get_program_id());
glClearColor(BG_RED, BG_BLUE, BG_GREEN, BG_OPACITY);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void process_input()
{
g_banzai_movement = glm::vec3(0.0f);
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
case SDL_WINDOWEVENT_CLOSE:
g_game_is_running = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_q:
g_game_is_running = false;
break;
case SDLK_p:
pause = !pause; //pause and resume the game
break;
case SDLK_t:
pve = !pve; //switch to pve
break;
default:
break;
}
default:
break;
}
}
const Uint8 *key_state = SDL_GetKeyboardState(NULL);
// if (key_state[SDL_SCANCODE_LEFT])
// {
// g_banzai_movement.x = -1.0f;
// }
// else if (key_state[SDL_SCANCODE_RIGHT]) //we need else for opposite side movement
// {
// g_banzai_movement.x = 1.0f;
// }
if (key_state[SDL_SCANCODE_W] && fabs(g_left_position.y)< top_bound) //this is player on the left, and also check up/down limit
{
g_left_movement.y = 1.0f;
}
else if (key_state[SDL_SCANCODE_S] && fabs(g_left_position.y)< top_bound) //else for opposite direction.
{
g_left_movement.y = -1.0f;
}
if (key_state[SDL_SCANCODE_UP] && !pve && fabs(g_right_position.y)< top_bound) //this is the player on the right, remember, if we have pve, we disable player control
{
g_right_movement.y = 1.0f;
}
else if (key_state[SDL_SCANCODE_DOWN] && !pve && fabs(g_right_position.y)< top_bound) //else for opposite direction.
{
g_right_movement.y = -1.0f;
}
// if (glm::length(g_banzai_movement) > 1.0f)
// {
// g_banzai_movement = glm::normalize(g_banzai_movement);
// }
}
float measure_distance(glm::vec3 &position_a, glm::vec3 &position_b) //
{ //
// ————————————————— Distance Formula ———————————————————————— //
return sqrt( //
pow(position_b[0] - position_a[0], 2) + //this is return (x1 - x2)^2 + (y1 - y2)^2 < detection range.
pow(position_b[1] - position_a[1], 2) //
); //
}
float detect_y_direction(glm::vec3 &position_a, glm::vec3 &position_b) //
{ //
// ————————————————— Distance Formula ———————————————————————— //
return position_a[1] - position_b[1]; //
//
}
void ai_control(){ //only call it when pve is true, we control movement of right player with ai
float range = measure_distance(g_banzai_position, g_right_position);
if(g_banzai_movement.y>0){ //a smart(dumb) ai that can speed up with banzai, and does not move if banzai not flying to it
if(g_banzai_movement.x<2.0f){
g_right_movement.y = 1.0f;
}else{
if(range<2.5f){
g_right_movement.y = 1.0f * detect_y_direction(g_banzai_position, g_right_position);
}else{
g_right_movement.y = 1.5f;
}
}
}else if(g_banzai_movement.y<0){
if(g_banzai_movement.x<2.0f){
g_right_movement.y = -1.0f;
}else{
if(range<2.5f){
g_right_movement.y = -1.0f * detect_y_direction(g_banzai_position, g_right_position);
}else{
g_right_movement.y = -1.5f;
}
} }else{
g_right_movement.y = 0.0f; //stand still
}
}
void timer(){ //must call this in update() so time will flow!
float ticks = (float) SDL_GetTicks() / 1000.0F; // get the current number of ticks
delta_time = ticks - g_previous_ticks; // the delta time is the difference from the last frame
g_previous_ticks = ticks;
}
// ————————————————————————— NEW STUFF ———————————————————————————— //
bool check_collision(glm::vec3 &position_a, glm::vec3 &position_b) //
{ //
// ————————————————— Distance Formula ———————————————————————— //
return sqrt( //
pow(position_b[0] - position_a[0], 2) + //this is return (x1 - x2)^2 + (y1 - y2)^2 < detection range.
pow(position_b[1] - position_a[1], 2) //
) < MINIMUM_COLLISION_DISTANCE; //
} //
// ———————————————————————————————————————————————————————————————— //
float rand_factor(){
// Seed the random number generator
std::random_device rd;
std::mt19937 gen(rd());
// Define a distribution for floating point numbers between -1.0f and 1.0f
std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
// Generate a random float number
float random_number = dist(gen);
// Output the random number
return random_number;
}
void banzai_control(){ //this one is fully automated
//this section is mid flight
//this section is collision
if(check_collision(g_banzai_position, g_left_position) || check_collision(g_banzai_position, g_right_position)){
g_banzai_movement.y *= rand_factor();
rounds++;
std::cout << std::time(nullptr) << ": Collision.\n"; //
}
g_banzai_movement.x = -1.0f * g_banzai_speed + (-0.01f)*sqrt(rounds);
g_banzai_movement.y =(-1.0f * g_banzai_speed + (-0.01f)*sqrt(rounds));
}
void detect_end_game(){
if(fabs(g_banzai_position.x)>5.0f){
end_game = true;
}
}
void update()
{
if(!end_game) { //end game!
detect_end_game();
timer();
if (!pause){ //when we pause we can still control the Goomba! fun!
banzai_control();
if (pve) {
ai_control();
}
}
g_banzai_matrix = glm::mat4(1.0f);
g_banzai_position += g_banzai_movement * g_banzai_speed * delta_time*flip;
g_banzai_matrix = glm::translate(g_banzai_matrix, g_banzai_position);
if(check_collision(g_banzai_position, g_right_position)|| check_collision(g_banzai_position, g_left_position)){
g_banzai_matrix = glm::rotate(g_banzai_matrix, glm::radians(180.0f), glm::vec3(0.0f, 1.0f, 0.0f));
flip = -flip;
} //this is 180 deg turn
if(fabs(g_banzai_position.y)>=4.0f){
} //this is 180 deg turn
g_left_matrix = glm::mat4(1.0f);
g_left_position += g_left_movement * g_left_speed * delta_time*left_flip;
g_left_matrix = glm::translate(g_left_matrix, g_left_position);
if(fabs(g_left_position.y)>=4.0f){
left_flip = -left_flip;
} //this is 180 deg turn
g_right_matrix = glm::mat4(1.0f);
g_right_position += g_right_movement * g_right_speed * delta_time*right_flip;
g_right_matrix = glm::translate(g_right_matrix, g_right_position);
if(fabs(g_right_position.y)>=4.0f){
right_flip = -right_flip;
} //this is 180 deg turn
}
// —————————————————————— NEW STUFF ——————————————————————— //
// if (check_collision(g_banzai_position, g_left_position)) //
// { //
// std::cout << std::time(nullptr) << ": Collision.\n"; //
// } //
// —————————————————————————————————————————————————————————//
}
void draw_object(glm::mat4 &object_model_matrix, GLuint &object_texture_id)
{
g_shader_program.set_model_matrix(object_model_matrix);
glBindTexture(GL_TEXTURE_2D, object_texture_id);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT);
float vertices[] = {
-0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f
};
float texture_coordinates[] = {
0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
};
glVertexAttribPointer(g_shader_program.get_position_attribute(), 2, GL_FLOAT, false, 0, vertices);
glEnableVertexAttribArray(g_shader_program.get_position_attribute());
glVertexAttribPointer(g_shader_program.get_tex_coordinate_attribute(), 2, GL_FLOAT, false, 0, texture_coordinates);
glEnableVertexAttribArray(g_shader_program.get_tex_coordinate_attribute());
draw_object(g_banzai_matrix, g_goomba_texture_id);
draw_object(g_left_matrix, g_banzai_texture_id);
draw_object(g_right_matrix, g_banzai_texture_id);
glDisableVertexAttribArray(g_shader_program.get_position_attribute());
glDisableVertexAttribArray(g_shader_program.get_tex_coordinate_attribute());
SDL_GL_SwapWindow(g_display_window);
}
void shutdown() { SDL_Quit(); }
int main(int argc, char* argv[])
{
initialise();
while (g_game_is_running)
{
process_input();
update();
render();
}
shutdown();
return 0;
}