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