test/testsprite2.c
changeset 1914 051df511279c
parent 1907 06c27a737b7a
child 1915 a228436a2404
     1.1 --- a/test/testsprite2.c	Mon Jul 17 06:47:33 2006 +0000
     1.2 +++ b/test/testsprite2.c	Tue Jul 18 07:49:51 2006 +0000
     1.3 @@ -4,18 +4,14 @@
     1.4  #include <stdio.h>
     1.5  #include <time.h>
     1.6  
     1.7 -#include "SDL.h"
     1.8 +#include "common.h"
     1.9  
    1.10 -#define NUM_WINDOWS 4
    1.11 -#define WINDOW_W    640
    1.12 -#define WINDOW_H    480
    1.13  #define NUM_SPRITES	100
    1.14  #define MAX_SPEED 	1
    1.15  #define BACKGROUND  0x00FFFFFF
    1.16  
    1.17 -static int num_windows;
    1.18 +static CommonState *state;
    1.19  static int num_sprites;
    1.20 -static SDL_WindowID *windows;
    1.21  static SDL_TextureID *sprites;
    1.22  static SDL_Rect *positions;
    1.23  static SDL_Rect *velocities;
    1.24 @@ -25,9 +21,6 @@
    1.25  static void
    1.26  quit(int rc)
    1.27  {
    1.28 -    if (windows) {
    1.29 -        SDL_free(windows);
    1.30 -    }
    1.31      if (sprites) {
    1.32          SDL_free(sprites);
    1.33      }
    1.34 @@ -37,7 +30,7 @@
    1.35      if (velocities) {
    1.36          SDL_free(velocities);
    1.37      }
    1.38 -    SDL_Quit();
    1.39 +    CommonQuit(state);
    1.40      exit(rc);
    1.41  }
    1.42  
    1.43 @@ -62,8 +55,8 @@
    1.44      }
    1.45  
    1.46      /* Create textures from the image */
    1.47 -    for (i = 0; i < num_windows; ++i) {
    1.48 -        SDL_SelectRenderer(windows[i]);
    1.49 +    for (i = 0; i < state->num_windows; ++i) {
    1.50 +        SDL_SelectRenderer(state->windows[i]);
    1.51          sprites[i] =
    1.52              SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp);
    1.53          if (!sprites[i]) {
    1.54 @@ -125,78 +118,45 @@
    1.55  int
    1.56  main(int argc, char *argv[])
    1.57  {
    1.58 -    int window_w, window_h;
    1.59 -    Uint32 window_flags = SDL_WINDOW_SHOWN;
    1.60 -    Uint32 render_flags = 0;
    1.61 -    SDL_DisplayMode *mode, fullscreen_mode;
    1.62      int i, done;
    1.63      SDL_Event event;
    1.64      Uint32 then, now, frames;
    1.65  
    1.66 -    /* Initialize SDL */
    1.67 -    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    1.68 -        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    1.69 -        return (1);
    1.70 +    /* Initialize parameters */
    1.71 +    num_sprites = NUM_SPRITES;
    1.72 +
    1.73 +    /* Initialize test framework */
    1.74 +    state = CommonCreateState(argv, SDL_INIT_VIDEO);
    1.75 +    if (!state) {
    1.76 +        return 1;
    1.77      }
    1.78 +    for (i = 1; i < argc;) {
    1.79 +        int consumed;
    1.80  
    1.81 -    num_windows = NUM_WINDOWS;
    1.82 -    num_sprites = NUM_SPRITES;
    1.83 -    window_w = WINDOW_W;
    1.84 -    window_h = WINDOW_H;
    1.85 -    for (i = 1; i < argc; ++i) {
    1.86 -        if (strcmp(argv[i], "-width") == 0 && (i + 1 < argc)) {
    1.87 -            window_w = atoi(argv[++i]);
    1.88 -        } else if (strcmp(argv[i], "-height") == 0 && (i + 1 < argc)) {
    1.89 -            window_h = atoi(argv[++i]);
    1.90 -        } else if (strcmp(argv[i], "-windows") == 0 && (i + 1 < argc)) {
    1.91 -            num_windows = atoi(argv[++i]);
    1.92 -            window_flags &= ~SDL_WINDOW_FULLSCREEN;
    1.93 -        } else if (strcmp(argv[i], "-fullscreen") == 0) {
    1.94 -            num_windows = 1;
    1.95 -            window_flags |= SDL_WINDOW_FULLSCREEN;
    1.96 -        } else if (strcmp(argv[i], "-sync") == 0) {
    1.97 -            render_flags |= SDL_Renderer_PresentVSync;
    1.98 -        } else if (isdigit(argv[i][0])) {
    1.99 -            num_sprites = atoi(argv[i]);
   1.100 -        } else {
   1.101 -            fprintf(stderr,
   1.102 -                    "Usage: %s [-width N] [-height N] [-windows N] [-fullscreen] [-sync] [numsprites]\n",
   1.103 -                    argv[0]);
   1.104 +        consumed = CommonArg(state, i);
   1.105 +        if (consumed < 0) {
   1.106 +            fprintf(stderr, "Usage: %s %s", argv[0], CommonUsage(state));
   1.107              quit(1);
   1.108          }
   1.109 +        if (consumed == 0) {
   1.110 +            num_sprites = SDL_atoi(argv[i]);
   1.111 +            consumed = 1;
   1.112 +        }
   1.113 +        i += consumed;
   1.114      }
   1.115 -
   1.116 -    if (window_flags & SDL_WINDOW_FULLSCREEN) {
   1.117 -        SDL_zero(fullscreen_mode);
   1.118 -        fullscreen_mode.w = window_w;
   1.119 -        fullscreen_mode.h = window_h;
   1.120 -        SDL_SetFullscreenDisplayMode(&fullscreen_mode);
   1.121 +    if (!CommonInit(state)) {
   1.122 +        quit(2);
   1.123      }
   1.124  
   1.125      /* Create the windows, initialize the renderers, and load the textures */
   1.126 -    windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
   1.127 -    sprites = (SDL_TextureID *) SDL_malloc(num_windows * sizeof(*sprites));
   1.128 -    if (!windows || !sprites) {
   1.129 +    sprites =
   1.130 +        (SDL_TextureID *) SDL_malloc(state->num_windows * sizeof(*sprites));
   1.131 +    if (!sprites) {
   1.132          fprintf(stderr, "Out of memory!\n");
   1.133          quit(2);
   1.134      }
   1.135 -    for (i = 0; i < num_windows; ++i) {
   1.136 -        char title[32];
   1.137 -
   1.138 -        SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1);
   1.139 -        windows[i] =
   1.140 -            SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
   1.141 -                             SDL_WINDOWPOS_UNDEFINED, window_w, window_h,
   1.142 -                             window_flags);
   1.143 -        if (!windows[i]) {
   1.144 -            fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
   1.145 -            quit(2);
   1.146 -        }
   1.147 -
   1.148 -        if (SDL_CreateRenderer(windows[i], -1, render_flags) < 0) {
   1.149 -            fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
   1.150 -            quit(2);
   1.151 -        }
   1.152 +    for (i = 0; i < state->num_windows; ++i) {
   1.153 +        SDL_SelectRenderer(state->windows[i]);
   1.154          SDL_RenderFill(NULL, BACKGROUND);
   1.155      }
   1.156      if (LoadSprite("icon.bmp") < 0) {
   1.157 @@ -212,8 +172,8 @@
   1.158      }
   1.159      srand(time(NULL));
   1.160      for (i = 0; i < num_sprites; ++i) {
   1.161 -        positions[i].x = rand() % (window_w - sprite_w);
   1.162 -        positions[i].y = rand() % (window_h - sprite_h);
   1.163 +        positions[i].x = rand() % (state->window_w - sprite_w);
   1.164 +        positions[i].y = rand() % (state->window_h - sprite_h);
   1.165          positions[i].w = sprite_w;
   1.166          positions[i].h = sprite_h;
   1.167          velocities[i].x = 0;
   1.168 @@ -232,6 +192,7 @@
   1.169          /* Check for events */
   1.170          ++frames;
   1.171          while (SDL_PollEvent(&event)) {
   1.172 +            CommonEvent(state, &event, &done);
   1.173              switch (event.type) {
   1.174              case SDL_WINDOWEVENT:
   1.175                  switch (event.window.event) {
   1.176 @@ -239,22 +200,14 @@
   1.177                      SDL_SelectRenderer(event.window.windowID);
   1.178                      SDL_RenderFill(NULL, BACKGROUND);
   1.179                      break;
   1.180 -                case SDL_WINDOWEVENT_CLOSE:
   1.181 -                    done = 1;
   1.182 -                    break;
   1.183                  }
   1.184                  break;
   1.185 -            case SDL_KEYDOWN:
   1.186 -                /* Any keypress quits the app... */
   1.187 -            case SDL_QUIT:
   1.188 -                done = 1;
   1.189 -                break;
   1.190              default:
   1.191                  break;
   1.192              }
   1.193          }
   1.194 -        for (i = 0; i < num_windows; ++i) {
   1.195 -            MoveSprites(windows[i], sprites[i]);
   1.196 +        for (i = 0; i < state->num_windows; ++i) {
   1.197 +            MoveSprites(state->windows[i], sprites[i]);
   1.198          }
   1.199      }
   1.200