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