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