test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 24 Jun 2006 16:51:01 +0000
branchSDL-1.3
changeset 1706 1577404809f0
child 1707 57ce47f033a5
permissions -rw-r--r--
Added a multi-window version of testsprite using the new API.
     1 /* Simple program:  Move N sprites around on the screen as fast as possible */
     2 
     3 #include <stdio.h>
     4 #include <stdlib.h>
     5 #include <string.h>
     6 #include <ctype.h>
     7 #include <math.h>
     8 #include <time.h>
     9 
    10 #include "SDL.h"
    11 
    12 #define NUM_WINDOWS 2
    13 #define WINDOW_W    640
    14 #define WINDOW_H    480
    15 #define NUM_SPRITES	100
    16 #define MAX_SPEED 	1
    17 
    18 static int num_windows;
    19 static int num_sprites;
    20 static SDL_WindowID *windows;
    21 static SDL_TextureID *sprites;
    22 static SDL_Rect *positions;
    23 static SDL_Rect *velocities;
    24 static int sprite_w, sprite_h;
    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 (windows) {
    31         SDL_free(windows);
    32     }
    33     if (sprites) {
    34         SDL_free(sprites);
    35     }
    36     if (positions) {
    37         SDL_free(positions);
    38     }
    39     if (velocities) {
    40         SDL_free(velocities);
    41     }
    42     SDL_Quit();
    43     exit(rc);
    44 }
    45 
    46 int
    47 LoadSprite(char *file)
    48 {
    49     int i;
    50     SDL_Surface *temp;
    51 
    52     /* Load the sprite image */
    53     temp = SDL_LoadBMP(file);
    54     if (temp == NULL) {
    55         fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    56         return (-1);
    57     }
    58     sprite_w = temp->w;
    59     sprite_h = temp->h;
    60 
    61     /* Set transparent pixel as the pixel at (0,0) */
    62     if (temp->format->palette) {
    63         SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
    64     }
    65 
    66     /* Create textures from the image */
    67     for (i = 0; i < num_windows; ++i) {
    68         SDL_SelectRenderer(windows[i]);
    69         sprites[i] =
    70             SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp);
    71         if (!sprites[i]) {
    72             fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    73             SDL_FreeSurface(temp);
    74             return (-1);
    75         }
    76     }
    77     SDL_FreeSurface(temp);
    78 
    79     /* We're ready to roll. :) */
    80     return (0);
    81 }
    82 
    83 void
    84 MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
    85 {
    86     int i, n;
    87     int window_w, window_h;
    88     SDL_Rect area, *position, *velocity;
    89 
    90     SDL_SelectRenderer(window);
    91 
    92     SDL_RenderFill(NULL, 0);
    93 
    94     /* Query the sizes */
    95     SDL_GetWindowSize(window, &window_w, &window_h);
    96 
    97     /* Move the sprite, bounce at the wall, and draw */
    98     n = 0;
    99     for (i = 0; i < num_sprites; ++i) {
   100         position = &positions[i];
   101         velocity = &velocities[i];
   102         position->x += velocity->x;
   103         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   104             velocity->x = -velocity->x;
   105             position->x += velocity->x;
   106         }
   107         position->y += velocity->y;
   108         if ((position->y < 0) || (position->y >= (window_h - sprite_w))) {
   109             velocity->y = -velocity->y;
   110             position->y += velocity->y;
   111         }
   112 
   113         /* Blit the sprite onto the screen */
   114         SDL_RenderCopy(sprite, NULL, position, SDL_TextureBlendMode_Mask,
   115                        SDL_TextureScaleMode_None);
   116     }
   117 
   118     /* Update the screen! */
   119     SDL_RenderPresent();
   120 }
   121 
   122 int
   123 main(int argc, char *argv[])
   124 {
   125     const SDL_DisplayMode *current_mode;
   126     int window_w, window_h;
   127     int i, done;
   128     SDL_Event event;
   129     Uint32 then, now, frames;
   130 
   131     /* Initialize SDL */
   132     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   133         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   134         return (1);
   135     }
   136 
   137     num_windows = NUM_WINDOWS;
   138     num_sprites = NUM_SPRITES;
   139     window_w = WINDOW_W;
   140     window_h = WINDOW_H;
   141     while (argc > 1) {
   142         --argc;
   143         if (strcmp(argv[argc - 1], "-width") == 0) {
   144             window_w = atoi(argv[argc]);
   145             --argc;
   146         } else if (strcmp(argv[argc - 1], "-height") == 0) {
   147             window_h = atoi(argv[argc]);
   148             --argc;
   149         } else if (isdigit(argv[argc][0])) {
   150             num_sprites = atoi(argv[argc]);
   151         } else {
   152             fprintf(stderr,
   153                     "Usage: %s [-width] [-height] [numsprites]\n", argv[0]);
   154             quit(1);
   155         }
   156     }
   157 
   158     /* Initialize the video mode, if necessary */
   159     current_mode = SDL_GetCurrentDisplayMode();
   160     if (!current_mode->w || !current_mode->h) {
   161         SDL_DisplayMode mode;
   162 
   163         /* Let the driver pick something it likes, we don't care */
   164         mode.format = 0;
   165         mode.w = 0;
   166         mode.h = 0;
   167         mode.refresh_rate = 0;
   168 
   169         if (SDL_SetDisplayMode(&mode) < 0) {
   170             fprintf(stderr, "Couldn't set display mode: %s\n",
   171                     SDL_GetError());
   172             quit(2);
   173         }
   174     }
   175 
   176     /* Create the windows, initialize the renderers, and load the textures */
   177     windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
   178     sprites = (SDL_TextureID *) SDL_malloc(num_windows * sizeof(*sprites));
   179     if (!windows || !sprites) {
   180         fprintf(stderr, "Out of memory!\n");
   181         quit(2);
   182     }
   183     for (i = 0; i < num_windows; ++i) {
   184         char title[32];
   185 
   186         SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1);
   187         windows[i] =
   188             SDL_CreateWindow(title, -1, -1, window_w, window_h,
   189                              SDL_WINDOW_SHOWN);
   190         if (!windows[i]) {
   191             fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
   192             quit(2);
   193         }
   194 
   195         if (SDL_CreateRenderer(windows[i], -1, 0) < 0) {
   196             fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
   197             quit(2);
   198         }
   199     }
   200     if (LoadSprite("icon.bmp") < 0) {
   201         quit(2);
   202     }
   203 
   204     /* Allocate memory for the sprite info */
   205     positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   206     velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   207     if (!positions || !velocities) {
   208         fprintf(stderr, "Out of memory!\n");
   209         quit(2);
   210     }
   211     srand(time(NULL));
   212     for (i = 0; i < num_sprites; ++i) {
   213         positions[i].x = rand() % (window_w - sprite_w);
   214         positions[i].y = rand() % (window_h - sprite_h);
   215         positions[i].w = sprite_w;
   216         positions[i].h = sprite_h;
   217         velocities[i].x = 0;
   218         velocities[i].y = 0;
   219         while (!velocities[i].x && !velocities[i].y) {
   220             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   221             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   222         }
   223     }
   224 
   225     /* Loop, blitting sprites and waiting for a keystroke */
   226     frames = 0;
   227     then = SDL_GetTicks();
   228     done = 0;
   229     while (!done) {
   230         /* Check for events */
   231         ++frames;
   232         while (SDL_PollEvent(&event)) {
   233             switch (event.type) {
   234             case SDL_KEYDOWN:
   235                 /* Any keypress quits the app... */
   236             case SDL_QUIT:
   237                 done = 1;
   238                 break;
   239             default:
   240                 break;
   241             }
   242         }
   243         for (i = 0; i < num_windows; ++i) {
   244             MoveSprites(windows[i], sprites[i]);
   245         }
   246     }
   247 
   248     /* Print out some timing information */
   249     now = SDL_GetTicks();
   250     if (now > then) {
   251         printf("%2.2f frames per second\n",
   252                ((double) frames * 1000) / (now - then));
   253     }
   254     quit(0);
   255 }
   256 
   257 /* vi: set ts=4 sw=4 expandtab: */