test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Jan 2011 22:44:43 -0800
changeset 5138 da10636e5eca
parent 4929 aa8888658021
child 5140 e743b9c3f6d6
permissions -rw-r--r--
Making the API simpler, scaling is always defined as linear interpolation and should be supported as much as possible on all renderers.
     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_MASK;
    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_SelectRenderer(state->windows[i]);
    80         sprites[i] = SDL_CreateTextureFromSurface(0, temp);
    81         if (!sprites[i]) {
    82 		SDL_SetColorKey(temp, 0, 0);
    83 		sprites[i] = SDL_CreateTextureFromSurface(0, temp);
    84 	}
    85         if (!sprites[i]) {
    86             fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    87             SDL_FreeSurface(temp);
    88             return (-1);
    89         }
    90         SDL_SetTextureBlendMode(sprites[i], blendMode);
    91     }
    92     SDL_FreeSurface(temp);
    93 
    94     /* We're ready to roll. :) */
    95     return (0);
    96 }
    97 
    98 void
    99 MoveSprites(SDL_Window * window, SDL_Texture * sprite)
   100 {
   101     int i, n;
   102     int window_w, window_h;
   103     SDL_Rect temp;
   104     SDL_Rect *position, *velocity;
   105 
   106     SDL_SelectRenderer(window);
   107 
   108     /* Query the sizes */
   109     SDL_GetWindowSize(window, &window_w, &window_h);
   110 
   111     /* Cycle the color and alpha, if desired */
   112     if (cycle_color) {
   113         current_color += cycle_direction;
   114         if (current_color < 0) {
   115             current_color = 0;
   116             cycle_direction = -cycle_direction;
   117         }
   118         if (current_color > 255) {
   119             current_color = 255;
   120             cycle_direction = -cycle_direction;
   121         }
   122         SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
   123                                (Uint8) current_color);
   124     }
   125     if (cycle_alpha) {
   126         current_alpha += cycle_direction;
   127         if (current_alpha < 0) {
   128             current_alpha = 0;
   129             cycle_direction = -cycle_direction;
   130         }
   131         if (current_alpha > 255) {
   132             current_alpha = 255;
   133             cycle_direction = -cycle_direction;
   134         }
   135         SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
   136     }
   137 
   138     /* Draw a gray background */
   139     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   140     SDL_RenderClear();
   141 
   142     /* Test points */
   143     SDL_SetRenderDrawColor(0xFF, 0x00, 0x00, 0xFF);
   144     SDL_RenderDrawPoint(0, 0);
   145     SDL_RenderDrawPoint(window_w-1, 0);
   146     SDL_RenderDrawPoint(0, window_h-1);
   147     SDL_RenderDrawPoint(window_w-1, window_h-1);
   148 
   149     /* Test horizontal and vertical lines */
   150     SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
   151     SDL_RenderDrawLine(1, 0, window_w-2, 0);
   152     SDL_RenderDrawLine(1, window_h-1, window_w-2, window_h-1);
   153     SDL_RenderDrawLine(0, 1, 0, window_h-2);
   154     SDL_RenderDrawLine(window_w-1, 1, window_w-1, window_h-2);
   155 
   156     /* Test fill and copy */
   157     SDL_SetRenderDrawColor(0xFF, 0xFF, 0xFF, 0xFF);
   158     temp.x = 1;
   159     temp.y = 1;
   160     temp.w = sprite_w;
   161     temp.h = sprite_h;
   162     SDL_RenderFillRect(&temp);
   163     SDL_RenderCopy(sprite, NULL, &temp);
   164     temp.x = window_w-sprite_w-1;
   165     temp.y = 1;
   166     temp.w = sprite_w;
   167     temp.h = sprite_h;
   168     SDL_RenderFillRect(&temp);
   169     SDL_RenderCopy(sprite, NULL, &temp);
   170     temp.x = 1;
   171     temp.y = window_h-sprite_h-1;
   172     temp.w = sprite_w;
   173     temp.h = sprite_h;
   174     SDL_RenderFillRect(&temp);
   175     SDL_RenderCopy(sprite, NULL, &temp);
   176     temp.x = window_w-sprite_w-1;
   177     temp.y = window_h-sprite_h-1;
   178     temp.w = sprite_w;
   179     temp.h = sprite_h;
   180     SDL_RenderFillRect(&temp);
   181     SDL_RenderCopy(sprite, NULL, &temp);
   182 
   183     /* Test diagonal lines */
   184     SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
   185     SDL_RenderDrawLine(sprite_w, sprite_h,
   186                        window_w-sprite_w-2, window_h-sprite_h-2);
   187     SDL_RenderDrawLine(window_w-sprite_w-2, sprite_h,
   188                        sprite_w, window_h-sprite_h-2);
   189 
   190     /* Move the sprite, bounce at the wall, and draw */
   191     n = 0;
   192     for (i = 0; i < num_sprites; ++i) {
   193         position = &positions[i];
   194         velocity = &velocities[i];
   195         position->x += velocity->x;
   196         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   197             velocity->x = -velocity->x;
   198             position->x += velocity->x;
   199         }
   200         position->y += velocity->y;
   201         if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   202             velocity->y = -velocity->y;
   203             position->y += velocity->y;
   204         }
   205 
   206         /* Blit the sprite onto the screen */
   207         SDL_RenderCopy(sprite, NULL, position);
   208     }
   209 
   210     /* Update the screen! */
   211     SDL_RenderPresent();
   212 }
   213 
   214 int
   215 main(int argc, char *argv[])
   216 {
   217     int i, done;
   218     SDL_Event event;
   219     Uint32 then, now, frames;
   220 
   221     /* Initialize parameters */
   222     num_sprites = NUM_SPRITES;
   223 
   224     /* Initialize test framework */
   225     state = CommonCreateState(argv, SDL_INIT_VIDEO);
   226     if (!state) {
   227         return 1;
   228     }
   229     for (i = 1; i < argc;) {
   230         int consumed;
   231 
   232         consumed = CommonArg(state, i);
   233         if (consumed == 0) {
   234             consumed = -1;
   235             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   236                 if (argv[i + 1]) {
   237                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   238                         blendMode = SDL_BLENDMODE_NONE;
   239                         consumed = 2;
   240                     } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
   241                         blendMode = SDL_BLENDMODE_MASK;
   242                         consumed = 2;
   243                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   244                         blendMode = SDL_BLENDMODE_BLEND;
   245                         consumed = 2;
   246                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   247                         blendMode = SDL_BLENDMODE_ADD;
   248                         consumed = 2;
   249                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   250                         blendMode = SDL_BLENDMODE_MOD;
   251                         consumed = 2;
   252                     }
   253                 }
   254             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   255                 cycle_color = SDL_TRUE;
   256                 consumed = 1;
   257             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   258                 cycle_alpha = SDL_TRUE;
   259                 consumed = 1;
   260             } else if (SDL_isdigit(*argv[i])) {
   261                 num_sprites = SDL_atoi(argv[i]);
   262                 consumed = 1;
   263             }
   264         }
   265         if (consumed < 0) {
   266             fprintf(stderr,
   267                     "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
   268                     argv[0], CommonUsage(state));
   269             quit(1);
   270         }
   271         i += consumed;
   272     }
   273     if (!CommonInit(state)) {
   274         quit(2);
   275     }
   276 
   277     /* Create the windows, initialize the renderers, and load the textures */
   278     sprites =
   279         (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites));
   280     if (!sprites) {
   281         fprintf(stderr, "Out of memory!\n");
   282         quit(2);
   283     }
   284     for (i = 0; i < state->num_windows; ++i) {
   285         SDL_SelectRenderer(state->windows[i]);
   286         SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   287         SDL_RenderClear();
   288     }
   289     if (LoadSprite("icon.bmp") < 0) {
   290         quit(2);
   291     }
   292 
   293     /* Allocate memory for the sprite info */
   294     positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   295     velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   296     if (!positions || !velocities) {
   297         fprintf(stderr, "Out of memory!\n");
   298         quit(2);
   299     }
   300     srand((unsigned int)time(NULL));
   301     for (i = 0; i < num_sprites; ++i) {
   302         positions[i].x = rand() % (state->window_w - sprite_w);
   303         positions[i].y = rand() % (state->window_h - sprite_h);
   304         positions[i].w = sprite_w;
   305         positions[i].h = sprite_h;
   306         velocities[i].x = 0;
   307         velocities[i].y = 0;
   308         while (!velocities[i].x && !velocities[i].y) {
   309             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   310             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   311         }
   312     }
   313 
   314     /* Main render loop */
   315     frames = 0;
   316     then = SDL_GetTicks();
   317     done = 0;
   318     while (!done) {
   319         /* Check for events */
   320         ++frames;
   321         while (SDL_PollEvent(&event)) {
   322             CommonEvent(state, &event, &done);
   323             switch (event.type) {
   324             case SDL_WINDOWEVENT:
   325                 switch (event.window.event) {
   326                 case SDL_WINDOWEVENT_EXPOSED:
   327                     SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
   328                     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   329                     SDL_RenderClear();
   330                     break;
   331                 }
   332                 break;
   333             default:
   334                 break;
   335             }
   336         }
   337         for (i = 0; i < state->num_windows; ++i) {
   338             MoveSprites(state->windows[i], sprites[i]);
   339         }
   340     }
   341 
   342     /* Print out some timing information */
   343     now = SDL_GetTicks();
   344     if (now > then) {
   345         double fps = ((double) frames * 1000) / (now - then);
   346         printf("%2.2f frames per second\n", fps);
   347     }
   348     quit(0);
   349 	return 0;
   350 }
   351 
   352 /* vi: set ts=4 sw=4 expandtab: */