-
Notifications
You must be signed in to change notification settings - Fork 0
/
spel.c
1181 lines (1004 loc) · 34 KB
/
spel.c
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include "menu.h"
#define scoreMax 10
///Screen dimension constants
const int SCREEN_WIDTH = 900;
const int SCREEN_HEIGHT = 700;
const double PI = 3.14159265;
const int FRAME_PER_SECOND = 40;
int Intervall;
///Time controll
int NextTick;
///Starts up SDL and creates window
bool init();
///Loads media
bool loadMedia();
///Frees media and shuts down SDL
void close();
///The window we'll be rendering to
SDL_Window* gWindow = NULL;
///The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
///The image we will load and show on the screen
SDL_Surface* gXOut = NULL;
///The ball
SDL_Surface* Ball = NULL;
///Ball position
SDL_Rect rcball;
///Ball direction
double DirectionX, DirectionY;
///The Player1
SDL_Surface* Player1;
///Player1 position
SDL_Rect rcPlayer1;
///The Player2
SDL_Surface* Player2;
///Player2 position
SDL_Rect rcPlayer2;
///The Player3
SDL_Surface* Player3;
///Player3 position
SDL_Rect rcPlayer3;
///The Player4
SDL_Surface* Player4;
///Player4 position
SDL_Rect rcPlayer4;
SDL_Surface* InvisablePlayer1;
SDL_Surface* InvisablePlayer2;
SDL_Surface* InvisablePlayer3;
SDL_Surface* InvisablePlayer4;
SDL_Surface* wall_p1;
SDL_Rect rcwall_p1;
SDL_Surface* wall_p2;
SDL_Rect rcwall_p2;
SDL_Surface* wall_p3;
SDL_Rect rcwall_p3;
SDL_Surface* wall_p4;
SDL_Rect rcwall_p4;
void resetPlayerPosition(){
///Start position Player1
rcPlayer1.x = SCREEN_WIDTH/2-75;
rcPlayer1.y = SCREEN_HEIGHT-50;
///Start position Player2
rcPlayer2.x = SCREEN_WIDTH/2-75;
rcPlayer2.y = 25;
///Start position Player3
rcPlayer3.x = 25;
rcPlayer3.y = SCREEN_HEIGHT/2-75;
///Start position Player4
rcPlayer4.x = SCREEN_HEIGHT-50;
rcPlayer4.y = SCREEN_WIDTH/2-75;
}
double angleEffect(struct SDL_Rect ball, struct SDL_Rect player, int playernum)
{
int ball_x,ball_y;
if(playernum == 1 || playernum == 2)
{
ball_x = ball.x + ball.w/2;
if(ball_x > (player.x + 50) && ball_x < (player.x + 100)){/// MITTEN 50 pixlar
return 0;
}
else if(ball_x > (player.x + 25) && ball_x < (player.x + 50)){/// vänster mitt
return -15;
}
else if(ball_x > (player.x + 100) && ball_x < (player.x + 125)){/// höger mitt
return 15;
}
else if( ball_x < (player.x + 25) ){ /// längst till vänster
return -30;
}
else if( ball_x > (player.x + 125) ){ ///högersida
return 30;
}
}
else
{
ball_y = ball.y + ball.h/2;
if(ball_y > (player.y + 50) && ball_y < (player.y + 100)){/// MITTEN 50 pixlar
return 0;
}
else if(ball_y > (player.y + 25) && ball_y < (player.y + 50)){/// vänster mitt
return 15;
}
else if(ball_y > (player.y + 100) && ball_y < (player.y + 125)){/// höger mitt
return -15;
}
else if( ball_y < (player.y + 25) ){ /// längst till vänster
return 30;
}
else if( ball_y > (player.y + 125) ){ ///högersida
return -30;
}
}
puts("INGEN EFFEKT!");
return 0;
}
double distance( int x1, int y1, int x2, int y2 ){
return sqrt( pow(x2-x1,2) + pow(y2-y1,2) );
}
bool Collition(struct SDL_Rect player, struct SDL_Rect ball){
int px, py,i;
int ball_x = ball.x + ball.w/2;/// Ball mid X
int ball_y = ball.y + ball.h/2;/// Ball mid Y
int ball_r = ball.w/2;
double dis, dis_y, dis_x;
// Check closest box x from the ball
for(i=0;i<ball.w;i++)
{
if( ball_x < (player.x) ){
px = player.x;
}
else if( ball_x > (player.x + player.w) ){
px = (player.x + player.w);
}
else{
px = ball.x + i;
}
//check closest box y from the ball
if( ball_y < player.y ){
py = player.y;
}
else if( ball_y > (player.y + player.h) ){
py = (player.y + player.h);
}
else{
py = ball.y + i;
}
if( distance(ball_x,ball_y,px,py) < ball_r){
return true;
}
}
return false;
}
///EFFECTS
bool invisable = false;
bool visable = true;
int points_made = 0; /// points made in the game
int invisable_effekt = 0; /// Number on player who gets the effekt
///Ball random
double acc_vel = 5;
double angle = 0, angle2=0;
double scale_x= 0, scale_y=0;
double Resultante=0;
double Vel_x=0, Vel_y=0;
int acc_distance = 0;
///Make the ball move smoothly
int StartPosition_x=0, StartPosition_y=0;
///Restart ball
void newDirectionBall(double angle, struct SDL_Rect &ball)
{
acc_distance = 0;
printf("\nRCBALL.X: %d ", ball.x);
printf("\nRCBALL.Y: %d ", ball.y);
scale_x = cos((PI*angle)/180);
scale_y = (sin((PI*angle)/180))*(-1);
Vel_x = scale_x * acc_vel;
Vel_y = scale_y * acc_vel;
StartPosition_x = ball.x;
StartPosition_y = ball.y;
Resultante = sqrt((scale_x*scale_x)+(scale_y*scale_y));
printf("\nANGLE: %f ", angle);
printf("\nSIN: %f ", scale_y);
printf("\nCOS: %f ", scale_x);
printf("\nRESULANTE: %f ", Resultante);
acc_vel += 0.1;
}
void RestartBall(struct SDL_Rect &ball)/// accumelated velosity
{
acc_vel = 13;
acc_distance = 0;
ball.x = SCREEN_WIDTH/2-30;
ball.y = SCREEN_HEIGHT/2-30;
}
void MoveBall(struct SDL_Rect &ball)
{
ball.x = StartPosition_x + (Vel_x + (Vel_x * acc_distance));
ball.y = StartPosition_y + (Vel_y + (Vel_y * acc_distance));
acc_distance++;
}
void FPS_Init()
{
NextTick = 0;
Intervall = 1*1000/FRAME_PER_SECOND;
return;
}
void FPS_Fn()
{
if(NextTick > SDL_GetTicks())
{
SDL_Delay(NextTick - SDL_GetTicks());
}
NextTick = SDL_GetTicks() + Intervall;
}
//Do not be used in the networking game
void checkEffects(int points, int player)
{ /// Points made in the game, which player point is made on
if(points_made == 5){
invisable = true;
invisable_effekt = player;
}
else{
invisable = false;
}
}
///Score
SDL_Surface* text1;
SDL_Surface* text2;
SDL_Surface* text3;
SDL_Surface* text4;
SDL_Surface* scoreMade;
SDL_Surface* endofgame;
SDL_Surface* Player1_text;
SDL_Surface* Player2_text;
SDL_Surface* Player3_text;
SDL_Surface* Player4_text;
SDL_Color colors[] = {{255,0,0},{255,51,0},{255,102,0},{255,153,0},{255,204,0},{255,255,0},{204,255,0},{153,255,0},{102,255,0},{51,255,0},{0,255,0}};
//{{0,255,0},{51,255,0},{102,255,0},{153,255,0},{204,255,0},{255,255,0},{255,204,0},{255,153,0},{255,102,0},{255,51,0},{255,0,0}};
SDL_Rect rctext1;
SDL_Rect rctext2;
SDL_Rect rctext3;
SDL_Rect rctext4;
SDL_Rect rcscoreMade;
SDL_Rect rcendofgame;
SDL_Rect rcPlayer1_text;
SDL_Rect rcPlayer2_text;
SDL_Rect rcPlayer3_text;
SDL_Rect rcPlayer4_text;
SDL_Color color[2] = {{255,255,255},{255,0,0}};
///Font
TTF_Font* font = NULL;
TTF_Font* font2 = NULL;
TTF_Font* font3 = NULL;
TTF_Font* font4 = NULL;
const char* score[] = {" ","D","D D","D D D","D D D D","D D D D D","D D D D D D","D D D D D D D","D D D D D D D D","D D D D D D D D D","D D D D D D D D D D"};
//{"DDDDDDDDDD","DDDDDDDDD","DDDDDDDD","DDDDDDD","DDDDDD","DDDDD","DDDD","DDD","DD","D","Game Over"}; ///Hearts
///The Music
Mix_Chunk* effect;
Mix_Music* music;
Mix_Chunk* collision;
double test;
bool init()
{
///Initialization flag
bool success = true;
///Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
///Create window
gWindow = SDL_CreateWindow( "Ping-Pong", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
///Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
//Sound
if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 1024) == -1)
{
printf("Mix_GetError()\n");
return -1;
}
///Initialize TTF
if (TTF_Init() == -1)
{
return false;
}
return success;
}
/// Every players points, player1 at points[1], player2 at points[2] ....you get it
int points[5] = {0,9,9,9,9};
bool loadMedia()
{
///Loading success flag
bool success = true;
///Load splash image
gXOut = SDL_LoadBMP( "stars.bmp" );
if( gXOut == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", "m31_small.bmp", SDL_GetError() );
success = false;
}
///Create a window surface
/*gScreenSurface = SDL_GetWindowSurface(gWindow);
///Fill the surface
SDL_FillRect(gScreenSurface, NULL, SDL_MapRGB(gScreenSurface->format, 50, 64, 64));
*/
InvisablePlayer1 = SDL_LoadBMP("block_invisable_p1_p2.bmp");
if(InvisablePlayer1 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "block_invisable_p1_p2.bmp", SDL_GetError() );
success = false;
}
InvisablePlayer2 = SDL_LoadBMP("block_invisable_p1_p2.bmp");
if(InvisablePlayer2 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "block_invisable_p1_p2.bmp", SDL_GetError() );
success = false;
}
InvisablePlayer3 = SDL_LoadBMP("block_invisable_p3_p4.bmp");
if(InvisablePlayer3 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "block_invisable_p3_p4.bmp", SDL_GetError() );
success = false;
}
InvisablePlayer4 = SDL_LoadBMP("block_invisable_p3_p4.bmp");
if(InvisablePlayer4 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "block_invisable_p3_p4.bmp", SDL_GetError() );
success = false;
}
wall_p1 = SDL_LoadBMP("wall_p1.bmp");
if(wall_p1 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "wall_p1.bmp", SDL_GetError() );
success = false;
}
wall_p2 = SDL_LoadBMP("wall_p2.bmp");
if(wall_p2 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "wall_p2.bmp", SDL_GetError() );
success = false;
}
wall_p3 = SDL_LoadBMP("wall_p3.bmp");
if(wall_p3 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "wall_p3.bmp", SDL_GetError() );
success = false;
}
wall_p4 = SDL_LoadBMP("wall_p4.bmp");
if(wall_p4 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "wall_p4.bmp", SDL_GetError() );
success = false;
}
///Load a ball
Ball = SDL_LoadBMP("neon_ball2.bmp");
if(Ball == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "ball4.bmp", SDL_GetError() );
success = false;
}
///Load a Player1
Player1 = SDL_LoadBMP("neon3_p1_p2.bmp");
if(Player1 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "block_p1.bmp", SDL_GetError() );
success = false;
}
///Load a Player2
Player2 = SDL_LoadBMP("neon3_p1_p2.bmp");
if(Player2 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "rectangle_yellow2.bmp", SDL_GetError() );
success = false;
}
Player3 = SDL_LoadBMP("neon3_p3_p4.bmp");
if(Player3 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "rectangle_blue.bmp", SDL_GetError() );
success = false;
}
///Load a Player2
Player4 = SDL_LoadBMP("neon3_p3_p4.bmp");
if(Player3 == NULL)
{
printf( "Unable to load image %s! SDL Error: %s\n", "rectangle4.bmp", SDL_GetError() );
success = false;
}
///Load a Font
font = TTF_OpenFont("good.ttf", 32);
if(font == NULL)
{
printf( "Unable to load font %s! SDL Error: %s\n", "good.ttf", SDL_GetError() );
success = false;
}
font2 = TTF_OpenFont("Jura-Regular.ttf", 20);
if(font2 == NULL)
{
printf( "Unable to load font %s! SDL Error: %s\n", "Jura-Regular.ttf", SDL_GetError() );
success = false;
}
font3 = TTF_OpenFont("Heart.ttf", 20);
if(font3 == NULL)
{
printf( "Unable to load font %s! SDL Error: %s\n", "Heart.ttf", SDL_GetError() );
success = false;
}
font4 = TTF_OpenFont("good.ttf", 60);
if(font4 == NULL)
{
printf( "Unable to load font %s! SDL Error: %s\n", "good.ttf", SDL_GetError() );
success = false;
}
///Load a text
text1 = TTF_RenderText_Blended(font3, score[points[1]], colors[points[1]]);
if(text1 == NULL)
{
return -1;
}
text2 = TTF_RenderText_Blended(font3, score[points[2]], colors[points[2]]);
if(text2 == NULL)
{
return -1;
}
text3 = TTF_RenderText_Blended_Wrapped(font3, score[points[3]], colors[points[3]],30);
if(text3 == NULL)
{
return -1;
}
text4 = TTF_RenderText_Blended_Wrapped(font3, score[points[4]], colors[points[4]],30);
if(text4 == NULL)
{
return -1;
}
scoreMade = TTF_RenderText_Blended(font4, "SCORE!", color[0]);
if(scoreMade==NULL)
{
return -1;
}
Player1_text = TTF_RenderText_Blended(font2, "Player 1: ", color[0]);
if(Player1_text == NULL)
{
return -1;
}
Player2_text = TTF_RenderText_Blended(font2, "Player 2: ", color[0]);
if(Player2_text == NULL)
{
return -1;
}
Player3_text = TTF_RenderText_Blended_Wrapped(font2, "P l a y e r 3", color[0],15);
if(Player3_text == NULL)
{
return -1;
}
Player4_text = TTF_RenderText_Blended_Wrapped(font2, "P l a y e r 4", color[0],15);
if(Player4_text == NULL)
{
return -1;
}
//Blitsurface to save the data of the wall players and ball to send to the server
SDL_BlitSurface(wall_p1, NULL, gScreenSurface, &rcwall_p1);
SDL_BlitSurface(wall_p2, NULL, gScreenSurface, &rcwall_p2);
SDL_BlitSurface(wall_p3, NULL, gScreenSurface, &rcwall_p3);
SDL_BlitSurface(wall_p4, NULL, gScreenSurface, &rcwall_p4);
SDL_BlitSurface(Player1, NULL, gScreenSurface, &rcPlayer1);
SDL_BlitSurface(Player2, NULL, gScreenSurface, &rcPlayer2);
SDL_BlitSurface(Player3, NULL, gScreenSurface, &rcPlayer3);
SDL_BlitSurface(Player4, NULL, gScreenSurface, &rcPlayer4);
SDL_BlitSurface(Ball, NULL, gScreenSurface, &rcball);
rctext1.x = SCREEN_WIDTH/2-rcPlayer1.w/2;
rctext1.y = SCREEN_HEIGHT-20;
rctext2.x = SCREEN_WIDTH/2-rcPlayer2.w/2;
rctext2.y = 0;
rctext3.x = 0;
rctext3.y = SCREEN_HEIGHT/2-rcPlayer1.w/2;
rctext4.x = SCREEN_WIDTH-20;
rctext4.y = SCREEN_HEIGHT/2-rcPlayer1.w/2;
rcscoreMade.x = SCREEN_WIDTH/2-120; /// Center of the screen
rcscoreMade.y = SCREEN_HEIGHT/2-30;
rcPlayer1_text.x = SCREEN_WIDTH/2-100;
rcPlayer1_text.y = SCREEN_HEIGHT-20;
rcPlayer2_text.x = SCREEN_WIDTH/2-100;
rcPlayer2_text.y = 0;
rcPlayer3_text.x = 0;
rcPlayer3_text.y = SCREEN_HEIGHT/2-100;
rcPlayer4_text.x = SCREEN_WIDTH-15;
rcPlayer4_text.y = SCREEN_HEIGHT/2-100;
rcwall_p1.x = 0;
rcwall_p1.y = SCREEN_HEIGHT-20;
rcwall_p2.x = 0;
rcwall_p2.y = 0;
rcwall_p3.x = 0;
rcwall_p3.y = 0;
rcwall_p4.x = SCREEN_WIDTH-20;
rcwall_p4.y = 0;
///Start position Player1
rcPlayer1.x = SCREEN_WIDTH/2-rcPlayer1.w/2; //Funkar bra rcPlayer1.x = SCREEN_WIDTH/2-100;
rcPlayer1.y = SCREEN_HEIGHT-50;
///Start position Player2
rcPlayer2.x = SCREEN_WIDTH/2-rcPlayer2.w/2;
rcPlayer2.y = 25;
///Start position Player3
rcPlayer3.x = 25;
rcPlayer3.y = SCREEN_HEIGHT/2-rcPlayer1.w/2;
///Start position Player4
rcPlayer4.x = SCREEN_WIDTH-50;
rcPlayer4.y = SCREEN_HEIGHT/2-rcPlayer1.w/2;
///Music
music = Mix_LoadMUS("beat.wav");
if(music == NULL)
{
fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
}
effect = Mix_LoadWAV("low.wav");
if(effect == NULL)
{
fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
}
collision = Mix_LoadWAV("LASER.wav");
if(collision == NULL)
{
fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
}
///Load StartMenu
if(!loadMenu(gWindow, gScreenSurface, font, effect))
{
success = false;
}
return success;
}
void close()
{
///Deallocate surface
SDL_FreeSurface( gXOut );
SDL_FreeSurface(Ball);
SDL_FreeSurface(Player1);
SDL_FreeSurface(Player2);
SDL_FreeSurface(text1);
SDL_FreeSurface(text2);
gXOut = NULL;
Ball = NULL;
Player1 = NULL;
Player2 = NULL;
Mix_FreeChunk(effect);
Mix_FreeMusic(music);
Mix_CloseAudio();
TTF_CloseFont(font);
///Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
TTF_Quit();
///Quit SDL subsystems
SDL_Quit();
}
//Do not be used in the same way on the networking version of the game
void printScore() /// prints "score" on the screen
{
int i;
for(i=0;i<60;i++)/// Loop must be there otherwise it gets really f-uped and only shows "score" for 1ms
{
SDL_BlitSurface(scoreMade, NULL, gScreenSurface, &rcscoreMade);
SDL_UpdateWindowSurface( gWindow );
SDL_Delay( 20 );
}
}
void checkExtraLifeWall(int points, bool &extraLife)
{
if(points==1 && extraLife == false){
SDL_BlitSurface(wall_p2,NULL,gScreenSurface,&rcwall_p2);
extraLife = true;
}
}
int main( int argc, char* args[] )
{
/// Träffat kloss
bool extraLife = false;
int j=0, i=0;
int life1 = 0, life2 = 0, life3 = 0, life4 = 0;
srand(time(NULL));
///Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
///Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
///Main loop flag
bool gameover = false;
///Event handler
SDL_Event event;
///Initialize Frames per second
FPS_Init();
RestartBall(rcball);
angle = rand() % 361;
newDirectionBall(angle,rcball);
/*if(Mix_PlayMusic(music,-1) == -1)
{
fprintf(stderr, "Unable to play WAV file: %s\n", Mix_GetError());
}*/
///While application is running
while( !gameover )
{
///Look for events
if( SDL_PollEvent( &event ) )
{
///An event was found
switch (event.type)
{
///Close button clicked
case SDL_QUIT:
gameover = 1;
break;
///Handle the keybord
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_q:
gameover = 1;
break;
case SDLK_LEFT:
rcPlayer1.x -= 40;
if(rcPlayer1.x < 0 )
{
rcPlayer1.x = 0;
}
break;
case SDLK_RIGHT:
rcPlayer1.x += 40;
if(rcPlayer1.x > SCREEN_WIDTH - 200)
{
rcPlayer1.x = SCREEN_WIDTH - 200;
}
break;
case SDLK_a:
rcPlayer2.x -= 40;
if(rcPlayer2.x < 0 )
{
rcPlayer2.x = 0;
}
break;
case SDLK_d:
rcPlayer2.x += 40;
if(rcPlayer2.x > SCREEN_WIDTH - 200)
{
rcPlayer2.x = SCREEN_WIDTH - 200;
}
break;
case SDLK_w:
rcPlayer3.y -= 40;
if(rcPlayer3.y < 0 )
{
rcPlayer3.y = 0;
}
break;
case SDLK_s:
rcPlayer3.y += 40;
if(rcPlayer3.y > SCREEN_HEIGHT - 200)
{
rcPlayer3.y = SCREEN_HEIGHT - 200;
}
break;
case SDLK_UP:
rcPlayer4.y -= 40;
if(rcPlayer4.y < 0 )
{
rcPlayer4.y = 0;
}
break;
case SDLK_DOWN:
rcPlayer4.y += 40;
if(rcPlayer4.y > SCREEN_HEIGHT - 200)
{
rcPlayer4.y = SCREEN_HEIGHT - 200;
}
break;
}
break;
}
} // if poll end
///Apply the image
SDL_BlitSurface( gXOut, NULL, gScreenSurface, NULL );
SDL_BlitSurface(Ball, NULL, gScreenSurface, &rcball);
if(invisable && invisable_effekt == 1){
SDL_BlitSurface(Player1, NULL, gScreenSurface, &rcPlayer1);
SDL_BlitSurface(InvisablePlayer2, NULL, gScreenSurface, &rcPlayer2);
SDL_BlitSurface(InvisablePlayer3, NULL, gScreenSurface, &rcPlayer3);
SDL_BlitSurface(InvisablePlayer4, NULL, gScreenSurface, &rcPlayer4);
}
else if(invisable && invisable_effekt == 2){
SDL_BlitSurface(InvisablePlayer1, NULL, gScreenSurface, &rcPlayer1);
SDL_BlitSurface(Player2, NULL, gScreenSurface, &rcPlayer2);
SDL_BlitSurface(InvisablePlayer3, NULL, gScreenSurface, &rcPlayer3);
SDL_BlitSurface(InvisablePlayer4, NULL, gScreenSurface, &rcPlayer4);
}
else if(invisable && invisable_effekt == 3){
SDL_BlitSurface(InvisablePlayer1, NULL, gScreenSurface, &rcPlayer1);
SDL_BlitSurface(InvisablePlayer2, NULL, gScreenSurface, &rcPlayer2);
SDL_BlitSurface(Player3, NULL, gScreenSurface, &rcPlayer3);
SDL_BlitSurface(InvisablePlayer4, NULL, gScreenSurface, &rcPlayer4);
}
else if(invisable && invisable_effekt == 4){
SDL_BlitSurface(InvisablePlayer1, NULL, gScreenSurface, &rcPlayer1);
SDL_BlitSurface(InvisablePlayer2, NULL, gScreenSurface, &rcPlayer2);
SDL_BlitSurface(InvisablePlayer3, NULL, gScreenSurface, &rcPlayer3);
SDL_BlitSurface(Player4, NULL, gScreenSurface, &rcPlayer4);
}
else{
SDL_BlitSurface(Player1, NULL, gScreenSurface, &rcPlayer1);
SDL_BlitSurface(Player2, NULL, gScreenSurface, &rcPlayer2);
SDL_BlitSurface(Player3, NULL, gScreenSurface, &rcPlayer3);
SDL_BlitSurface(Player4, NULL, gScreenSurface, &rcPlayer4);
}
if(points[1]==1 && life1==1){
SDL_BlitSurface(wall_p1, NULL, gScreenSurface, &rcwall_p1);
}
if(points[2]==1 && life2==1){
SDL_BlitSurface(wall_p2, NULL, gScreenSurface, &rcwall_p2);
}
if(points[3]==1 && life3==1){
SDL_BlitSurface(wall_p3, NULL, gScreenSurface, &rcwall_p3);
}
if(points[4]==1 && life4==1){
SDL_BlitSurface(wall_p4, NULL, gScreenSurface, &rcwall_p4);
}
SDL_BlitSurface(text1, NULL, gScreenSurface, &rctext1);
SDL_BlitSurface(text2, NULL, gScreenSurface, &rctext2);
SDL_BlitSurface(text3, NULL, gScreenSurface, &rctext3);
SDL_BlitSurface(text4, NULL, gScreenSurface, &rctext4);
SDL_UpdateWindowSurface( gWindow );
/// PLAYER2s WALL
if(rcball.y < 1){ ///Touched the top of the screen
points[2]--;
printScore();///prints "score" on the screen
if(points[2]==1 && extraLife == false){/// check if players point is so low that he gets "the wall" of extra life
text2 = TTF_RenderText_Blended(font3,score[points[2]],colors[1]);
SDL_BlitSurface(wall_p2,NULL,gScreenSurface,&rcwall_p2);
extraLife = true;
life2++; /// this is for collitiondetection in the wall of extra life, look under each players collition detection
}
else{
text2 = TTF_RenderText_Blended(font3, score[points[2]], colors[points[2]]);
}
SDL_BlitSurface(text2, NULL, gScreenSurface, &rcPlayer2);
RestartBall(rcball); /// resets ball position and speed
angle = rand() % 361;/// reset angel to a random one
newDirectionBall(angle,rcball);/// starts ball in a new direction from center ( bacause we did resetball before)
points_made ++;/// add 1 to points made in the game
checkEffects(points_made,2);/// Checks if there is an effekt to happen att this many points made in the game
}
///PLAYER1s WALL
else if(rcball.y > (SCREEN_HEIGHT - rcball.h - 1) ){ /// touched the bottom of the screen
points[1]--;
printScore();///prints "score" on the screen
if(points[1]==1 && extraLife == false){
text1 = TTF_RenderText_Blended(font3, score[points[1]], colors[points[1]]);
SDL_BlitSurface(wall_p1, NULL, gScreenSurface, &rcwall_p1);
extraLife = true;
life1++;
}
else
{
text1 = TTF_RenderText_Blended(font3, score[points[1]], colors[points[1]]);
}
SDL_BlitSurface(text1, NULL, gScreenSurface, &rcPlayer1);/// prints out the new score of hearts
RestartBall(rcball); /// resets ball position and speed
angle = rand() % 361; /// reset angel to a random one
newDirectionBall(angle,rcball); /// starts ball in a new direction from center ( bacause we did resetball before)
points_made ++; /// add 1 to points made in the game
checkEffects(points_made,1); /// Checks if there is an effekt to happen att this many points made in the game
}
/// PLAYER3s WALL
else if(rcball.x < 1){
points[3]--;
printScore();
if(points[3]==1 && extraLife == false)
{
text3 = TTF_RenderText_Blended_Wrapped(font3, score[points[3]], colors[1],15);
SDL_BlitSurface(wall_p3, NULL, gScreenSurface, &rcwall_p3);
extraLife = true;
rcball.y += 5;
life3++;
}
else
{
text3 = TTF_RenderText_Blended_Wrapped(font3, score[points[3]], colors[points[3]],30);
}
SDL_BlitSurface(text3, NULL, gScreenSurface, &rcPlayer3);
RestartBall(rcball); /// resets ball position and speed
angle = rand() % 361;/// reset angel to a random one
newDirectionBall(angle,rcball);/// starts ball in a new direction from center ( bacause we did resetball before)
points_made ++;/// add 1 to points made in the game
checkEffects(points_made,3);/// Checks if there is an effekt to happen att this many points made in the game
}
///PLAYER4s WALL
else if(rcball.x > (SCREEN_WIDTH - rcball.w -1)){
points[4]--;
printScore();
if(points[4]==1 && extraLife == false)
{
text4 = TTF_RenderText_Blended_Wrapped(font3, score[points[4]], colors[1],15);
SDL_BlitSurface(wall_p4, NULL, gScreenSurface, &rcwall_p4);
extraLife = true;
life4++;
}
else
{
text4 = TTF_RenderText_Blended_Wrapped(font3, score[points[4]], colors[points[4]],30);
}
SDL_BlitSurface(text4, NULL, gScreenSurface, &rcPlayer4);
RestartBall(rcball); /// resets ball position and speed
angle = rand() % 361;/// reset angel to a random one
newDirectionBall(angle,rcball);/// starts ball in a new direction from center ( bacause we did resetball before)
points_made ++;/// add 1 to points made in the game
checkEffects(points_made,4);/// Checks if there is an effekt to happen att this many points made in the game
}
/*========================COLLITION PLAYER1 =========================================*/
if(Collition(rcPlayer1, rcball))/// Returns true if collition is detected
{
puts("True");
if( (angle > 211 && angle < 329) || (angle > -149 && angle < -31) ){ /// To avoid changing of direction
angle = 360 - angle - angleEffect(rcball,rcPlayer1,1);
}
else{