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