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