LinuxDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


Retro Gaming Hacks, Part 2: Add Paddles to Pong
Pages: 1, 2, 3, 4, 5, 6

And add the following implementation at the end of the file:



/* Function: resetSprites()
 *
 * Moves all sprites back to their starting positions.
 *
 * Parameters:
 *
 *   *game  - game data
 *    erase - if true, sprites will be erased first
 */

void resetSprites( GameData *game, int erase ) {

  // Erase sprites from current locations?
  if (erase) {

    SDL_FillRect( game->screen, &(game->ball), game->black );
    SDL_FillRect( game->screen, &(game->p1),   game->black );
    SDL_FillRect( game->screen, &(game->p2),   game->black );
    game->rects[game->num_rects++] = game->ball;
    game->rects[game->num_rects++] = game->p1;
    game->rects[game->num_rects++] = game->p2;

  } // if (erasing sprites)

  // The ball is a 2x2 sprite, centred both horizontally and vertically
  game->ball.x = BALL_X;
  game->ball.y = BALL_Y;
  game->ball.w = BALL_W;
  game->ball.h = BALL_H;

  // Player 1's paddle is a 5x50 sprite, flush left and centred vertically
  game->p1.x = P1_X;
  game->p1.y = P1_Y;
  game->p1.w = P1_W;
  game->p1.h = P1_H;

  // Player 2's paddle is a 5x50 sprite, flush left and centred vertically
  game->p2.x = P2_X;
  game->p2.y = P2_Y;
  game->p2.w = P2_W;
  game->p2.h = P2_H;
    
  // Draw the playing field
  SDL_FillRect( game->screen, &(game->ball), game->white );
  SDL_FillRect( game->screen, &(game->p1),   game->white );
  SDL_FillRect( game->screen, &(game->p2),   game->white );
  game->rects[game->num_rects++] = game->ball;
  game->rects[game->num_rects++] = game->p1;
  game->rects[game->num_rects++] = game->p2;

} // resetSprites()

The first parameter passed into resetSprites() is the GameData structure, and the second one controls whether the sprites are first erased (by filling their current locations with black, the background color) before they are reset and redrawn. Since all we are concerned with right now is drawing the sprites, and have therefore set the second parameter resetSprites() to 0, all resetSprites() has to do is put all three sprites back in their original positions by setting the x, y, w (width), and h (height) of each rectangle to the initial values that we defined as macros in the beginning of this section. SDL_FillRect() is then called, with the target surface (the main window) as the first parameter, a pointer to the SDL_Rect structure that is being filled as the second, and the fill color as the third. Since resetSprites() itself receives the GameData structure as a pointer, it must de-reference the game pointer to get at the rectangle in question. But since that is a structure and not a pointer, it must pass the address of the SDL_Rect structure to SDL_FillRect(); again, don't worry too much about this if it is making your head hurt. As long as you always use this notation within the confines of the resetSprites() function, you will be fine.

Believe it or not, calling SDL_FillRect() does not necessarily mean that the screen will be updated to display the newly filled rectangle, since SDL has all sorts of performance concerns to weigh. This is why you will find the following three crazy lines of code in the resetSprites() function:

  game->rects[game->num_rects++] = game->ball;
  game->rects[game->num_rects++] = game->p1;
  game->rects[game->num_rects++] = game->p2;

What this code is doing is adding the three rectangles that were just filled to the array of rectangles that will be updated first thing in the main loop (well, once we actually write the main loop). Note that we are incrementing the num_rects member of the GameData structure in the same line in which we do the assignment.

To actually force the screen update, add the lines of code shown in bold at the very beginning of the main loop:

  // Main loop
  while (1) {

    // (Re)draw the screen
    if (game.num_rects) {

      SDL_UpdateRects( game.screen, game.num_rects, game.rects );
      game.num_rects = 0;

    } // if (updating screen)

SDL_UpdateRects() takes as its parameters the surface to update (the main window), the number of rectangular regions to update, and the array of rectangles itself. Because only a few regions of the screen are being updated, you can achieve flicker-free graphics (for Pong, at least) without having to deal with double-buffering (which, in SDL, requires a hardware surface, and we are getting away with just a software surface).

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow




Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!


Linux Resources
  • Linux Online
  • The Linux FAQ
  • linux.java.net
  • Linux Kernel Archives
  • Kernel Traffic
  • DistroWatch.com


  • Sponsored by: