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