test/testwm2.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 28 Jun 2006 08:12:07 +0000
branchSDL-1.3
changeset 1719 5b9f50c957ed
parent 1713 3e66ed1690e4
child 1725 98a3207ddde8
permissions -rw-r--r--
You can now create multiple windows on Win32
     1 /* Simple program:  Move N sprites around on the screen as fast as possible */
     2 
     3 #include "SDL.h"
     4 
     5 #define NUM_WINDOWS 2
     6 #define WINDOW_W    640
     7 #define WINDOW_H    480
     8 
     9 static int num_windows;
    10 static SDL_WindowID *windows;
    11 
    12 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    13 static void
    14 quit(int rc)
    15 {
    16     if (windows) {
    17         SDL_free(windows);
    18     }
    19     SDL_Quit();
    20     exit(rc);
    21 }
    22 
    23 int
    24 main(int argc, char *argv[])
    25 {
    26     int window_w, window_h;
    27     int i, done;
    28     SDL_Event event;
    29 
    30     /* Initialize SDL */
    31     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    32         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    33         return (1);
    34     }
    35 
    36     num_windows = NUM_WINDOWS;
    37     window_w = WINDOW_W;
    38     window_h = WINDOW_H;
    39     while (argc > 1) {
    40         --argc;
    41         if (strcmp(argv[argc - 1], "-width") == 0) {
    42             window_w = atoi(argv[argc]);
    43             --argc;
    44         } else if (strcmp(argv[argc - 1], "-height") == 0) {
    45             window_h = atoi(argv[argc]);
    46             --argc;
    47         } else {
    48             fprintf(stderr, "Usage: %s [-width] [-height]\n", argv[0]);
    49             quit(1);
    50         }
    51     }
    52 
    53     /* Set the desktop mode, we don't care what it is */
    54     if (SDL_SetDisplayMode(NULL) < 0) {
    55         fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError());
    56         quit(2);
    57     }
    58 
    59     /* Create the windows */
    60     windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
    61     if (!windows) {
    62         fprintf(stderr, "Out of memory!\n");
    63         quit(2);
    64     }
    65     for (i = 0; i < num_windows; ++i) {
    66         char title[32];
    67         int x, y;
    68 
    69         SDL_snprintf(title, sizeof(title), "testwm %d", i + 1);
    70         if (i == 0) {
    71             x = SDL_WINDOWPOS_CENTERED;
    72             y = SDL_WINDOWPOS_CENTERED;
    73         } else {
    74             x = SDL_WINDOWPOS_UNDEFINED;
    75             y = SDL_WINDOWPOS_UNDEFINED;
    76         }
    77         windows[i] =
    78             SDL_CreateWindow(title, x, y, window_w, window_h,
    79                              SDL_WINDOW_SHOWN);
    80         if (!windows[i]) {
    81             fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    82             quit(2);
    83         }
    84     }
    85 
    86     /* Loop, blitting sprites and waiting for a keystroke */
    87     done = 0;
    88     while (!done) {
    89         /* Check for events */
    90         while (SDL_PollEvent(&event)) {
    91             switch (event.type) {
    92             case SDL_KEYDOWN:
    93                 /* Any keypress quits the app... */
    94             case SDL_QUIT:
    95                 done = 1;
    96                 break;
    97             default:
    98                 break;
    99             }
   100         }
   101     }
   102 
   103     quit(0);
   104 }
   105 
   106 /* vi: set ts=4 sw=4 expandtab: */