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