home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Programming / SDL-Amiga / demo / testsprite.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-22  |  6.7 KB  |  278 lines

  1.  
  2. /* Simple program:  Move N sprites around on the screen as fast as possible */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <time.h>
  9.  
  10. #include "SDL.h"
  11.  
  12. #define NUM_SPRITES    100
  13. #define MAX_SPEED     1
  14.  
  15. SDL_Surface *sprite;
  16. int numsprites;
  17. SDL_Rect *sprite_rects;
  18. SDL_Rect *positions;
  19. SDL_Rect *velocities;
  20. int sprites_visible;
  21.  
  22. int LoadSprite(SDL_Surface *screen, char *file)
  23. {
  24.     SDL_Surface *temp;
  25.  
  26.     /* Load the sprite image */
  27.     sprite = SDL_LoadBMP(file);
  28.     if ( sprite == NULL ) {
  29.         fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
  30.         return(-1);
  31.     }
  32.  
  33.     /* Set transparent pixel as the pixel at (0,0) */
  34.     if ( sprite->format->palette ) {
  35.         SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
  36.                         *(Uint8 *)sprite->pixels);
  37.     }
  38.  
  39.     /* Convert sprite to video format */
  40.     temp = SDL_DisplayFormat(sprite);
  41.     SDL_FreeSurface(sprite);
  42.     if ( temp == NULL ) {
  43.         fprintf(stderr, "Couldn't convert background: %s\n",
  44.                             SDL_GetError());
  45.         return(-1);
  46.     }
  47.     sprite = temp;
  48.  
  49.     /* We're ready to roll. :) */
  50.     return(0);
  51. }
  52.  
  53. void MoveSprites(SDL_Surface *screen, Uint32 background)
  54. {
  55.     int i, nupdates;
  56.     SDL_Rect area, *position, *velocity;
  57.  
  58.     nupdates = 0;
  59.     /* Erase all the sprites if necessary */
  60.     if ( sprites_visible ) {
  61.         SDL_FillRect(screen, NULL, background);
  62.     }
  63.  
  64.     /* Move the sprite, bounce at the wall, and draw */
  65.     for ( i=0; i<numsprites; ++i ) {
  66.         position = &positions[i];
  67.         velocity = &velocities[i];
  68.         position->x += velocity->x;
  69.         if ( (position->x < 0) || (position->x >= screen->w) ) {
  70.             velocity->x = -velocity->x;
  71.             position->x += velocity->x;
  72.         }
  73.         position->y += velocity->y;
  74.         if ( (position->y < 0) || (position->y >= screen->h) ) {
  75.             velocity->y = -velocity->y;
  76.             position->y += velocity->y;
  77.         }
  78.  
  79.         /* Blit the sprite onto the screen */
  80.         area = *position;
  81.         SDL_BlitSurface(sprite, NULL, screen, &area);
  82.         sprite_rects[nupdates++] = area;
  83.     }
  84.  
  85.     /* Update the screen! */
  86.     if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
  87.         SDL_Flip(screen);
  88.     } else {
  89.         SDL_UpdateRects(screen, nupdates, sprite_rects);
  90.     }
  91.     sprites_visible = 1;
  92. }
  93.  
  94. /* This is a way of telling whether or not to use hardware surfaces */
  95. Uint32 FastestFlags(Uint32 flags)
  96. {
  97.     const SDL_VideoInfo *info;
  98.  
  99.     /* Hardware acceleration is only used in fullscreen mode */
  100.     flags |= SDL_FULLSCREEN;
  101.  
  102.     /* Check for various video capabilities */
  103.     info = SDL_GetVideoInfo();
  104.     if ( info->blit_hw_CC && info->blit_fill ) {
  105.         /* We use accelerated colorkeying and color filling */
  106.         flags |= SDL_HWSURFACE;
  107.     }
  108.     /* If we have enough video memory, and will use accelerated
  109.        blits directly to it, then use page flipping.
  110.      */
  111.     if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
  112.         /* Direct hardware blitting without double-buffering
  113.            causes really bad flickering.
  114.          */
  115.         if ( info->video_mem > 640*480 ) {
  116.             flags |= SDL_DOUBLEBUF;
  117.         } else {
  118.             flags &= ~SDL_HWSURFACE;
  119.         }
  120.     }
  121.  
  122.     /* Return the flags */
  123.     return(flags);
  124. }
  125.  
  126. int main(int argc, char *argv[])
  127. {
  128.     SDL_Surface *screen;
  129.     Uint8 *mem;
  130.     Uint8  video_bpp;
  131.     Uint32 videoflags;
  132.     Uint32 background;
  133.     int    i, done;
  134.     SDL_Event event;
  135.     Uint32 then, now, frames;
  136.  
  137.     /* Initialize SDL */
  138.     if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
  139.         fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
  140.         exit(1);
  141.     }
  142.     atexit(SDL_Quit);
  143.  
  144.     numsprites = NUM_SPRITES;
  145.     videoflags = SDL_SWSURFACE|SDL_ANYFORMAT;
  146.     video_bpp = 8;
  147.     while ( argc > 1 ) {
  148.         --argc;
  149.         if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
  150.             video_bpp = atoi(argv[argc]);
  151.             videoflags &= ~SDL_ANYFORMAT;
  152.             --argc;
  153.         } else
  154.         if ( strcmp(argv[argc], "-fast") == 0 ) {
  155.             videoflags = FastestFlags(videoflags);
  156.         } else
  157.         if ( strcmp(argv[argc], "-hw") == 0 ) {
  158.             videoflags ^= SDL_HWSURFACE;
  159.         } else
  160.         if ( strcmp(argv[argc], "-flip") == 0 ) {
  161.             videoflags ^= SDL_DOUBLEBUF;
  162.         } else
  163.         if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
  164.             videoflags ^= SDL_FULLSCREEN;
  165.         } else
  166.         if ( isdigit(argv[argc][0]) ) {
  167.             numsprites = atoi(argv[argc]);
  168.         } else {
  169.             fprintf(stderr, 
  170.     "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
  171.                                 argv[0]);
  172.             exit(1);
  173.         }
  174.     }
  175.  
  176.     /* Set 640x480 video mode */
  177.     if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
  178.         fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
  179.                                 SDL_GetError());
  180.         exit(2);
  181.     }
  182.  
  183.     /* Load the sprite */
  184.     if ( LoadSprite(screen, "icon.bmp") < 0 ) {
  185.         exit(1);
  186.     }
  187.  
  188.     /* Allocate memory for the sprite info */
  189.     mem = (Uint8 *)malloc(4*sizeof(SDL_Rect)*numsprites);
  190.     if ( mem == NULL ) {
  191.         SDL_FreeSurface(sprite);
  192.         fprintf(stderr, "Out of memory!\n");
  193.         exit(2);
  194.     }
  195.     sprite_rects = (SDL_Rect *)mem;
  196.     positions = sprite_rects;
  197.     sprite_rects += numsprites;
  198.     velocities = sprite_rects;
  199.     sprite_rects += numsprites;
  200.     srand(time(NULL));
  201.     for ( i=0; i<numsprites; ++i ) {
  202.         positions[i].x = rand()%screen->w;
  203.         positions[i].y = rand()%screen->h;
  204.         positions[i].w = sprite->w;
  205.         positions[i].h = sprite->h;
  206.         velocities[i].x = 0;
  207.         velocities[i].y = 0;
  208.         while ( ! velocities[i].x && ! velocities[i].y ) {
  209.             velocities[i].x = (rand()%(MAX_SPEED*2+1))-MAX_SPEED;
  210.             velocities[i].y = (rand()%(MAX_SPEED*2+1))-MAX_SPEED;
  211.         }
  212.     }
  213.     background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
  214.  
  215.     /* Print out information about our surfaces */
  216.     printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
  217.     if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
  218.         printf("Screen is in video memory\n");
  219.     } else {
  220.         printf("Screen is in system memory\n");
  221.     }
  222.     if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
  223.         printf("Screen has double-buffering enabled\n");
  224.     }
  225.     if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
  226.         printf("Sprite is in video memory\n");
  227.     } else {
  228.         printf("Sprite is in system memory\n");
  229.     }
  230.     /* Run a sample blit to trigger blit acceleration */
  231.     { SDL_Rect dst;
  232.         dst.x = 0;
  233.         dst.y = 0;
  234.         dst.w = sprite->w;
  235.         dst.h = sprite->h;
  236.         SDL_BlitSurface(sprite, NULL, screen, &dst);
  237.         SDL_FillRect(screen, &dst, background);
  238.     }
  239.     if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
  240.         printf("Sprite blit uses hardware acceleration\n");
  241.     }
  242.     if ( (sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
  243.         printf("Sprite blit uses RLE acceleration\n");
  244.     }
  245.  
  246.     /* Loop, blitting sprites and waiting for a keystroke */
  247.     frames = 0;
  248.     then = SDL_GetTicks();
  249.     done = 0;
  250.     sprites_visible = 0;
  251.     while ( !done ) {
  252.         /* Check for events */
  253.         ++frames;
  254.         while ( SDL_PollEvent(&event) ) {
  255.             switch (event.type) {
  256.                 case SDL_KEYDOWN:
  257.                     /* Any keypress quits the app... */
  258.                 case SDL_QUIT:
  259.                     done = 1;
  260.                     break;
  261.                 default:
  262.                     break;
  263.             }
  264.         }
  265.         MoveSprites(screen, background);
  266.     }
  267.     SDL_FreeSurface(sprite);
  268.     free(mem);
  269.  
  270.     /* Print out some timing information */
  271.     now = SDL_GetTicks();
  272.     if ( now > then ) {
  273.         printf("%2.2f frames per second\n",
  274.                     ((double)frames*1000)/(now-then));
  275.     }
  276.     return 0;
  277. }
  278.