test/common.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 13 Jul 2010 23:05:14 -0700
changeset 4517 7b5e4396bcaa
parent 4503 524dfefd554c
child 4527 3ce59cbecaa3
permissions -rw-r--r--
Added test case for maximize code
slouken@1914
     1
slouken@1914
     2
/* A simple test program framework */
slouken@1914
     3
slouken@1914
     4
#include <stdio.h>
slouken@1914
     5
slouken@1914
     6
#include "common.h"
slouken@1914
     7
slouken@1914
     8
#define VIDEO_USAGE \
slouken@4482
     9
"[--video driver] [--renderer driver] [--info all|video|modes|render|event] [--display N] [--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@3500
    79
    state->window_flags = 0;
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;
slouken@3571
   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);
slouken@3571
   662
        if (state->gl_accelerated >= 0) {
slouken@3571
   663
            SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL,
slouken@3571
   664
                                state->gl_accelerated);
slouken@3571
   665
        }
lestat@3408
   666
        SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, state->gl_retained_backing);
lestat@3408
   667
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, state->gl_major_version);
lestat@3408
   668
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, state->gl_minor_version);
lestat@3408
   669
slouken@1914
   670
        if (state->verbose & VERBOSE_MODES) {
slouken@1967
   671
            SDL_DisplayMode mode;
slouken@1914
   672
            int bpp;
slouken@1914
   673
            Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1914
   674
slouken@1914
   675
            n = SDL_GetNumVideoDisplays();
slouken@1914
   676
            fprintf(stderr, "Number of displays: %d\n", n);
slouken@1914
   677
            for (i = 0; i < n; ++i) {
slouken@1914
   678
                fprintf(stderr, "Display %d:\n", i);
slouken@1914
   679
                SDL_SelectVideoDisplay(i);
slouken@1914
   680
slouken@1967
   681
                SDL_GetDesktopDisplayMode(&mode);
slouken@1967
   682
                SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask,
slouken@1914
   683
                                           &Bmask, &Amask);
slouken@1914
   684
                fprintf(stderr,
lestat@3184
   685
                        "  Current mode: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
lestat@3184
   686
                        mode.w, mode.h, mode.refresh_rate, bpp,
lestat@3184
   687
                        PixelFormatToString(mode.format));
slouken@1914
   688
                if (Rmask || Gmask || Bmask) {
lestat@3184
   689
                    fprintf(stderr, "      Red Mask   = 0x%.8x\n", Rmask);
slouken@1914
   690
                    fprintf(stderr, "      Green Mask = 0x%.8x\n", Gmask);
lestat@3184
   691
                    fprintf(stderr, "      Blue Mask  = 0x%.8x\n", Bmask);
slouken@1914
   692
                    if (Amask)
slouken@1914
   693
                        fprintf(stderr, "      Alpha Mask = 0x%.8x\n", Amask);
slouken@1914
   694
                }
slouken@1914
   695
slouken@1914
   696
                /* Print available fullscreen video modes */
slouken@1914
   697
                m = SDL_GetNumDisplayModes();
slouken@1914
   698
                if (m == 0) {
slouken@1914
   699
                    fprintf(stderr, "No available fullscreen video modes\n");
slouken@1914
   700
                } else {
slouken@1914
   701
                    fprintf(stderr, "  Fullscreen video modes:\n");
slouken@1914
   702
                    for (j = 0; j < m; ++j) {
slouken@1967
   703
                        SDL_GetDisplayMode(j, &mode);
slouken@1967
   704
                        SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask,
slouken@1914
   705
                                                   &Gmask, &Bmask, &Amask);
slouken@1914
   706
                        fprintf(stderr,
lestat@3184
   707
                                "    Mode %d: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
lestat@3184
   708
                                j, mode.w, mode.h, mode.refresh_rate, bpp,
lestat@3184
   709
                                PixelFormatToString(mode.format));
slouken@1914
   710
                        if (Rmask || Gmask || Bmask) {
lestat@3184
   711
                            fprintf(stderr, "        Red Mask   = 0x%.8x\n",
slouken@1914
   712
                                    Rmask);
slouken@1914
   713
                            fprintf(stderr, "        Green Mask = 0x%.8x\n",
slouken@1914
   714
                                    Gmask);
lestat@3184
   715
                            fprintf(stderr, "        Blue Mask  = 0x%.8x\n",
slouken@1914
   716
                                    Bmask);
slouken@1914
   717
                            if (Amask)
slouken@1914
   718
                                fprintf(stderr,
slouken@1914
   719
                                        "        Alpha Mask = 0x%.8x\n",
slouken@1914
   720
                                        Amask);
slouken@1914
   721
                        }
slouken@1914
   722
                    }
slouken@1914
   723
                }
slouken@1914
   724
            }
slouken@1914
   725
        }
slouken@1914
   726
slouken@1914
   727
        SDL_SelectVideoDisplay(state->display);
slouken@1914
   728
        if (state->verbose & VERBOSE_RENDER) {
slouken@1914
   729
            SDL_RendererInfo info;
slouken@1914
   730
slouken@1969
   731
            n = SDL_GetNumRenderDrivers();
slouken@1914
   732
            if (n == 0) {
slouken@1914
   733
                fprintf(stderr, "No built-in render drivers\n");
slouken@1914
   734
            } else {
slouken@1923
   735
                fprintf(stderr, "Built-in render drivers:\n");
slouken@1914
   736
                for (i = 0; i < n; ++i) {
slouken@1969
   737
                    SDL_GetRenderDriverInfo(i, &info);
slouken@1914
   738
                    PrintRenderer(&info);
slouken@1914
   739
                }
slouken@1914
   740
            }
slouken@1914
   741
        }
slouken@1914
   742
slouken@3500
   743
        SDL_zero(fullscreen_mode);
slouken@1914
   744
        switch (state->depth) {
slouken@1914
   745
        case 8:
slouken@1965
   746
            fullscreen_mode.format = SDL_PIXELFORMAT_INDEX8;
slouken@1914
   747
            break;
slouken@1914
   748
        case 15:
slouken@1965
   749
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB555;
slouken@1914
   750
            break;
slouken@1914
   751
        case 16:
slouken@1965
   752
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB565;
slouken@1914
   753
            break;
lestat@3184
   754
        case 24:
lestat@3184
   755
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB24;
lestat@3184
   756
            break;
slouken@1914
   757
        default:
slouken@1965
   758
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB888;
slouken@1914
   759
            break;
slouken@1914
   760
        }
slouken@1914
   761
        fullscreen_mode.refresh_rate = state->refresh_rate;
slouken@1914
   762
slouken@1914
   763
        state->windows =
slouken@3685
   764
            (SDL_Window **) SDL_malloc(state->num_windows *
slouken@1914
   765
                                        sizeof(*state->windows));
slouken@1914
   766
        if (!state->windows) {
slouken@1914
   767
            fprintf(stderr, "Out of memory!\n");
slouken@1914
   768
            return SDL_FALSE;
slouken@1914
   769
        }
slouken@1914
   770
        for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   771
            char title[1024];
slouken@1914
   772
slouken@1914
   773
            if (state->num_windows > 1) {
slouken@1914
   774
                SDL_snprintf(title, SDL_arraysize(title), "%s %d",
slouken@1914
   775
                             state->window_title, i + 1);
slouken@1914
   776
            } else {
slouken@1914
   777
                SDL_strlcpy(title, state->window_title, SDL_arraysize(title));
slouken@1914
   778
            }
slouken@1914
   779
            state->windows[i] =
slouken@1914
   780
                SDL_CreateWindow(title, state->window_x, state->window_y,
slouken@1914
   781
                                 state->window_w, state->window_h,
slouken@1914
   782
                                 state->window_flags);
slouken@1914
   783
            if (!state->windows[i]) {
slouken@1914
   784
                fprintf(stderr, "Couldn't create window: %s\n",
slouken@1914
   785
                        SDL_GetError());
slouken@1914
   786
                return SDL_FALSE;
slouken@1914
   787
            }
slouken@1914
   788
slouken@3500
   789
            if (SDL_SetWindowDisplayMode(state->windows[i], &fullscreen_mode) < 0) {
slouken@3500
   790
                fprintf(stderr, "Can't set up fullscreen display mode: %s\n",
slouken@3500
   791
                        SDL_GetError());
slouken@3500
   792
                return SDL_FALSE;
slouken@3500
   793
            }
slouken@3500
   794
            SDL_ShowWindow(state->windows[i]);
slouken@3500
   795
slouken@1933
   796
            if (!state->skip_renderer
slouken@1933
   797
                && (state->renderdriver
slouken@1933
   798
                    || !(state->window_flags & SDL_WINDOW_OPENGL))) {
slouken@1914
   799
                m = -1;
slouken@1914
   800
                if (state->renderdriver) {
slouken@1914
   801
                    SDL_RendererInfo info;
slouken@1969
   802
                    n = SDL_GetNumRenderDrivers();
slouken@1914
   803
                    for (j = 0; j < n; ++j) {
slouken@1969
   804
                        SDL_GetRenderDriverInfo(j, &info);
slouken@1914
   805
                        if (SDL_strcasecmp(info.name, state->renderdriver) ==
slouken@1914
   806
                            0) {
slouken@1914
   807
                            m = j;
slouken@1914
   808
                            break;
slouken@1914
   809
                        }
slouken@1914
   810
                    }
slouken@1914
   811
                    if (m == n) {
slouken@1914
   812
                        fprintf(stderr,
slouken@1914
   813
                                "Couldn't find render driver named %s",
slouken@1914
   814
                                state->renderdriver);
slouken@1914
   815
                        return SDL_FALSE;
slouken@1914
   816
                    }
slouken@1914
   817
                }
slouken@1914
   818
                if (SDL_CreateRenderer
slouken@1914
   819
                    (state->windows[i], m, state->render_flags) < 0) {
slouken@1914
   820
                    fprintf(stderr, "Couldn't create renderer: %s\n",
slouken@1914
   821
                            SDL_GetError());
slouken@1914
   822
                    return SDL_FALSE;
slouken@1914
   823
                }
slouken@1923
   824
                if (state->verbose & VERBOSE_RENDER) {
slouken@1923
   825
                    SDL_RendererInfo info;
slouken@1923
   826
slouken@1923
   827
                    fprintf(stderr, "Current renderer:\n");
slouken@1969
   828
                    SDL_GetRendererInfo(&info);
slouken@1923
   829
                    PrintRenderer(&info);
slouken@1923
   830
                }
slouken@1914
   831
            }
slouken@1914
   832
        }
slouken@1914
   833
        SDL_SelectRenderer(state->windows[0]);
slouken@1914
   834
    }
slouken@1914
   835
slouken@1914
   836
    if (state->flags & SDL_INIT_AUDIO) {
slouken@1914
   837
        if (state->verbose & VERBOSE_AUDIO) {
slouken@1914
   838
            n = SDL_GetNumAudioDrivers();
slouken@1914
   839
            if (n == 0) {
slouken@1914
   840
                fprintf(stderr, "No built-in audio drivers\n");
slouken@1914
   841
            } else {
slouken@1914
   842
                fprintf(stderr, "Built-in audio drivers:");
slouken@1914
   843
                for (i = 0; i < n; ++i) {
slouken@1914
   844
                    if (i > 0) {
slouken@1914
   845
                        fprintf(stderr, ",");
slouken@1914
   846
                    }
slouken@1914
   847
                    fprintf(stderr, " %s", SDL_GetAudioDriver(i));
slouken@1914
   848
                }
slouken@1914
   849
                fprintf(stderr, "\n");
slouken@1914
   850
            }
slouken@1914
   851
        }
slouken@1914
   852
        if (SDL_AudioInit(state->audiodriver) < 0) {
slouken@1914
   853
            fprintf(stderr, "Couldn't initialize audio driver: %s\n",
slouken@1914
   854
                    SDL_GetError());
slouken@1914
   855
            return SDL_FALSE;
slouken@1914
   856
        }
slouken@1914
   857
        if (state->verbose & VERBOSE_VIDEO) {
slouken@1914
   858
            fprintf(stderr, "Audio driver: %s\n",
slouken@1914
   859
                    SDL_GetCurrentAudioDriver());
slouken@1914
   860
        }
slouken@1914
   861
slouken@1914
   862
        if (SDL_OpenAudio(&state->audiospec, NULL) < 0) {
slouken@1914
   863
            fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@1914
   864
            return SDL_FALSE;
slouken@1914
   865
        }
slouken@1914
   866
    }
slouken@1914
   867
slouken@1914
   868
    return SDL_TRUE;
slouken@1914
   869
}
slouken@1914
   870
slouken@1914
   871
static void
slouken@1914
   872
PrintEvent(SDL_Event * event)
slouken@1914
   873
{
slouken@1914
   874
    fprintf(stderr, "SDL EVENT: ");
slouken@1914
   875
    switch (event->type) {
slouken@1914
   876
    case SDL_WINDOWEVENT:
slouken@1914
   877
        switch (event->window.event) {
slouken@1914
   878
        case SDL_WINDOWEVENT_SHOWN:
slouken@1914
   879
            fprintf(stderr, "Window %d shown", event->window.windowID);
slouken@1914
   880
            break;
slouken@1914
   881
        case SDL_WINDOWEVENT_HIDDEN:
slouken@1914
   882
            fprintf(stderr, "Window %d hidden", event->window.windowID);
slouken@1914
   883
            break;
slouken@1914
   884
        case SDL_WINDOWEVENT_EXPOSED:
slouken@1970
   885
            fprintf(stderr, "Window %d exposed", event->window.windowID);
slouken@1914
   886
            break;
slouken@1914
   887
        case SDL_WINDOWEVENT_MOVED:
slouken@1970
   888
            fprintf(stderr, "Window %d moved to %d,%d",
slouken@1914
   889
                    event->window.windowID, event->window.data1,
slouken@1914
   890
                    event->window.data2);
slouken@1914
   891
            break;
slouken@1914
   892
        case SDL_WINDOWEVENT_RESIZED:
slouken@1970
   893
            fprintf(stderr, "Window %d resized to %dx%d",
slouken@1914
   894
                    event->window.windowID, event->window.data1,
slouken@1914
   895
                    event->window.data2);
slouken@1914
   896
            break;
slouken@1914
   897
        case SDL_WINDOWEVENT_MINIMIZED:
slouken@1914
   898
            fprintf(stderr, "Window %d minimized", event->window.windowID);
slouken@1914
   899
            break;
slouken@1914
   900
        case SDL_WINDOWEVENT_MAXIMIZED:
slouken@1914
   901
            fprintf(stderr, "Window %d maximized", event->window.windowID);
slouken@1914
   902
            break;
slouken@1914
   903
        case SDL_WINDOWEVENT_RESTORED:
slouken@1914
   904
            fprintf(stderr, "Window %d restored", event->window.windowID);
slouken@1914
   905
            break;
slouken@1914
   906
        case SDL_WINDOWEVENT_ENTER:
slouken@1914
   907
            fprintf(stderr, "Mouse entered window %d",
slouken@1914
   908
                    event->window.windowID);
slouken@1914
   909
            break;
slouken@1914
   910
        case SDL_WINDOWEVENT_LEAVE:
slouken@1914
   911
            fprintf(stderr, "Mouse left window %d", event->window.windowID);
slouken@1914
   912
            break;
slouken@1914
   913
        case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1914
   914
            fprintf(stderr, "Window %d gained keyboard focus",
slouken@1914
   915
                    event->window.windowID);
slouken@1914
   916
            break;
slouken@1914
   917
        case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1914
   918
            fprintf(stderr, "Window %d lost keyboard focus",
slouken@1914
   919
                    event->window.windowID);
slouken@1914
   920
            break;
slouken@1933
   921
        case SDL_WINDOWEVENT_CLOSE:
slouken@1933
   922
            fprintf(stderr, "Window %d closed", event->window.windowID);
slouken@1933
   923
            break;
slouken@1914
   924
        default:
slouken@1914
   925
            fprintf(stderr, "Window %d got unknown event %d",
slouken@1914
   926
                    event->window.windowID, event->window.event);
slouken@1914
   927
            break;
slouken@1914
   928
        }
slouken@1914
   929
        break;
slouken@1914
   930
    case SDL_KEYDOWN:
slouken@2268
   931
        fprintf(stderr,
slouken@4465
   932
                "Keyboard: key pressed  in window %d: scancode 0x%08X = %s, keycode 0x%08X = %s",
slouken@4465
   933
                event->key.windowID,
slouken@2306
   934
                event->key.keysym.scancode,
slouken@2306
   935
                SDL_GetScancodeName(event->key.keysym.scancode),
slouken@2306
   936
                event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym));
slouken@1914
   937
        break;
slouken@1914
   938
    case SDL_KEYUP:
slouken@2268
   939
        fprintf(stderr,
slouken@4465
   940
                "Keyboard: key released in window %d: scancode 0x%08X = %s, keycode 0x%08X = %s",
slouken@4465
   941
                event->key.windowID,
slouken@2306
   942
                event->key.keysym.scancode,
slouken@2306
   943
                SDL_GetScancodeName(event->key.keysym.scancode),
slouken@2306
   944
                event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym));
slouken@1914
   945
        break;
slouken@1914
   946
    case SDL_TEXTINPUT:
slouken@4465
   947
        fprintf(stderr, "Keyboard: text input \"%s\" in window %d",
slouken@4465
   948
                event->text.text, event->text.windowID);
slouken@1914
   949
        break;
slouken@1914
   950
    case SDL_MOUSEMOTION:
slouken@4465
   951
        fprintf(stderr, "Mouse: moved to %d,%d (%d,%d) in window %d",
slouken@4465
   952
                event->motion.x, event->motion.y,
slouken@1914
   953
                event->motion.xrel, event->motion.yrel,
slouken@1914
   954
                event->motion.windowID);
slouken@1914
   955
        break;
slouken@1914
   956
    case SDL_MOUSEBUTTONDOWN:
slouken@4465
   957
        fprintf(stderr, "Mouse: button %d pressed at %d,%d in window %d",
slouken@4465
   958
                event->button.button, event->button.x, event->button.y,
slouken@4465
   959
                event->button.windowID);
slouken@1914
   960
        break;
slouken@1914
   961
    case SDL_MOUSEBUTTONUP:
slouken@4465
   962
        fprintf(stderr, "Mouse: button %d released at %d,%d in window %d",
slouken@4465
   963
                event->button.button, event->button.x, event->button.y,
slouken@4465
   964
                event->button.windowID);
slouken@1914
   965
        break;
slouken@1914
   966
    case SDL_MOUSEWHEEL:
slouken@2174
   967
        fprintf(stderr,
slouken@4465
   968
                "Mouse: wheel scrolled %d in x and %d in y in window %d",
slouken@4465
   969
                event->wheel.x, event->wheel.y, event->wheel.windowID);
slouken@1914
   970
        break;
slouken@1914
   971
    case SDL_JOYBALLMOTION:
slouken@1914
   972
        fprintf(stderr, "Joystick %d: ball %d moved by %d,%d",
slouken@1914
   973
                event->jball.which, event->jball.ball, event->jball.xrel,
slouken@1914
   974
                event->jball.yrel);
slouken@1914
   975
        break;
slouken@1914
   976
    case SDL_JOYHATMOTION:
slouken@1914
   977
        fprintf(stderr, "Joystick %d: hat %d moved to ", event->jhat.which,
slouken@1914
   978
                event->jhat.hat);
slouken@1914
   979
        switch (event->jhat.value) {
slouken@1914
   980
        case SDL_HAT_CENTERED:
slouken@1914
   981
            fprintf(stderr, "CENTER");
slouken@1914
   982
            break;
slouken@1914
   983
        case SDL_HAT_UP:
slouken@1914
   984
            fprintf(stderr, "UP");
slouken@1914
   985
            break;
slouken@1914
   986
        case SDL_HAT_RIGHTUP:
slouken@1914
   987
            fprintf(stderr, "RIGHTUP");
slouken@1914
   988
            break;
slouken@1914
   989
        case SDL_HAT_RIGHT:
slouken@1914
   990
            fprintf(stderr, "RIGHT");
slouken@1914
   991
            break;
slouken@1914
   992
        case SDL_HAT_RIGHTDOWN:
slouken@1914
   993
            fprintf(stderr, "RIGHTDOWN");
slouken@1914
   994
            break;
slouken@1914
   995
        case SDL_HAT_DOWN:
slouken@1914
   996
            fprintf(stderr, "DOWN");
slouken@1914
   997
            break;
slouken@1914
   998
        case SDL_HAT_LEFTDOWN:
slouken@1914
   999
            fprintf(stderr, "LEFTDOWN");
slouken@1914
  1000
            break;
slouken@1914
  1001
        case SDL_HAT_LEFT:
slouken@1914
  1002
            fprintf(stderr, "LEFT");
slouken@1914
  1003
            break;
slouken@1914
  1004
        case SDL_HAT_LEFTUP:
slouken@1914
  1005
            fprintf(stderr, "LEFTUP");
slouken@1914
  1006
            break;
slouken@1914
  1007
        default:
slouken@1914
  1008
            fprintf(stderr, "UNKNOWN");
slouken@1914
  1009
            break;
slouken@1914
  1010
        }
slouken@1914
  1011
        break;
slouken@1914
  1012
    case SDL_JOYBUTTONDOWN:
slouken@1914
  1013
        fprintf(stderr, "Joystick %d: button %d pressed",
slouken@1914
  1014
                event->jbutton.which, event->jbutton.button);
slouken@1914
  1015
        break;
slouken@1914
  1016
    case SDL_JOYBUTTONUP:
slouken@1914
  1017
        fprintf(stderr, "Joystick %d: button %d released",
slouken@1914
  1018
                event->jbutton.which, event->jbutton.button);
slouken@1914
  1019
        break;
slouken@4503
  1020
    case SDL_CLIPBOARDUPDATE:
slouken@4503
  1021
        fprintf(stderr, "Clipboard updated");
slouken@4503
  1022
        break;
slouken@1914
  1023
    case SDL_QUIT:
slouken@1914
  1024
        fprintf(stderr, "Quit requested");
slouken@1914
  1025
        break;
slouken@1914
  1026
    case SDL_USEREVENT:
slouken@1914
  1027
        fprintf(stderr, "User event %d", event->user.code);
slouken@1914
  1028
        break;
slouken@1914
  1029
    default:
slouken@1914
  1030
        fprintf(stderr, "Unknown event %d", event->type);
slouken@1914
  1031
        break;
slouken@1914
  1032
    }
slouken@1914
  1033
    fprintf(stderr, "\n");
slouken@1914
  1034
}
slouken@1914
  1035
slouken@1914
  1036
void
slouken@1914
  1037
CommonEvent(CommonState * state, SDL_Event * event, int *done)
slouken@1914
  1038
{
slouken@3514
  1039
    int i;
slouken@3514
  1040
slouken@1914
  1041
    if (state->verbose & VERBOSE_EVENT) {
slouken@1914
  1042
        PrintEvent(event);
slouken@1914
  1043
    }
slouken@1914
  1044
slouken@1914
  1045
    switch (event->type) {
slouken@1914
  1046
    case SDL_WINDOWEVENT:
slouken@1914
  1047
        switch (event->window.event) {
slouken@1914
  1048
        case SDL_WINDOWEVENT_CLOSE:
slouken@1914
  1049
            *done = 1;
slouken@1914
  1050
            break;
slouken@1914
  1051
        }
slouken@1914
  1052
        break;
slouken@1914
  1053
    case SDL_KEYDOWN:
slouken@1914
  1054
        switch (event->key.keysym.sym) {
slouken@1914
  1055
            /* Add hotkeys here */
slouken@4497
  1056
        case SDLK_c:
slouken@4497
  1057
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@4497
  1058
                /* Ctrl-C copy awesome text! */
slouken@4497
  1059
                SDL_SetClipboardText("SDL rocks!\nYou know it!");
slouken@4497
  1060
                printf("Copied text to clipboard\n");
slouken@4497
  1061
            }
slouken@4497
  1062
            break;
slouken@4497
  1063
        case SDLK_v:
slouken@4497
  1064
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@4497
  1065
                /* Ctrl-V paste awesome text! */
slouken@4497
  1066
                char *text = SDL_GetClipboardText();
slouken@4497
  1067
                if (*text) {
slouken@4497
  1068
                    printf("Clipboard: %s\n", text);
slouken@4497
  1069
                } else {
slouken@4497
  1070
                    printf("Clipboard is empty\n");
slouken@4497
  1071
                }
slouken@4497
  1072
                SDL_free(text);
slouken@4497
  1073
            }
slouken@4497
  1074
            break;
slouken@1965
  1075
        case SDLK_g:
slouken@1965
  1076
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@1965
  1077
                /* Ctrl-G toggle grab */
slouken@1965
  1078
            }
slouken@1965
  1079
            break;
slouken@4517
  1080
        case SDLK_m:
slouken@4517
  1081
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@4517
  1082
                /* Ctrl-M maximize */
slouken@4517
  1083
                /* FIXME: Which window has focus for this keyboard? */
slouken@4517
  1084
                for (i = 0; i < state->num_windows; ++i) {
slouken@4517
  1085
                    if (SDL_GetWindowFlags(state->windows[i]) & SDL_WINDOW_MAXIMIZED) {
slouken@4517
  1086
                        SDL_RestoreWindow(state->windows[i]);
slouken@4517
  1087
                    } else {
slouken@4517
  1088
                        SDL_MaximizeWindow(state->windows[i]);
slouken@4517
  1089
                    }
slouken@4517
  1090
                }
slouken@4517
  1091
            }
slouken@4517
  1092
            break;
slouken@3514
  1093
        case SDLK_z:
slouken@3514
  1094
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@3514
  1095
                /* Ctrl-Z minimize */
slouken@3514
  1096
                /* FIXME: Which window has focus for this keyboard? */
slouken@3514
  1097
                for (i = 0; i < state->num_windows; ++i) {
slouken@3514
  1098
                    SDL_MinimizeWindow(state->windows[i]);
slouken@3514
  1099
                }
slouken@3514
  1100
            }
slouken@3514
  1101
            break;
slouken@1914
  1102
        case SDLK_ESCAPE:
slouken@1914
  1103
            *done = 1;
slouken@1914
  1104
            break;
slouken@1914
  1105
        default:
slouken@1914
  1106
            break;
slouken@1914
  1107
        }
slouken@1914
  1108
        break;
slouken@1914
  1109
    case SDL_QUIT:
slouken@1914
  1110
        *done = 1;
slouken@1914
  1111
        break;
slouken@1914
  1112
    }
slouken@1914
  1113
}
slouken@1914
  1114
slouken@1914
  1115
void
slouken@1914
  1116
CommonQuit(CommonState * state)
slouken@1914
  1117
{
slouken@1914
  1118
    if (state->flags & SDL_INIT_VIDEO) {
slouken@1914
  1119
        SDL_VideoQuit();
slouken@1914
  1120
    }
slouken@1914
  1121
    if (state->flags & SDL_INIT_AUDIO) {
slouken@1914
  1122
        SDL_AudioQuit();
slouken@1914
  1123
    }
slouken@1914
  1124
    if (state->windows) {
slouken@1914
  1125
        SDL_free(state->windows);
slouken@1914
  1126
    }
slouken@1914
  1127
    SDL_free(state);
slouken@1914
  1128
}
slouken@3514
  1129
slouken@3514
  1130
/* vi: set ts=4 sw=4 expandtab: */