test/common.c
author Mike Gorchak <lestat@i.com.ua>
Tue, 13 Oct 2009 11:24:36 +0000
changeset 3395 45f140dd4b08
parent 3373 5dc34052ee6c
child 3408 55541ddf13e3
permissions -rw-r--r--
Options --double --triple have been added to test double and triple buffering.
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 \
lestat@3395
     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] [--double] [--triple]"
slouken@1914
    10
slouken@1914
    11
#define AUDIO_USAGE \
slouken@1914
    12
"[--rate N] [--format U8|S8|U16|U16LE|U16BE|S16|S16LE|S16BE] [--channels N] [--samples N]"
slouken@1914
    13
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
    }
lestat@3395
   236
    if (SDL_strcasecmp(argv[index], "--double") == 0) {
lestat@3395
   237
        state->render_flags |= SDL_RENDERER_PRESENTFLIP2;
lestat@3395
   238
        return 1;
lestat@3395
   239
    }
lestat@3395
   240
    if (SDL_strcasecmp(argv[index], "--triple") == 0) {
lestat@3395
   241
        state->render_flags |= SDL_RENDERER_PRESENTFLIP3;
lestat@3395
   242
        return 1;
lestat@3395
   243
    }
slouken@1914
   244
    if (SDL_strcasecmp(argv[index], "--noframe") == 0) {
slouken@1914
   245
        state->window_flags |= SDL_WINDOW_BORDERLESS;
slouken@1914
   246
        return 1;
slouken@1914
   247
    }
slouken@1914
   248
    if (SDL_strcasecmp(argv[index], "--resize") == 0) {
slouken@1914
   249
        state->window_flags |= SDL_WINDOW_RESIZABLE;
slouken@1914
   250
        return 1;
slouken@1914
   251
    }
slouken@1914
   252
    if (SDL_strcasecmp(argv[index], "--minimize") == 0) {
slouken@1914
   253
        state->window_flags |= SDL_WINDOW_MINIMIZED;
slouken@1914
   254
        return 1;
slouken@1914
   255
    }
slouken@1914
   256
    if (SDL_strcasecmp(argv[index], "--maximize") == 0) {
slouken@1914
   257
        state->window_flags |= SDL_WINDOW_MAXIMIZED;
slouken@1914
   258
        return 1;
slouken@1914
   259
    }
slouken@1914
   260
    if (SDL_strcasecmp(argv[index], "--grab") == 0) {
slouken@1914
   261
        state->window_flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@1914
   262
        return 1;
slouken@1914
   263
    }
slouken@1914
   264
    if (SDL_strcasecmp(argv[index], "--rate") == 0) {
slouken@1914
   265
        ++index;
slouken@1914
   266
        if (!argv[index]) {
slouken@1914
   267
            return -1;
slouken@1914
   268
        }
slouken@1914
   269
        state->audiospec.freq = SDL_atoi(argv[index]);
slouken@1914
   270
        return 2;
slouken@1914
   271
    }
slouken@1914
   272
    if (SDL_strcasecmp(argv[index], "--format") == 0) {
slouken@1914
   273
        ++index;
slouken@1914
   274
        if (!argv[index]) {
slouken@1914
   275
            return -1;
slouken@1914
   276
        }
slouken@1914
   277
        if (SDL_strcasecmp(argv[index], "U8") == 0) {
slouken@1914
   278
            state->audiospec.format = AUDIO_U8;
slouken@1914
   279
            return 2;
slouken@1914
   280
        }
slouken@1914
   281
        if (SDL_strcasecmp(argv[index], "S8") == 0) {
slouken@1914
   282
            state->audiospec.format = AUDIO_S8;
slouken@1914
   283
            return 2;
slouken@1914
   284
        }
slouken@1914
   285
        if (SDL_strcasecmp(argv[index], "U16") == 0) {
slouken@1914
   286
            state->audiospec.format = AUDIO_U16;
slouken@1914
   287
            return 2;
slouken@1914
   288
        }
slouken@1914
   289
        if (SDL_strcasecmp(argv[index], "U16LE") == 0) {
slouken@1914
   290
            state->audiospec.format = AUDIO_U16LSB;
slouken@1914
   291
            return 2;
slouken@1914
   292
        }
slouken@1914
   293
        if (SDL_strcasecmp(argv[index], "U16BE") == 0) {
slouken@1914
   294
            state->audiospec.format = AUDIO_U16MSB;
slouken@1914
   295
            return 2;
slouken@1914
   296
        }
slouken@1914
   297
        if (SDL_strcasecmp(argv[index], "S16") == 0) {
slouken@1914
   298
            state->audiospec.format = AUDIO_S16;
slouken@1914
   299
            return 2;
slouken@1914
   300
        }
slouken@1914
   301
        if (SDL_strcasecmp(argv[index], "S16LE") == 0) {
slouken@1914
   302
            state->audiospec.format = AUDIO_S16LSB;
slouken@1914
   303
            return 2;
slouken@1914
   304
        }
slouken@1914
   305
        if (SDL_strcasecmp(argv[index], "S16BE") == 0) {
slouken@1914
   306
            state->audiospec.format = AUDIO_S16MSB;
slouken@1914
   307
            return 2;
slouken@1914
   308
        }
slouken@1914
   309
        return -1;
slouken@1914
   310
    }
slouken@1914
   311
    if (SDL_strcasecmp(argv[index], "--channels") == 0) {
slouken@1914
   312
        ++index;
slouken@1914
   313
        if (!argv[index]) {
slouken@1914
   314
            return -1;
slouken@1914
   315
        }
bob@2185
   316
        state->audiospec.channels = (Uint8) SDL_atoi(argv[index]);
slouken@1914
   317
        return 2;
slouken@1914
   318
    }
slouken@1914
   319
    if (SDL_strcasecmp(argv[index], "--samples") == 0) {
slouken@1914
   320
        ++index;
slouken@1914
   321
        if (!argv[index]) {
slouken@1914
   322
            return -1;
slouken@1914
   323
        }
bob@2185
   324
        state->audiospec.samples = (Uint16) SDL_atoi(argv[index]);
slouken@1914
   325
        return 2;
slouken@1914
   326
    }
slouken@1914
   327
    if ((SDL_strcasecmp(argv[index], "-h") == 0)
slouken@1914
   328
        || (SDL_strcasecmp(argv[index], "--help") == 0)) {
slouken@1914
   329
        /* Print the usage message */
slouken@1914
   330
        return -1;
slouken@1914
   331
    }
slouken@1914
   332
    return 0;
slouken@1914
   333
}
slouken@1914
   334
slouken@1914
   335
const char *
slouken@1914
   336
CommonUsage(CommonState * state)
slouken@1914
   337
{
slouken@1914
   338
    switch (state->flags & (SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
slouken@1914
   339
    case SDL_INIT_VIDEO:
slouken@1914
   340
        return VIDEO_USAGE;
slouken@1914
   341
    case SDL_INIT_AUDIO:
slouken@1914
   342
        return AUDIO_USAGE;
slouken@1914
   343
    case (SDL_INIT_VIDEO | SDL_INIT_AUDIO):
slouken@1914
   344
        return VIDEO_USAGE " " AUDIO_USAGE;
slouken@1914
   345
    default:
slouken@1914
   346
        return "";
slouken@1914
   347
    }
slouken@1914
   348
}
slouken@1914
   349
slouken@1914
   350
static void
slouken@1914
   351
PrintRendererFlag(Uint32 flag)
slouken@1914
   352
{
slouken@1914
   353
    switch (flag) {
slouken@1965
   354
    case SDL_RENDERER_SINGLEBUFFER:
slouken@1914
   355
        fprintf(stderr, "SingleBuffer");
slouken@1914
   356
        break;
slouken@1965
   357
    case SDL_RENDERER_PRESENTCOPY:
slouken@1914
   358
        fprintf(stderr, "PresentCopy");
slouken@1914
   359
        break;
slouken@1965
   360
    case SDL_RENDERER_PRESENTFLIP2:
slouken@1914
   361
        fprintf(stderr, "PresentFlip2");
slouken@1914
   362
        break;
slouken@1965
   363
    case SDL_RENDERER_PRESENTFLIP3:
slouken@1914
   364
        fprintf(stderr, "PresentFlip3");
slouken@1914
   365
        break;
slouken@1965
   366
    case SDL_RENDERER_PRESENTDISCARD:
slouken@1914
   367
        fprintf(stderr, "PresentDiscard");
slouken@1914
   368
        break;
slouken@1965
   369
    case SDL_RENDERER_PRESENTVSYNC:
slouken@1914
   370
        fprintf(stderr, "PresentVSync");
slouken@1914
   371
        break;
slouken@1965
   372
    case SDL_RENDERER_ACCELERATED:
slouken@1921
   373
        fprintf(stderr, "Accelerated");
slouken@1921
   374
        break;
slouken@1914
   375
    default:
slouken@1914
   376
        fprintf(stderr, "0x%8.8x", flag);
slouken@1914
   377
        break;
slouken@1914
   378
    }
slouken@1914
   379
}
slouken@1914
   380
slouken@1914
   381
static void
slouken@1914
   382
PrintBlendMode(Uint32 flag)
slouken@1914
   383
{
slouken@1914
   384
    switch (flag) {
slouken@2884
   385
    case SDL_BLENDMODE_NONE:
slouken@1914
   386
        fprintf(stderr, "None");
slouken@1914
   387
        break;
slouken@2884
   388
    case SDL_BLENDMODE_MASK:
slouken@1914
   389
        fprintf(stderr, "Mask");
slouken@1914
   390
        break;
slouken@2884
   391
    case SDL_BLENDMODE_BLEND:
slouken@1914
   392
        fprintf(stderr, "Blend");
slouken@1914
   393
        break;
slouken@2884
   394
    case SDL_BLENDMODE_ADD:
slouken@1914
   395
        fprintf(stderr, "Add");
slouken@1914
   396
        break;
slouken@2884
   397
    case SDL_BLENDMODE_MOD:
slouken@1914
   398
        fprintf(stderr, "Mod");
slouken@1914
   399
        break;
slouken@1914
   400
    default:
slouken@1914
   401
        fprintf(stderr, "0x%8.8x", flag);
slouken@1914
   402
        break;
slouken@1914
   403
    }
slouken@1914
   404
}
slouken@1914
   405
slouken@1914
   406
static void
slouken@1914
   407
PrintScaleMode(Uint32 flag)
slouken@1914
   408
{
slouken@1914
   409
    switch (flag) {
slouken@1965
   410
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1914
   411
        fprintf(stderr, "None");
slouken@1914
   412
        break;
slouken@1965
   413
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1914
   414
        fprintf(stderr, "Fast");
slouken@1914
   415
        break;
slouken@1965
   416
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1914
   417
        fprintf(stderr, "Slow");
slouken@1914
   418
        break;
slouken@1965
   419
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1914
   420
        fprintf(stderr, "Best");
slouken@1914
   421
        break;
slouken@1914
   422
    default:
slouken@1914
   423
        fprintf(stderr, "0x%8.8x", flag);
slouken@1914
   424
        break;
slouken@1914
   425
    }
slouken@1914
   426
}
slouken@1914
   427
slouken@1914
   428
static void
slouken@1914
   429
PrintPixelFormat(Uint32 format)
slouken@1914
   430
{
slouken@1914
   431
    switch (format) {
slouken@1965
   432
    case SDL_PIXELFORMAT_UNKNOWN:
slouken@1914
   433
        fprintf(stderr, "Unknwon");
slouken@1914
   434
        break;
slouken@1965
   435
    case SDL_PIXELFORMAT_INDEX1LSB:
slouken@1914
   436
        fprintf(stderr, "Index1LSB");
slouken@1914
   437
        break;
slouken@1965
   438
    case SDL_PIXELFORMAT_INDEX1MSB:
slouken@1914
   439
        fprintf(stderr, "Index1MSB");
slouken@1914
   440
        break;
slouken@1965
   441
    case SDL_PIXELFORMAT_INDEX4LSB:
slouken@1914
   442
        fprintf(stderr, "Index4LSB");
slouken@1914
   443
        break;
slouken@1965
   444
    case SDL_PIXELFORMAT_INDEX4MSB:
slouken@1914
   445
        fprintf(stderr, "Index4MSB");
slouken@1914
   446
        break;
slouken@1965
   447
    case SDL_PIXELFORMAT_INDEX8:
slouken@1914
   448
        fprintf(stderr, "Index8");
slouken@1914
   449
        break;
slouken@1965
   450
    case SDL_PIXELFORMAT_RGB332:
slouken@1914
   451
        fprintf(stderr, "RGB332");
slouken@1914
   452
        break;
slouken@1965
   453
    case SDL_PIXELFORMAT_RGB444:
slouken@1914
   454
        fprintf(stderr, "RGB444");
slouken@1914
   455
        break;
slouken@1965
   456
    case SDL_PIXELFORMAT_RGB555:
slouken@1914
   457
        fprintf(stderr, "RGB555");
slouken@1914
   458
        break;
slouken@1965
   459
    case SDL_PIXELFORMAT_ARGB4444:
slouken@1914
   460
        fprintf(stderr, "ARGB4444");
slouken@1914
   461
        break;
lestat@3184
   462
    case SDL_PIXELFORMAT_ABGR4444:
lestat@3184
   463
        fprintf(stderr, "ABGR4444");
lestat@3184
   464
        break;
slouken@1965
   465
    case SDL_PIXELFORMAT_ARGB1555:
slouken@1914
   466
        fprintf(stderr, "ARGB1555");
slouken@1914
   467
        break;
lestat@3184
   468
    case SDL_PIXELFORMAT_ABGR1555:
lestat@3184
   469
        fprintf(stderr, "ABGR1555");
lestat@3184
   470
        break;
slouken@1965
   471
    case SDL_PIXELFORMAT_RGB565:
slouken@1914
   472
        fprintf(stderr, "RGB565");
slouken@1914
   473
        break;
lestat@3184
   474
    case SDL_PIXELFORMAT_BGR565:
lestat@3184
   475
        fprintf(stderr, "BGR565");
lestat@3184
   476
        break;
slouken@1965
   477
    case SDL_PIXELFORMAT_RGB24:
slouken@1914
   478
        fprintf(stderr, "RGB24");
slouken@1914
   479
        break;
slouken@1965
   480
    case SDL_PIXELFORMAT_BGR24:
slouken@1914
   481
        fprintf(stderr, "BGR24");
slouken@1914
   482
        break;
slouken@1965
   483
    case SDL_PIXELFORMAT_RGB888:
slouken@1914
   484
        fprintf(stderr, "RGB888");
slouken@1914
   485
        break;
slouken@1965
   486
    case SDL_PIXELFORMAT_BGR888:
slouken@1914
   487
        fprintf(stderr, "BGR888");
slouken@1914
   488
        break;
slouken@1965
   489
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1914
   490
        fprintf(stderr, "ARGB8888");
slouken@1914
   491
        break;
slouken@1965
   492
    case SDL_PIXELFORMAT_RGBA8888:
slouken@1914
   493
        fprintf(stderr, "RGBA8888");
slouken@1914
   494
        break;
slouken@1965
   495
    case SDL_PIXELFORMAT_ABGR8888:
slouken@1914
   496
        fprintf(stderr, "ABGR8888");
slouken@1914
   497
        break;
slouken@1965
   498
    case SDL_PIXELFORMAT_BGRA8888:
slouken@1914
   499
        fprintf(stderr, "BGRA8888");
slouken@1914
   500
        break;
slouken@1965
   501
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@1914
   502
        fprintf(stderr, "ARGB2101010");
slouken@1914
   503
        break;
slouken@1965
   504
    case SDL_PIXELFORMAT_YV12:
slouken@1914
   505
        fprintf(stderr, "YV12");
slouken@1914
   506
        break;
slouken@1965
   507
    case SDL_PIXELFORMAT_IYUV:
slouken@1914
   508
        fprintf(stderr, "IYUV");
slouken@1914
   509
        break;
slouken@1965
   510
    case SDL_PIXELFORMAT_YUY2:
slouken@1914
   511
        fprintf(stderr, "YUY2");
slouken@1914
   512
        break;
slouken@1965
   513
    case SDL_PIXELFORMAT_UYVY:
slouken@1914
   514
        fprintf(stderr, "UYVY");
slouken@1914
   515
        break;
slouken@1965
   516
    case SDL_PIXELFORMAT_YVYU:
slouken@1914
   517
        fprintf(stderr, "YVYU");
slouken@1914
   518
        break;
slouken@1914
   519
    default:
slouken@1914
   520
        fprintf(stderr, "0x%8.8x", format);
slouken@1914
   521
        break;
slouken@1914
   522
    }
slouken@1914
   523
}
slouken@1914
   524
slouken@1914
   525
static void
slouken@1914
   526
PrintRenderer(SDL_RendererInfo * info)
slouken@1914
   527
{
slouken@1914
   528
    int i, count;
slouken@1914
   529
slouken@1914
   530
    fprintf(stderr, "  Renderer %s:\n", info->name);
slouken@1914
   531
slouken@1914
   532
    fprintf(stderr, "    Flags: 0x%8.8X", info->flags);
slouken@1914
   533
    fprintf(stderr, " (");
slouken@1914
   534
    count = 0;
slouken@1914
   535
    for (i = 0; i < sizeof(info->flags) * 8; ++i) {
slouken@1914
   536
        Uint32 flag = (1 << i);
slouken@1914
   537
        if (info->flags & flag) {
slouken@1914
   538
            if (count > 0) {
slouken@1914
   539
                fprintf(stderr, " | ");
slouken@1914
   540
            }
slouken@1914
   541
            PrintRendererFlag(flag);
slouken@1914
   542
            ++count;
slouken@1914
   543
        }
slouken@1914
   544
    }
slouken@1914
   545
    fprintf(stderr, ")\n");
slouken@1914
   546
slouken@1914
   547
    fprintf(stderr, "    Blend: 0x%8.8X", info->blend_modes);
slouken@1914
   548
    fprintf(stderr, " (");
slouken@1914
   549
    count = 0;
slouken@1914
   550
    for (i = 0; i < sizeof(info->blend_modes) * 8; ++i) {
slouken@1914
   551
        Uint32 flag = (1 << i);
slouken@1914
   552
        if (info->blend_modes & flag) {
slouken@1914
   553
            if (count > 0) {
slouken@1914
   554
                fprintf(stderr, " | ");
slouken@1914
   555
            }
slouken@1914
   556
            PrintBlendMode(flag);
slouken@1914
   557
            ++count;
slouken@1914
   558
        }
slouken@1914
   559
    }
slouken@1914
   560
    fprintf(stderr, ")\n");
slouken@1914
   561
slouken@1914
   562
    fprintf(stderr, "    Scale: 0x%8.8X", info->scale_modes);
slouken@1914
   563
    fprintf(stderr, " (");
slouken@1914
   564
    count = 0;
slouken@1914
   565
    for (i = 0; i < sizeof(info->scale_modes) * 8; ++i) {
slouken@1914
   566
        Uint32 flag = (1 << i);
slouken@1914
   567
        if (info->scale_modes & flag) {
slouken@1914
   568
            if (count > 0) {
slouken@1914
   569
                fprintf(stderr, " | ");
slouken@1914
   570
            }
slouken@1914
   571
            PrintScaleMode(flag);
slouken@1914
   572
            ++count;
slouken@1914
   573
        }
slouken@1914
   574
    }
slouken@1914
   575
    fprintf(stderr, ")\n");
slouken@1914
   576
slouken@1914
   577
    fprintf(stderr, "    Texture formats (%d): ", info->num_texture_formats);
bob@2185
   578
    for (i = 0; i < (int) info->num_texture_formats; ++i) {
slouken@1914
   579
        if (i > 0) {
slouken@1914
   580
            fprintf(stderr, ", ");
slouken@1914
   581
        }
slouken@1914
   582
        PrintPixelFormat(info->texture_formats[i]);
slouken@1914
   583
    }
slouken@1914
   584
    fprintf(stderr, "\n");
slouken@1914
   585
slouken@1914
   586
    if (info->max_texture_width || info->max_texture_height) {
slouken@1914
   587
        fprintf(stderr, "    Max Texture Size: %dx%d\n",
slouken@1914
   588
                info->max_texture_width, info->max_texture_height);
slouken@1914
   589
    }
slouken@1914
   590
}
slouken@1914
   591
slouken@1914
   592
SDL_bool
slouken@1914
   593
CommonInit(CommonState * state)
slouken@1914
   594
{
slouken@1914
   595
    int i, j, m, n;
slouken@1914
   596
    SDL_DisplayMode fullscreen_mode;
slouken@1914
   597
slouken@1914
   598
    if (state->flags & SDL_INIT_VIDEO) {
slouken@1914
   599
        if (state->verbose & VERBOSE_VIDEO) {
slouken@1914
   600
            n = SDL_GetNumVideoDrivers();
slouken@1914
   601
            if (n == 0) {
slouken@1914
   602
                fprintf(stderr, "No built-in video drivers\n");
slouken@1914
   603
            } else {
slouken@1914
   604
                fprintf(stderr, "Built-in video drivers:");
slouken@1914
   605
                for (i = 0; i < n; ++i) {
slouken@1914
   606
                    if (i > 0) {
slouken@1914
   607
                        fprintf(stderr, ",");
slouken@1914
   608
                    }
slouken@1914
   609
                    fprintf(stderr, " %s", SDL_GetVideoDriver(i));
slouken@1914
   610
                }
slouken@1914
   611
                fprintf(stderr, "\n");
slouken@1914
   612
            }
slouken@1914
   613
        }
slouken@1914
   614
        if (SDL_VideoInit(state->videodriver, 0) < 0) {
slouken@1914
   615
            fprintf(stderr, "Couldn't initialize video driver: %s\n",
slouken@1914
   616
                    SDL_GetError());
slouken@1914
   617
            return SDL_FALSE;
slouken@1914
   618
        }
slouken@1914
   619
        if (state->verbose & VERBOSE_VIDEO) {
slouken@1914
   620
            fprintf(stderr, "Video driver: %s\n",
slouken@1914
   621
                    SDL_GetCurrentVideoDriver());
slouken@1914
   622
        }
slouken@1914
   623
slouken@1914
   624
        if (state->verbose & VERBOSE_MODES) {
slouken@1967
   625
            SDL_DisplayMode mode;
slouken@1914
   626
            int bpp;
slouken@1914
   627
            Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1914
   628
slouken@1914
   629
            n = SDL_GetNumVideoDisplays();
slouken@1914
   630
            fprintf(stderr, "Number of displays: %d\n", n);
slouken@1914
   631
            for (i = 0; i < n; ++i) {
slouken@1914
   632
                fprintf(stderr, "Display %d:\n", i);
slouken@1914
   633
                SDL_SelectVideoDisplay(i);
slouken@1914
   634
slouken@1967
   635
                SDL_GetDesktopDisplayMode(&mode);
slouken@1967
   636
                SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask,
slouken@1914
   637
                                           &Bmask, &Amask);
slouken@1914
   638
                fprintf(stderr,
lestat@3184
   639
                        "  Current mode: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
lestat@3184
   640
                        mode.w, mode.h, mode.refresh_rate, bpp,
lestat@3184
   641
                        PixelFormatToString(mode.format));
slouken@1914
   642
                if (Rmask || Gmask || Bmask) {
lestat@3184
   643
                    fprintf(stderr, "      Red Mask   = 0x%.8x\n", Rmask);
slouken@1914
   644
                    fprintf(stderr, "      Green Mask = 0x%.8x\n", Gmask);
lestat@3184
   645
                    fprintf(stderr, "      Blue Mask  = 0x%.8x\n", Bmask);
slouken@1914
   646
                    if (Amask)
slouken@1914
   647
                        fprintf(stderr, "      Alpha Mask = 0x%.8x\n", Amask);
slouken@1914
   648
                }
slouken@1914
   649
slouken@1914
   650
                /* Print available fullscreen video modes */
slouken@1914
   651
                m = SDL_GetNumDisplayModes();
slouken@1914
   652
                if (m == 0) {
slouken@1914
   653
                    fprintf(stderr, "No available fullscreen video modes\n");
slouken@1914
   654
                } else {
slouken@1914
   655
                    fprintf(stderr, "  Fullscreen video modes:\n");
slouken@1914
   656
                    for (j = 0; j < m; ++j) {
slouken@1967
   657
                        SDL_GetDisplayMode(j, &mode);
slouken@1967
   658
                        SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask,
slouken@1914
   659
                                                   &Gmask, &Bmask, &Amask);
slouken@1914
   660
                        fprintf(stderr,
lestat@3184
   661
                                "    Mode %d: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
lestat@3184
   662
                                j, mode.w, mode.h, mode.refresh_rate, bpp,
lestat@3184
   663
                                PixelFormatToString(mode.format));
slouken@1914
   664
                        if (Rmask || Gmask || Bmask) {
lestat@3184
   665
                            fprintf(stderr, "        Red Mask   = 0x%.8x\n",
slouken@1914
   666
                                    Rmask);
slouken@1914
   667
                            fprintf(stderr, "        Green Mask = 0x%.8x\n",
slouken@1914
   668
                                    Gmask);
lestat@3184
   669
                            fprintf(stderr, "        Blue Mask  = 0x%.8x\n",
slouken@1914
   670
                                    Bmask);
slouken@1914
   671
                            if (Amask)
slouken@1914
   672
                                fprintf(stderr,
slouken@1914
   673
                                        "        Alpha Mask = 0x%.8x\n",
slouken@1914
   674
                                        Amask);
slouken@1914
   675
                        }
slouken@1914
   676
                    }
slouken@1914
   677
                }
slouken@1914
   678
            }
slouken@1914
   679
        }
slouken@1914
   680
slouken@1914
   681
        SDL_SelectVideoDisplay(state->display);
slouken@1914
   682
        if (state->verbose & VERBOSE_RENDER) {
slouken@1914
   683
            SDL_RendererInfo info;
slouken@1914
   684
slouken@1969
   685
            n = SDL_GetNumRenderDrivers();
slouken@1914
   686
            if (n == 0) {
slouken@1914
   687
                fprintf(stderr, "No built-in render drivers\n");
slouken@1914
   688
            } else {
slouken@1923
   689
                fprintf(stderr, "Built-in render drivers:\n");
slouken@1914
   690
                for (i = 0; i < n; ++i) {
slouken@1969
   691
                    SDL_GetRenderDriverInfo(i, &info);
slouken@1914
   692
                    PrintRenderer(&info);
slouken@1914
   693
                }
slouken@1914
   694
            }
slouken@1914
   695
        }
slouken@1914
   696
slouken@1914
   697
        switch (state->depth) {
slouken@1914
   698
        case 8:
slouken@1965
   699
            fullscreen_mode.format = SDL_PIXELFORMAT_INDEX8;
slouken@1914
   700
            break;
slouken@1914
   701
        case 15:
slouken@1965
   702
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB555;
slouken@1914
   703
            break;
slouken@1914
   704
        case 16:
slouken@1965
   705
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB565;
slouken@1914
   706
            break;
lestat@3184
   707
        case 24:
lestat@3184
   708
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB24;
lestat@3184
   709
            break;
slouken@1914
   710
        default:
slouken@1965
   711
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB888;
slouken@1914
   712
            break;
slouken@1914
   713
        }
slouken@1914
   714
        fullscreen_mode.w = state->window_w;
slouken@1914
   715
        fullscreen_mode.h = state->window_h;
slouken@1914
   716
        fullscreen_mode.refresh_rate = state->refresh_rate;
lestat@3194
   717
        if (SDL_SetFullscreenDisplayMode(&fullscreen_mode)<0) {
lestat@3184
   718
            fprintf(stderr, "Can't switch to fullscreen display mode: %s\n",
lestat@3184
   719
                    SDL_GetError());
lestat@3184
   720
            return SDL_FALSE;
lestat@3184
   721
        }
slouken@1914
   722
slouken@1914
   723
        state->windows =
slouken@1914
   724
            (SDL_WindowID *) SDL_malloc(state->num_windows *
slouken@1914
   725
                                        sizeof(*state->windows));
slouken@1914
   726
        if (!state->windows) {
slouken@1914
   727
            fprintf(stderr, "Out of memory!\n");
slouken@1914
   728
            return SDL_FALSE;
slouken@1914
   729
        }
slouken@1914
   730
        for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   731
            char title[1024];
slouken@1914
   732
slouken@1914
   733
            if (state->num_windows > 1) {
slouken@1914
   734
                SDL_snprintf(title, SDL_arraysize(title), "%s %d",
slouken@1914
   735
                             state->window_title, i + 1);
slouken@1914
   736
            } else {
slouken@1914
   737
                SDL_strlcpy(title, state->window_title, SDL_arraysize(title));
slouken@1914
   738
            }
slouken@1914
   739
            state->windows[i] =
slouken@1914
   740
                SDL_CreateWindow(title, state->window_x, state->window_y,
slouken@1914
   741
                                 state->window_w, state->window_h,
slouken@1914
   742
                                 state->window_flags);
slouken@1914
   743
            if (!state->windows[i]) {
slouken@1914
   744
                fprintf(stderr, "Couldn't create window: %s\n",
slouken@1914
   745
                        SDL_GetError());
slouken@1914
   746
                return SDL_FALSE;
slouken@1914
   747
            }
slouken@1914
   748
slouken@1933
   749
            if (!state->skip_renderer
slouken@1933
   750
                && (state->renderdriver
slouken@1933
   751
                    || !(state->window_flags & SDL_WINDOW_OPENGL))) {
slouken@1914
   752
                m = -1;
slouken@1914
   753
                if (state->renderdriver) {
slouken@1914
   754
                    SDL_RendererInfo info;
slouken@1969
   755
                    n = SDL_GetNumRenderDrivers();
slouken@1914
   756
                    for (j = 0; j < n; ++j) {
slouken@1969
   757
                        SDL_GetRenderDriverInfo(j, &info);
slouken@1914
   758
                        if (SDL_strcasecmp(info.name, state->renderdriver) ==
slouken@1914
   759
                            0) {
slouken@1914
   760
                            m = j;
slouken@1914
   761
                            break;
slouken@1914
   762
                        }
slouken@1914
   763
                    }
slouken@1914
   764
                    if (m == n) {
slouken@1914
   765
                        fprintf(stderr,
slouken@1914
   766
                                "Couldn't find render driver named %s",
slouken@1914
   767
                                state->renderdriver);
slouken@1914
   768
                        return SDL_FALSE;
slouken@1914
   769
                    }
slouken@1914
   770
                }
slouken@1914
   771
                if (SDL_CreateRenderer
slouken@1914
   772
                    (state->windows[i], m, state->render_flags) < 0) {
slouken@1914
   773
                    fprintf(stderr, "Couldn't create renderer: %s\n",
slouken@1914
   774
                            SDL_GetError());
slouken@1914
   775
                    return SDL_FALSE;
slouken@1914
   776
                }
slouken@1923
   777
                if (state->verbose & VERBOSE_RENDER) {
slouken@1923
   778
                    SDL_RendererInfo info;
slouken@1923
   779
slouken@1923
   780
                    fprintf(stderr, "Current renderer:\n");
slouken@1969
   781
                    SDL_GetRendererInfo(&info);
slouken@1923
   782
                    PrintRenderer(&info);
slouken@1923
   783
                }
slouken@1914
   784
            }
slouken@1914
   785
        }
slouken@1914
   786
        SDL_SelectRenderer(state->windows[0]);
slouken@1914
   787
    }
slouken@1914
   788
slouken@1914
   789
    if (state->flags & SDL_INIT_AUDIO) {
slouken@1914
   790
        if (state->verbose & VERBOSE_AUDIO) {
slouken@1914
   791
            n = SDL_GetNumAudioDrivers();
slouken@1914
   792
            if (n == 0) {
slouken@1914
   793
                fprintf(stderr, "No built-in audio drivers\n");
slouken@1914
   794
            } else {
slouken@1914
   795
                fprintf(stderr, "Built-in audio drivers:");
slouken@1914
   796
                for (i = 0; i < n; ++i) {
slouken@1914
   797
                    if (i > 0) {
slouken@1914
   798
                        fprintf(stderr, ",");
slouken@1914
   799
                    }
slouken@1914
   800
                    fprintf(stderr, " %s", SDL_GetAudioDriver(i));
slouken@1914
   801
                }
slouken@1914
   802
                fprintf(stderr, "\n");
slouken@1914
   803
            }
slouken@1914
   804
        }
slouken@1914
   805
        if (SDL_AudioInit(state->audiodriver) < 0) {
slouken@1914
   806
            fprintf(stderr, "Couldn't initialize audio driver: %s\n",
slouken@1914
   807
                    SDL_GetError());
slouken@1914
   808
            return SDL_FALSE;
slouken@1914
   809
        }
slouken@1914
   810
        if (state->verbose & VERBOSE_VIDEO) {
slouken@1914
   811
            fprintf(stderr, "Audio driver: %s\n",
slouken@1914
   812
                    SDL_GetCurrentAudioDriver());
slouken@1914
   813
        }
slouken@1914
   814
slouken@1914
   815
        if (SDL_OpenAudio(&state->audiospec, NULL) < 0) {
slouken@1914
   816
            fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@1914
   817
            return SDL_FALSE;
slouken@1914
   818
        }
slouken@1914
   819
    }
slouken@1914
   820
slouken@1914
   821
    return SDL_TRUE;
slouken@1914
   822
}
slouken@1914
   823
slouken@1914
   824
static void
slouken@1914
   825
PrintEvent(SDL_Event * event)
slouken@1914
   826
{
slouken@1914
   827
    fprintf(stderr, "SDL EVENT: ");
slouken@1914
   828
    switch (event->type) {
slouken@1914
   829
    case SDL_WINDOWEVENT:
slouken@1914
   830
        switch (event->window.event) {
slouken@1914
   831
        case SDL_WINDOWEVENT_SHOWN:
slouken@1914
   832
            fprintf(stderr, "Window %d shown", event->window.windowID);
slouken@1914
   833
            break;
slouken@1914
   834
        case SDL_WINDOWEVENT_HIDDEN:
slouken@1914
   835
            fprintf(stderr, "Window %d hidden", event->window.windowID);
slouken@1914
   836
            break;
slouken@1914
   837
        case SDL_WINDOWEVENT_EXPOSED:
slouken@1970
   838
            fprintf(stderr, "Window %d exposed", event->window.windowID);
slouken@1914
   839
            break;
slouken@1914
   840
        case SDL_WINDOWEVENT_MOVED:
slouken@1970
   841
            fprintf(stderr, "Window %d moved to %d,%d",
slouken@1914
   842
                    event->window.windowID, event->window.data1,
slouken@1914
   843
                    event->window.data2);
slouken@1914
   844
            break;
slouken@1914
   845
        case SDL_WINDOWEVENT_RESIZED:
slouken@1970
   846
            fprintf(stderr, "Window %d resized to %dx%d",
slouken@1914
   847
                    event->window.windowID, event->window.data1,
slouken@1914
   848
                    event->window.data2);
slouken@1914
   849
            break;
slouken@1914
   850
        case SDL_WINDOWEVENT_MINIMIZED:
slouken@1914
   851
            fprintf(stderr, "Window %d minimized", event->window.windowID);
slouken@1914
   852
            break;
slouken@1914
   853
        case SDL_WINDOWEVENT_MAXIMIZED:
slouken@1914
   854
            fprintf(stderr, "Window %d maximized", event->window.windowID);
slouken@1914
   855
            break;
slouken@1914
   856
        case SDL_WINDOWEVENT_RESTORED:
slouken@1914
   857
            fprintf(stderr, "Window %d restored", event->window.windowID);
slouken@1914
   858
            break;
slouken@1914
   859
        case SDL_WINDOWEVENT_ENTER:
slouken@1914
   860
            fprintf(stderr, "Mouse entered window %d",
slouken@1914
   861
                    event->window.windowID);
slouken@1914
   862
            break;
slouken@1914
   863
        case SDL_WINDOWEVENT_LEAVE:
slouken@1914
   864
            fprintf(stderr, "Mouse left window %d", event->window.windowID);
slouken@1914
   865
            break;
slouken@1914
   866
        case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1914
   867
            fprintf(stderr, "Window %d gained keyboard focus",
slouken@1914
   868
                    event->window.windowID);
slouken@1914
   869
            break;
slouken@1914
   870
        case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1914
   871
            fprintf(stderr, "Window %d lost keyboard focus",
slouken@1914
   872
                    event->window.windowID);
slouken@1914
   873
            break;
slouken@1933
   874
        case SDL_WINDOWEVENT_CLOSE:
slouken@1933
   875
            fprintf(stderr, "Window %d closed", event->window.windowID);
slouken@1933
   876
            break;
slouken@1914
   877
        default:
slouken@1914
   878
            fprintf(stderr, "Window %d got unknown event %d",
slouken@1914
   879
                    event->window.windowID, event->window.event);
slouken@1914
   880
            break;
slouken@1914
   881
        }
slouken@1914
   882
        break;
slouken@1914
   883
    case SDL_KEYDOWN:
slouken@2268
   884
        fprintf(stderr,
slouken@2306
   885
                "Keyboard %d: key pressed  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_KEYUP:
slouken@2268
   892
        fprintf(stderr,
slouken@2306
   893
                "Keyboard %d: key released in window %d: scancode 0x%08X = %s, keycode 0x%08X = %s",
slouken@2306
   894
                event->key.which, event->key.windowID,
slouken@2306
   895
                event->key.keysym.scancode,
slouken@2306
   896
                SDL_GetScancodeName(event->key.keysym.scancode),
slouken@2306
   897
                event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym));
slouken@1914
   898
        break;
slouken@1914
   899
    case SDL_TEXTINPUT:
slouken@1914
   900
        fprintf(stderr, "Keyboard %d: text input \"%s\" in window %d",
slouken@1914
   901
                event->text.which, event->text.text, event->text.windowID);
slouken@1914
   902
        break;
slouken@1914
   903
    case SDL_MOUSEMOTION:
slouken@1914
   904
        fprintf(stderr, "Mouse %d: moved to %d,%d (%d,%d) in window %d",
slouken@1914
   905
                event->motion.which, event->motion.x, event->motion.y,
slouken@1914
   906
                event->motion.xrel, event->motion.yrel,
slouken@1914
   907
                event->motion.windowID);
slouken@1914
   908
        break;
slouken@1914
   909
    case SDL_MOUSEBUTTONDOWN:
slouken@1914
   910
        fprintf(stderr, "Mouse %d: button %d pressed at %d,%d in window %d",
slouken@1914
   911
                event->button.which, event->button.button, event->button.x,
slouken@1914
   912
                event->button.y, event->button.windowID);
slouken@1914
   913
        break;
slouken@1914
   914
    case SDL_MOUSEBUTTONUP:
slouken@1914
   915
        fprintf(stderr, "Mouse %d: button %d released at %d,%d in window %d",
slouken@1914
   916
                event->button.which, event->button.button, event->button.x,
slouken@1914
   917
                event->button.y, event->button.windowID);
slouken@1914
   918
        break;
slouken@1914
   919
    case SDL_MOUSEWHEEL:
slouken@2174
   920
        fprintf(stderr,
slouken@2174
   921
                "Mouse %d: wheel scrolled %d in x and %d in y in window %d",
bob@2154
   922
                event->wheel.which, event->wheel.x, event->wheel.y,
slouken@1957
   923
                event->wheel.windowID);
slouken@1914
   924
        break;
slouken@1914
   925
    case SDL_JOYBALLMOTION:
slouken@1914
   926
        fprintf(stderr, "Joystick %d: ball %d moved by %d,%d",
slouken@1914
   927
                event->jball.which, event->jball.ball, event->jball.xrel,
slouken@1914
   928
                event->jball.yrel);
slouken@1914
   929
        break;
slouken@1914
   930
    case SDL_JOYHATMOTION:
slouken@1914
   931
        fprintf(stderr, "Joystick %d: hat %d moved to ", event->jhat.which,
slouken@1914
   932
                event->jhat.hat);
slouken@1914
   933
        switch (event->jhat.value) {
slouken@1914
   934
        case SDL_HAT_CENTERED:
slouken@1914
   935
            fprintf(stderr, "CENTER");
slouken@1914
   936
            break;
slouken@1914
   937
        case SDL_HAT_UP:
slouken@1914
   938
            fprintf(stderr, "UP");
slouken@1914
   939
            break;
slouken@1914
   940
        case SDL_HAT_RIGHTUP:
slouken@1914
   941
            fprintf(stderr, "RIGHTUP");
slouken@1914
   942
            break;
slouken@1914
   943
        case SDL_HAT_RIGHT:
slouken@1914
   944
            fprintf(stderr, "RIGHT");
slouken@1914
   945
            break;
slouken@1914
   946
        case SDL_HAT_RIGHTDOWN:
slouken@1914
   947
            fprintf(stderr, "RIGHTDOWN");
slouken@1914
   948
            break;
slouken@1914
   949
        case SDL_HAT_DOWN:
slouken@1914
   950
            fprintf(stderr, "DOWN");
slouken@1914
   951
            break;
slouken@1914
   952
        case SDL_HAT_LEFTDOWN:
slouken@1914
   953
            fprintf(stderr, "LEFTDOWN");
slouken@1914
   954
            break;
slouken@1914
   955
        case SDL_HAT_LEFT:
slouken@1914
   956
            fprintf(stderr, "LEFT");
slouken@1914
   957
            break;
slouken@1914
   958
        case SDL_HAT_LEFTUP:
slouken@1914
   959
            fprintf(stderr, "LEFTUP");
slouken@1914
   960
            break;
slouken@1914
   961
        default:
slouken@1914
   962
            fprintf(stderr, "UNKNOWN");
slouken@1914
   963
            break;
slouken@1914
   964
        }
slouken@1914
   965
        break;
slouken@1914
   966
    case SDL_JOYBUTTONDOWN:
slouken@1914
   967
        fprintf(stderr, "Joystick %d: button %d pressed",
slouken@1914
   968
                event->jbutton.which, event->jbutton.button);
slouken@1914
   969
        break;
slouken@1914
   970
    case SDL_JOYBUTTONUP:
slouken@1914
   971
        fprintf(stderr, "Joystick %d: button %d released",
slouken@1914
   972
                event->jbutton.which, event->jbutton.button);
slouken@1914
   973
        break;
slouken@1914
   974
    case SDL_QUIT:
slouken@1914
   975
        fprintf(stderr, "Quit requested");
slouken@1914
   976
        break;
slouken@1914
   977
    case SDL_USEREVENT:
slouken@1914
   978
        fprintf(stderr, "User event %d", event->user.code);
slouken@1914
   979
        break;
slouken@1914
   980
    default:
slouken@1914
   981
        fprintf(stderr, "Unknown event %d", event->type);
slouken@1914
   982
        break;
slouken@1914
   983
    }
slouken@1914
   984
    fprintf(stderr, "\n");
slouken@1914
   985
}
slouken@1914
   986
slouken@1914
   987
void
slouken@1914
   988
CommonEvent(CommonState * state, SDL_Event * event, int *done)
slouken@1914
   989
{
slouken@1914
   990
    if (state->verbose & VERBOSE_EVENT) {
slouken@1914
   991
        PrintEvent(event);
slouken@1914
   992
    }
slouken@1914
   993
slouken@1914
   994
    switch (event->type) {
slouken@1914
   995
    case SDL_WINDOWEVENT:
slouken@1914
   996
        switch (event->window.event) {
slouken@1914
   997
        case SDL_WINDOWEVENT_CLOSE:
slouken@1914
   998
            *done = 1;
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@1965
  1005
        case SDLK_g:
slouken@1965
  1006
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@1965
  1007
                /* Ctrl-G toggle grab */
slouken@1965
  1008
            }
slouken@1965
  1009
            break;
slouken@1914
  1010
        case SDLK_ESCAPE:
slouken@1914
  1011
            *done = 1;
slouken@1914
  1012
            break;
slouken@1914
  1013
        default:
slouken@1914
  1014
            break;
slouken@1914
  1015
        }
slouken@1914
  1016
        break;
slouken@1914
  1017
    case SDL_QUIT:
slouken@1914
  1018
        *done = 1;
slouken@1914
  1019
        break;
slouken@1914
  1020
    }
slouken@1914
  1021
}
slouken@1914
  1022
slouken@1914
  1023
void
slouken@1914
  1024
CommonQuit(CommonState * state)
slouken@1914
  1025
{
slouken@1914
  1026
    if (state->flags & SDL_INIT_VIDEO) {
slouken@1914
  1027
        SDL_VideoQuit();
slouken@1914
  1028
    }
slouken@1914
  1029
    if (state->flags & SDL_INIT_AUDIO) {
slouken@1914
  1030
        SDL_AudioQuit();
slouken@1914
  1031
    }
slouken@1914
  1032
    if (state->windows) {
slouken@1914
  1033
        SDL_free(state->windows);
slouken@1914
  1034
    }
slouken@1914
  1035
    SDL_free(state);
slouken@1914
  1036
}