test/common.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 19 Feb 2011 11:26:49 -0800
changeset 5349 02770b3c43f8
parent 5304 59e6e16351e6
child 5363 96799a165806
permissions -rw-r--r--
We have the correct window, we don't have to search for it, duh. :)
slouken@1914
     1
slouken@1914
     2
/* A simple test program framework */
slouken@1914
     3
slouken@1914
     4
#include <stdio.h>
slouken@1914
     5
slouken@1914
     6
#include "common.h"
slouken@1914
     7
slouken@1914
     8
#define VIDEO_USAGE \
slouken@5234
     9
"[--video driver] [--renderer driver] [--info all|video|modes|render|event] [--log all|error|system|audio|video|render|input] [--display N] [--fullscreen | --windows N] [--title title] [--icon icon.bmp] [--center | --position X,Y] [--geometry WxH] [--depth N] [--refresh R] [--vsync] [--noframe] [--resize] [--minimize] [--maximize] [--grab]"
slouken@1914
    10
slouken@1914
    11
#define AUDIO_USAGE \
slouken@1914
    12
"[--rate N] [--format U8|S8|U16|U16LE|U16BE|S16|S16LE|S16BE] [--channels N] [--samples N]"
slouken@1914
    13
slouken@1914
    14
CommonState *
slouken@1914
    15
CommonCreateState(char **argv, Uint32 flags)
slouken@1914
    16
{
slouken@1914
    17
    CommonState *state = SDL_calloc(1, sizeof(*state));
slouken@1914
    18
    if (!state) {
slouken@1914
    19
        SDL_OutOfMemory();
slouken@1914
    20
        return NULL;
slouken@1914
    21
    }
slouken@1914
    22
slouken@1914
    23
    /* Initialize some defaults */
slouken@1914
    24
    state->argv = argv;
slouken@1914
    25
    state->flags = flags;
slouken@1914
    26
    state->window_title = argv[0];
slouken@3500
    27
    state->window_flags = 0;
slouken@1914
    28
    state->window_x = SDL_WINDOWPOS_UNDEFINED;
slouken@1914
    29
    state->window_y = SDL_WINDOWPOS_UNDEFINED;
lestat@3194
    30
    state->window_w = DEFAULT_WINDOW_WIDTH;
lestat@3194
    31
    state->window_h = DEFAULT_WINDOW_HEIGHT;
slouken@1914
    32
    state->num_windows = 1;
slouken@1914
    33
    state->audiospec.freq = 22050;
slouken@1914
    34
    state->audiospec.format = AUDIO_S16;
slouken@1914
    35
    state->audiospec.channels = 2;
slouken@1914
    36
    state->audiospec.samples = 2048;
lestat@3408
    37
lestat@3408
    38
    /* Set some very sane GL defaults */
lestat@3408
    39
    state->gl_red_size = 3;
lestat@3408
    40
    state->gl_green_size = 3;
lestat@3408
    41
    state->gl_blue_size = 2;
lestat@3408
    42
    state->gl_alpha_size = 0;
lestat@3408
    43
    state->gl_buffer_size = 0;
lestat@3408
    44
    state->gl_depth_size = 16;
lestat@3408
    45
    state->gl_stencil_size = 0;
lestat@3408
    46
    state->gl_double_buffer = 1;
lestat@3408
    47
    state->gl_accum_red_size = 0;
lestat@3408
    48
    state->gl_accum_green_size = 0;
lestat@3408
    49
    state->gl_accum_blue_size = 0;
lestat@3408
    50
    state->gl_accum_alpha_size = 0;
lestat@3408
    51
    state->gl_stereo = 0;
lestat@3408
    52
    state->gl_multisamplebuffers = 0;
lestat@3408
    53
    state->gl_multisamplesamples = 0;
lestat@3408
    54
    state->gl_retained_backing = 1;
slouken@3571
    55
    state->gl_accelerated = -1;
lestat@3408
    56
    state->gl_major_version = 2;
lestat@3408
    57
    state->gl_minor_version = 1;
lestat@3408
    58
slouken@1914
    59
    return state;
slouken@1914
    60
}
slouken@1914
    61
slouken@1914
    62
int
slouken@1914
    63
CommonArg(CommonState * state, int index)
slouken@1914
    64
{
slouken@1914
    65
    char **argv = state->argv;
slouken@1914
    66
slouken@1914
    67
    if (SDL_strcasecmp(argv[index], "--video") == 0) {
slouken@1914
    68
        ++index;
slouken@1914
    69
        if (!argv[index]) {
slouken@1914
    70
            return -1;
slouken@1914
    71
        }
slouken@1914
    72
        state->videodriver = argv[index];
slouken@1914
    73
        return 2;
slouken@1914
    74
    }
slouken@1914
    75
    if (SDL_strcasecmp(argv[index], "--renderer") == 0) {
slouken@1914
    76
        ++index;
slouken@1914
    77
        if (!argv[index]) {
slouken@1914
    78
            return -1;
slouken@1914
    79
        }
slouken@1914
    80
        state->renderdriver = argv[index];
slouken@1914
    81
        return 2;
slouken@1914
    82
    }
slouken@1914
    83
    if (SDL_strcasecmp(argv[index], "--info") == 0) {
slouken@1914
    84
        ++index;
slouken@1914
    85
        if (!argv[index]) {
slouken@1914
    86
            return -1;
slouken@1914
    87
        }
slouken@1914
    88
        if (SDL_strcasecmp(argv[index], "all") == 0) {
slouken@1914
    89
            state->verbose |=
slouken@1914
    90
                (VERBOSE_VIDEO | VERBOSE_MODES | VERBOSE_RENDER |
slouken@1914
    91
                 VERBOSE_EVENT);
slouken@1914
    92
            return 2;
slouken@1914
    93
        }
slouken@1914
    94
        if (SDL_strcasecmp(argv[index], "video") == 0) {
slouken@1914
    95
            state->verbose |= VERBOSE_VIDEO;
slouken@1914
    96
            return 2;
slouken@1914
    97
        }
slouken@1914
    98
        if (SDL_strcasecmp(argv[index], "modes") == 0) {
slouken@1914
    99
            state->verbose |= VERBOSE_MODES;
slouken@1914
   100
            return 2;
slouken@1914
   101
        }
slouken@1914
   102
        if (SDL_strcasecmp(argv[index], "render") == 0) {
slouken@1914
   103
            state->verbose |= VERBOSE_RENDER;
slouken@1914
   104
            return 2;
slouken@1914
   105
        }
slouken@1914
   106
        if (SDL_strcasecmp(argv[index], "event") == 0) {
slouken@1914
   107
            state->verbose |= VERBOSE_EVENT;
slouken@1914
   108
            return 2;
slouken@1914
   109
        }
slouken@1914
   110
        return -1;
slouken@1914
   111
    }
slouken@5234
   112
    if (SDL_strcasecmp(argv[index], "--log") == 0) {
slouken@5234
   113
        ++index;
slouken@5234
   114
        if (!argv[index]) {
slouken@5234
   115
            return -1;
slouken@5234
   116
        }
slouken@5234
   117
        if (SDL_strcasecmp(argv[index], "all") == 0) {
slouken@5234
   118
            SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   119
            return 2;
slouken@5234
   120
        }
slouken@5234
   121
        if (SDL_strcasecmp(argv[index], "error") == 0) {
slouken@5234
   122
            SDL_LogSetPriority(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   123
            return 2;
slouken@5234
   124
        }
slouken@5234
   125
        if (SDL_strcasecmp(argv[index], "system") == 0) {
slouken@5234
   126
            SDL_LogSetPriority(SDL_LOG_CATEGORY_SYSTEM, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   127
            return 2;
slouken@5234
   128
        }
slouken@5234
   129
        if (SDL_strcasecmp(argv[index], "audio") == 0) {
slouken@5234
   130
            SDL_LogSetPriority(SDL_LOG_CATEGORY_AUDIO, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   131
            return 2;
slouken@5234
   132
        }
slouken@5234
   133
        if (SDL_strcasecmp(argv[index], "video") == 0) {
slouken@5234
   134
            SDL_LogSetPriority(SDL_LOG_CATEGORY_VIDEO, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   135
            return 2;
slouken@5234
   136
        }
slouken@5234
   137
        if (SDL_strcasecmp(argv[index], "render") == 0) {
slouken@5234
   138
            SDL_LogSetPriority(SDL_LOG_CATEGORY_RENDER, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   139
            return 2;
slouken@5234
   140
        }
slouken@5234
   141
        if (SDL_strcasecmp(argv[index], "input") == 0) {
slouken@5234
   142
            SDL_LogSetPriority(SDL_LOG_CATEGORY_INPUT, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   143
            return 2;
slouken@5234
   144
        }
slouken@5234
   145
        return -1;
slouken@5234
   146
    }
slouken@1914
   147
    if (SDL_strcasecmp(argv[index], "--display") == 0) {
slouken@1914
   148
        ++index;
slouken@1914
   149
        if (!argv[index]) {
slouken@1914
   150
            return -1;
slouken@1914
   151
        }
slouken@1914
   152
        state->display = SDL_atoi(argv[index]);
slouken@5247
   153
        if (SDL_WINDOWPOS_ISUNDEFINED(state->window_x)) {
slouken@5247
   154
            state->window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(state->display);
slouken@5247
   155
            state->window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(state->display);
slouken@5247
   156
        }
slouken@5304
   157
        if (SDL_WINDOWPOS_ISCENTERED(state->window_x)) {
slouken@5304
   158
            state->window_x = SDL_WINDOWPOS_CENTERED_DISPLAY(state->display);
slouken@5304
   159
            state->window_y = SDL_WINDOWPOS_CENTERED_DISPLAY(state->display);
slouken@5304
   160
        }
slouken@1914
   161
        return 2;
slouken@1914
   162
    }
slouken@1914
   163
    if (SDL_strcasecmp(argv[index], "--fullscreen") == 0) {
slouken@1914
   164
        state->window_flags |= SDL_WINDOW_FULLSCREEN;
slouken@1914
   165
        state->num_windows = 1;
slouken@1914
   166
        return 1;
slouken@1914
   167
    }
slouken@1914
   168
    if (SDL_strcasecmp(argv[index], "--windows") == 0) {
slouken@1914
   169
        ++index;
slouken@1916
   170
        if (!argv[index] || !SDL_isdigit(*argv[index])) {
slouken@1914
   171
            return -1;
slouken@1914
   172
        }
slouken@1914
   173
        if (!(state->window_flags & SDL_WINDOW_FULLSCREEN)) {
slouken@1914
   174
            state->num_windows = SDL_atoi(argv[index]);
slouken@1914
   175
        }
slouken@1914
   176
        return 2;
slouken@1914
   177
    }
slouken@1914
   178
    if (SDL_strcasecmp(argv[index], "--title") == 0) {
slouken@1914
   179
        ++index;
slouken@1914
   180
        if (!argv[index]) {
slouken@1914
   181
            return -1;
slouken@1914
   182
        }
slouken@1914
   183
        state->window_title = argv[index];
slouken@1914
   184
        return 2;
slouken@1914
   185
    }
slouken@4527
   186
    if (SDL_strcasecmp(argv[index], "--icon") == 0) {
slouken@4527
   187
        ++index;
slouken@4527
   188
        if (!argv[index]) {
slouken@4527
   189
            return -1;
slouken@4527
   190
        }
slouken@4527
   191
        state->window_icon = argv[index];
slouken@4527
   192
        return 2;
slouken@4527
   193
    }
slouken@1914
   194
    if (SDL_strcasecmp(argv[index], "--center") == 0) {
slouken@1914
   195
        state->window_x = SDL_WINDOWPOS_CENTERED;
slouken@1914
   196
        state->window_y = SDL_WINDOWPOS_CENTERED;
slouken@1914
   197
        return 1;
slouken@1914
   198
    }
slouken@1914
   199
    if (SDL_strcasecmp(argv[index], "--position") == 0) {
slouken@1914
   200
        char *x, *y;
slouken@1914
   201
        ++index;
slouken@1914
   202
        if (!argv[index]) {
slouken@1914
   203
            return -1;
slouken@1914
   204
        }
slouken@1914
   205
        x = argv[index];
slouken@1914
   206
        y = argv[index];
slouken@1914
   207
        while (*y && *y != ',') {
slouken@1914
   208
            ++y;
slouken@1914
   209
        }
slouken@1914
   210
        if (!*y) {
slouken@1914
   211
            return -1;
slouken@1914
   212
        }
slouken@1914
   213
        *y++ = '\0';
slouken@1914
   214
        state->window_x = SDL_atoi(x);
slouken@1914
   215
        state->window_y = SDL_atoi(y);
slouken@1914
   216
        return 2;
slouken@1914
   217
    }
slouken@1914
   218
    if (SDL_strcasecmp(argv[index], "--geometry") == 0) {
slouken@1914
   219
        char *w, *h;
slouken@1914
   220
        ++index;
slouken@1914
   221
        if (!argv[index]) {
slouken@1914
   222
            return -1;
slouken@1914
   223
        }
slouken@1914
   224
        w = argv[index];
slouken@1914
   225
        h = argv[index];
slouken@1914
   226
        while (*h && *h != 'x') {
slouken@1914
   227
            ++h;
slouken@1914
   228
        }
slouken@1914
   229
        if (!*h) {
slouken@1914
   230
            return -1;
slouken@1914
   231
        }
slouken@1914
   232
        *h++ = '\0';
slouken@1914
   233
        state->window_w = SDL_atoi(w);
slouken@1914
   234
        state->window_h = SDL_atoi(h);
slouken@1914
   235
        return 2;
slouken@1914
   236
    }
slouken@2209
   237
    if (SDL_strcasecmp(argv[index], "--depth") == 0) {
slouken@2209
   238
        ++index;
slouken@2209
   239
        if (!argv[index]) {
slouken@2209
   240
            return -1;
slouken@2209
   241
        }
slouken@2209
   242
        state->depth = SDL_atoi(argv[index]);
slouken@2209
   243
        return 2;
slouken@2209
   244
    }
slouken@1914
   245
    if (SDL_strcasecmp(argv[index], "--refresh") == 0) {
slouken@1914
   246
        ++index;
slouken@1914
   247
        if (!argv[index]) {
slouken@1914
   248
            return -1;
slouken@1914
   249
        }
slouken@1914
   250
        state->refresh_rate = SDL_atoi(argv[index]);
slouken@1914
   251
        return 2;
slouken@1914
   252
    }
slouken@1914
   253
    if (SDL_strcasecmp(argv[index], "--vsync") == 0) {
slouken@1965
   254
        state->render_flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1914
   255
        return 1;
slouken@1914
   256
    }
slouken@1914
   257
    if (SDL_strcasecmp(argv[index], "--noframe") == 0) {
slouken@1914
   258
        state->window_flags |= SDL_WINDOW_BORDERLESS;
slouken@1914
   259
        return 1;
slouken@1914
   260
    }
slouken@1914
   261
    if (SDL_strcasecmp(argv[index], "--resize") == 0) {
slouken@1914
   262
        state->window_flags |= SDL_WINDOW_RESIZABLE;
slouken@1914
   263
        return 1;
slouken@1914
   264
    }
slouken@1914
   265
    if (SDL_strcasecmp(argv[index], "--minimize") == 0) {
slouken@1914
   266
        state->window_flags |= SDL_WINDOW_MINIMIZED;
slouken@1914
   267
        return 1;
slouken@1914
   268
    }
slouken@1914
   269
    if (SDL_strcasecmp(argv[index], "--maximize") == 0) {
slouken@1914
   270
        state->window_flags |= SDL_WINDOW_MAXIMIZED;
slouken@1914
   271
        return 1;
slouken@1914
   272
    }
slouken@1914
   273
    if (SDL_strcasecmp(argv[index], "--grab") == 0) {
slouken@1914
   274
        state->window_flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@1914
   275
        return 1;
slouken@1914
   276
    }
slouken@1914
   277
    if (SDL_strcasecmp(argv[index], "--rate") == 0) {
slouken@1914
   278
        ++index;
slouken@1914
   279
        if (!argv[index]) {
slouken@1914
   280
            return -1;
slouken@1914
   281
        }
slouken@1914
   282
        state->audiospec.freq = SDL_atoi(argv[index]);
slouken@1914
   283
        return 2;
slouken@1914
   284
    }
slouken@1914
   285
    if (SDL_strcasecmp(argv[index], "--format") == 0) {
slouken@1914
   286
        ++index;
slouken@1914
   287
        if (!argv[index]) {
slouken@1914
   288
            return -1;
slouken@1914
   289
        }
slouken@1914
   290
        if (SDL_strcasecmp(argv[index], "U8") == 0) {
slouken@1914
   291
            state->audiospec.format = AUDIO_U8;
slouken@1914
   292
            return 2;
slouken@1914
   293
        }
slouken@1914
   294
        if (SDL_strcasecmp(argv[index], "S8") == 0) {
slouken@1914
   295
            state->audiospec.format = AUDIO_S8;
slouken@1914
   296
            return 2;
slouken@1914
   297
        }
slouken@1914
   298
        if (SDL_strcasecmp(argv[index], "U16") == 0) {
slouken@1914
   299
            state->audiospec.format = AUDIO_U16;
slouken@1914
   300
            return 2;
slouken@1914
   301
        }
slouken@1914
   302
        if (SDL_strcasecmp(argv[index], "U16LE") == 0) {
slouken@1914
   303
            state->audiospec.format = AUDIO_U16LSB;
slouken@1914
   304
            return 2;
slouken@1914
   305
        }
slouken@1914
   306
        if (SDL_strcasecmp(argv[index], "U16BE") == 0) {
slouken@1914
   307
            state->audiospec.format = AUDIO_U16MSB;
slouken@1914
   308
            return 2;
slouken@1914
   309
        }
slouken@1914
   310
        if (SDL_strcasecmp(argv[index], "S16") == 0) {
slouken@1914
   311
            state->audiospec.format = AUDIO_S16;
slouken@1914
   312
            return 2;
slouken@1914
   313
        }
slouken@1914
   314
        if (SDL_strcasecmp(argv[index], "S16LE") == 0) {
slouken@1914
   315
            state->audiospec.format = AUDIO_S16LSB;
slouken@1914
   316
            return 2;
slouken@1914
   317
        }
slouken@1914
   318
        if (SDL_strcasecmp(argv[index], "S16BE") == 0) {
slouken@1914
   319
            state->audiospec.format = AUDIO_S16MSB;
slouken@1914
   320
            return 2;
slouken@1914
   321
        }
slouken@1914
   322
        return -1;
slouken@1914
   323
    }
slouken@1914
   324
    if (SDL_strcasecmp(argv[index], "--channels") == 0) {
slouken@1914
   325
        ++index;
slouken@1914
   326
        if (!argv[index]) {
slouken@1914
   327
            return -1;
slouken@1914
   328
        }
bob@2185
   329
        state->audiospec.channels = (Uint8) SDL_atoi(argv[index]);
slouken@1914
   330
        return 2;
slouken@1914
   331
    }
slouken@1914
   332
    if (SDL_strcasecmp(argv[index], "--samples") == 0) {
slouken@1914
   333
        ++index;
slouken@1914
   334
        if (!argv[index]) {
slouken@1914
   335
            return -1;
slouken@1914
   336
        }
bob@2185
   337
        state->audiospec.samples = (Uint16) SDL_atoi(argv[index]);
slouken@1914
   338
        return 2;
slouken@1914
   339
    }
slouken@1914
   340
    if ((SDL_strcasecmp(argv[index], "-h") == 0)
slouken@1914
   341
        || (SDL_strcasecmp(argv[index], "--help") == 0)) {
slouken@1914
   342
        /* Print the usage message */
slouken@1914
   343
        return -1;
slouken@1914
   344
    }
slouken@1914
   345
    return 0;
slouken@1914
   346
}
slouken@1914
   347
slouken@1914
   348
const char *
slouken@1914
   349
CommonUsage(CommonState * state)
slouken@1914
   350
{
slouken@1914
   351
    switch (state->flags & (SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
slouken@1914
   352
    case SDL_INIT_VIDEO:
slouken@1914
   353
        return VIDEO_USAGE;
slouken@1914
   354
    case SDL_INIT_AUDIO:
slouken@1914
   355
        return AUDIO_USAGE;
slouken@1914
   356
    case (SDL_INIT_VIDEO | SDL_INIT_AUDIO):
slouken@1914
   357
        return VIDEO_USAGE " " AUDIO_USAGE;
slouken@1914
   358
    default:
slouken@1914
   359
        return "";
slouken@1914
   360
    }
slouken@1914
   361
}
slouken@1914
   362
slouken@1914
   363
static void
slouken@1914
   364
PrintRendererFlag(Uint32 flag)
slouken@1914
   365
{
slouken@1914
   366
    switch (flag) {
slouken@1965
   367
    case SDL_RENDERER_PRESENTVSYNC:
slouken@1914
   368
        fprintf(stderr, "PresentVSync");
slouken@1914
   369
        break;
slouken@1965
   370
    case SDL_RENDERER_ACCELERATED:
slouken@1921
   371
        fprintf(stderr, "Accelerated");
slouken@1921
   372
        break;
slouken@1914
   373
    default:
slouken@1914
   374
        fprintf(stderr, "0x%8.8x", flag);
slouken@1914
   375
        break;
slouken@1914
   376
    }
slouken@1914
   377
}
slouken@1914
   378
slouken@1914
   379
static void
slouken@1914
   380
PrintPixelFormat(Uint32 format)
slouken@1914
   381
{
slouken@1914
   382
    switch (format) {
slouken@1965
   383
    case SDL_PIXELFORMAT_UNKNOWN:
slouken@1914
   384
        fprintf(stderr, "Unknwon");
slouken@1914
   385
        break;
slouken@1965
   386
    case SDL_PIXELFORMAT_INDEX1LSB:
slouken@1914
   387
        fprintf(stderr, "Index1LSB");
slouken@1914
   388
        break;
slouken@1965
   389
    case SDL_PIXELFORMAT_INDEX1MSB:
slouken@1914
   390
        fprintf(stderr, "Index1MSB");
slouken@1914
   391
        break;
slouken@1965
   392
    case SDL_PIXELFORMAT_INDEX4LSB:
slouken@1914
   393
        fprintf(stderr, "Index4LSB");
slouken@1914
   394
        break;
slouken@1965
   395
    case SDL_PIXELFORMAT_INDEX4MSB:
slouken@1914
   396
        fprintf(stderr, "Index4MSB");
slouken@1914
   397
        break;
slouken@1965
   398
    case SDL_PIXELFORMAT_INDEX8:
slouken@1914
   399
        fprintf(stderr, "Index8");
slouken@1914
   400
        break;
slouken@1965
   401
    case SDL_PIXELFORMAT_RGB332:
slouken@1914
   402
        fprintf(stderr, "RGB332");
slouken@1914
   403
        break;
slouken@1965
   404
    case SDL_PIXELFORMAT_RGB444:
slouken@1914
   405
        fprintf(stderr, "RGB444");
slouken@1914
   406
        break;
slouken@1965
   407
    case SDL_PIXELFORMAT_RGB555:
slouken@1914
   408
        fprintf(stderr, "RGB555");
slouken@1914
   409
        break;
slouken@4621
   410
    case SDL_PIXELFORMAT_BGR555:
slouken@4621
   411
        fprintf(stderr, "BGR555");
slouken@4621
   412
        break;
slouken@1965
   413
    case SDL_PIXELFORMAT_ARGB4444:
slouken@1914
   414
        fprintf(stderr, "ARGB4444");
slouken@1914
   415
        break;
lestat@3184
   416
    case SDL_PIXELFORMAT_ABGR4444:
lestat@3184
   417
        fprintf(stderr, "ABGR4444");
lestat@3184
   418
        break;
slouken@1965
   419
    case SDL_PIXELFORMAT_ARGB1555:
slouken@1914
   420
        fprintf(stderr, "ARGB1555");
slouken@1914
   421
        break;
lestat@3184
   422
    case SDL_PIXELFORMAT_ABGR1555:
lestat@3184
   423
        fprintf(stderr, "ABGR1555");
lestat@3184
   424
        break;
slouken@1965
   425
    case SDL_PIXELFORMAT_RGB565:
slouken@1914
   426
        fprintf(stderr, "RGB565");
slouken@1914
   427
        break;
lestat@3184
   428
    case SDL_PIXELFORMAT_BGR565:
lestat@3184
   429
        fprintf(stderr, "BGR565");
lestat@3184
   430
        break;
slouken@1965
   431
    case SDL_PIXELFORMAT_RGB24:
slouken@1914
   432
        fprintf(stderr, "RGB24");
slouken@1914
   433
        break;
slouken@1965
   434
    case SDL_PIXELFORMAT_BGR24:
slouken@1914
   435
        fprintf(stderr, "BGR24");
slouken@1914
   436
        break;
slouken@1965
   437
    case SDL_PIXELFORMAT_RGB888:
slouken@1914
   438
        fprintf(stderr, "RGB888");
slouken@1914
   439
        break;
slouken@1965
   440
    case SDL_PIXELFORMAT_BGR888:
slouken@1914
   441
        fprintf(stderr, "BGR888");
slouken@1914
   442
        break;
slouken@1965
   443
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1914
   444
        fprintf(stderr, "ARGB8888");
slouken@1914
   445
        break;
slouken@1965
   446
    case SDL_PIXELFORMAT_RGBA8888:
slouken@1914
   447
        fprintf(stderr, "RGBA8888");
slouken@1914
   448
        break;
slouken@1965
   449
    case SDL_PIXELFORMAT_ABGR8888:
slouken@1914
   450
        fprintf(stderr, "ABGR8888");
slouken@1914
   451
        break;
slouken@1965
   452
    case SDL_PIXELFORMAT_BGRA8888:
slouken@1914
   453
        fprintf(stderr, "BGRA8888");
slouken@1914
   454
        break;
slouken@1965
   455
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@1914
   456
        fprintf(stderr, "ARGB2101010");
slouken@1914
   457
        break;
slouken@1965
   458
    case SDL_PIXELFORMAT_YV12:
slouken@1914
   459
        fprintf(stderr, "YV12");
slouken@1914
   460
        break;
slouken@1965
   461
    case SDL_PIXELFORMAT_IYUV:
slouken@1914
   462
        fprintf(stderr, "IYUV");
slouken@1914
   463
        break;
slouken@1965
   464
    case SDL_PIXELFORMAT_YUY2:
slouken@1914
   465
        fprintf(stderr, "YUY2");
slouken@1914
   466
        break;
slouken@1965
   467
    case SDL_PIXELFORMAT_UYVY:
slouken@1914
   468
        fprintf(stderr, "UYVY");
slouken@1914
   469
        break;
slouken@1965
   470
    case SDL_PIXELFORMAT_YVYU:
slouken@1914
   471
        fprintf(stderr, "YVYU");
slouken@1914
   472
        break;
slouken@1914
   473
    default:
slouken@1914
   474
        fprintf(stderr, "0x%8.8x", format);
slouken@1914
   475
        break;
slouken@1914
   476
    }
slouken@1914
   477
}
slouken@1914
   478
slouken@1914
   479
static void
slouken@1914
   480
PrintRenderer(SDL_RendererInfo * info)
slouken@1914
   481
{
slouken@1914
   482
    int i, count;
slouken@1914
   483
slouken@1914
   484
    fprintf(stderr, "  Renderer %s:\n", info->name);
slouken@1914
   485
slouken@1914
   486
    fprintf(stderr, "    Flags: 0x%8.8X", info->flags);
slouken@1914
   487
    fprintf(stderr, " (");
slouken@1914
   488
    count = 0;
slouken@1914
   489
    for (i = 0; i < sizeof(info->flags) * 8; ++i) {
slouken@1914
   490
        Uint32 flag = (1 << i);
slouken@1914
   491
        if (info->flags & flag) {
slouken@1914
   492
            if (count > 0) {
slouken@1914
   493
                fprintf(stderr, " | ");
slouken@1914
   494
            }
slouken@1914
   495
            PrintRendererFlag(flag);
slouken@1914
   496
            ++count;
slouken@1914
   497
        }
slouken@1914
   498
    }
slouken@1914
   499
    fprintf(stderr, ")\n");
slouken@1914
   500
slouken@1914
   501
    fprintf(stderr, "    Texture formats (%d): ", info->num_texture_formats);
bob@2185
   502
    for (i = 0; i < (int) info->num_texture_formats; ++i) {
slouken@1914
   503
        if (i > 0) {
slouken@1914
   504
            fprintf(stderr, ", ");
slouken@1914
   505
        }
slouken@1914
   506
        PrintPixelFormat(info->texture_formats[i]);
slouken@1914
   507
    }
slouken@1914
   508
    fprintf(stderr, "\n");
slouken@1914
   509
slouken@1914
   510
    if (info->max_texture_width || info->max_texture_height) {
slouken@1914
   511
        fprintf(stderr, "    Max Texture Size: %dx%d\n",
slouken@1914
   512
                info->max_texture_width, info->max_texture_height);
slouken@1914
   513
    }
slouken@1914
   514
}
slouken@1914
   515
slouken@4527
   516
static SDL_Surface *
slouken@4527
   517
LoadIcon(const char *file)
slouken@4527
   518
{
slouken@4527
   519
    SDL_Surface *icon;
slouken@4527
   520
slouken@4527
   521
    /* Load the icon surface */
slouken@4527
   522
    icon = SDL_LoadBMP(file);
slouken@4527
   523
    if (icon == NULL) {
slouken@4527
   524
        fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
slouken@4527
   525
        return (NULL);
slouken@4527
   526
    }
slouken@4527
   527
slouken@4527
   528
    if (icon->format->palette == NULL) {
slouken@4527
   529
        fprintf(stderr, "Icon must have a palette!\n");
slouken@4527
   530
        SDL_FreeSurface(icon);
slouken@4527
   531
        return (NULL);
slouken@4527
   532
    }
slouken@4527
   533
slouken@4527
   534
    /* Set the colorkey */
slouken@4527
   535
    SDL_SetColorKey(icon, 1, *((Uint8 *) icon->pixels));
slouken@4527
   536
slouken@4527
   537
    return (icon);
slouken@4527
   538
}
slouken@4527
   539
slouken@1914
   540
SDL_bool
slouken@1914
   541
CommonInit(CommonState * state)
slouken@1914
   542
{
slouken@1914
   543
    int i, j, m, n;
slouken@1914
   544
    SDL_DisplayMode fullscreen_mode;
slouken@1914
   545
slouken@1914
   546
    if (state->flags & SDL_INIT_VIDEO) {
slouken@1914
   547
        if (state->verbose & VERBOSE_VIDEO) {
slouken@1914
   548
            n = SDL_GetNumVideoDrivers();
slouken@1914
   549
            if (n == 0) {
slouken@1914
   550
                fprintf(stderr, "No built-in video drivers\n");
slouken@1914
   551
            } else {
slouken@1914
   552
                fprintf(stderr, "Built-in video drivers:");
slouken@1914
   553
                for (i = 0; i < n; ++i) {
slouken@1914
   554
                    if (i > 0) {
slouken@1914
   555
                        fprintf(stderr, ",");
slouken@1914
   556
                    }
slouken@1914
   557
                    fprintf(stderr, " %s", SDL_GetVideoDriver(i));
slouken@1914
   558
                }
slouken@1914
   559
                fprintf(stderr, "\n");
slouken@1914
   560
            }
slouken@1914
   561
        }
slouken@5123
   562
        if (SDL_VideoInit(state->videodriver) < 0) {
slouken@1914
   563
            fprintf(stderr, "Couldn't initialize video driver: %s\n",
slouken@1914
   564
                    SDL_GetError());
slouken@1914
   565
            return SDL_FALSE;
slouken@1914
   566
        }
slouken@1914
   567
        if (state->verbose & VERBOSE_VIDEO) {
slouken@1914
   568
            fprintf(stderr, "Video driver: %s\n",
slouken@1914
   569
                    SDL_GetCurrentVideoDriver());
slouken@1914
   570
        }
slouken@1914
   571
lestat@3408
   572
        /* Upload GL settings */
lestat@3408
   573
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, state->gl_red_size);
lestat@3408
   574
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, state->gl_green_size);
lestat@3408
   575
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, state->gl_blue_size);
lestat@3408
   576
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, state->gl_alpha_size);
lestat@3408
   577
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, state->gl_double_buffer);
lestat@3408
   578
        SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, state->gl_buffer_size);
lestat@3408
   579
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, state->gl_depth_size);
lestat@3408
   580
        SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, state->gl_stencil_size);
lestat@3408
   581
        SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, state->gl_accum_red_size);
lestat@3408
   582
        SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, state->gl_accum_green_size);
lestat@3408
   583
        SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, state->gl_accum_blue_size);
lestat@3408
   584
        SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, state->gl_accum_alpha_size);
lestat@3408
   585
        SDL_GL_SetAttribute(SDL_GL_STEREO, state->gl_stereo);
lestat@3408
   586
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, state->gl_multisamplebuffers);
lestat@3408
   587
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, state->gl_multisamplesamples);
slouken@3571
   588
        if (state->gl_accelerated >= 0) {
slouken@3571
   589
            SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL,
slouken@3571
   590
                                state->gl_accelerated);
slouken@3571
   591
        }
lestat@3408
   592
        SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, state->gl_retained_backing);
lestat@3408
   593
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, state->gl_major_version);
lestat@3408
   594
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, state->gl_minor_version);
lestat@3408
   595
slouken@1914
   596
        if (state->verbose & VERBOSE_MODES) {
slouken@1967
   597
            SDL_DisplayMode mode;
slouken@1914
   598
            int bpp;
slouken@1914
   599
            Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1914
   600
slouken@1914
   601
            n = SDL_GetNumVideoDisplays();
slouken@1914
   602
            fprintf(stderr, "Number of displays: %d\n", n);
slouken@1914
   603
            for (i = 0; i < n; ++i) {
slouken@1914
   604
                fprintf(stderr, "Display %d:\n", i);
slouken@1914
   605
slouken@5244
   606
                SDL_GetDesktopDisplayMode(i, &mode);
slouken@1967
   607
                SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask,
slouken@1914
   608
                                           &Bmask, &Amask);
slouken@1914
   609
                fprintf(stderr,
lestat@3184
   610
                        "  Current mode: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
lestat@3184
   611
                        mode.w, mode.h, mode.refresh_rate, bpp,
slouken@4982
   612
                        SDL_GetPixelFormatName(mode.format));
slouken@1914
   613
                if (Rmask || Gmask || Bmask) {
lestat@3184
   614
                    fprintf(stderr, "      Red Mask   = 0x%.8x\n", Rmask);
slouken@1914
   615
                    fprintf(stderr, "      Green Mask = 0x%.8x\n", Gmask);
lestat@3184
   616
                    fprintf(stderr, "      Blue Mask  = 0x%.8x\n", Bmask);
slouken@1914
   617
                    if (Amask)
slouken@1914
   618
                        fprintf(stderr, "      Alpha Mask = 0x%.8x\n", Amask);
slouken@1914
   619
                }
slouken@1914
   620
slouken@1914
   621
                /* Print available fullscreen video modes */
slouken@5244
   622
                m = SDL_GetNumDisplayModes(i);
slouken@1914
   623
                if (m == 0) {
slouken@1914
   624
                    fprintf(stderr, "No available fullscreen video modes\n");
slouken@1914
   625
                } else {
slouken@1914
   626
                    fprintf(stderr, "  Fullscreen video modes:\n");
slouken@1914
   627
                    for (j = 0; j < m; ++j) {
slouken@5244
   628
                        SDL_GetDisplayMode(i, j, &mode);
slouken@1967
   629
                        SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask,
slouken@1914
   630
                                                   &Gmask, &Bmask, &Amask);
slouken@1914
   631
                        fprintf(stderr,
lestat@3184
   632
                                "    Mode %d: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
lestat@3184
   633
                                j, mode.w, mode.h, mode.refresh_rate, bpp,
slouken@4982
   634
                                SDL_GetPixelFormatName(mode.format));
slouken@1914
   635
                        if (Rmask || Gmask || Bmask) {
lestat@3184
   636
                            fprintf(stderr, "        Red Mask   = 0x%.8x\n",
slouken@1914
   637
                                    Rmask);
slouken@1914
   638
                            fprintf(stderr, "        Green Mask = 0x%.8x\n",
slouken@1914
   639
                                    Gmask);
lestat@3184
   640
                            fprintf(stderr, "        Blue Mask  = 0x%.8x\n",
slouken@1914
   641
                                    Bmask);
slouken@1914
   642
                            if (Amask)
slouken@1914
   643
                                fprintf(stderr,
slouken@1914
   644
                                        "        Alpha Mask = 0x%.8x\n",
slouken@1914
   645
                                        Amask);
slouken@1914
   646
                        }
slouken@1914
   647
                    }
slouken@1914
   648
                }
slouken@1914
   649
            }
slouken@1914
   650
        }
slouken@1914
   651
slouken@1914
   652
        if (state->verbose & VERBOSE_RENDER) {
slouken@1914
   653
            SDL_RendererInfo info;
slouken@1914
   654
slouken@1969
   655
            n = SDL_GetNumRenderDrivers();
slouken@1914
   656
            if (n == 0) {
slouken@1914
   657
                fprintf(stderr, "No built-in render drivers\n");
slouken@1914
   658
            } else {
slouken@1923
   659
                fprintf(stderr, "Built-in render drivers:\n");
slouken@1914
   660
                for (i = 0; i < n; ++i) {
slouken@1969
   661
                    SDL_GetRenderDriverInfo(i, &info);
slouken@1914
   662
                    PrintRenderer(&info);
slouken@1914
   663
                }
slouken@1914
   664
            }
slouken@1914
   665
        }
slouken@1914
   666
slouken@3500
   667
        SDL_zero(fullscreen_mode);
slouken@1914
   668
        switch (state->depth) {
slouken@1914
   669
        case 8:
slouken@1965
   670
            fullscreen_mode.format = SDL_PIXELFORMAT_INDEX8;
slouken@1914
   671
            break;
slouken@1914
   672
        case 15:
slouken@1965
   673
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB555;
slouken@1914
   674
            break;
slouken@1914
   675
        case 16:
slouken@1965
   676
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB565;
slouken@1914
   677
            break;
lestat@3184
   678
        case 24:
lestat@3184
   679
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB24;
lestat@3184
   680
            break;
slouken@1914
   681
        default:
slouken@1965
   682
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB888;
slouken@1914
   683
            break;
slouken@1914
   684
        }
slouken@1914
   685
        fullscreen_mode.refresh_rate = state->refresh_rate;
slouken@1914
   686
slouken@1914
   687
        state->windows =
slouken@3685
   688
            (SDL_Window **) SDL_malloc(state->num_windows *
slouken@1914
   689
                                        sizeof(*state->windows));
slouken@5147
   690
        state->renderers =
slouken@5147
   691
            (SDL_Renderer **) SDL_malloc(state->num_windows *
slouken@5147
   692
                                        sizeof(*state->renderers));
slouken@5147
   693
        if (!state->windows || !state->renderers) {
slouken@1914
   694
            fprintf(stderr, "Out of memory!\n");
slouken@1914
   695
            return SDL_FALSE;
slouken@1914
   696
        }
slouken@1914
   697
        for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   698
            char title[1024];
slouken@1914
   699
slouken@1914
   700
            if (state->num_windows > 1) {
slouken@1914
   701
                SDL_snprintf(title, SDL_arraysize(title), "%s %d",
slouken@1914
   702
                             state->window_title, i + 1);
slouken@1914
   703
            } else {
slouken@1914
   704
                SDL_strlcpy(title, state->window_title, SDL_arraysize(title));
slouken@1914
   705
            }
slouken@1914
   706
            state->windows[i] =
slouken@1914
   707
                SDL_CreateWindow(title, state->window_x, state->window_y,
slouken@1914
   708
                                 state->window_w, state->window_h,
slouken@1914
   709
                                 state->window_flags);
slouken@1914
   710
            if (!state->windows[i]) {
slouken@1914
   711
                fprintf(stderr, "Couldn't create window: %s\n",
slouken@1914
   712
                        SDL_GetError());
slouken@1914
   713
                return SDL_FALSE;
slouken@1914
   714
            }
slouken@1914
   715
slouken@3500
   716
            if (SDL_SetWindowDisplayMode(state->windows[i], &fullscreen_mode) < 0) {
slouken@3500
   717
                fprintf(stderr, "Can't set up fullscreen display mode: %s\n",
slouken@3500
   718
                        SDL_GetError());
slouken@3500
   719
                return SDL_FALSE;
slouken@3500
   720
            }
slouken@4527
   721
slouken@4527
   722
            if (state->window_icon) {
slouken@4527
   723
                SDL_Surface *icon = LoadIcon(state->window_icon);
slouken@4527
   724
                if (icon) {
slouken@4527
   725
                    SDL_SetWindowIcon(state->windows[i], icon);
slouken@4527
   726
                    SDL_FreeSurface(icon);
slouken@4527
   727
                }
slouken@4527
   728
            }
slouken@4527
   729
slouken@3500
   730
            SDL_ShowWindow(state->windows[i]);
slouken@3500
   731
slouken@5147
   732
            state->renderers[i] = NULL;
slouken@5147
   733
slouken@1933
   734
            if (!state->skip_renderer
slouken@1933
   735
                && (state->renderdriver
slouken@1933
   736
                    || !(state->window_flags & SDL_WINDOW_OPENGL))) {
slouken@1914
   737
                m = -1;
slouken@1914
   738
                if (state->renderdriver) {
slouken@1914
   739
                    SDL_RendererInfo info;
slouken@1969
   740
                    n = SDL_GetNumRenderDrivers();
slouken@1914
   741
                    for (j = 0; j < n; ++j) {
slouken@1969
   742
                        SDL_GetRenderDriverInfo(j, &info);
slouken@1914
   743
                        if (SDL_strcasecmp(info.name, state->renderdriver) ==
slouken@1914
   744
                            0) {
slouken@1914
   745
                            m = j;
slouken@1914
   746
                            break;
slouken@1914
   747
                        }
slouken@1914
   748
                    }
slouken@1914
   749
                    if (m == n) {
slouken@1914
   750
                        fprintf(stderr,
slouken@1914
   751
                                "Couldn't find render driver named %s",
slouken@1914
   752
                                state->renderdriver);
slouken@1914
   753
                        return SDL_FALSE;
slouken@1914
   754
                    }
slouken@1914
   755
                }
slouken@5147
   756
                state->renderers[i] = SDL_CreateRenderer(state->windows[i],
slouken@5147
   757
                                            m, state->render_flags);
slouken@5147
   758
                if (!state->renderers[i]) {
slouken@1914
   759
                    fprintf(stderr, "Couldn't create renderer: %s\n",
slouken@1914
   760
                            SDL_GetError());
slouken@1914
   761
                    return SDL_FALSE;
slouken@1914
   762
                }
slouken@1923
   763
                if (state->verbose & VERBOSE_RENDER) {
slouken@1923
   764
                    SDL_RendererInfo info;
slouken@1923
   765
slouken@1923
   766
                    fprintf(stderr, "Current renderer:\n");
slouken@5147
   767
                    SDL_GetRendererInfo(state->renderers[i], &info);
slouken@1923
   768
                    PrintRenderer(&info);
slouken@1923
   769
                }
slouken@1914
   770
            }
slouken@1914
   771
        }
slouken@1914
   772
    }
slouken@1914
   773
slouken@1914
   774
    if (state->flags & SDL_INIT_AUDIO) {
slouken@1914
   775
        if (state->verbose & VERBOSE_AUDIO) {
slouken@1914
   776
            n = SDL_GetNumAudioDrivers();
slouken@1914
   777
            if (n == 0) {
slouken@1914
   778
                fprintf(stderr, "No built-in audio drivers\n");
slouken@1914
   779
            } else {
slouken@1914
   780
                fprintf(stderr, "Built-in audio drivers:");
slouken@1914
   781
                for (i = 0; i < n; ++i) {
slouken@1914
   782
                    if (i > 0) {
slouken@1914
   783
                        fprintf(stderr, ",");
slouken@1914
   784
                    }
slouken@1914
   785
                    fprintf(stderr, " %s", SDL_GetAudioDriver(i));
slouken@1914
   786
                }
slouken@1914
   787
                fprintf(stderr, "\n");
slouken@1914
   788
            }
slouken@1914
   789
        }
slouken@1914
   790
        if (SDL_AudioInit(state->audiodriver) < 0) {
slouken@1914
   791
            fprintf(stderr, "Couldn't initialize audio driver: %s\n",
slouken@1914
   792
                    SDL_GetError());
slouken@1914
   793
            return SDL_FALSE;
slouken@1914
   794
        }
slouken@1914
   795
        if (state->verbose & VERBOSE_VIDEO) {
slouken@1914
   796
            fprintf(stderr, "Audio driver: %s\n",
slouken@1914
   797
                    SDL_GetCurrentAudioDriver());
slouken@1914
   798
        }
slouken@1914
   799
slouken@1914
   800
        if (SDL_OpenAudio(&state->audiospec, NULL) < 0) {
slouken@1914
   801
            fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@1914
   802
            return SDL_FALSE;
slouken@1914
   803
        }
slouken@1914
   804
    }
slouken@1914
   805
slouken@1914
   806
    return SDL_TRUE;
slouken@1914
   807
}
slouken@1914
   808
slouken@1914
   809
static void
slouken@1914
   810
PrintEvent(SDL_Event * event)
slouken@1914
   811
{
slouken@1914
   812
    fprintf(stderr, "SDL EVENT: ");
slouken@1914
   813
    switch (event->type) {
slouken@1914
   814
    case SDL_WINDOWEVENT:
slouken@1914
   815
        switch (event->window.event) {
slouken@1914
   816
        case SDL_WINDOWEVENT_SHOWN:
slouken@1914
   817
            fprintf(stderr, "Window %d shown", event->window.windowID);
slouken@1914
   818
            break;
slouken@1914
   819
        case SDL_WINDOWEVENT_HIDDEN:
slouken@1914
   820
            fprintf(stderr, "Window %d hidden", event->window.windowID);
slouken@1914
   821
            break;
slouken@1914
   822
        case SDL_WINDOWEVENT_EXPOSED:
slouken@1970
   823
            fprintf(stderr, "Window %d exposed", event->window.windowID);
slouken@1914
   824
            break;
slouken@1914
   825
        case SDL_WINDOWEVENT_MOVED:
slouken@1970
   826
            fprintf(stderr, "Window %d moved to %d,%d",
slouken@1914
   827
                    event->window.windowID, event->window.data1,
slouken@1914
   828
                    event->window.data2);
slouken@1914
   829
            break;
slouken@1914
   830
        case SDL_WINDOWEVENT_RESIZED:
slouken@1970
   831
            fprintf(stderr, "Window %d resized to %dx%d",
slouken@1914
   832
                    event->window.windowID, event->window.data1,
slouken@1914
   833
                    event->window.data2);
slouken@1914
   834
            break;
slouken@1914
   835
        case SDL_WINDOWEVENT_MINIMIZED:
slouken@1914
   836
            fprintf(stderr, "Window %d minimized", event->window.windowID);
slouken@1914
   837
            break;
slouken@1914
   838
        case SDL_WINDOWEVENT_MAXIMIZED:
slouken@1914
   839
            fprintf(stderr, "Window %d maximized", event->window.windowID);
slouken@1914
   840
            break;
slouken@1914
   841
        case SDL_WINDOWEVENT_RESTORED:
slouken@1914
   842
            fprintf(stderr, "Window %d restored", event->window.windowID);
slouken@1914
   843
            break;
slouken@1914
   844
        case SDL_WINDOWEVENT_ENTER:
slouken@1914
   845
            fprintf(stderr, "Mouse entered window %d",
slouken@1914
   846
                    event->window.windowID);
slouken@1914
   847
            break;
slouken@1914
   848
        case SDL_WINDOWEVENT_LEAVE:
slouken@1914
   849
            fprintf(stderr, "Mouse left window %d", event->window.windowID);
slouken@1914
   850
            break;
slouken@1914
   851
        case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1914
   852
            fprintf(stderr, "Window %d gained keyboard focus",
slouken@1914
   853
                    event->window.windowID);
slouken@1914
   854
            break;
slouken@1914
   855
        case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1914
   856
            fprintf(stderr, "Window %d lost keyboard focus",
slouken@1914
   857
                    event->window.windowID);
slouken@1914
   858
            break;
slouken@1933
   859
        case SDL_WINDOWEVENT_CLOSE:
slouken@1933
   860
            fprintf(stderr, "Window %d closed", event->window.windowID);
slouken@1933
   861
            break;
slouken@1914
   862
        default:
slouken@1914
   863
            fprintf(stderr, "Window %d got unknown event %d",
slouken@1914
   864
                    event->window.windowID, event->window.event);
slouken@1914
   865
            break;
slouken@1914
   866
        }
slouken@1914
   867
        break;
slouken@1914
   868
    case SDL_KEYDOWN:
slouken@2268
   869
        fprintf(stderr,
slouken@4465
   870
                "Keyboard: key pressed  in window %d: scancode 0x%08X = %s, keycode 0x%08X = %s",
slouken@4465
   871
                event->key.windowID,
slouken@2306
   872
                event->key.keysym.scancode,
slouken@2306
   873
                SDL_GetScancodeName(event->key.keysym.scancode),
slouken@2306
   874
                event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym));
slouken@1914
   875
        break;
slouken@1914
   876
    case SDL_KEYUP:
slouken@2268
   877
        fprintf(stderr,
slouken@4465
   878
                "Keyboard: key released in window %d: scancode 0x%08X = %s, keycode 0x%08X = %s",
slouken@4465
   879
                event->key.windowID,
slouken@2306
   880
                event->key.keysym.scancode,
slouken@2306
   881
                SDL_GetScancodeName(event->key.keysym.scancode),
slouken@2306
   882
                event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym));
slouken@1914
   883
        break;
slouken@1914
   884
    case SDL_TEXTINPUT:
slouken@4465
   885
        fprintf(stderr, "Keyboard: text input \"%s\" in window %d",
slouken@4465
   886
                event->text.text, event->text.windowID);
slouken@1914
   887
        break;
slouken@1914
   888
    case SDL_MOUSEMOTION:
slouken@4465
   889
        fprintf(stderr, "Mouse: moved to %d,%d (%d,%d) in window %d",
slouken@4465
   890
                event->motion.x, event->motion.y,
slouken@1914
   891
                event->motion.xrel, event->motion.yrel,
slouken@1914
   892
                event->motion.windowID);
slouken@1914
   893
        break;
slouken@1914
   894
    case SDL_MOUSEBUTTONDOWN:
slouken@4465
   895
        fprintf(stderr, "Mouse: button %d pressed at %d,%d in window %d",
slouken@4465
   896
                event->button.button, event->button.x, event->button.y,
slouken@4465
   897
                event->button.windowID);
slouken@1914
   898
        break;
slouken@1914
   899
    case SDL_MOUSEBUTTONUP:
slouken@4465
   900
        fprintf(stderr, "Mouse: button %d released at %d,%d in window %d",
slouken@4465
   901
                event->button.button, event->button.x, event->button.y,
slouken@4465
   902
                event->button.windowID);
slouken@1914
   903
        break;
slouken@1914
   904
    case SDL_MOUSEWHEEL:
slouken@2174
   905
        fprintf(stderr,
slouken@4465
   906
                "Mouse: wheel scrolled %d in x and %d in y in window %d",
slouken@4465
   907
                event->wheel.x, event->wheel.y, event->wheel.windowID);
slouken@1914
   908
        break;
slouken@1914
   909
    case SDL_JOYBALLMOTION:
slouken@1914
   910
        fprintf(stderr, "Joystick %d: ball %d moved by %d,%d",
slouken@1914
   911
                event->jball.which, event->jball.ball, event->jball.xrel,
slouken@1914
   912
                event->jball.yrel);
slouken@1914
   913
        break;
slouken@1914
   914
    case SDL_JOYHATMOTION:
slouken@1914
   915
        fprintf(stderr, "Joystick %d: hat %d moved to ", event->jhat.which,
slouken@1914
   916
                event->jhat.hat);
slouken@1914
   917
        switch (event->jhat.value) {
slouken@1914
   918
        case SDL_HAT_CENTERED:
slouken@1914
   919
            fprintf(stderr, "CENTER");
slouken@1914
   920
            break;
slouken@1914
   921
        case SDL_HAT_UP:
slouken@1914
   922
            fprintf(stderr, "UP");
slouken@1914
   923
            break;
slouken@1914
   924
        case SDL_HAT_RIGHTUP:
slouken@1914
   925
            fprintf(stderr, "RIGHTUP");
slouken@1914
   926
            break;
slouken@1914
   927
        case SDL_HAT_RIGHT:
slouken@1914
   928
            fprintf(stderr, "RIGHT");
slouken@1914
   929
            break;
slouken@1914
   930
        case SDL_HAT_RIGHTDOWN:
slouken@1914
   931
            fprintf(stderr, "RIGHTDOWN");
slouken@1914
   932
            break;
slouken@1914
   933
        case SDL_HAT_DOWN:
slouken@1914
   934
            fprintf(stderr, "DOWN");
slouken@1914
   935
            break;
slouken@1914
   936
        case SDL_HAT_LEFTDOWN:
slouken@1914
   937
            fprintf(stderr, "LEFTDOWN");
slouken@1914
   938
            break;
slouken@1914
   939
        case SDL_HAT_LEFT:
slouken@1914
   940
            fprintf(stderr, "LEFT");
slouken@1914
   941
            break;
slouken@1914
   942
        case SDL_HAT_LEFTUP:
slouken@1914
   943
            fprintf(stderr, "LEFTUP");
slouken@1914
   944
            break;
slouken@1914
   945
        default:
slouken@1914
   946
            fprintf(stderr, "UNKNOWN");
slouken@1914
   947
            break;
slouken@1914
   948
        }
slouken@1914
   949
        break;
slouken@1914
   950
    case SDL_JOYBUTTONDOWN:
slouken@1914
   951
        fprintf(stderr, "Joystick %d: button %d pressed",
slouken@1914
   952
                event->jbutton.which, event->jbutton.button);
slouken@1914
   953
        break;
slouken@1914
   954
    case SDL_JOYBUTTONUP:
slouken@1914
   955
        fprintf(stderr, "Joystick %d: button %d released",
slouken@1914
   956
                event->jbutton.which, event->jbutton.button);
slouken@1914
   957
        break;
slouken@4503
   958
    case SDL_CLIPBOARDUPDATE:
slouken@4503
   959
        fprintf(stderr, "Clipboard updated");
slouken@4503
   960
        break;
slouken@1914
   961
    case SDL_QUIT:
slouken@1914
   962
        fprintf(stderr, "Quit requested");
slouken@1914
   963
        break;
slouken@1914
   964
    case SDL_USEREVENT:
slouken@1914
   965
        fprintf(stderr, "User event %d", event->user.code);
slouken@1914
   966
        break;
slouken@1914
   967
    default:
slouken@1914
   968
        fprintf(stderr, "Unknown event %d", event->type);
slouken@1914
   969
        break;
slouken@1914
   970
    }
slouken@1914
   971
    fprintf(stderr, "\n");
slouken@1914
   972
}
slouken@1914
   973
slouken@1914
   974
void
slouken@1914
   975
CommonEvent(CommonState * state, SDL_Event * event, int *done)
slouken@1914
   976
{
slouken@3514
   977
    int i;
slouken@3514
   978
slouken@1914
   979
    if (state->verbose & VERBOSE_EVENT) {
slouken@1914
   980
        PrintEvent(event);
slouken@1914
   981
    }
slouken@1914
   982
slouken@1914
   983
    switch (event->type) {
slouken@1914
   984
    case SDL_WINDOWEVENT:
slouken@1914
   985
        switch (event->window.event) {
slouken@1914
   986
        case SDL_WINDOWEVENT_CLOSE:
krogoway@5113
   987
			{
krogoway@5113
   988
				SDL_Window *pWindow = SDL_GetWindowFromID(event->window.windowID);
krogoway@5113
   989
				if ( pWindow ) {
krogoway@5113
   990
					SDL_DestroyWindow( pWindow );
krogoway@5113
   991
				}
krogoway@5113
   992
			}
slouken@1914
   993
            break;
slouken@1914
   994
        }
slouken@1914
   995
        break;
slouken@1914
   996
    case SDL_KEYDOWN:
slouken@1914
   997
        switch (event->key.keysym.sym) {
slouken@1914
   998
            /* Add hotkeys here */
slouken@4497
   999
        case SDLK_c:
slouken@4497
  1000
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@4497
  1001
                /* Ctrl-C copy awesome text! */
slouken@4497
  1002
                SDL_SetClipboardText("SDL rocks!\nYou know it!");
slouken@4497
  1003
                printf("Copied text to clipboard\n");
slouken@4497
  1004
            }
slouken@4497
  1005
            break;
slouken@4497
  1006
        case SDLK_v:
slouken@4497
  1007
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@4497
  1008
                /* Ctrl-V paste awesome text! */
slouken@4497
  1009
                char *text = SDL_GetClipboardText();
slouken@4497
  1010
                if (*text) {
slouken@4497
  1011
                    printf("Clipboard: %s\n", text);
slouken@4497
  1012
                } else {
slouken@4497
  1013
                    printf("Clipboard is empty\n");
slouken@4497
  1014
                }
slouken@4497
  1015
                SDL_free(text);
slouken@4497
  1016
            }
slouken@4497
  1017
            break;
slouken@1965
  1018
        case SDLK_g:
slouken@1965
  1019
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@1965
  1020
                /* Ctrl-G toggle grab */
slouken@5349
  1021
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
slouken@5349
  1022
                if (window) {
slouken@5349
  1023
                    SDL_SetWindowGrab(window, !SDL_GetWindowGrab(window));
slouken@5349
  1024
                }
slouken@1965
  1025
            }
slouken@1965
  1026
            break;
slouken@4517
  1027
        case SDLK_m:
slouken@4517
  1028
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@4517
  1029
                /* Ctrl-M maximize */
slouken@5349
  1030
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
slouken@5349
  1031
                if (window) {
slouken@5349
  1032
                    Uint32 flags = SDL_GetWindowFlags(window);
slouken@5349
  1033
                    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@5349
  1034
                        SDL_RestoreWindow(window);
slouken@5349
  1035
                    } else {
slouken@5349
  1036
                        SDL_MaximizeWindow(window);
slouken@4517
  1037
                    }
slouken@4517
  1038
                }
slouken@4517
  1039
            }
slouken@4517
  1040
            break;
slouken@3514
  1041
        case SDLK_z:
slouken@3514
  1042
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@3514
  1043
                /* Ctrl-Z minimize */
slouken@5349
  1044
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
slouken@5349
  1045
                if (window) {
slouken@5349
  1046
                    SDL_MinimizeWindow(window);
slouken@5246
  1047
                }
slouken@5246
  1048
            }
slouken@5246
  1049
            break;
slouken@5246
  1050
        case SDLK_RETURN:
slouken@5246
  1051
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@5246
  1052
                /* Ctrl-Enter toggle fullscreen */
slouken@5349
  1053
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
slouken@5349
  1054
                if (window) {
slouken@5349
  1055
                    Uint32 flags = SDL_GetWindowFlags(window);
slouken@5349
  1056
                    if (flags & SDL_WINDOW_FULLSCREEN) {
slouken@5349
  1057
                        SDL_SetWindowFullscreen(window, SDL_FALSE);
slouken@5349
  1058
                    } else {
slouken@5349
  1059
                        SDL_SetWindowFullscreen(window, SDL_TRUE);
slouken@5246
  1060
                    }
slouken@3514
  1061
                }
slouken@3514
  1062
            }
slouken@3514
  1063
            break;
slouken@1914
  1064
        case SDLK_ESCAPE:
slouken@1914
  1065
            *done = 1;
slouken@1914
  1066
            break;
slouken@1914
  1067
        default:
slouken@1914
  1068
            break;
slouken@1914
  1069
        }
slouken@1914
  1070
        break;
slouken@1914
  1071
    case SDL_QUIT:
slouken@1914
  1072
        *done = 1;
slouken@1914
  1073
        break;
slouken@1914
  1074
    }
slouken@1914
  1075
}
slouken@1914
  1076
slouken@1914
  1077
void
slouken@1914
  1078
CommonQuit(CommonState * state)
slouken@1914
  1079
{
slouken@5147
  1080
    int i;
slouken@5147
  1081
slouken@5147
  1082
    if (state->windows) {
slouken@5147
  1083
        SDL_free(state->windows);
slouken@5147
  1084
    }
slouken@5147
  1085
    if (state->renderers) {
slouken@5147
  1086
        for (i = 0; i < state->num_windows; ++i) {
slouken@5147
  1087
            if (state->renderers[i]) {
slouken@5147
  1088
                SDL_DestroyRenderer(state->renderers[i]);
slouken@5147
  1089
            }
slouken@5147
  1090
        }
slouken@5147
  1091
        SDL_free(state->renderers);
slouken@5147
  1092
    }
slouken@1914
  1093
    if (state->flags & SDL_INIT_VIDEO) {
slouken@1914
  1094
        SDL_VideoQuit();
slouken@1914
  1095
    }
slouken@1914
  1096
    if (state->flags & SDL_INIT_AUDIO) {
slouken@1914
  1097
        SDL_AudioQuit();
slouken@1914
  1098
    }
slouken@1914
  1099
    SDL_free(state);
slouken@1914
  1100
}
slouken@3514
  1101
slouken@3514
  1102
/* vi: set ts=4 sw=4 expandtab: */