test/testwm2.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 07 Jul 2006 08:05:39 +0000
branchSDL-1.3
changeset 1725 98a3207ddde8
parent 1719 5b9f50c957ed
child 1733 0b1070f2f94d
permissions -rw-r--r--
Implemented Win32 video mode support
     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         if (strcmp(argv[argc - 1], "-width") == 0) {
    41             window_w = atoi(argv[argc]);
    42             --argc;
    43         } else if (strcmp(argv[argc - 1], "-height") == 0) {
    44             window_h = atoi(argv[argc]);
    45             --argc;
    46         } else {
    47             fprintf(stderr, "Usage: %s [-width] [-height]\n", argv[0]);
    48             quit(1);
    49         }
    50     }
    51 
    52     /* Set the desktop mode, we don't care what it is */
    53     if (SDL_SetDisplayMode(NULL) < 0) {
    54         fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError());
    55         quit(2);
    56     }
    57 
    58     /* Create the windows */
    59     windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
    60     if (!windows) {
    61         fprintf(stderr, "Out of memory!\n");
    62         quit(2);
    63     }
    64     for (i = 0; i < num_windows; ++i) {
    65         char title[32];
    66         int x, y;
    67 
    68         SDL_snprintf(title, sizeof(title), "testwm %d", i + 1);
    69         if (i == 0) {
    70             x = SDL_WINDOWPOS_CENTERED;
    71             y = SDL_WINDOWPOS_CENTERED;
    72         } else {
    73             x = SDL_WINDOWPOS_UNDEFINED;
    74             y = SDL_WINDOWPOS_UNDEFINED;
    75         }
    76         windows[i] =
    77             SDL_CreateWindow(title, x, y, window_w, window_h,
    78                              SDL_WINDOW_SHOWN);
    79         if (!windows[i]) {
    80             fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    81             quit(2);
    82         }
    83     }
    84 
    85     /* Loop, blitting sprites and waiting for a keystroke */
    86     done = 0;
    87     while (!done) {
    88         /* Check for events */
    89         while (SDL_PollEvent(&event)) {
    90             switch (event.type) {
    91             case SDL_KEYDOWN:
    92                 /* Any keypress quits the app... */
    93             case SDL_QUIT:
    94                 done = 1;
    95                 break;
    96             default:
    97                 break;
    98             }
    99         }
   100     }
   101 
   102     quit(0);
   103 }
   104 
   105 /* vi: set ts=4 sw=4 expandtab: */