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