test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Jul 2006 07:41:16 +0000
changeset 1904 1a713f9d1f71
parent 1903 f132024010be
child 1907 06c27a737b7a
permissions -rw-r--r--
Yay! D3D renderer works!
     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     SDL_RenderFill(NULL, BACKGROUND);
    95     /*
    96        for (i = 0; i < num_sprites; ++i) {
    97        position = &positions[i];
    98        SDL_RenderFill(position, BACKGROUND);
    99        }
   100      */
   101     for (i = 0; i < num_sprites; ++i) {
   102         position = &positions[i];
   103         velocity = &velocities[i];
   104         position->x += velocity->x;
   105         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   106             velocity->x = -velocity->x;
   107             position->x += velocity->x;
   108         }
   109         position->y += velocity->y;
   110         if ((position->y < 0) || (position->y >= (window_h - sprite_w))) {
   111             velocity->y = -velocity->y;
   112             position->y += velocity->y;
   113         }
   114 
   115         /* Blit the sprite onto the screen */
   116         SDL_RenderCopy(sprite, NULL, position, SDL_TextureBlendMode_Mask,
   117                        SDL_TextureScaleMode_None);
   118     }
   119 
   120     /* Update the screen! */
   121     SDL_RenderPresent();
   122 }
   123 
   124 int
   125 main(int argc, char *argv[])
   126 {
   127     int window_w, window_h;
   128     Uint32 window_flags = SDL_WINDOW_SHOWN;
   129     SDL_DisplayMode *mode, fullscreen_mode;
   130     int i, done;
   131     SDL_Event event;
   132     Uint32 then, now, frames;
   133 
   134     /* Initialize SDL */
   135     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   136         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   137         return (1);
   138     }
   139 
   140     num_windows = NUM_WINDOWS;
   141     num_sprites = NUM_SPRITES;
   142     window_w = WINDOW_W;
   143     window_h = WINDOW_H;
   144     for (i = 1; i < argc; ++i) {
   145         if (strcmp(argv[i], "-width") == 0 && (i + 1 < argc)) {
   146             window_w = atoi(argv[++i]);
   147         } else if (strcmp(argv[i], "-height") == 0 && (i + 1 < argc)) {
   148             window_h = atoi(argv[++i]);
   149         } else if (strcmp(argv[i], "-windows") == 0 && (i + 1 < argc)) {
   150             num_windows = atoi(argv[++i]);
   151             window_flags &= ~SDL_WINDOW_FULLSCREEN;
   152         } else if (strcmp(argv[i], "-fullscreen") == 0) {
   153             num_windows = 1;
   154             window_flags |= SDL_WINDOW_FULLSCREEN;
   155         } else if (isdigit(argv[i][0])) {
   156             num_sprites = atoi(argv[i]);
   157         } else {
   158             fprintf(stderr,
   159                     "Usage: %s [-width N] [-height N] [-windows N] [-fullscreen] [numsprites]\n",
   160                     argv[0]);
   161             quit(1);
   162         }
   163     }
   164 
   165     if (window_flags & SDL_WINDOW_FULLSCREEN) {
   166         SDL_zero(fullscreen_mode);
   167         fullscreen_mode.w = window_w;
   168         fullscreen_mode.h = window_h;
   169         SDL_SetFullscreenDisplayMode(&fullscreen_mode);
   170     }
   171 
   172     /* Create the windows, initialize the renderers, and load the textures */
   173     windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
   174     sprites = (SDL_TextureID *) SDL_malloc(num_windows * sizeof(*sprites));
   175     if (!windows || !sprites) {
   176         fprintf(stderr, "Out of memory!\n");
   177         quit(2);
   178     }
   179     for (i = 0; i < num_windows; ++i) {
   180         char title[32];
   181 
   182         SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1);
   183         windows[i] =
   184             SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
   185                              SDL_WINDOWPOS_UNDEFINED, window_w, window_h,
   186                              window_flags);
   187         if (!windows[i]) {
   188             fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
   189             quit(2);
   190         }
   191 
   192         if (SDL_CreateRenderer(windows[i], -1, 0) < 0) {
   193             fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
   194             quit(2);
   195         }
   196         SDL_RenderFill(NULL, BACKGROUND);
   197     }
   198     if (LoadSprite("icon.bmp") < 0) {
   199         quit(2);
   200     }
   201 
   202     /* Allocate memory for the sprite info */
   203     positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   204     velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   205     if (!positions || !velocities) {
   206         fprintf(stderr, "Out of memory!\n");
   207         quit(2);
   208     }
   209     srand(time(NULL));
   210     for (i = 0; i < num_sprites; ++i) {
   211         positions[i].x = rand() % (window_w - sprite_w);
   212         positions[i].y = rand() % (window_h - sprite_h);
   213         positions[i].w = sprite_w;
   214         positions[i].h = sprite_h;
   215         velocities[i].x = 0;
   216         velocities[i].y = 0;
   217         while (!velocities[i].x && !velocities[i].y) {
   218             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   219             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   220         }
   221     }
   222 
   223     /* Loop, blitting sprites and waiting for a keystroke */
   224     frames = 0;
   225     then = SDL_GetTicks();
   226     done = 0;
   227     while (!done) {
   228         /* Check for events */
   229         ++frames;
   230         while (SDL_PollEvent(&event)) {
   231             switch (event.type) {
   232             case SDL_WINDOWEVENT:
   233                 switch (event.window.event) {
   234                 case SDL_WINDOWEVENT_EXPOSED:
   235                     SDL_SelectRenderer(event.window.windowID);
   236                     SDL_RenderFill(NULL, BACKGROUND);
   237                     break;
   238                 case SDL_WINDOWEVENT_CLOSE:
   239                     done = 1;
   240                     break;
   241                 }
   242                 break;
   243             case SDL_KEYDOWN:
   244                 /* Any keypress quits the app... */
   245             case SDL_QUIT:
   246                 done = 1;
   247                 break;
   248             default:
   249                 break;
   250             }
   251         }
   252         for (i = 0; i < num_windows; ++i) {
   253             MoveSprites(windows[i], sprites[i]);
   254         }
   255     }
   256 
   257     /* Print out some timing information */
   258     now = SDL_GetTicks();
   259     if (now > then) {
   260         printf("%2.2f frames per second\n",
   261                ((double) frames * 1000) / (now - then));
   262     }
   263     quit(0);
   264 }
   265 
   266 /* vi: set ts=4 sw=4 expandtab: */