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