test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 10 Jun 2009 13:34:20 +0000
changeset 3186 51750b7a966f
parent 3183 d1436442215f
child 3264 8fe0806637df
permissions -rw-r--r--
indent
     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     /* Move the sprite, bounce at the wall, and draw */
   138     n = 0;
   139     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   140     SDL_RenderFill(NULL);
   141     for (i = 0; i < num_sprites; ++i) {
   142         position = &positions[i];
   143         velocity = &velocities[i];
   144         position->x += velocity->x;
   145         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   146             velocity->x = -velocity->x;
   147             position->x += velocity->x;
   148         }
   149         position->y += velocity->y;
   150         if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   151             velocity->y = -velocity->y;
   152             position->y += velocity->y;
   153         }
   154 
   155         /* Blit the sprite onto the screen */
   156         SDL_RenderCopy(sprite, NULL, position);
   157     }
   158 
   159     /* Update the screen! */
   160     SDL_RenderPresent();
   161 }
   162 
   163 int
   164 main(int argc, char *argv[])
   165 {
   166     int i, done;
   167     SDL_Event event;
   168     Uint32 then, now, frames;
   169 
   170     /* Initialize parameters */
   171     num_sprites = NUM_SPRITES;
   172 
   173     /* Initialize test framework */
   174     state = CommonCreateState(argv, SDL_INIT_VIDEO);
   175     if (!state) {
   176         return 1;
   177     }
   178     for (i = 1; i < argc;) {
   179         int consumed;
   180 
   181         consumed = CommonArg(state, i);
   182         if (consumed == 0) {
   183             consumed = -1;
   184             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   185                 if (argv[i + 1]) {
   186                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   187                         blendMode = SDL_BLENDMODE_NONE;
   188                         consumed = 2;
   189                     } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
   190                         blendMode = SDL_BLENDMODE_MASK;
   191                         consumed = 2;
   192                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   193                         blendMode = SDL_BLENDMODE_BLEND;
   194                         consumed = 2;
   195                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   196                         blendMode = SDL_BLENDMODE_ADD;
   197                         consumed = 2;
   198                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   199                         blendMode = SDL_BLENDMODE_MOD;
   200                         consumed = 2;
   201                     }
   202                 }
   203             } else if (SDL_strcasecmp(argv[i], "--scale") == 0) {
   204                 if (argv[i + 1]) {
   205                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   206                         scaleMode = SDL_TEXTURESCALEMODE_NONE;
   207                         consumed = 2;
   208                     } else if (SDL_strcasecmp(argv[i + 1], "fast") == 0) {
   209                         scaleMode = SDL_TEXTURESCALEMODE_FAST;
   210                         consumed = 2;
   211                     } else if (SDL_strcasecmp(argv[i + 1], "slow") == 0) {
   212                         scaleMode = SDL_TEXTURESCALEMODE_SLOW;
   213                         consumed = 2;
   214                     } else if (SDL_strcasecmp(argv[i + 1], "best") == 0) {
   215                         scaleMode = SDL_TEXTURESCALEMODE_BEST;
   216                         consumed = 2;
   217                     }
   218                 }
   219             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   220                 cycle_color = SDL_TRUE;
   221                 consumed = 1;
   222             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   223                 cycle_alpha = SDL_TRUE;
   224                 consumed = 1;
   225             } else if (SDL_isdigit(*argv[i])) {
   226                 num_sprites = SDL_atoi(argv[i]);
   227                 consumed = 1;
   228             }
   229         }
   230         if (consumed < 0) {
   231             fprintf(stderr,
   232                     "Usage: %s %s [--blend none|mask|blend|add|mod] [--scale none|fast|slow|best] [--cyclecolor] [--cyclealpha]\n",
   233                     argv[0], CommonUsage(state));
   234             quit(1);
   235         }
   236         i += consumed;
   237     }
   238     if (!CommonInit(state)) {
   239         quit(2);
   240     }
   241 
   242     /* Create the windows, initialize the renderers, and load the textures */
   243     sprites =
   244         (SDL_TextureID *) SDL_malloc(state->num_windows * sizeof(*sprites));
   245     if (!sprites) {
   246         fprintf(stderr, "Out of memory!\n");
   247         quit(2);
   248     }
   249     for (i = 0; i < state->num_windows; ++i) {
   250         SDL_SelectRenderer(state->windows[i]);
   251         SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   252         SDL_RenderFill(NULL);
   253     }
   254     if (LoadSprite("icon.bmp") < 0) {
   255         quit(2);
   256     }
   257 
   258     /* Allocate memory for the sprite info */
   259     positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   260     velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   261     if (!positions || !velocities) {
   262         fprintf(stderr, "Out of memory!\n");
   263         quit(2);
   264     }
   265     srand(time(NULL));
   266     if (scaleMode != SDL_TEXTURESCALEMODE_NONE) {
   267         sprite_w += sprite_w / 2;
   268         sprite_h += sprite_h / 2;
   269     }
   270     for (i = 0; i < num_sprites; ++i) {
   271         positions[i].x = rand() % (state->window_w - sprite_w);
   272         positions[i].y = rand() % (state->window_h - sprite_h);
   273         positions[i].w = sprite_w;
   274         positions[i].h = sprite_h;
   275         velocities[i].x = 0;
   276         velocities[i].y = 0;
   277         while (!velocities[i].x && !velocities[i].y) {
   278             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   279             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   280         }
   281     }
   282 
   283     /* Main render loop */
   284     frames = 0;
   285     then = SDL_GetTicks();
   286     done = 0;
   287     while (!done) {
   288         /* Check for events */
   289         ++frames;
   290         while (SDL_PollEvent(&event)) {
   291             CommonEvent(state, &event, &done);
   292             switch (event.type) {
   293             case SDL_WINDOWEVENT:
   294                 switch (event.window.event) {
   295                 case SDL_WINDOWEVENT_EXPOSED:
   296                     SDL_SelectRenderer(event.window.windowID);
   297                     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   298                     SDL_RenderFill(NULL);
   299                     break;
   300                 }
   301                 break;
   302             default:
   303                 break;
   304             }
   305         }
   306         for (i = 0; i < state->num_windows; ++i) {
   307             MoveSprites(state->windows[i], sprites[i]);
   308         }
   309     }
   310 
   311     /* Print out some timing information */
   312     now = SDL_GetTicks();
   313     if (now > then) {
   314         double fps = ((double) frames * 1000) / (now - then);
   315         printf("%2.2f frames per second\n", fps);
   316     }
   317     quit(0);
   318 }
   319 
   320 /* vi: set ts=4 sw=4 expandtab: */