test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Jul 2006 06:40:53 +0000
changeset 1903 f132024010be
parent 1895 c121d94672cb
child 1904 1a713f9d1f71
permissions -rw-r--r--
More of the Direct3D renderer is implemented, I'm not sure why it's not showing texture copies yet...
     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 1
     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     while (argc > 1) {
   145         if (strcmp(argv[argc - 1], "-width") == 0) {
   146             window_w = atoi(argv[argc]);
   147             --argc;
   148         } else if (strcmp(argv[argc - 1], "-height") == 0) {
   149             window_h = atoi(argv[argc]);
   150             --argc;
   151         } else if (strcmp(argv[argc - 1], "-fullscreen") == 0) {
   152             num_windows = 1;
   153             window_flags |= SDL_WINDOW_FULLSCREEN;
   154             --argc;
   155         } else if (isdigit(argv[argc][0])) {
   156             num_sprites = atoi(argv[argc]);
   157         } else {
   158             fprintf(stderr,
   159                     "Usage: %s [-width] [-height] [numsprites]\n", argv[0]);
   160             quit(1);
   161         }
   162     }
   163 
   164     if (window_flags & SDL_WINDOW_FULLSCREEN) {
   165         SDL_zero(fullscreen_mode);
   166         fullscreen_mode.w = window_w;
   167         fullscreen_mode.h = window_h;
   168         SDL_SetFullscreenDisplayMode(&fullscreen_mode);
   169     }
   170 
   171     /* Create the windows, initialize the renderers, and load the textures */
   172     windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
   173     sprites = (SDL_TextureID *) SDL_malloc(num_windows * sizeof(*sprites));
   174     if (!windows || !sprites) {
   175         fprintf(stderr, "Out of memory!\n");
   176         quit(2);
   177     }
   178     for (i = 0; i < num_windows; ++i) {
   179         char title[32];
   180 
   181         SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1);
   182         windows[i] =
   183             SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
   184                              SDL_WINDOWPOS_UNDEFINED, window_w, window_h,
   185                              window_flags);
   186         if (!windows[i]) {
   187             fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
   188             quit(2);
   189         }
   190 
   191         if (SDL_CreateRenderer(windows[i], -1, 0) < 0) {
   192             fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
   193             quit(2);
   194         }
   195         SDL_RenderFill(NULL, BACKGROUND);
   196     }
   197     if (LoadSprite("icon.bmp") < 0) {
   198         quit(2);
   199     }
   200 
   201     /* Allocate memory for the sprite info */
   202     positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   203     velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
   204     if (!positions || !velocities) {
   205         fprintf(stderr, "Out of memory!\n");
   206         quit(2);
   207     }
   208     srand(time(NULL));
   209     for (i = 0; i < num_sprites; ++i) {
   210         positions[i].x = rand() % (window_w - sprite_w);
   211         positions[i].y = rand() % (window_h - sprite_h);
   212         positions[i].w = sprite_w;
   213         positions[i].h = sprite_h;
   214         velocities[i].x = 0;
   215         velocities[i].y = 0;
   216         while (!velocities[i].x && !velocities[i].y) {
   217             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   218             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   219         }
   220     }
   221 
   222     /* Loop, blitting sprites and waiting for a keystroke */
   223     frames = 0;
   224     then = SDL_GetTicks();
   225     done = 0;
   226     while (!done) {
   227         /* Check for events */
   228         ++frames;
   229         while (SDL_PollEvent(&event)) {
   230             switch (event.type) {
   231             case SDL_WINDOWEVENT:
   232                 switch (event.window.event) {
   233                 case SDL_WINDOWEVENT_EXPOSED:
   234                     SDL_SelectRenderer(event.window.windowID);
   235                     SDL_RenderFill(NULL, BACKGROUND);
   236                     break;
   237                 case SDL_WINDOWEVENT_CLOSE:
   238                     done = 1;
   239                     break;
   240                 }
   241                 break;
   242             case SDL_KEYDOWN:
   243                 ///* Any keypress quits the app... */
   244                 break;
   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: */