-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
407 lines (317 loc) · 10.9 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <string.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>
#include "player.h"
#include "ball.h"
#include "utils.h"
#include "text.h"
#include "audio.h"
#include "sprite.h"
#define FPS_INTERVAL 1
#define WLIGTH_COUNTER_DEFAULT 30
#define FPS_120 8
#define FPS_100 9
#define FPS_60 16
//Window and renderer, init is in the corresponding function
SDL_Window* window;
SDL_Renderer* renderer;
//Window icon
SDL_Surface* icon;
//if running is 1, the game is running
int running = 1;
//Strutures and strings to render score on screen
TextRenderer txt;
SDL_Rect score_p1_rect = {0,0,26,26};
SDL_Rect score_p2_rect = {800-52,0,26,26};
char p1_score[255];
char p2_score[255];
//time step
Uint32 dt;
//texture batch
SDL_Texture* textures[5];
//Where are the sprites?
Sprite* wall1;
Sprite* wall2;
SDL_Rect Player1Name = {0,28,20,20};
SDL_Rect Player2Name = {640,28,20,20};
SDL_Rect FPSRect = {400,600-56,10,10};
SDL_Rect FPSStrRect = {330,600-56,15,15};
Sprite* logo;
char fps[10];
char *FPS_str = "FPS:";
Player* player1;
Player* player2;
Ball* ball;
//https://patrickdearteaga.com/
int channel;
Audio audio;
int hit_sound;
int wall_hit_sound;
int music;
Particles* p = NULL;
Uint32 fps_frames = 0; //frames passed since the last recorded fps.
Uint32 fps_lasttime = 0; //the last recorded time.
Uint32 fps_current; //the current FPS.
int wlight_counter = WLIGTH_COUNTER_DEFAULT;//lighting effect on wall hit by the walls
Direction pd1 = UP;
Direction pd2 = DOWN;
SDL_Joystick* controller1;
SDL_Joystick* controller2;
void initGame(const char* title,int width,int height){
//Init SDL and the game state
SDL_Init(SDL_INIT_EVERYTHING);
/* if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
// Unrecoverable error, exit here.
printf("SDL_Init failed: %s\n", SDL_GetError());
exit(-1);
} */
window = SDL_CreateWindow(title,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height, SDL_WINDOW_SHOWN);
/* if (window == NULL) {
// Unrecoverable error, exit here.
printf("SDL_CreateWindow failed: %s\n", SDL_GetError());
exit(-1);
} */
//TODO:
//Add a trigger to enable disable VSYNC.
//When VSYNC enabled, check framerate and adapt physiscs
//Otherwisen physics/events at 100Hz
renderer = SDL_CreateRenderer(window,0,SDL_RENDERER_ACCELERATED);
/* if (renderer == NULL) {
// Unrecoverable error, exit here.
printf("SDL_CreateRenderer failed: %s\n", SDL_GetError());
exit(-1);
} */
IMG_Init(IMG_INIT_PNG);
//printf("IMG_Init: %s\n", IMG_GetError());
icon = IMG_Load("textures/icon.png");
//printf("IMG_Load: %s\n", IMG_GetError());
SDL_SetWindowIcon(window,icon);
audio = initAudio(44100,AUDIO_S16SYS,2,2048,2,1);
//TODO: Convert all textures to Sprite objects
textures[0] = loadTexture(renderer,"textures/ball.png");
textures[2] = loadTexture(renderer,"textures/player1.png");
textures[3] = loadTexture(renderer,"textures/player2.png");
wall1 = newSprite(renderer,"textures/wall.png");
wall2 = newSprite(renderer,"textures/wall.png");
logo = newSprite(renderer,"textures/logo.png");
moveSprite(wall1,0,0);
moveSprite(wall2,0,575);
moveSprite(logo,130,235);
hit_sound = loadSound(&audio,"hit.wav");
//printf("Mix_LoadWAV(\"hit.wav\"): %s\n", Mix_GetError());
wall_hit_sound = loadSound(&audio,"wall_hit.wav");
//printf("Mix_LoadWAV(\"wall_hit.wav\"): %s\n", Mix_GetError());
music = loadMusic(&audio,"interstellar_odyssey_125.ogg");
//printf("Mix_LoadWAV(\"music.wav\"): %s\n", Mix_GetError());
//To be fixed:
/*
1) Add a loop to check how many controllers are present
2) Check which controller is which by adding a "press a button on controller 1"
*/
controller1 = SDL_JoystickOpen(2);
printf("Name: %s\n", SDL_JoystickNameForIndex(2));
controller2 = SDL_JoystickOpen(1);
printf("Name: %s\n", SDL_JoystickNameForIndex(1));
}
void render(){
//rendering section
clearScreen(renderer);
//this will be the render function
SDL_RenderCopy(renderer,logo->txt, NULL, &logo->position);
SDL_RenderCopy(renderer,wall1->txt,NULL,&wall1->position);
SDL_RenderCopy(renderer,wall2->txt,NULL,&wall2->position);
SDL_RenderCopy(renderer,textures[player1->tex_index],NULL,&(player1->position));
SDL_RenderCopy(renderer,textures[player2->tex_index],NULL,&(player2->position));
SDL_RenderCopy(renderer,textures[ball->tex_index],NULL,&ball->rect);
writeTxt(&txt,&score_p1_rect,p1_score,renderer);
writeTxt(&txt,&score_p2_rect,p2_score,renderer);
writeTxt(&txt,&Player1Name,"Player 1",renderer);
writeTxt(&txt,&Player2Name,"Player 2",renderer);
writeTxt(&txt,&FPSStrRect,FPS_str,renderer);
snprintf(fps,10,"%d",fps_current);
writeTxt(&txt,&FPSRect,fps,renderer);
//Careful with this!
if(p!=NULL){
drawParticles(renderer,p);
p->lifetime -= 1;
}
SDL_RenderPresent(renderer);
}
void handle_events(){
SDL_Event evt;
SDL_PumpEvents();
//get keyboard input
const Uint8* keys = SDL_GetKeyboardState(NULL);
Sint16 y_move1,y_move2;
y_move1 = SDL_JoystickGetAxis(controller1, 1);
y_move2 = SDL_JoystickGetAxis(controller2, 1);
if(keys[SDL_SCANCODE_ESCAPE]){
running = 0;
}
//player 2
if(keys[SDL_SCANCODE_UP] || y_move2 < -256){
movePlayer(player2,UP);
pd2 = UP;
if(checkCollision(player2->position,wall1->position)){
movePlayer(player2,DOWN);
pd2 = DOWN;
}
}
if(keys[SDL_SCANCODE_DOWN] || y_move2 > 256){
movePlayer(player2,DOWN);
pd2 = DOWN;
if(checkCollision(player2->position,wall2->position)){
movePlayer(player2,UP);
pd2 = UP;
}
}
//Player 1
if(keys[SDL_SCANCODE_E]||y_move1 < -256){
movePlayer(player1,UP);
pd1 = UP;
if(checkCollision(player1->position,wall1->position)){
movePlayer(player1,DOWN);
pd1 = DOWN;
}
}
if(keys[SDL_SCANCODE_D]||y_move1 > 256){
movePlayer(player1,DOWN);
pd1 = DOWN;
if(checkCollision(player1->position,wall2->position)){
movePlayer(player1,UP);
pd1 = UP;
}
}
//Other events
while(SDL_PollEvent(&evt)){
if(evt.type == SDL_QUIT){
running = 0;
}
}//while
//update ball position
moveBall(ball,1,1);
//Check collisions
if(checkCollision(ball->rect,wall1->position)||checkCollision(ball->rect,wall2->position)){
SDL_SetTextureColorMod(wall2->txt,255,255,255);
SDL_SetTextureColorMod(wall1->txt,255,255,255);
SDL_SetTextureColorMod(logo->txt,255,255,255);
ball->y_speed = -(ball->y_speed);
playSound(&audio,wall_hit_sound);
}
else{
if(wlight_counter<=0){
SDL_SetTextureColorMod(wall2->txt,125,125,125);
SDL_SetTextureColorMod(wall1->txt,125,125,125);
SDL_SetTextureColorMod(logo->txt,125,125,125);
wlight_counter = WLIGTH_COUNTER_DEFAULT;
}
else{
wlight_counter -= 1;
}
}
if(checkCollision(ball->rect,player1->position)){
ball->x_speed = -(ball->x_speed);
ball->y_speed += pd1;
playSound(&audio,hit_sound);
//Generate particle effect
p = genParticles(ball->rect.x,ball->rect.y,500,LEFT,300);
}
if(checkCollision(ball->rect,player2->position)){
ball->x_speed = -(ball->x_speed);
ball->y_speed += pd2;
playSound(&audio,hit_sound);
//Generate particle effect
p = genParticles(ball->rect.x,ball->rect.y,500,RIGHT,100);
}
//Score update
if(ball->rect.x <0){
increaseScore(player2,1);
initBall(ball,0,30,6);
}else if(ball->rect.x > 800){
increaseScore(player1,1);
initBall(ball,0,30,6);
}
//check if particles are still alive
if(p!=NULL && p->lifetime <= 0){
killParticles(p);
p = NULL;
}
//Convert scores to string
snprintf(p1_score,200,"%d",player1->score);
snprintf(p2_score,200,"%d",player2->score);
}
void gameLoop(){
running = 1;
fps_lasttime = SDL_GetTicks();
Uint32 time = SDL_GetTicks();
while(running){
dt = SDL_GetTicks()-time;
if(dt > 10){
handle_events();
time = SDL_GetTicks();
//SDL_Delay(FPS_60-dt);
}
render();
//Temporary solution for testing
if(player1->score >= 25){
printf("Player 1 Wins!!");
running = 0;
}
if(player2->score >= 25){
printf("Player 2 Wins!!");
running = 0;
}
//Limit framerate to 60fps
fps_frames++;
if (fps_lasttime < SDL_GetTicks() - FPS_INTERVAL*1000)
{
fps_lasttime = SDL_GetTicks();
fps_current = fps_frames;
fps_frames = 0;
}
//lock max framerate
SDL_Delay(1);
}//big while
}//end gameloop function
void quitGame(){
//Free memory
freeSprite(wall1);
freeSprite(wall2);
freeSprite(logo);
for(int i=0;i<4;i++){
SDL_DestroyTexture(textures[i]);
}
SDL_DestroyTexture(txt.chrSheet);
//Quit modules
//SDL_GameControllerClose(controller);
//quitAudio(&audio);
IMG_Quit();
SDL_FreeSurface(icon);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
int main(int argc, char* argv[]){
initGame("F Pong",800,600);
//txt is global
initTextRenderer(&txt,renderer);
Player p1;
Player p2;
Ball b;
player1=&p1;
player2=&p2;
ball=&b;
initPlayer(player1,1,10,2);
initPlayer(player2,2,10,3);
initBall(ball,0,30, 8);
changeMusicVolume(64);
playMusic(&audio,music,true);
gameLoop();
quitGame();
return 0;
}