-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
559 lines (465 loc) · 16.2 KB
/
main.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
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include "Game_GCC/GameCodeStd.h"
#include "Game_GCC/Threads.h"
#include "Game_GCC/Sounds.h"
#include "Game_GCC/Pong/Entity.h"
#include "Game_GCC/Pong/GUI.h"
#include "Game_GCC/Pong/Networking.h"
sf::RenderWindow g_window(sf::VideoMode(SCREEN_W, SCREEN_H), "Pong - UENP - 2016");
class Paddle;
class Ball;
void RenderFrame(sf::RenderWindow* render, sf::Time DeltaTime);
void UpdateGameFrame(sf::Time DeltaTime);
void ProcessEvents();
void GameLoopStart();
void CheckCollisions();
bool g_IsProxy = false;
bool GameRunning = false;
bool ExitGame = false;
bool toasted = false;
bool toastNow = false;
bool isSlow = false;
sf::Clock toastClock;
sf::Time toastTimer = sf::Time::Zero;
sf::Time gameLifeTime = sf::Time::Zero;
sf::Time framerate;
sf::Clock slowMotionClock;
NetworkManager* netMgr = new NetworkManager();
GUI* menu = new GUI();
Paddle* p1 = new Paddle();
Paddle* p2 = new Paddle();
Ball* ball = new Ball();
sf::Text goalP1;
sf::Text goalP2;
sf::Text matchesP1;
sf::Text matchesP2;
sf::Text connectionFeedback;//instructions after click on on-line mode buttons
sf::Text debugText;
sf::Text hitCountText;
sf::RectangleShape middleLine;
sf::Thread toastThread(&PlaySoundEffect, "../Assets/Audio/toask_mk_effect.ogg");
sf::Sprite toastSprite;
int p1Points = 0;
int p2Points = 0;
int p1Matchs = 0;
int p2Matchs = 0;
void UpdateGameFrame(sf::Time DeltaTime){
if(!GameRunning){
sf::Packet packet;
std::string join = netMgr->Receive(packet, DeltaTime);
if(join == "join"){
if(netMgr->sender != sf::IpAddress::None){
menu->canStartGame = true;
}
}
}
if(!GameRunning && menu->canStartGame){
if(menu->IsP1P2Game){
}
else if(menu->IsP1CPUGame){
p2->AI = true;
p2->target = ball;
ball->p2 = p2;
}
else if(menu->IsProxy){
g_IsProxy = true;
p1->IsProxy = true;
ball->IsProxy = true;
ball->reseting = false;
//p2->controlledByP1 = true;
}
else{
p2->IsProxy = true;
//p1->controlledByP1 = true;
//send ball vX ?
}
GameRunning = true;
menu->startBtnClicked = false;
//TODO how to hide or dispose the menu?
}
if(!GameRunning){
// Update SFGUI with elapsed seconds since last call.
menu->Update(DeltaTime);
return;
}
//increase ball acceleration based on player points or game time
//that takes 1.5min to reach to the max velocity
if(gameLifeTime > sf::milliseconds(2000)){
if(ball->vX < ball->maxVel){
if(ball->vX < 0){
ball->vX -= 1;
}else{
ball->vX += 1;
}
}
//netMgr->SendMessage("addballvel"); // it seems auto sync with the proxy
if(ball->vX > ball->maxVel){
ball->vX = ball->maxVel;
}
gameLifeTime = sf::Time::Zero;
}
//game is fast && ball collided top or bottom && player distance is far way then ball
//collision with side screen
if(ball->vX >= ball->maxVel*0.75f && (ball->hitBottom || ball->hitTop)){
framerate = sf::seconds(1.f/30.f);
isSlow = true;
slowMotionClock.restart();
}
if(isSlow && slowMotionClock.getElapsedTime().asSeconds() > 2){
isSlow = false;
framerate = sf::seconds(1.f/60.f);
}
if(toastClock.getElapsedTime().asSeconds() >= 2){
toastNow = false;
}
if(!toasted && ball->hitCount >= 8 ||
(toasted && ball->hitCount >= 5 && ball->vX >= ball->maxVel * 0.6f)){
toastNow = true;
toastClock.restart();
}
if(toastNow && !toasted){
toastThread.launch();
toasted = true;
}
ball->Update(DeltaTime);
p1->Update(DeltaTime);
p2->Update(DeltaTime);
debugText.setString(TOSTRING(ball->vX));
//hitCountText.setString(TOSTRING(ball->hitCount));
if(g_IsProxy){
sf::Packet packet;
std::string cmd = "";// = netMgr->Receive(packet, DeltaTime);
sf::IpAddress tempSenderIp;
unsigned short tempSenderPort;
while(netMgr->sock->receive(packet, tempSenderIp, tempSenderPort) == sf::Socket::Done){
packet >> cmd;
std::cout << "RECEIVED KKK = " << cmd << "\n";
if(cmd == "addp1"){
break;
}
}
if(cmd == "addp1"){
p1Points += 1;
std::cout << "entrou addp1 recebido\n";
std::cout << "p1Points = " << p1Points << "\n";
goalP1.setString(TOSTRING(p1Points));
}
else if(cmd == "addp2"){
p2Points += 1;
goalP2.setString(TOSTRING(p2Points));
}
else if(cmd == "match1"){
p1Matchs++;
std::cout << "entrou p1Matchs recebido\n";
std::cout << "p1Matchs = " << p1Matchs << "\n";
matchesP1.setString(TOSTRING(p1Matchs));
}
else if(cmd == "match2"){
p2Matchs++;
matchesP2.setString(TOSTRING(p2Matchs));
}
return;
}
//player points
if(ball->hitLeft){
p2Points += 1;
if(p2Points >= POINTS_TO_WIN){
p1Points =0;
p2Points =0;
p2Matchs++;
matchesP2.setString(TOSTRING(p2Matchs));
netMgr->SendMessage("match2");
}else{
netMgr->SendMessage("addp2");
}
goalP2.setString(TOSTRING(p2Points));
ball->hitLeft = false;
ball->hitCount = 0;
toastNow = false;
toasted = false;
}
if(ball->hitRight){
p1Points += 1;
if(p1Points >= POINTS_TO_WIN){
p1Points =0;
p2Points =0;
p1Matchs++;
matchesP1.setString(TOSTRING(p1Matchs));
netMgr->SendMessage("match1");
}else{
netMgr->SendMessage("addp1");
}
goalP1.setString(TOSTRING(p1Points));
ball->hitRight = false;
ball->hitCount = 0;
toastNow = false;
toasted = false;
}
}
void CheckCollisions(){
ball->CheckCollision(p1, p2);
}
void RenderFrame(sf::RenderWindow* render, sf::Time DeltaTime){
g_window.clear();
if(!GameRunning){
// Update SFGUI with elapsed seconds since last call.
menu->Draw(render, DeltaTime);
if(menu->IsNetworkingGame && menu->startBtnClicked){
connectionFeedback.setString("Aguardando segundo jogador...");
render->draw(connectionFeedback);
}
g_window.display();
return;
}
render->draw(middleLine);
p1->Draw(render, DeltaTime);
p2->Draw(render, DeltaTime);
ball->Draw(render, DeltaTime);
render->draw(goalP1);
render->draw(goalP2);
render->draw(matchesP1);
render->draw(matchesP2);
//render->draw(debugText);
if(isSlow){
render->draw(hitCountText);
}
if(toastNow){
render->draw(toastSprite);
}
g_window.display();
}
int main(){
/* initialize random seed: */
srand (time(NULL));
framerate = sf::seconds(1.f / 60.f);
//Network_ID should be unique by entity
ball->Network_ID = 10;
p1->Network_ID = 20;
p2->Network_ID = 30;
p1->rectangle.setPosition(0, 150);
p2->rectangle.setPosition(SCREEN_W-15, 250);//50
p2->fixedX = SCREEN_W-15;
sf::Image icon;
icon.loadFromFile("../Assets/Art/pong_png.png");
// if (!diego.loadFromFile("../Assets/Art/SpriteSheet/toasty.png"));
// {
// std::cout << "Failed to load ICON\nPress enter to exit..." << std::endl;
// std::cin.ignore(10000, '\n');
// return 0;
// }
g_window.setIcon(128, 128, icon.getPixelsPtr());
sf::Font font;
if (!font.loadFromFile("../Assets/Font/sansation.ttf")){
std::cout << "Font not found at Assets/Font/sansation.ttf" << std::endl;
std::cout << "Press enter to exit..." << std::endl;
std::cin.ignore(10000, '\n');
return 0;
}
sf::Texture toastTexture;
if(!toastTexture.loadFromFile("../Assets/Art/SpriteSheet/toasty.png")){
std::cout << "Failed to load toast texture in ../Assets/Art/SpriteSheet/toasty.png" << std::endl;
std::cout << "Press enter to exit..." << std::endl;
std::cin.ignore(10000, '\n');
}
toastSprite.setTexture(toastTexture);
toastSprite.setTextureRect(sf::IntRect(0,0, 200, 200));
toastSprite.setPosition(SCREEN_W-200, SCREEN_H-200);
connectionFeedback.setFont(font);
connectionFeedback.setPosition(SCREEN_W/4, LOGINSCREEN_W+20);
goalP1.setFont(font);
goalP2.setFont(font);
goalP1.setPosition(20, 3);
goalP2.setPosition(SCREEN_W-32, 3);
goalP1.setString("0");
goalP2.setString("0");
matchesP1.setFont(font);
matchesP1.setPosition((SCREEN_W/2)-45, 3);
matchesP1.setString("0");
matchesP2.setFont(font);
matchesP2.setPosition((SCREEN_W/2)+25, 3);
matchesP2.setString("0");
debugText.setFont(font);
debugText.setFillColor(sf::Color::Red);
debugText.setPosition(SCREEN_W/2, 3);
debugText.setString("-5");
hitCountText.setFont(font);
hitCountText.setFillColor(sf::Color::Green);
hitCountText.setPosition(SCREEN_W/2, SCREEN_H/2);
hitCountText.setString("Slow");
middleLine.setFillColor(sf::Color::White);
middleLine.setSize(sf::Vector2f(10, SCREEN_W));
middleLine.setPosition((SCREEN_W/2) - (middleLine.getSize().x/2), 0);//395
ball->vX = BALL_INITIAL_VEL;//ball->maxVel;//rand() % 3 + 1;// generate random directions
ball->vY = 7;//rand() % 3 + 1;
ball->circle.setPosition(SCREEN_W/2, 200);
//NetworkManager for all
p1->netMgr = netMgr;
p2->netMgr = netMgr;
ball->netMgr = netMgr;
menu->netMgr = netMgr;
menu->Init(&g_window);
// create a thread with func() as entry point
sf::Thread backgroundMusicThread(&PlayMusicLooping, "../Assets/Audio/Music/OriginalTetrisTheme.ogg");
backgroundMusicThread.launch();
g_window.setFramerateLimit(60);
GameLoopStart();
delete menu;
backgroundMusicThread.terminate();
//delete p1;
//delete p2;
//delete ball;
g_window.close();
// Wait until the user presses 'enter' key
std::cout << "\nPress enter to exit..." << std::endl;
std::cin.ignore(10000, '\n');
return 0;
}
void GameLoopStart(){
sf::Clock clock;
sf::Clock gameClock;
sf::Time elapsed = sf::Time::Zero;
while(g_window.isOpen()){
if(ExitGame){
break;
}
elapsed += clock.restart();
if(GameRunning){
gameLifeTime += gameClock.restart();
//std::cout << "gameLifeTime " << gameLifeTime.asMilliseconds() << std::endl;
}
while(elapsed > framerate){
ProcessEvents();
elapsed -= framerate;
CheckCollisions();
UpdateGameFrame(framerate);
}
RenderFrame(&g_window, elapsed);
}
}
void ProcessEvents(){
sf::Event event;
while(g_window.pollEvent(event)){
if(!GameRunning){
menu->ProcessEvents(event);
}
switch(event.type){
case sf::Event::Closed:
{
ExitGame = true;
break;
}
case sf::Event::KeyPressed:
{
if(event.key.code == sf::Keyboard::Escape){
ExitGame = true;
break;
}
if(event.key.code == sf::Keyboard::F1){
framerate = sf::seconds(1.f/30.f);//slow motion :)
}
if(event.key.code == sf::Keyboard::F2){
framerate = sf::seconds(1.f/60.f);
}
if(g_IsProxy && event.key.code == sf::Keyboard::P){
ball->printPosition();
}
if(event.key.code == sf::Keyboard::F){
std::cout << "\ng_IsProxy " << g_IsProxy <<"\n";
}
if(menu->IsNetworkingGame){
if(event.key.code == sf::Keyboard::W || event.key.code == sf::Keyboard::Up){
if(!g_IsProxy){
p1->moving = true;
p1->up = true;
}else{
p2->moving = true;
p2->up = true;
}
}
if(event.key.code == sf::Keyboard::S || event.key.code == sf::Keyboard::Down){
if(!g_IsProxy){
p1->moving = true;
p1->up = false;
}else{
p2->moving = true;
p2->up = false;
}
}
}
else{
if(event.key.code == sf::Keyboard::W){
p1->moving = true;
p1->up = true;
}
if(event.key.code == sf::Keyboard::S){
p1->moving = true;
p1->up = false;
}
if(event.key.code == sf::Keyboard::Up){
p2->moving = true;
p2->up = true;
}
if(event.key.code == sf::Keyboard::Down){
p2->moving = true;
p2->up = false;
}
}
break;
}
case sf::Event::KeyReleased:
{
if(menu->IsNetworkingGame){
if(event.key.code == sf::Keyboard::W ||
event.key.code == sf::Keyboard::S ||
event.key.code == sf::Keyboard::Up ||
event.key.code == sf::Keyboard::Down){
if(!g_IsProxy){
p1->moving = false;
}else{
p2->moving = false;
}
}
}
else{
if(event.key.code == sf::Keyboard::W ||
event.key.code == sf::Keyboard::S){
p1->moving = false;
}
if(event.key.code == sf::Keyboard::Up ||
event.key.code == sf::Keyboard::Down){
p2->moving = false;
}
}
// if(event.key.code == sf::Keyboard::Up || event.key.code == sf::Keyboard::Down ||
// event.key.code == sf::Keyboard::Left || event.key.code == sf::Keyboard::Right){
// ball->moving = false;
// }
break;
}
}
}
}
/**
This is a simple SFML project to test Graphics, Audio, Networking, Game pad
and others things that are specific to this framework, so, you wont
see many OOP rules applied very well
#include "Game_GCC/GameCodeStd.h"
//#include "Game_GCC/Tests/SFMLStuffs/AssetManager.h"
//#include "Game_GCC/Tests/SFMLStuffs/ShaderExample.h"
//#include "Game_GCC/Tests/SFMLGameLoop.h"
#include "Game_GCC/Threads.h"
#include "Game_GCC/Sounds.h"
#include "Game_GCC/Pong/Entity.h"
#include "Game_GCC/Pong/GameApp.h"
sf::RenderWindow g_window(sf::VideoMode(800, 600), "Pong");
int main(){
//ShaderExample();
//GameLoop();
//ThreadsInit();
//SoundsInit("Assets/Audio/Music/TheWalkingDeadFullThemeSong.wav");
//MusicInit("canary.wav");
GameLoopStart();
// Wait until the user presses 'enter' key
std::cout << "\nPress enter to exit..." << std::endl;
std::cin.ignore(10000, '\n');
return 0;
}*/