-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
240 lines (175 loc) · 5.31 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
/* Sami Nasry - Pong Game - 2024*/
#include <SDL2/SDL.h>
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_video.h>
#include <stdbool.h>
#include <stdio.h>
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define PADDLE_WIDTH 10
#define PADDLE_HEIGHT 100
#define BALL_SIZE 10
#define PADDLE_SPEED 5
#define BALL_SPEED 5
// PADDLE STRUCTURE
typedef struct {
SDL_Rect rect;
int dy;
} Paddle;
// BALL STRUCTURE
typedef struct {
SDL_Rect rect;
int dx,dy;
} Ball;
// RESET BALL POSITION TO THE CENTER OF THE SCREEN AND RANDOME START DIRECTION
void reset_ball(Ball* ball)
{
ball->rect.x = (WINDOW_WIDTH / 2) - (BALL_SIZE / 2);
ball->rect.y = (WINDOW_HEIGHT / 2) - (BALL_SIZE / 2);
ball->dx = BALL_SPEED * (rand() % 2 == 0 ? 1 : -1);
ball->dy = BALL_SPEED * (rand() % 2 == 0 ? 1 : -1);
}
// MAIN GAME LOGIC
int main(int argc, char* argv[])
{
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
TTF_Font* font = NULL;
SDL_Surface* surface = NULL;
SDL_Texture* texture = NULL;
bool quit = false;
SDL_Event event;
// PADDLE, BALL AND SCORE INITIALIZATION
Paddle paddle1 = {{20, (WINDOW_HEIGHT / 2) - (PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT}, 0};
Paddle paddle2 = {{WINDOW_WIDTH - 30, (WINDOW_HEIGHT / 2) - (PADDLE_HEIGHT / 2),
PADDLE_WIDTH, PADDLE_HEIGHT}, 0};
Ball ball = {{WINDOW_WIDTH / 2 - BALL_SIZE / 2, WINDOW_HEIGHT / 2 - BALL_SIZE / 2, BALL_SIZE, BALL_SIZE}, BALL_SPEED, BALL_SPEED};
int score1 = 0, score2 = 0;
// INITIALIZATION OF VIDEO SUBSYTEM AND SDL_TTF LIBRARY
if (SDL_Init(SDL_INIT_VIDEO) < 0 || TTF_Init() < 0 )
{
printf("SDL/TTF could not initialize! ERROR : %s\n", SDL_GetError());
return 1;
}
// WINDOW CREATION
window = SDL_CreateWindow("Pong" , SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED
, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if(!window)
{
printf("Window could not be created! Error : %s\n" , SDL_GetError());
return 1;
}
// RENDERER CREATION
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(!renderer)
{
printf("Renderer could not be created! Error: %s\n", SDL_GetError());
return 1;
}
// FONT LOADING
font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 24);
if (!font)
{
printf("Failed to load font! Error : %s\n", TTF_GetError());
return 1;
}
// Main Game Logic
// POLL FOR PENDING EVENTS. HANDLEE QUIT EVENTS AND KEYBOARD EVENTS FOR PADDLE MOVEMENTS
while(!quit)
{
while (SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT)
{
quit = true;
}
else if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_w: paddle1.dy = -PADDLE_SPEED; break;
case SDLK_s: paddle1.dy = PADDLE_SPEED; break;
case SDLK_UP: paddle2.dy = -PADDLE_SPEED; break;
case SDLK_DOWN: paddle2.dy = PADDLE_SPEED; break;
}
}
else if (event.type == SDL_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_w:
case SDLK_s: paddle1.dy = 0;break;
case SDLK_UP:
case SDLK_DOWN: paddle2.dy = 0; break;
}
}
}
// MOVE THE PADDLES
paddle1.rect.y += paddle1.dy;
paddle2.rect.y += paddle2.dy;
// CONSTRAIN PADDLES TO THE WINDOW
if (paddle1.rect.y < 0) paddle1.rect.y = 0;
if (paddle1.rect.y > (WINDOW_HEIGHT - PADDLE_HEIGHT)) paddle1.rect.y = (WINDOW_HEIGHT - PADDLE_HEIGHT);
if (paddle2.rect.y < 0) paddle2.rect.y = 0;
if (paddle2.rect.y > (WINDOW_HEIGHT - PADDLE_HEIGHT)) paddle2.rect.y = (WINDOW_HEIGHT - PADDLE_HEIGHT);
// MOVE THE BALL
ball.rect.x += ball.dx;
ball.rect.y += ball.dy;
// HANDLE BALL COLLISION WITH TOP AND BOTTOM OF THE SCREEN
if (ball.rect.y <= 0 || ball.rect.y >= WINDOW_HEIGHT - BALL_SIZE)
{
ball.dy = -ball.dy;
}
// HANDLE BALL COLLISION WITH THE PADDLES
if (SDL_HasIntersection(&ball.rect, &paddle1.rect) || SDL_HasIntersection(&ball.rect, &paddle2.rect))
{
ball.dx = -ball.dx;
}
// BALL GOAL HANDLING
if (ball.rect.x <= 0)
{
score2++;
reset_ball(&ball);
}
else if (ball.rect.x >= WINDOW_WIDTH - BALL_SIZE)
{
score1++;
reset_ball(&ball);
}
// RENDERING PROCESS
// SET DRAW COLOR TO BLACK AND CLEAR THE SCREEN
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// SET THE DRAW COLOR TO WHITE AND THEN DRAWW THE PADDLES AND THE BALLS
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &paddle1.rect);
SDL_RenderFillRect(renderer, &paddle2.rect);
SDL_RenderFillRect(renderer, &ball.rect);
// RENDER THE SCORE TEXT
char score_text[64];
snprintf(score_text, sizeof(score_text), "%d - %d" , score1 , score2);
SDL_Color white = {255, 255, 255};
surface = TTF_RenderText_Solid(font, score_text, white);
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect score_rect = {WINDOW_WIDTH / 2 - surface->w / 2, 20, surface->w,
surface->h};
SDL_RenderCopy(renderer, texture, NULL, &score_rect);
// UPDATE SCREEN WITH RENDERED STUFF
SDL_RenderPresent(renderer);
// FREE THE SURFACE AND DESTROY TEXTURE AFTER EACH FRAME TO PREVENT MEMORY LEAKS
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
// FPS CONTROL
SDL_Delay(16); // Cap at 60 fps
}
// CLEAN UP
TTF_CloseFont(font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
}