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