-
Notifications
You must be signed in to change notification settings - Fork 0
/
asspongSDL.cpp
525 lines (454 loc) · 16.9 KB
/
asspongSDL.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
/*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
// Music from http://grayscale.scene.pl/msx_archive.php?lang=en
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
typedef int BOOL;
const BOOL TRUE = 1;
const BOOL FALSE = 0;
// constants
const double PI = 3.1415926535897932384626433832795028841971693993751058;
const int FRAMERATE_CAP = 60;
const int MILLISECONDS_CAP = 1000 / 60;
const unsigned int CLEAR_COLOR = 0x111111;
const unsigned int BALL_COLOR = 0x00d636;
const unsigned int PLAYER1_COLOR = 0x00d636;
const unsigned int PLAYER2_COLOR = 0x00d636;
const unsigned int BACKGROUND_COLOR = 0x00d636;
const int AUDIO_FREQUENCY = 44100;
const int AUDIO_CHANNELS = 2; // stereo
const int AUDIO_BUFFER_SIZE = 4096;
// function prototypes
void initialize_SDL();
void initializeVideoContext_SDL();
void shutdown_SDL();
void processEvents_SDL(BOOL* isRunning);
void clearScreen_SDL();
void drawPixel_SDL(const int posX, const int posY, const unsigned int color);
void initializeDimensions();
void resetEverything();
void update();
BOOL hasBallCollidedPlayer1();
BOOL hasBallCollidedPlayer2();
void drawBall(const int posX, const int posY, const size_t radius, const unsigned int color);
void drawRacket(const int posX, const int posY, const size_t w, const size_t h, const unsigned int color);
void drawBackground(const unsigned int color);
void drawEverything();
// global variables
int g_windowWidth;
int g_windowHeight;
size_t g_virtualScreenPixelSize;
size_t g_virtualScreenWidth;
size_t g_virtualScreenHeight;
size_t g_ballRadius;
size_t g_playerWidth;
size_t g_playerHeight;
double g_ballSpeedIncrease;
double g_playerSpeed;
size_t g_player1PositionX;
size_t g_player2PositionX;
SDL_Surface* g_screen = 0;
double g_deltaTime;
double g_player1PosY;
double g_player2PosY;
double g_ballPosX;
double g_ballPosY;
double g_ballSpeed;
double g_ballAngle;
BOOL g_isGameOver;
unsigned int g_player1Wins;
unsigned int g_player2Wins;
BOOL g_isKeyDownW;
BOOL g_isKeyDownS;
BOOL g_isKeyDownUP;
BOOL g_isKeyDownDOWN;
Mix_Music* g_music;
// main function
int main(int, char**) {
// introduction
printf(" +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+\n");
printf(" | CC322 - Organizacion de Computadoras I |\n");
printf(" | .-----------. |\n");
printf(" | / AssPong \\ |\n");
printf(" | '===============' |\n");
printf(" | |\n");
printf(" | *** Equipo 9 *** |\n");
printf(" | Cavazos Woo David |\n");
printf(" | Corona Garcia Erick Daniel |\n");
printf(" | |\n");
printf(" | Instrucciones: |\n");
printf(" | [ESC] Salir |\n");
printf(" | [SPACE] Reset |\n");
printf(" | [w] Mover jugador 1 arriba |\n");
printf(" | [s] Mover jugador 1 abajo |\n");
printf(" | [UP] Mover jugador 2 arriba |\n");
printf(" | [DOWN] Mover jugador 2 abajo |\n");
printf(" | |\n");
printf(" +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+\n");
initialize_SDL();
// SDL_mixer initialization
Mix_OpenAudio(AUDIO_FREQUENCY, MIX_DEFAULT_FORMAT, AUDIO_CHANNELS, AUDIO_BUFFER_SIZE);
g_music = Mix_LoadMUS("still_alive.ogg");
printf("Presione [Enter] para continuar...");
getchar();
initializeVideoContext_SDL();
// play music
if (g_music == 0)
printf("Unable to load music: %s", Mix_GetError());
else
Mix_PlayMusic(g_music, -1);
srand((unsigned int)(time(0)));
initializeDimensions();
resetEverything();
g_deltaTime = 0.0;
g_player1Wins = 0;
g_player2Wins = 0;
g_isKeyDownW = FALSE;
g_isKeyDownS = FALSE;
g_isKeyDownUP = FALSE;
g_isKeyDownDOWN = FALSE;
// main loop
Uint32 startTime;
BOOL isRunning = TRUE;
while (isRunning) {
startTime = SDL_GetTicks();
// update
processEvents_SDL(&isRunning);
update();
// draw
clearScreen_SDL();
drawEverything();
SDL_Flip(g_screen); // flip buffers
// framerate cap
g_deltaTime = SDL_GetTicks() - startTime;
if (MILLISECONDS_CAP > g_deltaTime)
SDL_Delay((Uint32)(MILLISECONDS_CAP - g_deltaTime));
g_deltaTime = (SDL_GetTicks() - startTime) * 0.001;
// show framerate
// stringstream title;
// title << "Pong - " << setprecision(1) << fixed <<
// (g_deltaTime == 0.0? double(FRAMERATE_CAP) : 1.0 / g_deltaTime) << " fps";
// SDL_WM_SetCaption(title.str().c_str(), "");
}
// shutdown
Mix_FreeMusic(g_music);
Mix_CloseAudio();
shutdown_SDL();
// show winner
printf("\n");
printf(" .--------------------------------.\n");
printf(" / ");
if (g_player1Wins == g_player2Wins)
printf(" ~~~~ Empate ~~~~ ");
else if (g_player1Wins > g_player2Wins)
printf("Ganador: Jugador 1 ");
else
printf("Ganador: Jugador 2 ");
printf("(%2u : %2u) \\\n", g_player1Wins, g_player2Wins);
printf(" '===================================='\n");
printf("Presione [Enter] para salir...");
getchar();
return EXIT_SUCCESS;
}
void initialize_SDL() {
printf("Usando contexto de SDL\n");
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { // 0 success, -1 failure
printf("Unable to initialize SDL: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
}
void initializeVideoContext_SDL() {
const SDL_VideoInfo* info = SDL_GetVideoInfo();
g_windowWidth = info->current_w;
g_windowHeight = info->current_h;
g_screen = SDL_SetVideoMode(g_windowWidth, g_windowHeight, info->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
if (g_screen == 0) {
printf("Unable to create SDL video context: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_ShowCursor(SDL_FALSE);
}
void shutdown_SDL() {
SDL_Quit();
}
void processEvents_SDL(BOOL* isRunning) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
*isRunning = FALSE;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
// quit
case SDLK_ESCAPE:
*isRunning = FALSE;
break;
// reset
case SDLK_SPACE:
resetEverything();
break;
// player 1
case SDLK_w:
g_isKeyDownW = TRUE;
break;
case SDLK_s:
g_isKeyDownS = TRUE;
break;
// player 2
case SDLK_UP:
g_isKeyDownUP = TRUE;
break;
case SDLK_DOWN:
g_isKeyDownDOWN = TRUE;
break;
default:
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
// player 1
case SDLK_w:
g_isKeyDownW = FALSE;
break;
case SDLK_s:
g_isKeyDownS = FALSE;
break;
// player 2
case SDLK_UP:
g_isKeyDownUP = FALSE;
break;
case SDLK_DOWN:
g_isKeyDownDOWN = FALSE;
break;
default:
break;
}
break;
default:
break;
}
}
}
void clearScreen_SDL() {
SDL_FillRect(g_screen, 0, CLEAR_COLOR);
}
void drawPixel_SDL(const int posX, const int posY, const unsigned int color) {
SDL_Rect rect;
rect.x = (Sint16)(posX * g_virtualScreenPixelSize);
rect.y = (Sint16)(posY * g_virtualScreenPixelSize);
rect.w = (Uint16)(g_virtualScreenPixelSize);
rect.h = (Uint16)(g_virtualScreenPixelSize);
SDL_FillRect(g_screen, &rect, color);
}
void initializeDimensions() {
g_virtualScreenPixelSize = g_windowWidth / 160;
g_virtualScreenWidth = g_windowWidth / g_virtualScreenPixelSize;
g_virtualScreenHeight = g_windowHeight / g_virtualScreenPixelSize;
g_ballRadius = g_virtualScreenWidth / 40;
g_playerWidth = g_virtualScreenWidth / 40;
g_playerHeight = g_virtualScreenHeight / 6;
g_ballSpeedIncrease = g_virtualScreenWidth * 0.05;
g_playerSpeed = g_virtualScreenWidth * 1.0;
g_player1PositionX = (size_t)(g_virtualScreenWidth * 0.08);
g_player2PositionX = g_virtualScreenWidth - g_player1PositionX - g_playerWidth;
}
void resetEverything() {
g_player1PosY = (g_virtualScreenHeight - g_playerHeight) / 2;
g_player2PosY = g_player1PosY;
g_ballPosX = g_virtualScreenWidth / 2;
g_ballPosY = g_virtualScreenHeight / 2;
g_ballSpeed = g_virtualScreenWidth * 0.5;
g_ballAngle = (double)(rand()) / (double)(RAND_MAX) * PI * 0.5 - PI * 0.25;
if (rand() % 2 == 0)
g_ballAngle += PI;
g_isGameOver = FALSE;
}
void update() {
// update players
if (g_isKeyDownW) {
g_player1PosY -= g_playerSpeed * g_deltaTime;
if (g_player1PosY < 2.0)
g_player1PosY = 2.0;
}
if (g_isKeyDownS) {
g_player1PosY += g_playerSpeed * g_deltaTime;
if (g_player1PosY > g_virtualScreenHeight - g_playerHeight - 2.0)
g_player1PosY = g_virtualScreenHeight - g_playerHeight - 2.0;
}
if (g_isKeyDownUP) {
g_player2PosY -= g_playerSpeed * g_deltaTime;
if (g_player2PosY < 2.0)
g_player2PosY = 2.0;
}
if (g_isKeyDownDOWN) {
g_player2PosY += g_playerSpeed * g_deltaTime;
if (g_player2PosY > g_virtualScreenHeight - g_playerHeight - 2.0)
g_player2PosY = g_virtualScreenHeight - g_playerHeight - 2.0;
}
// update ball
g_ballPosX += g_ballSpeed * cos(g_ballAngle) * g_deltaTime;
g_ballPosY -= g_ballSpeed * sin(g_ballAngle) * g_deltaTime;
// check ball collisions with players
double distX, distY;
if (hasBallCollidedPlayer1() && g_ballPosX > g_player1PositionX) {
distY = g_player1PosY + g_playerHeight / 2 - g_ballPosY;
distX = g_ballPosX;
g_ballAngle = atan(distY / distX);
g_ballSpeed += g_ballSpeedIncrease;
g_ballPosX = g_player1PositionX + g_playerWidth + g_ballRadius;
}
else if (hasBallCollidedPlayer2() && g_ballPosX < g_player2PositionX + g_playerWidth) {
distY = g_player2PosY + g_playerHeight / 2 - g_ballPosY;
distX = g_virtualScreenWidth - g_ballPosX;
g_ballAngle = PI - atan(distY / distX);
g_ballSpeed += g_ballSpeedIncrease;
g_ballPosX = g_player2PositionX - g_ballRadius - 1;
}
else if (g_ballPosY < g_ballRadius + 2.0) {
g_ballPosY = g_ballRadius + 2.0;
g_ballAngle = -g_ballAngle;
}
else if (g_ballPosY > g_virtualScreenHeight - g_ballRadius - 2.0) {
g_ballPosY = g_virtualScreenHeight - g_ballRadius - 2.0;
g_ballAngle = -g_ballAngle;
}
// check win conditions
if (!g_isGameOver) {
if (g_ballPosX >= g_virtualScreenWidth - g_ballRadius) {
printf("Jugador 1\n");
g_isGameOver = TRUE;
++g_player1Wins;
}
else if (g_ballPosX <= g_ballRadius) {
printf("Jugador 2\n");
g_isGameOver = TRUE;
++g_player2Wins;
}
}
}
BOOL hasBallCollidedPlayer1() {
if (g_ballPosX - g_ballRadius < g_player1PositionX + g_playerWidth + 1) {
if (g_ballPosY >= g_player1PosY && g_ballPosY <= g_player1PosY + g_playerHeight) // racket 1 body
return TRUE;
double radiusSqr = g_ballRadius * g_ballRadius;
double distX = g_player1PositionX + g_playerWidth - g_ballPosX;
double distY = g_player1PosY - g_ballPosY;
double distSqr = distX * distX + distY * distY;
if (distSqr <= radiusSqr) // racket 1 upper corner
return TRUE;
distY = g_player1PosY + g_playerHeight - g_ballPosY;
distSqr = distX * distX + distY * distY;
if (distSqr <= radiusSqr) // racket 1 lower corner
return TRUE;
}
return FALSE;
}
BOOL hasBallCollidedPlayer2() {
if (g_ballPosX + g_ballRadius > g_player2PositionX - 1) {
if (g_ballPosY >= g_player2PosY && g_ballPosY <= g_player2PosY + g_playerHeight) // racket 2 body
return TRUE;
double radiusSqr = g_ballRadius * g_ballRadius;
double distX = g_player2PositionX - g_ballPosX;
double distY = g_player2PosY - g_ballPosY;
double distSqr = distX * distX + distY * distY;
if (distSqr <= radiusSqr) // racket 2 upper corner
return TRUE;
distY = g_player2PosY + g_playerHeight - g_ballPosY;
distSqr = distX * distX + distY * distY;
if (distSqr <= radiusSqr) // racket 2 lower corner
return TRUE;
}
return FALSE;
}
void drawBall(const int posX, const int posY, const size_t radius, const unsigned int color) {
int x, y, p;
// fill square with background color
int endX = posX + 2 * radius - 4;
int endY = posY + 2 * radius - 4;
for (y = posY - radius + 1; y < endY; ++y) {
for (x = posX - radius + 1; x < endX; ++x)
drawPixel_SDL(x, y, CLEAR_COLOR);
}
// draw circle
x = 0;
y = radius;
p = 1 - radius;
while(x <= y) {
drawPixel_SDL(posX + x, posY + y, color);
drawPixel_SDL(posX + x, posY - y, color);
drawPixel_SDL(posX - x, posY + y, color);
drawPixel_SDL(posX - x, posY - y, color);
drawPixel_SDL(posX + y, posY + x, color);
drawPixel_SDL(posX + y, posY - x, color);
drawPixel_SDL(posX - y, posY + x, color);
drawPixel_SDL(posX - y, posY - x, color);
if(p < 0) {
++x;
p += 2 * x + 1;
}
else {
++x;
--y;
p += 2 * (x - y) + 1;
}
}
}
void drawRacket(const int posX, const int posY, const size_t w, const size_t h, const unsigned int color) {
size_t x, y;
size_t maxX = posX + w - 1;
size_t maxY = posY + h - 1;
for (x = posX; x <= maxX; ++x) {
drawPixel_SDL(x, posY, color);
drawPixel_SDL(x, maxY, color);
}
for (y = posY + 1; y <= maxY; ++y) {
drawPixel_SDL(posX, y, color);
drawPixel_SDL(maxX, y, color);
}
}
void drawBackground(const unsigned int color) {
size_t half = g_virtualScreenWidth / 2;
size_t hLineEnd = g_virtualScreenWidth - 1;
size_t vLineEnd = g_virtualScreenHeight - 2;
size_t i;
for (i = 1; i < hLineEnd; ++i) {
drawPixel_SDL(i, 1, color);
drawPixel_SDL(i, vLineEnd, color);
}
for (i = 2; i < vLineEnd; ++i) {
if (i % 8 != 0)
drawPixel_SDL(half, i, color);
}
}
void drawEverything() {
drawBackground(BACKGROUND_COLOR);
drawBall((size_t)(g_ballPosX), (size_t)(g_ballPosY), g_ballRadius, BALL_COLOR);
drawRacket(g_player1PositionX, (size_t)(g_player1PosY), g_playerWidth, g_playerHeight, PLAYER1_COLOR);
drawRacket(g_player2PositionX, (size_t)(g_player2PosY), g_playerWidth, g_playerHeight, PLAYER2_COLOR);
}