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