/
spong.c
375 lines (318 loc) · 10.2 KB
/
spong.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
#include "spong.h"
int main(int argc, char *argv[])
{
/* initialize SDL and its subsystems */
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) die();
if (TTF_Init() == -1) die();
/* set the height and width of the main window, as well as the number
of bits per pixel; this needs to be done prior to initializing the
main window (*screen, below) */
initWindowAttributes();
/* the frame buffer */
SDL_Surface *screen = SDL_SetVideoMode(W_WIDTH, W_HEIGHT,
W_COLOR_DEPTH, SDL_HWSURFACE|SDL_FULLSCREEN);
if (!screen) die();
/* hide the mouse cursor */
SDL_ShowCursor(SDL_DISABLE);
/* the background color of the screen */
const Uint32 clearColor = CLEAR_COLOR(screen->format);
clearScreen(screen, clearColor);
/* clearColor as an SDL_Color, for use with TTF */
Uint8 r, g, b;
SDL_GetRGB(clearColor, screen->format, &r, &g, &b);
SDL_Color bgColor = { r: r, g: g, b: b };
/* the score font */
TTF_Font *font = TTF_OpenFont("resources/VeraMono.ttf", FONT_SIZE);
if (!font) die();
SDL_Color fontColor = FONT_COLOR;
/* the score text; we'll allow three digits plus the terminating '\0' */
char *lScoreStr = malloc(sizeof(char) * 4);
char *rScoreStr = malloc(sizeof(char) * 4);
if (!lScoreStr || !rScoreStr) die();
SDL_Surface *lScore = NULL, *rScore = NULL;
SDL_Rect lScorePos = { x: C_X+FONT_SIZE, y: C_HEIGHT/5,
w: F_WIDTH, h: F_HEIGHT };
SDL_Rect rScorePos = { x: (C_X+C_WIDTH)-3*FONT_SIZE, y: C_HEIGHT/5,
w: F_WIDTH, h: F_HEIGHT };
/* set up the playing court */
Court *court = makeCourt(screen);
if (!court) die();
/* set up the players and their paddles */
Player *lPlayer = makePlayer(screen);
Player *rPlayer = makePlayer(screen);
if (!lPlayer || !rPlayer) die();
rPlayer->paddle.rect.x = C_X + C_WIDTH - P_WIDTH;
/* add the ball */
Ball *ball = makeBall(screen);
if (!ball) die();
/* because SDL_KEY(UP|DOWN) occurs only once, not continuously while
the key is pressed, we need to keep track of whether a key is
(still) pressed */
bool lPlayerShouldMoveUp = false, lPlayerShouldMoveDown = false,
rPlayerShouldMoveUp = false, rPlayerShouldMoveDown = false;
Uint32 startTime; /* denotes the beginning of each iteration
of the main event loop */
bool running = true; /* true till the application should exit */
while (running) {
startTime = SDL_GetTicks();
/* clear the previous frame's paddles and ball */
SDL_FillRect(screen, &lPlayer->paddle.rect, clearColor);
SDL_FillRect(screen, &rPlayer->paddle.rect, clearColor);
SDL_FillRect(screen, &ball->rect, clearColor);
/* clear the previous frame's score */
SDL_FillRect(screen, &lScorePos, clearColor);
SDL_FillRect(screen, &rScorePos, clearColor);
/* redraw the walls in case they were clipped by the ball
in a previous frame */
SDL_FillRect(screen, &court->upperWall, court->color);
SDL_FillRect(screen, &court->lowerWall, court->color);
/* get the current state of the players' controls */
readPlayerInput(&running,
&lPlayerShouldMoveUp, &lPlayerShouldMoveDown,
&rPlayerShouldMoveUp, &rPlayerShouldMoveDown);
/* save the current position of the paddles */
lPlayer->paddle.prevY = lPlayer->paddle.rect.y;
rPlayer->paddle.prevY = rPlayer->paddle.rect.y;
/* move the paddles if appropriate */
if (lPlayerShouldMoveUp)
movePaddle(court, &lPlayer->paddle, UP);
else if (lPlayerShouldMoveDown)
movePaddle(court, &lPlayer->paddle, DOWN);
if (rPlayerShouldMoveUp)
movePaddle(court, &rPlayer->paddle, UP);
else if (rPlayerShouldMoveDown)
movePaddle(court, &rPlayer->paddle, DOWN);
/* move the ball */
moveBall(court, ball, lPlayer, rPlayer);
/* update the score */
updateScore(ball, lPlayer, rPlayer);
/* update the on-screen score */
if (lScore) SDL_FreeSurface(lScore);
snprintf(lScoreStr, 4, "%2d", lPlayer->points);
lScore = TTF_RenderText_Shaded(font, lScoreStr, fontColor,
bgColor);
if (rScore) SDL_FreeSurface(rScore);
snprintf(rScoreStr, 4, "%2d", rPlayer->points);
rScore = TTF_RenderText_Shaded(font, rScoreStr, fontColor,
bgColor);
/* draw the score */
SDL_BlitSurface(lScore, NULL, screen, &lScorePos);
SDL_BlitSurface(rScore, NULL, screen, &rScorePos);
/* draw the paddles */
SDL_FillRect(screen, &lPlayer->paddle.rect,
lPlayer->paddle.color);
SDL_FillRect(screen, &rPlayer->paddle.rect,
rPlayer->paddle.color);
/* draw the ball */
SDL_FillRect(screen, &ball->rect, ball->color);
/* render frame to screen */
SDL_Flip(screen);
/* keep a steady frame rate */
Uint8 elapsedTime = SDL_GetTicks() - startTime;
if (elapsedTime < FRAME_DURATION)
SDL_Delay(FRAME_DURATION - elapsedTime);
}
/* free resources */
free(lScoreStr);
free(rScoreStr);
free(court);
free(lPlayer);
free(rPlayer);
free(ball);
TTF_CloseFont(font);
TTF_Quit();
SDL_FreeSurface(lScore);
SDL_FreeSurface(rScore);
SDL_Quit();
return EXIT_SUCCESS;
}
inline void clearScreen(SDL_Surface *screen, Uint32 clearColor)
{
SDL_FillRect(screen, &screen->clip_rect, clearColor);
}
Court *makeCourt(SDL_Surface *screen)
{
Court *court = malloc(sizeof(Court));
court->x = C_X;
court->y = C_Y;
court->w = C_WIDTH;
court->h = C_HEIGHT;
court->upperWall.x = C_X;
court->upperWall.y = C_Y;
court->upperWall.w = C_WIDTH;
court->upperWall.h = C_WALL;
court->lowerWall.x = C_X;
court->lowerWall.y = C_Y + C_HEIGHT - C_WALL;
court->lowerWall.w = C_WIDTH;
court->lowerWall.h = C_WALL;
court->color = WALL_COLOR(screen->format);
SDL_FillRect(screen, &court->upperWall, court->color);
SDL_FillRect(screen, &court->lowerWall, court->color);
return court;
}
Player *makePlayer(SDL_Surface *screen)
{
Player *player = malloc(sizeof(Player));
/* make the leftmost player (x = C_X) by default */
player->paddle.rect.x = C_X;
player->paddle.rect.y = (C_Y+C_HEIGHT)/2 - P_HEIGHT/2;
player->paddle.prevY = player->paddle.rect.y;
player->paddle.rect.w = P_WIDTH;
player->paddle.rect.h = P_HEIGHT;
player->paddle.color = PADDLE_COLOR(screen->format);
player->points = 0;
return player;
}
Ball *makeBall(SDL_Surface *screen)
{
Ball *ball = malloc(sizeof(Ball));
ball->rect.x = (C_X+C_WIDTH)/2 - B_SIZE/2;
ball->rect.y = (C_Y+C_HEIGHT)/2 - B_SIZE/2;
ball->rect.w = B_SIZE;
ball->rect.h = B_SIZE;
ball->color = BALL_COLOR(screen->format);
ball->vx = B_VX;
ball->vy = 0;
return ball;
}
void readPlayerInput(bool *running,
bool *lPlayerShouldMoveUp, bool *lPlayerShouldMoveDown,
bool *rPlayerShouldMoveUp, bool *rPlayerShouldMoveDown)
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
*running = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case QUIT_KEY:
*running = false;
break;
case LPLAYER_UP_KEY:
*lPlayerShouldMoveUp = true;
break;
case LPLAYER_DOWN_KEY:
*lPlayerShouldMoveDown = true;
break;
case RPLAYER_UP_KEY:
*rPlayerShouldMoveUp = true;
break;
case RPLAYER_DOWN_KEY:
*rPlayerShouldMoveDown = true;
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case LPLAYER_UP_KEY:
*lPlayerShouldMoveUp = false;
break;
case LPLAYER_DOWN_KEY:
*lPlayerShouldMoveDown = false;
break;
case RPLAYER_UP_KEY:
*rPlayerShouldMoveUp = false;
break;
case RPLAYER_DOWN_KEY:
*rPlayerShouldMoveDown = false;
break;
}
break;
}
}
}
void movePaddle(Court *court, Paddle *paddle, Direction direction)
{
if (direction == UP) {
Uint32 upperBound = court->y + court->upperWall.h;
if (paddle->rect.y > upperBound+P_STEP)
paddle->rect.y -= P_STEP;
else
paddle->rect.y = court->y + court->upperWall.h;
} else if (direction == DOWN) {
Uint32 lowerBound = court->y + court->h - court->lowerWall.h;
if (paddle->rect.y+paddle->rect.h < lowerBound-P_STEP)
paddle->rect.y += P_STEP;
else
paddle->rect.y = lowerBound - paddle->rect.h;
}
}
void moveBall(Court *court, Ball *ball, Player *lPlayer, Player *rPlayer)
{
Paddle *lPaddle = &lPlayer->paddle;
Paddle *rPaddle = &rPlayer->paddle;
/* set the velocity of the ball */
if (ball->rect.x <= lPaddle->rect.x+lPaddle->rect.w && ball->vx < 0) {
/* the ball is on the left boundary of the court
and traveling leftwards */
Uint32 upperBound = lPaddle->rect.y;
Uint32 lowerBound = lPaddle->rect.y + lPaddle->rect.h;
if (ball->rect.y+ball->rect.h >= upperBound
&& ball->rect.y <= lowerBound) {
ball->vx = -ball->vx;
if (lPaddle->prevY != lPaddle->rect.y)
ball->vy += lPaddle->prevY - lPaddle->rect.y;
/* make sure the ball isn't clipped by the paddle */
ball->rect.w = ball->rect.h = B_SIZE;
}
} else if (ball->rect.x+ball->rect.w >= rPaddle->rect.x
&& ball->vx > 0) {
/* the ball is on the right boundary of the court
and traveling rightwards */
Uint32 upperBound = rPaddle->rect.y;
Uint32 lowerBound = rPaddle->rect.y + rPaddle->rect.h;
if (ball->rect.y+ball->rect.h >= upperBound
&& ball->rect.y <= lowerBound) {
ball->vx = -ball->vx;
if (rPaddle->prevY != rPaddle->rect.y)
ball->vy += rPaddle->prevY - rPaddle->rect.y;
/* make sure the ball isn't clipped by the paddle */
ball->rect.w = ball->rect.h = B_SIZE;
}
}
if (ball->rect.y <= court->upperWall.y+court->upperWall.h
|| ball->rect.y+ball->rect.h >= court->lowerWall.y) {
/* the ball has collided with a wall */
ball->vy = -ball->vy;
}
/* actually move the ball */
ball->rect.x += ball->vx;
ball->rect.y += ball->vy;
}
void updateScore(Ball *ball, Player *lPlayer, Player *rPlayer)
{
if (ball->rect.x >= C_X+C_WIDTH) {
/* lPlayer scored a point */
lPlayer->points++;
resetAndServe(ball, LEFT);
} else if (ball->rect.x+ball->rect.w <= C_X) {
/* rPlayer scored a point */
rPlayer->points++;
resetAndServe(ball, RIGHT);
}
}
void resetAndServe(Ball *ball, Direction direction)
{
/* the ball will be clipped by the edge of the screen,
so we have to set its width and height to the correct value */
ball->rect.w = ball->rect.h = B_SIZE;
/* center the ball on the court */
ball->rect.x = (C_X+C_WIDTH)/2 - B_SIZE/2;
ball->rect.y = (C_Y+C_HEIGHT)/2 - B_SIZE/2;
/* the winner serves */
ball->vy = 0;
if (direction == RIGHT)
ball->vx = -B_VX;
else if (direction == LEFT)
ball->vx = B_VX;
}
inline void die()
{
if (strcmp(SDL_GetError(), "") != 0)
fprintf(stderr, "spong: %s\n", SDL_GetError());
if (errno)
perror("spong: ");
exit(1);
}