test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 04 Feb 2011 19:50:56 -0800
changeset 5184 d976b67150c5
parent 5158 b3ccd1947786
child 5297 1800dc39b74c
permissions -rw-r--r--
Restored SDL_BLENDMODE_MOD for MAME
     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_Texture **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_BLEND;
    24 
    25 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    26 static void
    27 quit(int rc)
    28 {
    29     if (sprites) {
    30         SDL_free(sprites);
    31     }
    32     if (positions) {
    33         SDL_free(positions);
    34     }
    35     if (velocities) {
    36         SDL_free(velocities);
    37     }
    38     CommonQuit(state);
    39     exit(rc);
    40 }
    41 
    42 int
    43 LoadSprite(char *file)
    44 {
    45     int i;
    46     SDL_Surface *temp;
    47 
    48     /* Load the sprite image */
    49     temp = SDL_LoadBMP(file);
    50     if (temp == NULL) {
    51         fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    52         return (-1);
    53     }
    54     sprite_w = temp->w;
    55     sprite_h = temp->h;
    56 
    57     /* Set transparent pixel as the pixel at (0,0) */
    58     if (temp->format->palette) {
    59         SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
    60     } else {
    61         switch (temp->format->BitsPerPixel) {
    62         case 15:
    63             SDL_SetColorKey(temp, 1, (*(Uint16 *) temp->pixels) & 0x00007FFF);
    64             break;
    65         case 16:
    66             SDL_SetColorKey(temp, 1, *(Uint16 *) temp->pixels);
    67             break;
    68         case 24:
    69             SDL_SetColorKey(temp, 1, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    70             break;
    71         case 32:
    72             SDL_SetColorKey(temp, 1, *(Uint32 *) temp->pixels);
    73             break;
    74         }
    75     }
    76 
    77     /* Create textures from the image */
    78     for (i = 0; i < state->num_windows; ++i) {
    79         SDL_Renderer *renderer = state->renderers[i];
    80         sprites[i] = SDL_CreateTextureFromSurface(renderer, temp);
    81         if (!sprites[i]) {
    82             fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    83             SDL_FreeSurface(temp);
    84             return (-1);
    85         }
    86         SDL_SetTextureBlendMode(sprites[i], blendMode);
    87     }
    88     SDL_FreeSurface(temp);
    89 
    90     /* We're ready to roll. :) */
    91     return (0);
    92 }
    93 
    94 void
    95 MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
    96 {
    97     int i, n;
    98     int window_w, window_h;
    99     SDL_Rect temp;
   100     SDL_Rect *position, *velocity;
   101 
   102     /* Query the sizes */
   103     SDL_GetWindowSize(window, &window_w, &window_h);
   104 
   105     /* Cycle the color and alpha, if desired */
   106     if (cycle_color) {
   107         current_color += cycle_direction;
   108         if (current_color < 0) {
   109             current_color = 0;
   110             cycle_direction = -cycle_direction;
   111         }
   112         if (current_color > 255) {
   113             current_color = 255;
   114             cycle_direction = -cycle_direction;
   115         }
   116         SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
   117                                (Uint8) current_color);
   118     }
   119     if (cycle_alpha) {
   120         current_alpha += cycle_direction;
   121         if (current_alpha < 0) {
   122             current_alpha = 0;
   123             cycle_direction = -cycle_direction;
   124         }
   125         if (current_alpha > 255) {
   126             current_alpha = 255;
   127             cycle_direction = -cycle_direction;
   128         }
   129         SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
   130     }
   131 
   132     /* Draw a gray background */
   133     SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   134     SDL_RenderClear(renderer);
   135 
   136     /* Test points */
   137     SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
   138     SDL_RenderDrawPoint(renderer, 0, 0);
   139     SDL_RenderDrawPoint(renderer, window_w-1, 0);
   140     SDL_RenderDrawPoint(renderer, 0, window_h-1);
   141     SDL_RenderDrawPoint(renderer, window_w-1, window_h-1);
   142 
   143     /* Test horizontal and vertical lines */
   144     SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
   145     SDL_RenderDrawLine(renderer, 1, 0, window_w-2, 0);
   146     SDL_RenderDrawLine(renderer, 1, window_h-1, window_w-2, window_h-1);
   147     SDL_RenderDrawLine(renderer, 0, 1, 0, window_h-2);
   148     SDL_RenderDrawLine(renderer, window_w-1, 1, window_w-1, window_h-2);
   149 
   150     /* Test fill and copy */
   151     SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
   152     temp.x = 1;
   153     temp.y = 1;
   154     temp.w = sprite_w;
   155     temp.h = sprite_h;
   156     SDL_RenderFillRect(renderer, &temp);
   157     SDL_RenderCopy(renderer, sprite, NULL, &temp);
   158     temp.x = window_w-sprite_w-1;
   159     temp.y = 1;
   160     temp.w = sprite_w;
   161     temp.h = sprite_h;
   162     SDL_RenderFillRect(renderer, &temp);
   163     SDL_RenderCopy(renderer, sprite, NULL, &temp);
   164     temp.x = 1;
   165     temp.y = window_h-sprite_h-1;
   166     temp.w = sprite_w;
   167     temp.h = sprite_h;
   168     SDL_RenderFillRect(renderer, &temp);
   169     SDL_RenderCopy(renderer, sprite, NULL, &temp);
   170     temp.x = window_w-sprite_w-1;
   171     temp.y = window_h-sprite_h-1;
   172     temp.w = sprite_w;
   173     temp.h = sprite_h;
   174     SDL_RenderFillRect(renderer, &temp);
   175     SDL_RenderCopy(renderer, sprite, NULL, &temp);
   176 
   177     /* Test diagonal lines */
   178     SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
   179     SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
   180                        window_w-sprite_w-2, window_h-sprite_h-2);
   181     SDL_RenderDrawLine(renderer, window_w-sprite_w-2, sprite_h,
   182                        sprite_w, window_h-sprite_h-2);
   183 
   184     /* Move the sprite, bounce at the wall, and draw */
   185     n = 0;
   186     for (i = 0; i < num_sprites; ++i) {
   187         position = &positions[i];
   188         velocity = &velocities[i];
   189         position->x += velocity->x;
   190         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   191             velocity->x = -velocity->x;
   192             position->x += velocity->x;
   193         }
   194         position->y += velocity->y;
   195         if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   196             velocity->y = -velocity->y;
   197             position->y += velocity->y;
   198         }
   199 
   200         /* Blit the sprite onto the screen */
   201         SDL_RenderCopy(renderer, sprite, NULL, position);
   202     }
   203 
   204     /* Update the screen! */
   205     SDL_RenderPresent(renderer);
   206 }
   207 
   208 int
   209 main(int argc, char *argv[])
   210 {
   211     int i, done;
   212     SDL_Event event;
   213     Uint32 then, now, frames;
   214 
   215     /* Initialize parameters */
   216     num_sprites = NUM_SPRITES;
   217 
   218     /* Initialize test framework */
   219     state = CommonCreateState(argv, SDL_INIT_VIDEO);
   220     if (!state) {
   221         return 1;
   222     }
   223     for (i = 1; i < argc;) {
   224         int consumed;
   225 
   226         consumed = CommonArg(state, i);
   227         if (consumed == 0) {
   228             consumed = -1;
   229             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   230                 if (argv[i + 1]) {
   231                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   232                         blendMode = SDL_BLENDMODE_NONE;
   233                         consumed = 2;
   234                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   235                         blendMode = SDL_BLENDMODE_BLEND;
   236                         consumed = 2;
   237                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   238                         blendMode = SDL_BLENDMODE_ADD;
   239                         consumed = 2;
   240                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   241                         blendMode = SDL_BLENDMODE_MOD;
   242                         consumed = 2;
   243                     }
   244                 }
   245             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   246                 cycle_color = SDL_TRUE;
   247                 consumed = 1;
   248             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   249                 cycle_alpha = SDL_TRUE;
   250                 consumed = 1;
   251             } else if (SDL_isdigit(*argv[i])) {
   252                 num_sprites = SDL_atoi(argv[i]);
   253                 consumed = 1;
   254             }
   255         }
   256         if (consumed < 0) {
   257             fprintf(stderr,
   258                     "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
   259                     argv[0], CommonUsage(state));
   260             quit(1);
   261         }
   262         i += consumed;
   263     }
   264     if (!CommonInit(state)) {
   265         quit(2);
   266     }
   267 
   268     /* Create the windows, initialize the renderers, and load the textures */
   269     sprites =
   270         (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites));
   271     if (!sprites) {
   272         fprintf(stderr, "Out of memory!\n");
   273         quit(2);
   274     }
   275     for (i = 0; i < state->num_windows; ++i) {
   276         SDL_Renderer *renderer = state->renderers[i];
   277         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   278         SDL_RenderClear(renderer);
   279     }
   280     if (LoadSprite("icon.bmp") < 0) {
   281         quit(2);
   282     }
   283 
   284     /* Allocate memory for the sprite info */
   285     positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   286     velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   287     if (!positions || !velocities) {
   288         fprintf(stderr, "Out of memory!\n");
   289         quit(2);
   290     }
   291     srand((unsigned int)time(NULL));
   292     for (i = 0; i < num_sprites; ++i) {
   293         positions[i].x = rand() % (state->window_w - sprite_w);
   294         positions[i].y = rand() % (state->window_h - sprite_h);
   295         positions[i].w = sprite_w;
   296         positions[i].h = sprite_h;
   297         velocities[i].x = 0;
   298         velocities[i].y = 0;
   299         while (!velocities[i].x && !velocities[i].y) {
   300             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   301             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   302         }
   303     }
   304 
   305     /* Main render loop */
   306     frames = 0;
   307     then = SDL_GetTicks();
   308     done = 0;
   309     while (!done) {
   310         /* Check for events */
   311         ++frames;
   312         while (SDL_PollEvent(&event)) {
   313             CommonEvent(state, &event, &done);
   314         }
   315         for (i = 0; i < state->num_windows; ++i) {
   316             MoveSprites(state->windows[i], state->renderers[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     return 0;
   328 }
   329 
   330 /* vi: set ts=4 sw=4 expandtab: */