test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 19 Sep 2009 05:12:26 +0000
changeset 3264 8fe0806637df
parent 3186 51750b7a966f
child 3265 8f534bf37bef
permissions -rw-r--r--
Fixed bug #783

Fixed coordinate positioning with OpenGL renderer, and added a test case
     1 /* Simple program:  Move N sprites around on the screen as fast as possible */
     2 
     3 #include <stdlib.h>
     4 #include <stdio.h>
     5 #include <time.h>
     6 
     7 #include "common.h"
     8 
     9 #define NUM_SPRITES	100
    10 #define MAX_SPEED 	1
    11 
    12 static CommonState *state;
    13 static int num_sprites;
    14 static SDL_TextureID *sprites;
    15 static SDL_bool cycle_color;
    16 static SDL_bool cycle_alpha;
    17 static int cycle_direction = 1;
    18 static int current_alpha = 0;
    19 static int current_color = 0;
    20 static SDL_Rect *positions;
    21 static SDL_Rect *velocities;
    22 static int sprite_w, sprite_h;
    23 static SDL_BlendMode blendMode = SDL_BLENDMODE_MASK;
    24 static SDL_TextureScaleMode scaleMode = SDL_TEXTURESCALEMODE_NONE;
    25 
    26 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    27 static void
    28 quit(int rc)
    29 {
    30     if (sprites) {
    31         SDL_free(sprites);
    32     }
    33     if (positions) {
    34         SDL_free(positions);
    35     }
    36     if (velocities) {
    37         SDL_free(velocities);
    38     }
    39     CommonQuit(state);
    40     exit(rc);
    41 }
    42 
    43 int
    44 LoadSprite(char *file)
    45 {
    46     int i;
    47     SDL_Surface *temp;
    48 
    49     /* Load the sprite image */
    50     temp = SDL_LoadBMP(file);
    51     if (temp == NULL) {
    52         fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    53         return (-1);
    54     }
    55     sprite_w = temp->w;
    56     sprite_h = temp->h;
    57 
    58     /* Set transparent pixel as the pixel at (0,0) */
    59     if (temp->format->palette) {
    60         SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
    61     } else {
    62         switch (temp->format->BitsPerPixel) {
    63         case 15:
    64             SDL_SetColorKey(temp, SDL_SRCCOLORKEY,
    65                             (*(Uint16 *) temp->pixels) & 0x00007FFF);
    66             break;
    67         case 16:
    68             SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint16 *) temp->pixels);
    69             break;
    70         case 24:
    71             SDL_SetColorKey(temp, SDL_SRCCOLORKEY,
    72                             (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    73             break;
    74         case 32:
    75             SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint32 *) temp->pixels);
    76             break;
    77         }
    78     }
    79 
    80     /* Create textures from the image */
    81     for (i = 0; i < state->num_windows; ++i) {
    82         SDL_SelectRenderer(state->windows[i]);
    83         sprites[i] = SDL_CreateTextureFromSurface(0, temp);
    84         if (!sprites[i]) {
    85             fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    86             SDL_FreeSurface(temp);
    87             return (-1);
    88         }
    89         SDL_SetTextureBlendMode(sprites[i], blendMode);
    90         SDL_SetTextureScaleMode(sprites[i], scaleMode);
    91     }
    92     SDL_FreeSurface(temp);
    93 
    94     /* We're ready to roll. :) */
    95     return (0);
    96 }
    97 
    98 void
    99 MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
   100 {
   101     int i, n;
   102     int window_w, window_h;
   103     SDL_Rect *position, *velocity;
   104 
   105     SDL_SelectRenderer(window);
   106 
   107     /* Query the sizes */
   108     SDL_GetWindowSize(window, &window_w, &window_h);
   109 
   110     /* Cycle the color and alpha, if desired */
   111     if (cycle_color) {
   112         current_color += cycle_direction;
   113         if (current_color < 0) {
   114             current_color = 0;
   115             cycle_direction = -cycle_direction;
   116         }
   117         if (current_color > 255) {
   118             current_color = 255;
   119             cycle_direction = -cycle_direction;
   120         }
   121         SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
   122                                (Uint8) current_color);
   123     }
   124     if (cycle_alpha) {
   125         current_alpha += cycle_direction;
   126         if (current_alpha < 0) {
   127             current_alpha = 0;
   128             cycle_direction = -cycle_direction;
   129         }
   130         if (current_alpha > 255) {
   131             current_alpha = 255;
   132             cycle_direction = -cycle_direction;
   133         }
   134         SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
   135     }
   136 
   137     /* Draw a gray background */
   138     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   139     SDL_RenderFill(NULL);
   140 
   141     /* Draw two red points to make sure they show up correctly */
   142     SDL_SetRenderDrawColor(0xFF, 0x00, 0x00, 0xFF);
   143     SDL_RenderPoint(0, 0);
   144     SDL_RenderPoint(window_w-1, 0);
   145     SDL_RenderPoint(0, window_h-1);
   146     SDL_RenderPoint(window_w-1, window_h-1);
   147 
   148     /* Move the sprite, bounce at the wall, and draw */
   149     n = 0;
   150     for (i = 0; i < num_sprites; ++i) {
   151         position = &positions[i];
   152         velocity = &velocities[i];
   153         position->x += velocity->x;
   154         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   155             velocity->x = -velocity->x;
   156             position->x += velocity->x;
   157         }
   158         position->y += velocity->y;
   159         if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   160             velocity->y = -velocity->y;
   161             position->y += velocity->y;
   162         }
   163 
   164         /* Blit the sprite onto the screen */
   165         SDL_RenderCopy(sprite, NULL, position);
   166     }
   167 
   168     /* Update the screen! */
   169     SDL_RenderPresent();
   170 }
   171 
   172 int
   173 main(int argc, char *argv[])
   174 {
   175     int i, done;
   176     SDL_Event event;
   177     Uint32 then, now, frames;
   178 
   179     /* Initialize parameters */
   180     num_sprites = NUM_SPRITES;
   181 
   182     /* Initialize test framework */
   183     state = CommonCreateState(argv, SDL_INIT_VIDEO);
   184     if (!state) {
   185         return 1;
   186     }
   187     for (i = 1; i < argc;) {
   188         int consumed;
   189 
   190         consumed = CommonArg(state, i);
   191         if (consumed == 0) {
   192             consumed = -1;
   193             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   194                 if (argv[i + 1]) {
   195                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   196                         blendMode = SDL_BLENDMODE_NONE;
   197                         consumed = 2;
   198                     } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
   199                         blendMode = SDL_BLENDMODE_MASK;
   200                         consumed = 2;
   201                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   202                         blendMode = SDL_BLENDMODE_BLEND;
   203                         consumed = 2;
   204                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   205                         blendMode = SDL_BLENDMODE_ADD;
   206                         consumed = 2;
   207                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   208                         blendMode = SDL_BLENDMODE_MOD;
   209                         consumed = 2;
   210                     }
   211                 }
   212             } else if (SDL_strcasecmp(argv[i], "--scale") == 0) {
   213                 if (argv[i + 1]) {
   214                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   215                         scaleMode = SDL_TEXTURESCALEMODE_NONE;
   216                         consumed = 2;
   217                     } else if (SDL_strcasecmp(argv[i + 1], "fast") == 0) {
   218                         scaleMode = SDL_TEXTURESCALEMODE_FAST;
   219                         consumed = 2;
   220                     } else if (SDL_strcasecmp(argv[i + 1], "slow") == 0) {
   221                         scaleMode = SDL_TEXTURESCALEMODE_SLOW;
   222                         consumed = 2;
   223                     } else if (SDL_strcasecmp(argv[i + 1], "best") == 0) {
   224                         scaleMode = SDL_TEXTURESCALEMODE_BEST;
   225                         consumed = 2;
   226                     }
   227                 }
   228             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   229                 cycle_color = SDL_TRUE;
   230                 consumed = 1;
   231             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   232                 cycle_alpha = SDL_TRUE;
   233                 consumed = 1;
   234             } else if (SDL_isdigit(*argv[i])) {
   235                 num_sprites = SDL_atoi(argv[i]);
   236                 consumed = 1;
   237             }
   238         }
   239         if (consumed < 0) {
   240             fprintf(stderr,
   241                     "Usage: %s %s [--blend none|mask|blend|add|mod] [--scale none|fast|slow|best] [--cyclecolor] [--cyclealpha]\n",
   242                     argv[0], CommonUsage(state));
   243             quit(1);
   244         }
   245         i += consumed;
   246     }
   247     if (!CommonInit(state)) {
   248         quit(2);
   249     }
   250 
   251     /* Create the windows, initialize the renderers, and load the textures */
   252     sprites =
   253         (SDL_TextureID *) SDL_malloc(state->num_windows * sizeof(*sprites));
   254     if (!sprites) {
   255         fprintf(stderr, "Out of memory!\n");
   256         quit(2);
   257     }
   258     for (i = 0; i < state->num_windows; ++i) {
   259         SDL_SelectRenderer(state->windows[i]);
   260         SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   261         SDL_RenderFill(NULL);
   262     }
   263     if (LoadSprite("icon.bmp") < 0) {
   264         quit(2);
   265     }
   266 
   267     /* Allocate memory for the sprite info */
   268     positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   269     velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   270     if (!positions || !velocities) {
   271         fprintf(stderr, "Out of memory!\n");
   272         quit(2);
   273     }
   274     srand(time(NULL));
   275     if (scaleMode != SDL_TEXTURESCALEMODE_NONE) {
   276         sprite_w += sprite_w / 2;
   277         sprite_h += sprite_h / 2;
   278     }
   279     for (i = 0; i < num_sprites; ++i) {
   280         positions[i].x = rand() % (state->window_w - sprite_w);
   281         positions[i].y = rand() % (state->window_h - sprite_h);
   282         positions[i].w = sprite_w;
   283         positions[i].h = sprite_h;
   284         velocities[i].x = 0;
   285         velocities[i].y = 0;
   286         while (!velocities[i].x && !velocities[i].y) {
   287             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   288             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   289         }
   290     }
   291 
   292     /* Main render loop */
   293     frames = 0;
   294     then = SDL_GetTicks();
   295     done = 0;
   296     while (!done) {
   297         /* Check for events */
   298         ++frames;
   299         while (SDL_PollEvent(&event)) {
   300             CommonEvent(state, &event, &done);
   301             switch (event.type) {
   302             case SDL_WINDOWEVENT:
   303                 switch (event.window.event) {
   304                 case SDL_WINDOWEVENT_EXPOSED:
   305                     SDL_SelectRenderer(event.window.windowID);
   306                     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   307                     SDL_RenderFill(NULL);
   308                     break;
   309                 }
   310                 break;
   311             default:
   312                 break;
   313             }
   314         }
   315         for (i = 0; i < state->num_windows; ++i) {
   316             MoveSprites(state->windows[i], sprites[i]);
   317         }
   318     }
   319 
   320     /* Print out some timing information */
   321     now = SDL_GetTicks();
   322     if (now > then) {
   323         double fps = ((double) frames * 1000) / (now - then);
   324         printf("%2.2f frames per second\n", fps);
   325     }
   326     quit(0);
   327 }
   328 
   329 /* vi: set ts=4 sw=4 expandtab: */