-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
479 lines (397 loc) · 12.1 KB
/
main.cpp
File metadata and controls
479 lines (397 loc) · 12.1 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
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <SOIL.h>
#include <SFML/Window.hpp>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <map>
#include "Shader.cpp"
#include "Sprite.h"
#include "Texture.h"
#include "Framebuffer.h"
#include "ParticleSystem.h"
#define GLSL(src) "#version 330 core\n" #src
using namespace std;
using namespace sf;
using namespace glm;
const GLchar* vertexSource = GLSL(
layout(location=0) in vec2 pos;
layout(location=1) in vec2 texc;
out vec2 texcoord;
uniform mat4 model;
uniform mat4 proj;
void main()
{
texcoord = texc;
gl_Position = proj * model * vec4(pos, 0.0, 1.0);
}
);
const GLchar* fragmentSource = GLSL(
in vec2 texcoord;
out vec4 outColor;
uniform vec3 color;
uniform sampler2D tex;
void main()
{
outColor = texture(tex, texcoord) * vec4(color, 1.0);
}
);
const GLchar* frameVSource = GLSL(
layout (location = 0) in vec2 pos;
layout (location = 1) in vec2 texc;
out vec2 texcoord;
uniform bool shake;
uniform float time;
void main()
{
texcoord = texc;
gl_Position = vec4(pos, 0.0, 1.0);
if (shake)
{
float strength = 0.01;
gl_Position.x += strength * cos(10*time);
gl_Position.y += strength * cos(15*time);
}
}
);
const GLchar* frameFSource = GLSL(
in vec2 texcoord;
out vec4 outColor;
uniform sampler2D scene;
uniform bool invert = false;
uniform bool gray = false;
void main()
{
if (invert) {
outColor = vec4(1.0 - vec3(texture(scene, texcoord)), 1.0);
} else if (gray) {
vec4 origColor = texture(scene, texcoord);
float average = 0.2126 * origColor.r + 0.7152 * origColor.g + 0.0722 * origColor.b;
outColor = vec4(average, average, average, 1.0);
} else {
outColor = vec4(vec3(texture(scene, texcoord)), 1.0);
}
}
);
const GLchar* particleVSource = GLSL(
layout(location=0) in vec2 position;
uniform mat4 proj;
uniform mat4 model;
void main()
{
gl_Position = proj * model * vec4(position, 0.0, 1.0);
}
);
const GLchar* particleGSource = GLSL(
layout (points) in;
layout (line_strip, max_vertices = 4) out;
uniform float squareSize = 0.1;
void main()
{
gl_Position = gl_in[0].gl_Position + vec4(-squareSize, squareSize, 0.0, 0.0);
EmitVertex();
gl_Position = gl_in[0].gl_Position + vec4(squareSize, squareSize, 0.0, 0.0);
EmitVertex();
gl_Position = gl_in[0].gl_Position + vec4(squareSize, -squareSize, 0.0, 0.0);
EmitVertex();
gl_Position = gl_in[0].gl_Position + vec4(-squareSize, -squareSize, 0.0, 0.0);
}
);
const GLchar* particleFSource = GLSL(
out vec4 outColor;
uniform vec3 color;
uniform float alpha = 1.0;
void main()
{
outColor = vec4(color, alpha);
}
);
void initGL()
{
glewExperimental = GL_TRUE;
glewInit();
//glEnable(GL_DEPTH_TEST);
//glEnable(GL_STENCIL_TEST);
}
enum GameState {
GAME_ACTIVE,
GAME_MENU,
GAME_WIN,
};
int randUInt(int rmin, int rmax)
{
int mod = rmax - rmin + 1;
int randint = (rand()%mod) + rmin;
return randint;
}
/*int randInt(int rmin, int rmax) //TO BE IMPLEMENTED
{
if (rmin < 0)
{
int rand_sign = (rand()%2) ? -1 : 1;
} else {
return randUInt(rmin, rmax);
}
}*/
class Game
{
public:
// Game state
GameState state;
int width, height;
SpriteRenderer *renderer;
Texture2D BLANK;
map <string, Texture2D> textures;
Framebuffer *fb;
float shakeTime = 0.0;
bool invert;
Sprite *playButton;
Sprite *quitButton;
Sprite *player1;
Sprite *ball;
vec2 ballVel;
Sprite *cursorSpr;
//ParticleSystem *ps;
vec2 mousePos;
vector<Event> events;
double totalTime;
//Clock speedClock;
bool lost = false;
// Constructor/Destructor
Game(int w, int h);
~Game();
// Initialize game state (load all shaders/textures/levels)
void init();
// GameLoop
void update(float dt);
void render();
};
Game::Game(int w, int h)
{
width = w;
height = h;
}
void Game::init()
{
//initialize textures and such here...
srand(time(NULL)); //seed
mat4 proj = ortho(0.0f, static_cast<float>(width), static_cast<float>(height), 0.0f, -1.0f, 1.0f); //projection
Shader spriteShader; //shader for sprites
spriteShader.Compile(vertexSource, fragmentSource);
Shader frameShader; //frame buffer shader
frameShader.Compile(frameVSource, frameFSource);
Shader particleShader;
particleShader.Compile(particleVSource, particleFSource, particleGSource);
fb = new Framebuffer(frameShader, width, height);
playButton = new Sprite(vec2(130, 45), vec2(335, 250)); //button for playing
player1 = new Sprite(vec2(45, 130), vec2(30, 20));
player1->color = vec3(0.0f, 1.0f, 0.0f); //player 1
ball = new Sprite(vec2(70, 70), vec2(randUInt(70, 700), randUInt(70, 500)));
//ball->color = vec3(255, 0, 216);
float magnitude = randUInt(600, 900);
int ang = randUInt(25, 70);
ballVel = vec2(magnitude * cos(ang), magnitude*sin(ang));
//ps = new ParticleSystem(ball->position, particleShader, 10, proj, 3, ballVel);
cursorSpr = new Sprite(vec2(30, 30));
Texture2D Face;
int w, h;
unsigned char* image =
SOIL_load_image("textures\\awesomeface.png", &w, &h, 0, SOIL_LOAD_RGBA);
Face.Generate(w, h, image);
SOIL_free_image_data(image);
textures["face"] = Face;
Texture2D Cat;
image = SOIL_load_image("textures\\cat.jpg", &w, &h, 0, SOIL_LOAD_RGBA);
Cat.Generate(w, h, image);
SOIL_free_image_data(image);
textures["cat"] = Cat;
BLANK.GenerateBlank(); //blank texture
renderer = new SpriteRenderer(spriteShader, proj); //renderer
state = GAME_ACTIVE;
fb->BindTextureBuffer();
}
void Game::update(float dt)
{
totalTime += dt;
fb->shader.SetFloat("time", totalTime);
switch (state)
{
case GAME_ACTIVE:
{
while (events.size() != 0)
{
Event ev = events[events.size() - 1];
events.pop_back();
switch (ev.type)
{
case Event::MouseButtonPressed:
if (player1->contains(mousePos) && (!lost))
{
player1->color = (player1->color == vec3(0.0, 1.0, 0.0)) ?
vec3(1.0, 0.6666, 0.98) : vec3(0.0, 1.0, 0.0);
}
break;
case Event::KeyReleased:
if (ev.key.code == Keyboard::I)
{
if (invert)
{
invert = false;
fb->shader.SetBool("invert", false);
} else {
invert = true;
fb->shader.SetBool("invert", true);
}
}
break;
}
}
if (shakeTime > 0.0)
{
shakeTime -= dt;
} else {
fb->shader.SetBool("shake", false);
}
if (Keyboard::isKeyPressed(Keyboard::Up)) //handle input
{
if ((player1->position.y >= 0.0f) && (!lost))
{
player1->position.y -= 450.0f*dt;
}
}
if (Keyboard::isKeyPressed(Keyboard::Down))
{
if ((player1->position.y + player1->size.y <= 600.0f) && (!lost))
{
player1->position.y += 450.0f*dt;
}
}
//do collisions
vec2 ballCenter = ball->position + (0.5f*ball->size);
float radius = 0.5f*ball->size.x;
if (!lost)
{
if (ballCenter.x + radius >= width) //keep the ball inside the court
{
ballVel.x *= -1.0f;
ball->move(-0.5, 0.0);
} else if (ballCenter.x - radius <= 0) {
ballVel.x *= -1.0f;
ball->move(0.5, 0.0);
lost = true;
fb->shader.SetBool("gray", true);
}
if (ballCenter.y - radius <= 0)
{
ballVel.y *= -1.0f;
ball->move(0, 0.5);
} else if (ballCenter.y + radius >= height) {
ballVel.y *= -1.0f;
ball->move(0, -0.5);
}
}
if (checkCollision(*player1, *ball) && !(lost)) //handle collisions
{
shakeTime = 0.07;
fb->shader.SetBool("shake", true);
if (ballCenter.x <= player1->position.x + player1->size.x)
{
ballVel.y *= -1.0f;
} else {
ballVel.x *= -1.0f;
}
}
if (!lost)
{
ball->move(ballVel*dt);
ball->rotate(dt); //spin ball
}
cursorSpr->position = mousePos - (0.5f*cursorSpr->size);
break;
}
case GAME_MENU:
{
while (events.size() != 0)
{
Event ev = events[events.size() - 1];
events.pop_back();
switch (ev.type)
{
}
}
break;
}
}
}
void Game::render()
{
fb->BeginRender();
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
switch (state)
{
case GAME_ACTIVE:
renderer->drawSprite(textures["face"], *ball);
renderer->drawSprite(BLANK, *player1);
break;
}
fb->EndRender();
fb->Render(true);
}
Game::~Game()
{
delete renderer;
delete player1;
delete ball;
delete cursorSpr;
}
int main()
{
ContextSettings settings; //Create a window
settings.depthBits = 24;
settings.stencilBits = 8;
Game game(800, 600);
game.state = GAME_MENU;
Window window(VideoMode(800, 600), "Pong", Style::Default, settings);
initGL(); //initialize OpenGL
game.init();
Clock clock; //loop
bool running = true;
while (running)
{
Event ev;
while (window.pollEvent(ev))
{
switch(ev.type) //handle events
{
case Event::Closed:
running = false;
break;
case Event::KeyPressed:
if (ev.key.code == Keyboard::Escape)
{
running = false;
} else {
game.events.push_back(ev);
}
break;
default:
game.events.push_back(ev);
break;
}
}
game.mousePos = vec2(Mouse::getPosition(window).x, Mouse::getPosition(window).y);
//update
game.update(clock.restart().asSeconds());
//render
game.render();
window.display();
}
window.close();
//cin.ignore();
//cin.ignore();
return 0;
}