test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 01 Feb 2011 19:19:43 -0800
changeset 5147 ad50b3db78bd
parent 5140 e743b9c3f6d6
child 5158 b3ccd1947786
permissions -rw-r--r--
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
     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, 0, temp);
    81         if (!sprites[i]) {
    82             SDL_SetColorKey(temp, 0, 0);
    83             sprites[i] = SDL_CreateTextureFromSurface(renderer, 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_Renderer * renderer, 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     /* Query the sizes */
   107     SDL_GetWindowSize(window, &window_w, &window_h);
   108 
   109     /* Cycle the color and alpha, if desired */
   110     if (cycle_color) {
   111         current_color += cycle_direction;
   112         if (current_color < 0) {
   113             current_color = 0;
   114             cycle_direction = -cycle_direction;
   115         }
   116         if (current_color > 255) {
   117             current_color = 255;
   118             cycle_direction = -cycle_direction;
   119         }
   120         SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
   121                                (Uint8) current_color);
   122     }
   123     if (cycle_alpha) {
   124         current_alpha += cycle_direction;
   125         if (current_alpha < 0) {
   126             current_alpha = 0;
   127             cycle_direction = -cycle_direction;
   128         }
   129         if (current_alpha > 255) {
   130             current_alpha = 255;
   131             cycle_direction = -cycle_direction;
   132         }
   133         SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
   134     }
   135 
   136     /* Draw a gray background */
   137     SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   138     SDL_RenderClear(renderer);
   139 
   140     /* Test points */
   141     SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
   142     SDL_RenderDrawPoint(renderer, 0, 0);
   143     SDL_RenderDrawPoint(renderer, window_w-1, 0);
   144     SDL_RenderDrawPoint(renderer, 0, window_h-1);
   145     SDL_RenderDrawPoint(renderer, window_w-1, window_h-1);
   146 
   147     /* Test horizontal and vertical lines */
   148     SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
   149     SDL_RenderDrawLine(renderer, 1, 0, window_w-2, 0);
   150     SDL_RenderDrawLine(renderer, 1, window_h-1, window_w-2, window_h-1);
   151     SDL_RenderDrawLine(renderer, 0, 1, 0, window_h-2);
   152     SDL_RenderDrawLine(renderer, window_w-1, 1, window_w-1, window_h-2);
   153 
   154     /* Test fill and copy */
   155     SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
   156     temp.x = 1;
   157     temp.y = 1;
   158     temp.w = sprite_w;
   159     temp.h = sprite_h;
   160     SDL_RenderFillRect(renderer, &temp);
   161     SDL_RenderCopy(renderer, sprite, NULL, &temp);
   162     temp.x = window_w-sprite_w-1;
   163     temp.y = 1;
   164     temp.w = sprite_w;
   165     temp.h = sprite_h;
   166     SDL_RenderFillRect(renderer, &temp);
   167     SDL_RenderCopy(renderer, sprite, NULL, &temp);
   168     temp.x = 1;
   169     temp.y = window_h-sprite_h-1;
   170     temp.w = sprite_w;
   171     temp.h = sprite_h;
   172     SDL_RenderFillRect(renderer, &temp);
   173     SDL_RenderCopy(renderer, sprite, NULL, &temp);
   174     temp.x = window_w-sprite_w-1;
   175     temp.y = window_h-sprite_h-1;
   176     temp.w = sprite_w;
   177     temp.h = sprite_h;
   178     SDL_RenderFillRect(renderer, &temp);
   179     SDL_RenderCopy(renderer, sprite, NULL, &temp);
   180 
   181     /* Test diagonal lines */
   182     SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
   183     SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
   184                        window_w-sprite_w-2, window_h-sprite_h-2);
   185     SDL_RenderDrawLine(renderer, window_w-sprite_w-2, sprite_h,
   186                        sprite_w, window_h-sprite_h-2);
   187 
   188     /* Move the sprite, bounce at the wall, and draw */
   189     n = 0;
   190     for (i = 0; i < num_sprites; ++i) {
   191         position = &positions[i];
   192         velocity = &velocities[i];
   193         position->x += velocity->x;
   194         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   195             velocity->x = -velocity->x;
   196             position->x += velocity->x;
   197         }
   198         position->y += velocity->y;
   199         if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   200             velocity->y = -velocity->y;
   201             position->y += velocity->y;
   202         }
   203 
   204         /* Blit the sprite onto the screen */
   205         SDL_RenderCopy(renderer, sprite, NULL, position);
   206     }
   207 
   208     /* Update the screen! */
   209     SDL_RenderPresent(renderer);
   210 }
   211 
   212 int
   213 main(int argc, char *argv[])
   214 {
   215     int i, done;
   216     SDL_Event event;
   217     Uint32 then, now, frames;
   218 
   219     /* Initialize parameters */
   220     num_sprites = NUM_SPRITES;
   221 
   222     /* Initialize test framework */
   223     state = CommonCreateState(argv, SDL_INIT_VIDEO);
   224     if (!state) {
   225         return 1;
   226     }
   227     for (i = 1; i < argc;) {
   228         int consumed;
   229 
   230         consumed = CommonArg(state, i);
   231         if (consumed == 0) {
   232             consumed = -1;
   233             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   234                 if (argv[i + 1]) {
   235                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   236                         blendMode = SDL_BLENDMODE_NONE;
   237                         consumed = 2;
   238                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   239                         blendMode = SDL_BLENDMODE_BLEND;
   240                         consumed = 2;
   241                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   242                         blendMode = SDL_BLENDMODE_ADD;
   243                         consumed = 2;
   244                     }
   245                 }
   246             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   247                 cycle_color = SDL_TRUE;
   248                 consumed = 1;
   249             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   250                 cycle_alpha = SDL_TRUE;
   251                 consumed = 1;
   252             } else if (SDL_isdigit(*argv[i])) {
   253                 num_sprites = SDL_atoi(argv[i]);
   254                 consumed = 1;
   255             }
   256         }
   257         if (consumed < 0) {
   258             fprintf(stderr,
   259                     "Usage: %s %s [--blend none|blend|add] [--cyclecolor] [--cyclealpha]\n",
   260                     argv[0], CommonUsage(state));
   261             quit(1);
   262         }
   263         i += consumed;
   264     }
   265     if (!CommonInit(state)) {
   266         quit(2);
   267     }
   268 
   269     /* Create the windows, initialize the renderers, and load the textures */
   270     sprites =
   271         (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites));
   272     if (!sprites) {
   273         fprintf(stderr, "Out of memory!\n");
   274         quit(2);
   275     }
   276     for (i = 0; i < state->num_windows; ++i) {
   277         SDL_Renderer *renderer = state->renderers[i];
   278         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   279         SDL_RenderClear(renderer);
   280     }
   281     if (LoadSprite("icon.bmp") < 0) {
   282         quit(2);
   283     }
   284 
   285     /* Allocate memory for the sprite info */
   286     positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   287     velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   288     if (!positions || !velocities) {
   289         fprintf(stderr, "Out of memory!\n");
   290         quit(2);
   291     }
   292     srand((unsigned int)time(NULL));
   293     for (i = 0; i < num_sprites; ++i) {
   294         positions[i].x = rand() % (state->window_w - sprite_w);
   295         positions[i].y = rand() % (state->window_h - sprite_h);
   296         positions[i].w = sprite_w;
   297         positions[i].h = sprite_h;
   298         velocities[i].x = 0;
   299         velocities[i].y = 0;
   300         while (!velocities[i].x && !velocities[i].y) {
   301             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   302             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   303         }
   304     }
   305 
   306     /* Main render loop */
   307     frames = 0;
   308     then = SDL_GetTicks();
   309     done = 0;
   310     while (!done) {
   311         /* Check for events */
   312         ++frames;
   313         while (SDL_PollEvent(&event)) {
   314             CommonEvent(state, &event, &done);
   315         }
   316         for (i = 0; i < state->num_windows; ++i) {
   317             MoveSprites(state->windows[i], state->renderers[i], sprites[i]);
   318         }
   319     }
   320 
   321     /* Print out some timing information */
   322     now = SDL_GetTicks();
   323     if (now > then) {
   324         double fps = ((double) frames * 1000) / (now - then);
   325         printf("%2.2f frames per second\n", fps);
   326     }
   327     quit(0);
   328     return 0;
   329 }
   330 
   331 /* vi: set ts=4 sw=4 expandtab: */