Simple DirectMedia Layer: Gaming Platform for C++, Java, and More
Running the Game
The main logic loop is, of course, all inside RunGame(). This function is about 200 lines of code, so still too much to look at all at once, but you will examine some of the more important sections by way of explaining SDL features. Where I've deleted some code, I'll indicate with an ellipsis ( ... ) in the comments.
301 void RunGame(void) 302 { 303 int i, j; 304 SDL_Event event; 305 Uint8 *keys; 306 307 /* Paint the background */ 308 numupdates = 0; 309 for ( i=0; i<screen->w; i += background->w ) { 310 SDL_Rect dst; 311 312 dst.x = i; 313 dst.y = 0; 314 dst.w = background->w; 315 dst.h = background->h; 316 SDL_BlitSurface(background, NULL, screen, &dst); 317 } 318 SDL_UpdateRect(screen, 0, 0, 0, 0);
In the first section (Lines #301 to 318), you are simply blit-ing slices of background color onto the screen buffer sequentially with SDL_BlitSurface() and finally set up a repaint with SDL_UpdateRect().
319 320 /* Initialize the objects */ 321 // . . . 333 CreateAlien(); 334 DrawObject(&aliens[0]); 335 UpdateScreen(); 336 337 while ( player.alive ) { 338 /* Wait for the next frame */ 339 WaitFrame(); 340 341 /* Poll input queue, run keyboard loop */ 342 while ( SDL_PollEvent(&event) ) { 343 if ( event.type == SDL_QUIT ) 344 return; 345 } 346 keys = SDL_GetKeyState(NULL); 347 348 /* Erase everything from the screen */ 349 // . . . 366 /* Decrement the lifetime of the explosions */ 367 // . . . 373 /* Create new aliens */ 374 if ( (rand()%ALIEN_ODDS) == 0 ) { 375 CreateAlien(); 376 } 377 378 /* Create new shots */ 379 if ( ! reloading ) { 380 if ( keys[SDLK_SPACE] == SDL_PRESSED ) { 381 for ( i=0; i<MAX_SHOTS; ++i ) { 382 if ( ! shots[i].alive ) { 383 break; 384 } 385 } 386 if ( i != MAX_SHOTS ) { 387 shots[i].x = player.x + 388 (player.image->w-shots[i].image->w)/2; 389 shots[i].y = player.y - 390 shots[i].image->h; 391 shots[i].alive = 1; 392 Mix_PlayChannel(SHOT_WAV, 393 sounds[SHOT_WAV], 0); 394 } 395 } 396 }
Now, look at the input-management part of the RunGame() loop. The deceptively simple SDL_PollEvent() returns an SDL_Event object with notes about interesting things that might have happened. At this time, you check for an ESC key press with the special value of SDL_QUIT. Because this is a purely keyboard-driven game, you can use SDL_GetKeyState() to get all the info you need. Remember that the PC keyboard is designed to allow for multiple simultaneous key downs, so the result is returned in an array that you index by a keyboard code value such as SDLK_SPACE (in other words, spacebar).
397 reloading = (keys[SDLK_SPACE] == SDL_PRESSED); 398 399 /* Move the player */ 400 //. . . 415 /* Move the aliens and the shots */ 416 // . . . 443 444 /* Detect collisions */ 445 for ( j=0; j<MAX_SHOTS; ++j ) { 446 for ( i=0; i<MAX_ALIENS; ++i ) { 447 if ( shots[j].alive && aliens[i].alive && 448 Collide(&shots[j], &aliens[i]) ) { 449 aliens[i].alive = 0; 450 explosions[i].x = aliens[i].x; 451 explosions[i].y = aliens[i].y; 452 explosions[i].alive = EXPLODE_TIME; 453 Mix_PlayChannel(EXPLODE_WAV, 454 sounds[EXPLODE_WAV], 0); 455 shots[j].alive = 0; 456 break; 457 } 458 } 459 }
Page 2 of 3
This article was originally published on April 3, 2008