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