test/common.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 03 Jun 2011 16:03:10 -0400
changeset 5547 4ccecd0901e2
parent 5535 96594ac5fd1a
child 6175 42c40787f382
permissions -rw-r--r--
Assert code's stdio interface was reading from the wrong variable.

Thanks to Frank Zago for the catch.
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@1914
    12
slouken@1914
    13
/* A simple test program framework */
slouken@1914
    14
slouken@1914
    15
#include <stdio.h>
slouken@1914
    16
slouken@1914
    17
#include "common.h"
slouken@1914
    18
slouken@1914
    19
#define VIDEO_USAGE \
slouken@5234
    20
"[--video driver] [--renderer driver] [--info all|video|modes|render|event] [--log all|error|system|audio|video|render|input] [--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]"
slouken@1914
    21
slouken@1914
    22
#define AUDIO_USAGE \
slouken@1914
    23
"[--rate N] [--format U8|S8|U16|U16LE|U16BE|S16|S16LE|S16BE] [--channels N] [--samples N]"
slouken@1914
    24
slouken@1914
    25
CommonState *
slouken@1914
    26
CommonCreateState(char **argv, Uint32 flags)
slouken@1914
    27
{
slouken@1914
    28
    CommonState *state = SDL_calloc(1, sizeof(*state));
slouken@1914
    29
    if (!state) {
slouken@1914
    30
        SDL_OutOfMemory();
slouken@1914
    31
        return NULL;
slouken@1914
    32
    }
slouken@1914
    33
slouken@1914
    34
    /* Initialize some defaults */
slouken@1914
    35
    state->argv = argv;
slouken@1914
    36
    state->flags = flags;
slouken@5517
    37
#ifdef __NDS__
slouken@5517
    38
    state->window_title = "";
slouken@5517
    39
#else
slouken@1914
    40
    state->window_title = argv[0];
slouken@5517
    41
#endif
slouken@3500
    42
    state->window_flags = 0;
slouken@1914
    43
    state->window_x = SDL_WINDOWPOS_UNDEFINED;
slouken@1914
    44
    state->window_y = SDL_WINDOWPOS_UNDEFINED;
lestat@3194
    45
    state->window_w = DEFAULT_WINDOW_WIDTH;
lestat@3194
    46
    state->window_h = DEFAULT_WINDOW_HEIGHT;
slouken@1914
    47
    state->num_windows = 1;
slouken@1914
    48
    state->audiospec.freq = 22050;
slouken@1914
    49
    state->audiospec.format = AUDIO_S16;
slouken@1914
    50
    state->audiospec.channels = 2;
slouken@1914
    51
    state->audiospec.samples = 2048;
lestat@3408
    52
lestat@3408
    53
    /* Set some very sane GL defaults */
lestat@3408
    54
    state->gl_red_size = 3;
lestat@3408
    55
    state->gl_green_size = 3;
lestat@3408
    56
    state->gl_blue_size = 2;
lestat@3408
    57
    state->gl_alpha_size = 0;
lestat@3408
    58
    state->gl_buffer_size = 0;
lestat@3408
    59
    state->gl_depth_size = 16;
lestat@3408
    60
    state->gl_stencil_size = 0;
lestat@3408
    61
    state->gl_double_buffer = 1;
lestat@3408
    62
    state->gl_accum_red_size = 0;
lestat@3408
    63
    state->gl_accum_green_size = 0;
lestat@3408
    64
    state->gl_accum_blue_size = 0;
lestat@3408
    65
    state->gl_accum_alpha_size = 0;
lestat@3408
    66
    state->gl_stereo = 0;
lestat@3408
    67
    state->gl_multisamplebuffers = 0;
lestat@3408
    68
    state->gl_multisamplesamples = 0;
lestat@3408
    69
    state->gl_retained_backing = 1;
slouken@3571
    70
    state->gl_accelerated = -1;
lestat@3408
    71
slouken@1914
    72
    return state;
slouken@1914
    73
}
slouken@1914
    74
slouken@1914
    75
int
slouken@1914
    76
CommonArg(CommonState * state, int index)
slouken@1914
    77
{
slouken@1914
    78
    char **argv = state->argv;
slouken@1914
    79
slouken@5517
    80
#ifdef __NDS__
slouken@5517
    81
    return 0;
slouken@5517
    82
#endif
slouken@5517
    83
slouken@1914
    84
    if (SDL_strcasecmp(argv[index], "--video") == 0) {
slouken@1914
    85
        ++index;
slouken@1914
    86
        if (!argv[index]) {
slouken@1914
    87
            return -1;
slouken@1914
    88
        }
slouken@1914
    89
        state->videodriver = argv[index];
slouken@1914
    90
        return 2;
slouken@1914
    91
    }
slouken@1914
    92
    if (SDL_strcasecmp(argv[index], "--renderer") == 0) {
slouken@1914
    93
        ++index;
slouken@1914
    94
        if (!argv[index]) {
slouken@1914
    95
            return -1;
slouken@1914
    96
        }
slouken@1914
    97
        state->renderdriver = argv[index];
slouken@1914
    98
        return 2;
slouken@1914
    99
    }
slouken@1914
   100
    if (SDL_strcasecmp(argv[index], "--info") == 0) {
slouken@1914
   101
        ++index;
slouken@1914
   102
        if (!argv[index]) {
slouken@1914
   103
            return -1;
slouken@1914
   104
        }
slouken@1914
   105
        if (SDL_strcasecmp(argv[index], "all") == 0) {
slouken@1914
   106
            state->verbose |=
slouken@1914
   107
                (VERBOSE_VIDEO | VERBOSE_MODES | VERBOSE_RENDER |
slouken@1914
   108
                 VERBOSE_EVENT);
slouken@1914
   109
            return 2;
slouken@1914
   110
        }
slouken@1914
   111
        if (SDL_strcasecmp(argv[index], "video") == 0) {
slouken@1914
   112
            state->verbose |= VERBOSE_VIDEO;
slouken@1914
   113
            return 2;
slouken@1914
   114
        }
slouken@1914
   115
        if (SDL_strcasecmp(argv[index], "modes") == 0) {
slouken@1914
   116
            state->verbose |= VERBOSE_MODES;
slouken@1914
   117
            return 2;
slouken@1914
   118
        }
slouken@1914
   119
        if (SDL_strcasecmp(argv[index], "render") == 0) {
slouken@1914
   120
            state->verbose |= VERBOSE_RENDER;
slouken@1914
   121
            return 2;
slouken@1914
   122
        }
slouken@1914
   123
        if (SDL_strcasecmp(argv[index], "event") == 0) {
slouken@1914
   124
            state->verbose |= VERBOSE_EVENT;
slouken@1914
   125
            return 2;
slouken@1914
   126
        }
slouken@1914
   127
        return -1;
slouken@1914
   128
    }
slouken@5234
   129
    if (SDL_strcasecmp(argv[index], "--log") == 0) {
slouken@5234
   130
        ++index;
slouken@5234
   131
        if (!argv[index]) {
slouken@5234
   132
            return -1;
slouken@5234
   133
        }
slouken@5234
   134
        if (SDL_strcasecmp(argv[index], "all") == 0) {
slouken@5234
   135
            SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   136
            return 2;
slouken@5234
   137
        }
slouken@5234
   138
        if (SDL_strcasecmp(argv[index], "error") == 0) {
slouken@5234
   139
            SDL_LogSetPriority(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   140
            return 2;
slouken@5234
   141
        }
slouken@5234
   142
        if (SDL_strcasecmp(argv[index], "system") == 0) {
slouken@5234
   143
            SDL_LogSetPriority(SDL_LOG_CATEGORY_SYSTEM, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   144
            return 2;
slouken@5234
   145
        }
slouken@5234
   146
        if (SDL_strcasecmp(argv[index], "audio") == 0) {
slouken@5234
   147
            SDL_LogSetPriority(SDL_LOG_CATEGORY_AUDIO, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   148
            return 2;
slouken@5234
   149
        }
slouken@5234
   150
        if (SDL_strcasecmp(argv[index], "video") == 0) {
slouken@5234
   151
            SDL_LogSetPriority(SDL_LOG_CATEGORY_VIDEO, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   152
            return 2;
slouken@5234
   153
        }
slouken@5234
   154
        if (SDL_strcasecmp(argv[index], "render") == 0) {
slouken@5234
   155
            SDL_LogSetPriority(SDL_LOG_CATEGORY_RENDER, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   156
            return 2;
slouken@5234
   157
        }
slouken@5234
   158
        if (SDL_strcasecmp(argv[index], "input") == 0) {
slouken@5234
   159
            SDL_LogSetPriority(SDL_LOG_CATEGORY_INPUT, SDL_LOG_PRIORITY_VERBOSE);
slouken@5234
   160
            return 2;
slouken@5234
   161
        }
slouken@5234
   162
        return -1;
slouken@5234
   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@5247
   170
        if (SDL_WINDOWPOS_ISUNDEFINED(state->window_x)) {
slouken@5247
   171
            state->window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(state->display);
slouken@5247
   172
            state->window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(state->display);
slouken@5247
   173
        }
slouken@5304
   174
        if (SDL_WINDOWPOS_ISCENTERED(state->window_x)) {
slouken@5304
   175
            state->window_x = SDL_WINDOWPOS_CENTERED_DISPLAY(state->display);
slouken@5304
   176
            state->window_y = SDL_WINDOWPOS_CENTERED_DISPLAY(state->display);
slouken@5304
   177
        }
slouken@1914
   178
        return 2;
slouken@1914
   179
    }
slouken@1914
   180
    if (SDL_strcasecmp(argv[index], "--fullscreen") == 0) {
slouken@1914
   181
        state->window_flags |= SDL_WINDOW_FULLSCREEN;
slouken@1914
   182
        state->num_windows = 1;
slouken@1914
   183
        return 1;
slouken@1914
   184
    }
slouken@1914
   185
    if (SDL_strcasecmp(argv[index], "--windows") == 0) {
slouken@1914
   186
        ++index;
slouken@1916
   187
        if (!argv[index] || !SDL_isdigit(*argv[index])) {
slouken@1914
   188
            return -1;
slouken@1914
   189
        }
slouken@1914
   190
        if (!(state->window_flags & SDL_WINDOW_FULLSCREEN)) {
slouken@1914
   191
            state->num_windows = SDL_atoi(argv[index]);
slouken@1914
   192
        }
slouken@1914
   193
        return 2;
slouken@1914
   194
    }
slouken@1914
   195
    if (SDL_strcasecmp(argv[index], "--title") == 0) {
slouken@1914
   196
        ++index;
slouken@1914
   197
        if (!argv[index]) {
slouken@1914
   198
            return -1;
slouken@1914
   199
        }
slouken@1914
   200
        state->window_title = argv[index];
slouken@1914
   201
        return 2;
slouken@1914
   202
    }
slouken@4527
   203
    if (SDL_strcasecmp(argv[index], "--icon") == 0) {
slouken@4527
   204
        ++index;
slouken@4527
   205
        if (!argv[index]) {
slouken@4527
   206
            return -1;
slouken@4527
   207
        }
slouken@4527
   208
        state->window_icon = argv[index];
slouken@4527
   209
        return 2;
slouken@4527
   210
    }
slouken@1914
   211
    if (SDL_strcasecmp(argv[index], "--center") == 0) {
slouken@1914
   212
        state->window_x = SDL_WINDOWPOS_CENTERED;
slouken@1914
   213
        state->window_y = SDL_WINDOWPOS_CENTERED;
slouken@1914
   214
        return 1;
slouken@1914
   215
    }
slouken@1914
   216
    if (SDL_strcasecmp(argv[index], "--position") == 0) {
slouken@1914
   217
        char *x, *y;
slouken@1914
   218
        ++index;
slouken@1914
   219
        if (!argv[index]) {
slouken@1914
   220
            return -1;
slouken@1914
   221
        }
slouken@1914
   222
        x = argv[index];
slouken@1914
   223
        y = argv[index];
slouken@1914
   224
        while (*y && *y != ',') {
slouken@1914
   225
            ++y;
slouken@1914
   226
        }
slouken@1914
   227
        if (!*y) {
slouken@1914
   228
            return -1;
slouken@1914
   229
        }
slouken@1914
   230
        *y++ = '\0';
slouken@1914
   231
        state->window_x = SDL_atoi(x);
slouken@1914
   232
        state->window_y = SDL_atoi(y);
slouken@1914
   233
        return 2;
slouken@1914
   234
    }
slouken@1914
   235
    if (SDL_strcasecmp(argv[index], "--geometry") == 0) {
slouken@1914
   236
        char *w, *h;
slouken@1914
   237
        ++index;
slouken@1914
   238
        if (!argv[index]) {
slouken@1914
   239
            return -1;
slouken@1914
   240
        }
slouken@1914
   241
        w = argv[index];
slouken@1914
   242
        h = argv[index];
slouken@1914
   243
        while (*h && *h != 'x') {
slouken@1914
   244
            ++h;
slouken@1914
   245
        }
slouken@1914
   246
        if (!*h) {
slouken@1914
   247
            return -1;
slouken@1914
   248
        }
slouken@1914
   249
        *h++ = '\0';
slouken@1914
   250
        state->window_w = SDL_atoi(w);
slouken@1914
   251
        state->window_h = SDL_atoi(h);
slouken@1914
   252
        return 2;
slouken@1914
   253
    }
slouken@2209
   254
    if (SDL_strcasecmp(argv[index], "--depth") == 0) {
slouken@2209
   255
        ++index;
slouken@2209
   256
        if (!argv[index]) {
slouken@2209
   257
            return -1;
slouken@2209
   258
        }
slouken@2209
   259
        state->depth = SDL_atoi(argv[index]);
slouken@2209
   260
        return 2;
slouken@2209
   261
    }
slouken@1914
   262
    if (SDL_strcasecmp(argv[index], "--refresh") == 0) {
slouken@1914
   263
        ++index;
slouken@1914
   264
        if (!argv[index]) {
slouken@1914
   265
            return -1;
slouken@1914
   266
        }
slouken@1914
   267
        state->refresh_rate = SDL_atoi(argv[index]);
slouken@1914
   268
        return 2;
slouken@1914
   269
    }
slouken@1914
   270
    if (SDL_strcasecmp(argv[index], "--vsync") == 0) {
slouken@1965
   271
        state->render_flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1914
   272
        return 1;
slouken@1914
   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_PRESENTVSYNC:
slouken@1914
   385
        fprintf(stderr, "PresentVSync");
slouken@1914
   386
        break;
slouken@1965
   387
    case SDL_RENDERER_ACCELERATED:
slouken@1921
   388
        fprintf(stderr, "Accelerated");
slouken@1921
   389
        break;
slouken@1914
   390
    default:
slouken@1914
   391
        fprintf(stderr, "0x%8.8x", flag);
slouken@1914
   392
        break;
slouken@1914
   393
    }
slouken@1914
   394
}
slouken@1914
   395
slouken@1914
   396
static void
slouken@1914
   397
PrintPixelFormat(Uint32 format)
slouken@1914
   398
{
slouken@1914
   399
    switch (format) {
slouken@1965
   400
    case SDL_PIXELFORMAT_UNKNOWN:
slouken@1914
   401
        fprintf(stderr, "Unknwon");
slouken@1914
   402
        break;
slouken@1965
   403
    case SDL_PIXELFORMAT_INDEX1LSB:
slouken@1914
   404
        fprintf(stderr, "Index1LSB");
slouken@1914
   405
        break;
slouken@1965
   406
    case SDL_PIXELFORMAT_INDEX1MSB:
slouken@1914
   407
        fprintf(stderr, "Index1MSB");
slouken@1914
   408
        break;
slouken@1965
   409
    case SDL_PIXELFORMAT_INDEX4LSB:
slouken@1914
   410
        fprintf(stderr, "Index4LSB");
slouken@1914
   411
        break;
slouken@1965
   412
    case SDL_PIXELFORMAT_INDEX4MSB:
slouken@1914
   413
        fprintf(stderr, "Index4MSB");
slouken@1914
   414
        break;
slouken@1965
   415
    case SDL_PIXELFORMAT_INDEX8:
slouken@1914
   416
        fprintf(stderr, "Index8");
slouken@1914
   417
        break;
slouken@1965
   418
    case SDL_PIXELFORMAT_RGB332:
slouken@1914
   419
        fprintf(stderr, "RGB332");
slouken@1914
   420
        break;
slouken@1965
   421
    case SDL_PIXELFORMAT_RGB444:
slouken@1914
   422
        fprintf(stderr, "RGB444");
slouken@1914
   423
        break;
slouken@1965
   424
    case SDL_PIXELFORMAT_RGB555:
slouken@1914
   425
        fprintf(stderr, "RGB555");
slouken@1914
   426
        break;
slouken@4621
   427
    case SDL_PIXELFORMAT_BGR555:
slouken@4621
   428
        fprintf(stderr, "BGR555");
slouken@4621
   429
        break;
slouken@1965
   430
    case SDL_PIXELFORMAT_ARGB4444:
slouken@1914
   431
        fprintf(stderr, "ARGB4444");
slouken@1914
   432
        break;
lestat@3184
   433
    case SDL_PIXELFORMAT_ABGR4444:
lestat@3184
   434
        fprintf(stderr, "ABGR4444");
lestat@3184
   435
        break;
slouken@1965
   436
    case SDL_PIXELFORMAT_ARGB1555:
slouken@1914
   437
        fprintf(stderr, "ARGB1555");
slouken@1914
   438
        break;
lestat@3184
   439
    case SDL_PIXELFORMAT_ABGR1555:
lestat@3184
   440
        fprintf(stderr, "ABGR1555");
lestat@3184
   441
        break;
slouken@1965
   442
    case SDL_PIXELFORMAT_RGB565:
slouken@1914
   443
        fprintf(stderr, "RGB565");
slouken@1914
   444
        break;
lestat@3184
   445
    case SDL_PIXELFORMAT_BGR565:
lestat@3184
   446
        fprintf(stderr, "BGR565");
lestat@3184
   447
        break;
slouken@1965
   448
    case SDL_PIXELFORMAT_RGB24:
slouken@1914
   449
        fprintf(stderr, "RGB24");
slouken@1914
   450
        break;
slouken@1965
   451
    case SDL_PIXELFORMAT_BGR24:
slouken@1914
   452
        fprintf(stderr, "BGR24");
slouken@1914
   453
        break;
slouken@1965
   454
    case SDL_PIXELFORMAT_RGB888:
slouken@1914
   455
        fprintf(stderr, "RGB888");
slouken@1914
   456
        break;
slouken@1965
   457
    case SDL_PIXELFORMAT_BGR888:
slouken@1914
   458
        fprintf(stderr, "BGR888");
slouken@1914
   459
        break;
slouken@1965
   460
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1914
   461
        fprintf(stderr, "ARGB8888");
slouken@1914
   462
        break;
slouken@1965
   463
    case SDL_PIXELFORMAT_RGBA8888:
slouken@1914
   464
        fprintf(stderr, "RGBA8888");
slouken@1914
   465
        break;
slouken@1965
   466
    case SDL_PIXELFORMAT_ABGR8888:
slouken@1914
   467
        fprintf(stderr, "ABGR8888");
slouken@1914
   468
        break;
slouken@1965
   469
    case SDL_PIXELFORMAT_BGRA8888:
slouken@1914
   470
        fprintf(stderr, "BGRA8888");
slouken@1914
   471
        break;
slouken@1965
   472
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@1914
   473
        fprintf(stderr, "ARGB2101010");
slouken@1914
   474
        break;
slouken@1965
   475
    case SDL_PIXELFORMAT_YV12:
slouken@1914
   476
        fprintf(stderr, "YV12");
slouken@1914
   477
        break;
slouken@1965
   478
    case SDL_PIXELFORMAT_IYUV:
slouken@1914
   479
        fprintf(stderr, "IYUV");
slouken@1914
   480
        break;
slouken@1965
   481
    case SDL_PIXELFORMAT_YUY2:
slouken@1914
   482
        fprintf(stderr, "YUY2");
slouken@1914
   483
        break;
slouken@1965
   484
    case SDL_PIXELFORMAT_UYVY:
slouken@1914
   485
        fprintf(stderr, "UYVY");
slouken@1914
   486
        break;
slouken@1965
   487
    case SDL_PIXELFORMAT_YVYU:
slouken@1914
   488
        fprintf(stderr, "YVYU");
slouken@1914
   489
        break;
slouken@1914
   490
    default:
slouken@1914
   491
        fprintf(stderr, "0x%8.8x", format);
slouken@1914
   492
        break;
slouken@1914
   493
    }
slouken@1914
   494
}
slouken@1914
   495
slouken@1914
   496
static void
slouken@1914
   497
PrintRenderer(SDL_RendererInfo * info)
slouken@1914
   498
{
slouken@1914
   499
    int i, count;
slouken@1914
   500
slouken@1914
   501
    fprintf(stderr, "  Renderer %s:\n", info->name);
slouken@1914
   502
slouken@1914
   503
    fprintf(stderr, "    Flags: 0x%8.8X", info->flags);
slouken@1914
   504
    fprintf(stderr, " (");
slouken@1914
   505
    count = 0;
slouken@1914
   506
    for (i = 0; i < sizeof(info->flags) * 8; ++i) {
slouken@1914
   507
        Uint32 flag = (1 << i);
slouken@1914
   508
        if (info->flags & flag) {
slouken@1914
   509
            if (count > 0) {
slouken@1914
   510
                fprintf(stderr, " | ");
slouken@1914
   511
            }
slouken@1914
   512
            PrintRendererFlag(flag);
slouken@1914
   513
            ++count;
slouken@1914
   514
        }
slouken@1914
   515
    }
slouken@1914
   516
    fprintf(stderr, ")\n");
slouken@1914
   517
slouken@1914
   518
    fprintf(stderr, "    Texture formats (%d): ", info->num_texture_formats);
bob@2185
   519
    for (i = 0; i < (int) info->num_texture_formats; ++i) {
slouken@1914
   520
        if (i > 0) {
slouken@1914
   521
            fprintf(stderr, ", ");
slouken@1914
   522
        }
slouken@1914
   523
        PrintPixelFormat(info->texture_formats[i]);
slouken@1914
   524
    }
slouken@1914
   525
    fprintf(stderr, "\n");
slouken@1914
   526
slouken@1914
   527
    if (info->max_texture_width || info->max_texture_height) {
slouken@1914
   528
        fprintf(stderr, "    Max Texture Size: %dx%d\n",
slouken@1914
   529
                info->max_texture_width, info->max_texture_height);
slouken@1914
   530
    }
slouken@1914
   531
}
slouken@1914
   532
slouken@4527
   533
static SDL_Surface *
slouken@4527
   534
LoadIcon(const char *file)
slouken@4527
   535
{
slouken@4527
   536
    SDL_Surface *icon;
slouken@4527
   537
slouken@4527
   538
    /* Load the icon surface */
slouken@4527
   539
    icon = SDL_LoadBMP(file);
slouken@4527
   540
    if (icon == NULL) {
slouken@4527
   541
        fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
slouken@4527
   542
        return (NULL);
slouken@4527
   543
    }
slouken@4527
   544
slouken@4527
   545
    if (icon->format->palette == NULL) {
slouken@4527
   546
        fprintf(stderr, "Icon must have a palette!\n");
slouken@4527
   547
        SDL_FreeSurface(icon);
slouken@4527
   548
        return (NULL);
slouken@4527
   549
    }
slouken@4527
   550
slouken@4527
   551
    /* Set the colorkey */
slouken@4527
   552
    SDL_SetColorKey(icon, 1, *((Uint8 *) icon->pixels));
slouken@4527
   553
slouken@4527
   554
    return (icon);
slouken@4527
   555
}
slouken@4527
   556
slouken@1914
   557
SDL_bool
slouken@1914
   558
CommonInit(CommonState * state)
slouken@1914
   559
{
slouken@1914
   560
    int i, j, m, n;
slouken@1914
   561
    SDL_DisplayMode fullscreen_mode;
slouken@1914
   562
slouken@1914
   563
    if (state->flags & SDL_INIT_VIDEO) {
slouken@1914
   564
        if (state->verbose & VERBOSE_VIDEO) {
slouken@1914
   565
            n = SDL_GetNumVideoDrivers();
slouken@1914
   566
            if (n == 0) {
slouken@1914
   567
                fprintf(stderr, "No built-in video drivers\n");
slouken@1914
   568
            } else {
slouken@1914
   569
                fprintf(stderr, "Built-in video drivers:");
slouken@1914
   570
                for (i = 0; i < n; ++i) {
slouken@1914
   571
                    if (i > 0) {
slouken@1914
   572
                        fprintf(stderr, ",");
slouken@1914
   573
                    }
slouken@1914
   574
                    fprintf(stderr, " %s", SDL_GetVideoDriver(i));
slouken@1914
   575
                }
slouken@1914
   576
                fprintf(stderr, "\n");
slouken@1914
   577
            }
slouken@1914
   578
        }
slouken@5123
   579
        if (SDL_VideoInit(state->videodriver) < 0) {
slouken@1914
   580
            fprintf(stderr, "Couldn't initialize video driver: %s\n",
slouken@1914
   581
                    SDL_GetError());
slouken@1914
   582
            return SDL_FALSE;
slouken@1914
   583
        }
slouken@1914
   584
        if (state->verbose & VERBOSE_VIDEO) {
slouken@1914
   585
            fprintf(stderr, "Video driver: %s\n",
slouken@1914
   586
                    SDL_GetCurrentVideoDriver());
slouken@1914
   587
        }
slouken@1914
   588
lestat@3408
   589
        /* Upload GL settings */
lestat@3408
   590
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, state->gl_red_size);
lestat@3408
   591
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, state->gl_green_size);
lestat@3408
   592
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, state->gl_blue_size);
lestat@3408
   593
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, state->gl_alpha_size);
lestat@3408
   594
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, state->gl_double_buffer);
lestat@3408
   595
        SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, state->gl_buffer_size);
lestat@3408
   596
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, state->gl_depth_size);
lestat@3408
   597
        SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, state->gl_stencil_size);
lestat@3408
   598
        SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, state->gl_accum_red_size);
lestat@3408
   599
        SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, state->gl_accum_green_size);
lestat@3408
   600
        SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, state->gl_accum_blue_size);
lestat@3408
   601
        SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, state->gl_accum_alpha_size);
lestat@3408
   602
        SDL_GL_SetAttribute(SDL_GL_STEREO, state->gl_stereo);
lestat@3408
   603
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, state->gl_multisamplebuffers);
lestat@3408
   604
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, state->gl_multisamplesamples);
slouken@3571
   605
        if (state->gl_accelerated >= 0) {
slouken@3571
   606
            SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL,
slouken@3571
   607
                                state->gl_accelerated);
slouken@3571
   608
        }
lestat@3408
   609
        SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, state->gl_retained_backing);
slouken@5363
   610
        if (state->gl_major_version) {
slouken@5363
   611
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, state->gl_major_version);
slouken@5363
   612
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, state->gl_minor_version);
slouken@5363
   613
        }
lestat@3408
   614
slouken@1914
   615
        if (state->verbose & VERBOSE_MODES) {
slouken@1967
   616
            SDL_DisplayMode mode;
slouken@1914
   617
            int bpp;
slouken@1914
   618
            Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1914
   619
slouken@1914
   620
            n = SDL_GetNumVideoDisplays();
slouken@1914
   621
            fprintf(stderr, "Number of displays: %d\n", n);
slouken@1914
   622
            for (i = 0; i < n; ++i) {
slouken@1914
   623
                fprintf(stderr, "Display %d:\n", i);
slouken@1914
   624
slouken@5244
   625
                SDL_GetDesktopDisplayMode(i, &mode);
slouken@1967
   626
                SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask,
slouken@1914
   627
                                           &Bmask, &Amask);
slouken@1914
   628
                fprintf(stderr,
lestat@3184
   629
                        "  Current mode: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
lestat@3184
   630
                        mode.w, mode.h, mode.refresh_rate, bpp,
slouken@4982
   631
                        SDL_GetPixelFormatName(mode.format));
slouken@1914
   632
                if (Rmask || Gmask || Bmask) {
lestat@3184
   633
                    fprintf(stderr, "      Red Mask   = 0x%.8x\n", Rmask);
slouken@1914
   634
                    fprintf(stderr, "      Green Mask = 0x%.8x\n", Gmask);
lestat@3184
   635
                    fprintf(stderr, "      Blue Mask  = 0x%.8x\n", Bmask);
slouken@1914
   636
                    if (Amask)
slouken@1914
   637
                        fprintf(stderr, "      Alpha Mask = 0x%.8x\n", Amask);
slouken@1914
   638
                }
slouken@1914
   639
slouken@1914
   640
                /* Print available fullscreen video modes */
slouken@5244
   641
                m = SDL_GetNumDisplayModes(i);
slouken@1914
   642
                if (m == 0) {
slouken@1914
   643
                    fprintf(stderr, "No available fullscreen video modes\n");
slouken@1914
   644
                } else {
slouken@1914
   645
                    fprintf(stderr, "  Fullscreen video modes:\n");
slouken@1914
   646
                    for (j = 0; j < m; ++j) {
slouken@5244
   647
                        SDL_GetDisplayMode(i, j, &mode);
slouken@1967
   648
                        SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask,
slouken@1914
   649
                                                   &Gmask, &Bmask, &Amask);
slouken@1914
   650
                        fprintf(stderr,
lestat@3184
   651
                                "    Mode %d: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
lestat@3184
   652
                                j, mode.w, mode.h, mode.refresh_rate, bpp,
slouken@4982
   653
                                SDL_GetPixelFormatName(mode.format));
slouken@1914
   654
                        if (Rmask || Gmask || Bmask) {
lestat@3184
   655
                            fprintf(stderr, "        Red Mask   = 0x%.8x\n",
slouken@1914
   656
                                    Rmask);
slouken@1914
   657
                            fprintf(stderr, "        Green Mask = 0x%.8x\n",
slouken@1914
   658
                                    Gmask);
lestat@3184
   659
                            fprintf(stderr, "        Blue Mask  = 0x%.8x\n",
slouken@1914
   660
                                    Bmask);
slouken@1914
   661
                            if (Amask)
slouken@1914
   662
                                fprintf(stderr,
slouken@1914
   663
                                        "        Alpha Mask = 0x%.8x\n",
slouken@1914
   664
                                        Amask);
slouken@1914
   665
                        }
slouken@1914
   666
                    }
slouken@1914
   667
                }
slouken@1914
   668
            }
slouken@1914
   669
        }
slouken@1914
   670
slouken@1914
   671
        if (state->verbose & VERBOSE_RENDER) {
slouken@1914
   672
            SDL_RendererInfo info;
slouken@1914
   673
slouken@1969
   674
            n = SDL_GetNumRenderDrivers();
slouken@1914
   675
            if (n == 0) {
slouken@1914
   676
                fprintf(stderr, "No built-in render drivers\n");
slouken@1914
   677
            } else {
slouken@1923
   678
                fprintf(stderr, "Built-in render drivers:\n");
slouken@1914
   679
                for (i = 0; i < n; ++i) {
slouken@1969
   680
                    SDL_GetRenderDriverInfo(i, &info);
slouken@1914
   681
                    PrintRenderer(&info);
slouken@1914
   682
                }
slouken@1914
   683
            }
slouken@1914
   684
        }
slouken@1914
   685
slouken@3500
   686
        SDL_zero(fullscreen_mode);
slouken@1914
   687
        switch (state->depth) {
slouken@1914
   688
        case 8:
slouken@1965
   689
            fullscreen_mode.format = SDL_PIXELFORMAT_INDEX8;
slouken@1914
   690
            break;
slouken@1914
   691
        case 15:
slouken@1965
   692
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB555;
slouken@1914
   693
            break;
slouken@1914
   694
        case 16:
slouken@1965
   695
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB565;
slouken@1914
   696
            break;
lestat@3184
   697
        case 24:
lestat@3184
   698
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB24;
lestat@3184
   699
            break;
slouken@1914
   700
        default:
slouken@1965
   701
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB888;
slouken@1914
   702
            break;
slouken@1914
   703
        }
slouken@1914
   704
        fullscreen_mode.refresh_rate = state->refresh_rate;
slouken@1914
   705
slouken@1914
   706
        state->windows =
slouken@3685
   707
            (SDL_Window **) SDL_malloc(state->num_windows *
slouken@1914
   708
                                        sizeof(*state->windows));
slouken@5147
   709
        state->renderers =
slouken@5147
   710
            (SDL_Renderer **) SDL_malloc(state->num_windows *
slouken@5147
   711
                                        sizeof(*state->renderers));
slouken@5147
   712
        if (!state->windows || !state->renderers) {
slouken@1914
   713
            fprintf(stderr, "Out of memory!\n");
slouken@1914
   714
            return SDL_FALSE;
slouken@1914
   715
        }
slouken@1914
   716
        for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   717
            char title[1024];
slouken@1914
   718
slouken@1914
   719
            if (state->num_windows > 1) {
slouken@1914
   720
                SDL_snprintf(title, SDL_arraysize(title), "%s %d",
slouken@1914
   721
                             state->window_title, i + 1);
slouken@1914
   722
            } else {
slouken@1914
   723
                SDL_strlcpy(title, state->window_title, SDL_arraysize(title));
slouken@1914
   724
            }
slouken@1914
   725
            state->windows[i] =
slouken@1914
   726
                SDL_CreateWindow(title, state->window_x, state->window_y,
slouken@1914
   727
                                 state->window_w, state->window_h,
slouken@1914
   728
                                 state->window_flags);
slouken@1914
   729
            if (!state->windows[i]) {
slouken@1914
   730
                fprintf(stderr, "Couldn't create window: %s\n",
slouken@1914
   731
                        SDL_GetError());
slouken@1914
   732
                return SDL_FALSE;
slouken@1914
   733
            }
slouken@5363
   734
            SDL_GetWindowSize(state->windows[i], &state->window_w, &state->window_h);
slouken@1914
   735
slouken@3500
   736
            if (SDL_SetWindowDisplayMode(state->windows[i], &fullscreen_mode) < 0) {
slouken@3500
   737
                fprintf(stderr, "Can't set up fullscreen display mode: %s\n",
slouken@3500
   738
                        SDL_GetError());
slouken@3500
   739
                return SDL_FALSE;
slouken@3500
   740
            }
slouken@4527
   741
slouken@4527
   742
            if (state->window_icon) {
slouken@4527
   743
                SDL_Surface *icon = LoadIcon(state->window_icon);
slouken@4527
   744
                if (icon) {
slouken@4527
   745
                    SDL_SetWindowIcon(state->windows[i], icon);
slouken@4527
   746
                    SDL_FreeSurface(icon);
slouken@4527
   747
                }
slouken@4527
   748
            }
slouken@4527
   749
slouken@3500
   750
            SDL_ShowWindow(state->windows[i]);
slouken@3500
   751
slouken@5147
   752
            state->renderers[i] = NULL;
slouken@5147
   753
slouken@1933
   754
            if (!state->skip_renderer
slouken@1933
   755
                && (state->renderdriver
slouken@1933
   756
                    || !(state->window_flags & SDL_WINDOW_OPENGL))) {
slouken@1914
   757
                m = -1;
slouken@1914
   758
                if (state->renderdriver) {
slouken@1914
   759
                    SDL_RendererInfo info;
slouken@1969
   760
                    n = SDL_GetNumRenderDrivers();
slouken@1914
   761
                    for (j = 0; j < n; ++j) {
slouken@1969
   762
                        SDL_GetRenderDriverInfo(j, &info);
slouken@1914
   763
                        if (SDL_strcasecmp(info.name, state->renderdriver) ==
slouken@1914
   764
                            0) {
slouken@1914
   765
                            m = j;
slouken@1914
   766
                            break;
slouken@1914
   767
                        }
slouken@1914
   768
                    }
slouken@1914
   769
                    if (m == n) {
slouken@1914
   770
                        fprintf(stderr,
slouken@1914
   771
                                "Couldn't find render driver named %s",
slouken@1914
   772
                                state->renderdriver);
slouken@1914
   773
                        return SDL_FALSE;
slouken@1914
   774
                    }
slouken@1914
   775
                }
slouken@5147
   776
                state->renderers[i] = SDL_CreateRenderer(state->windows[i],
slouken@5147
   777
                                            m, state->render_flags);
slouken@5147
   778
                if (!state->renderers[i]) {
slouken@1914
   779
                    fprintf(stderr, "Couldn't create renderer: %s\n",
slouken@1914
   780
                            SDL_GetError());
slouken@1914
   781
                    return SDL_FALSE;
slouken@1914
   782
                }
slouken@1923
   783
                if (state->verbose & VERBOSE_RENDER) {
slouken@1923
   784
                    SDL_RendererInfo info;
slouken@1923
   785
slouken@1923
   786
                    fprintf(stderr, "Current renderer:\n");
slouken@5147
   787
                    SDL_GetRendererInfo(state->renderers[i], &info);
slouken@1923
   788
                    PrintRenderer(&info);
slouken@1923
   789
                }
slouken@1914
   790
            }
slouken@1914
   791
        }
slouken@1914
   792
    }
slouken@1914
   793
slouken@1914
   794
    if (state->flags & SDL_INIT_AUDIO) {
slouken@1914
   795
        if (state->verbose & VERBOSE_AUDIO) {
slouken@1914
   796
            n = SDL_GetNumAudioDrivers();
slouken@1914
   797
            if (n == 0) {
slouken@1914
   798
                fprintf(stderr, "No built-in audio drivers\n");
slouken@1914
   799
            } else {
slouken@1914
   800
                fprintf(stderr, "Built-in audio drivers:");
slouken@1914
   801
                for (i = 0; i < n; ++i) {
slouken@1914
   802
                    if (i > 0) {
slouken@1914
   803
                        fprintf(stderr, ",");
slouken@1914
   804
                    }
slouken@1914
   805
                    fprintf(stderr, " %s", SDL_GetAudioDriver(i));
slouken@1914
   806
                }
slouken@1914
   807
                fprintf(stderr, "\n");
slouken@1914
   808
            }
slouken@1914
   809
        }
slouken@1914
   810
        if (SDL_AudioInit(state->audiodriver) < 0) {
slouken@1914
   811
            fprintf(stderr, "Couldn't initialize audio driver: %s\n",
slouken@1914
   812
                    SDL_GetError());
slouken@1914
   813
            return SDL_FALSE;
slouken@1914
   814
        }
slouken@1914
   815
        if (state->verbose & VERBOSE_VIDEO) {
slouken@1914
   816
            fprintf(stderr, "Audio driver: %s\n",
slouken@1914
   817
                    SDL_GetCurrentAudioDriver());
slouken@1914
   818
        }
slouken@1914
   819
slouken@1914
   820
        if (SDL_OpenAudio(&state->audiospec, NULL) < 0) {
slouken@1914
   821
            fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@1914
   822
            return SDL_FALSE;
slouken@1914
   823
        }
slouken@1914
   824
    }
slouken@1914
   825
slouken@1914
   826
    return SDL_TRUE;
slouken@1914
   827
}
slouken@1914
   828
slouken@1914
   829
static void
slouken@1914
   830
PrintEvent(SDL_Event * event)
slouken@1914
   831
{
slouken@5404
   832
    if (event->type == SDL_MOUSEMOTION) {
slouken@5404
   833
        /* Mouse motion is really spammy */
slouken@5406
   834
        //return;
slouken@5404
   835
    }
slouken@5404
   836
slouken@1914
   837
    fprintf(stderr, "SDL EVENT: ");
slouken@1914
   838
    switch (event->type) {
slouken@1914
   839
    case SDL_WINDOWEVENT:
slouken@1914
   840
        switch (event->window.event) {
slouken@1914
   841
        case SDL_WINDOWEVENT_SHOWN:
slouken@1914
   842
            fprintf(stderr, "Window %d shown", event->window.windowID);
slouken@1914
   843
            break;
slouken@1914
   844
        case SDL_WINDOWEVENT_HIDDEN:
slouken@1914
   845
            fprintf(stderr, "Window %d hidden", event->window.windowID);
slouken@1914
   846
            break;
slouken@1914
   847
        case SDL_WINDOWEVENT_EXPOSED:
slouken@1970
   848
            fprintf(stderr, "Window %d exposed", event->window.windowID);
slouken@1914
   849
            break;
slouken@1914
   850
        case SDL_WINDOWEVENT_MOVED:
slouken@1970
   851
            fprintf(stderr, "Window %d moved to %d,%d",
slouken@1914
   852
                    event->window.windowID, event->window.data1,
slouken@1914
   853
                    event->window.data2);
slouken@1914
   854
            break;
slouken@1914
   855
        case SDL_WINDOWEVENT_RESIZED:
slouken@1970
   856
            fprintf(stderr, "Window %d resized to %dx%d",
slouken@1914
   857
                    event->window.windowID, event->window.data1,
slouken@1914
   858
                    event->window.data2);
slouken@1914
   859
            break;
slouken@1914
   860
        case SDL_WINDOWEVENT_MINIMIZED:
slouken@1914
   861
            fprintf(stderr, "Window %d minimized", event->window.windowID);
slouken@1914
   862
            break;
slouken@1914
   863
        case SDL_WINDOWEVENT_MAXIMIZED:
slouken@1914
   864
            fprintf(stderr, "Window %d maximized", event->window.windowID);
slouken@1914
   865
            break;
slouken@1914
   866
        case SDL_WINDOWEVENT_RESTORED:
slouken@1914
   867
            fprintf(stderr, "Window %d restored", event->window.windowID);
slouken@1914
   868
            break;
slouken@1914
   869
        case SDL_WINDOWEVENT_ENTER:
slouken@1914
   870
            fprintf(stderr, "Mouse entered window %d",
slouken@1914
   871
                    event->window.windowID);
slouken@1914
   872
            break;
slouken@1914
   873
        case SDL_WINDOWEVENT_LEAVE:
slouken@1914
   874
            fprintf(stderr, "Mouse left window %d", event->window.windowID);
slouken@1914
   875
            break;
slouken@1914
   876
        case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1914
   877
            fprintf(stderr, "Window %d gained keyboard focus",
slouken@1914
   878
                    event->window.windowID);
slouken@1914
   879
            break;
slouken@1914
   880
        case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1914
   881
            fprintf(stderr, "Window %d lost keyboard focus",
slouken@1914
   882
                    event->window.windowID);
slouken@1914
   883
            break;
slouken@1933
   884
        case SDL_WINDOWEVENT_CLOSE:
slouken@1933
   885
            fprintf(stderr, "Window %d closed", event->window.windowID);
slouken@1933
   886
            break;
slouken@1914
   887
        default:
slouken@1914
   888
            fprintf(stderr, "Window %d got unknown event %d",
slouken@1914
   889
                    event->window.windowID, event->window.event);
slouken@1914
   890
            break;
slouken@1914
   891
        }
slouken@1914
   892
        break;
slouken@1914
   893
    case SDL_KEYDOWN:
slouken@2268
   894
        fprintf(stderr,
slouken@4465
   895
                "Keyboard: key pressed  in window %d: scancode 0x%08X = %s, keycode 0x%08X = %s",
slouken@4465
   896
                event->key.windowID,
slouken@2306
   897
                event->key.keysym.scancode,
slouken@2306
   898
                SDL_GetScancodeName(event->key.keysym.scancode),
slouken@2306
   899
                event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym));
slouken@1914
   900
        break;
slouken@1914
   901
    case SDL_KEYUP:
slouken@2268
   902
        fprintf(stderr,
slouken@4465
   903
                "Keyboard: key released in window %d: scancode 0x%08X = %s, keycode 0x%08X = %s",
slouken@4465
   904
                event->key.windowID,
slouken@2306
   905
                event->key.keysym.scancode,
slouken@2306
   906
                SDL_GetScancodeName(event->key.keysym.scancode),
slouken@2306
   907
                event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym));
slouken@1914
   908
        break;
slouken@1914
   909
    case SDL_TEXTINPUT:
slouken@4465
   910
        fprintf(stderr, "Keyboard: text input \"%s\" in window %d",
slouken@4465
   911
                event->text.text, event->text.windowID);
slouken@1914
   912
        break;
slouken@1914
   913
    case SDL_MOUSEMOTION:
slouken@4465
   914
        fprintf(stderr, "Mouse: moved to %d,%d (%d,%d) in window %d",
slouken@4465
   915
                event->motion.x, event->motion.y,
slouken@1914
   916
                event->motion.xrel, event->motion.yrel,
slouken@1914
   917
                event->motion.windowID);
slouken@1914
   918
        break;
slouken@1914
   919
    case SDL_MOUSEBUTTONDOWN:
slouken@4465
   920
        fprintf(stderr, "Mouse: button %d pressed at %d,%d in window %d",
slouken@4465
   921
                event->button.button, event->button.x, event->button.y,
slouken@4465
   922
                event->button.windowID);
slouken@1914
   923
        break;
slouken@1914
   924
    case SDL_MOUSEBUTTONUP:
slouken@4465
   925
        fprintf(stderr, "Mouse: button %d released at %d,%d in window %d",
slouken@4465
   926
                event->button.button, event->button.x, event->button.y,
slouken@4465
   927
                event->button.windowID);
slouken@1914
   928
        break;
slouken@1914
   929
    case SDL_MOUSEWHEEL:
slouken@2174
   930
        fprintf(stderr,
slouken@4465
   931
                "Mouse: wheel scrolled %d in x and %d in y in window %d",
slouken@4465
   932
                event->wheel.x, event->wheel.y, event->wheel.windowID);
slouken@1914
   933
        break;
slouken@1914
   934
    case SDL_JOYBALLMOTION:
slouken@1914
   935
        fprintf(stderr, "Joystick %d: ball %d moved by %d,%d",
slouken@1914
   936
                event->jball.which, event->jball.ball, event->jball.xrel,
slouken@1914
   937
                event->jball.yrel);
slouken@1914
   938
        break;
slouken@1914
   939
    case SDL_JOYHATMOTION:
slouken@1914
   940
        fprintf(stderr, "Joystick %d: hat %d moved to ", event->jhat.which,
slouken@1914
   941
                event->jhat.hat);
slouken@1914
   942
        switch (event->jhat.value) {
slouken@1914
   943
        case SDL_HAT_CENTERED:
slouken@1914
   944
            fprintf(stderr, "CENTER");
slouken@1914
   945
            break;
slouken@1914
   946
        case SDL_HAT_UP:
slouken@1914
   947
            fprintf(stderr, "UP");
slouken@1914
   948
            break;
slouken@1914
   949
        case SDL_HAT_RIGHTUP:
slouken@1914
   950
            fprintf(stderr, "RIGHTUP");
slouken@1914
   951
            break;
slouken@1914
   952
        case SDL_HAT_RIGHT:
slouken@1914
   953
            fprintf(stderr, "RIGHT");
slouken@1914
   954
            break;
slouken@1914
   955
        case SDL_HAT_RIGHTDOWN:
slouken@1914
   956
            fprintf(stderr, "RIGHTDOWN");
slouken@1914
   957
            break;
slouken@1914
   958
        case SDL_HAT_DOWN:
slouken@1914
   959
            fprintf(stderr, "DOWN");
slouken@1914
   960
            break;
slouken@1914
   961
        case SDL_HAT_LEFTDOWN:
slouken@1914
   962
            fprintf(stderr, "LEFTDOWN");
slouken@1914
   963
            break;
slouken@1914
   964
        case SDL_HAT_LEFT:
slouken@1914
   965
            fprintf(stderr, "LEFT");
slouken@1914
   966
            break;
slouken@1914
   967
        case SDL_HAT_LEFTUP:
slouken@1914
   968
            fprintf(stderr, "LEFTUP");
slouken@1914
   969
            break;
slouken@1914
   970
        default:
slouken@1914
   971
            fprintf(stderr, "UNKNOWN");
slouken@1914
   972
            break;
slouken@1914
   973
        }
slouken@1914
   974
        break;
slouken@1914
   975
    case SDL_JOYBUTTONDOWN:
slouken@1914
   976
        fprintf(stderr, "Joystick %d: button %d pressed",
slouken@1914
   977
                event->jbutton.which, event->jbutton.button);
slouken@1914
   978
        break;
slouken@1914
   979
    case SDL_JOYBUTTONUP:
slouken@1914
   980
        fprintf(stderr, "Joystick %d: button %d released",
slouken@1914
   981
                event->jbutton.which, event->jbutton.button);
slouken@1914
   982
        break;
slouken@4503
   983
    case SDL_CLIPBOARDUPDATE:
slouken@4503
   984
        fprintf(stderr, "Clipboard updated");
slouken@4503
   985
        break;
slouken@1914
   986
    case SDL_QUIT:
slouken@1914
   987
        fprintf(stderr, "Quit requested");
slouken@1914
   988
        break;
slouken@1914
   989
    case SDL_USEREVENT:
slouken@1914
   990
        fprintf(stderr, "User event %d", event->user.code);
slouken@1914
   991
        break;
slouken@1914
   992
    default:
slouken@1914
   993
        fprintf(stderr, "Unknown event %d", event->type);
slouken@1914
   994
        break;
slouken@1914
   995
    }
slouken@1914
   996
    fprintf(stderr, "\n");
slouken@1914
   997
}
slouken@1914
   998
slouken@5463
   999
static void
slouken@5463
  1000
ScreenShot(SDL_Renderer *renderer)
slouken@5463
  1001
{
slouken@5463
  1002
    SDL_Rect viewport;
slouken@5463
  1003
    SDL_Surface *surface;
slouken@5463
  1004
slouken@5463
  1005
    if (!renderer) {
slouken@5463
  1006
        return;
slouken@5463
  1007
    }
slouken@5463
  1008
slouken@5463
  1009
    SDL_RenderGetViewport(renderer, &viewport);
slouken@5463
  1010
    surface = SDL_CreateRGBSurface(0, viewport.w, viewport.h, 24,
slouken@5463
  1011
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@5463
  1012
                    0x00FF0000, 0x0000FF00, 0x000000FF,
slouken@5463
  1013
#else
slouken@5463
  1014
                    0x000000FF, 0x0000FF00, 0x00FF0000,
slouken@5463
  1015
#endif
slouken@5463
  1016
                    0x00000000);
slouken@5463
  1017
    if (!surface) {
slouken@5463
  1018
        fprintf(stderr, "Couldn't create surface: %s\n", SDL_GetError());
slouken@5463
  1019
        return;
slouken@5463
  1020
    }
slouken@5463
  1021
slouken@5463
  1022
    if (SDL_RenderReadPixels(renderer, NULL, surface->format->format,
slouken@5463
  1023
                             surface->pixels, surface->pitch) < 0) {
slouken@5463
  1024
        fprintf(stderr, "Couldn't read screen: %s\n", SDL_GetError());
slouken@5463
  1025
        return;
slouken@5463
  1026
    }
slouken@5463
  1027
slouken@5463
  1028
    if (SDL_SaveBMP(surface, "screenshot.bmp") < 0) {
slouken@5463
  1029
        fprintf(stderr, "Couldn't save screenshot.bmp: %s\n", SDL_GetError());
slouken@5463
  1030
        return;
slouken@5463
  1031
    }
slouken@5463
  1032
}
slouken@5463
  1033
slouken@1914
  1034
void
slouken@1914
  1035
CommonEvent(CommonState * state, SDL_Event * event, int *done)
slouken@1914
  1036
{
slouken@3514
  1037
    int i;
slouken@3514
  1038
slouken@1914
  1039
    if (state->verbose & VERBOSE_EVENT) {
slouken@1914
  1040
        PrintEvent(event);
slouken@1914
  1041
    }
slouken@1914
  1042
slouken@1914
  1043
    switch (event->type) {
slouken@1914
  1044
    case SDL_WINDOWEVENT:
slouken@1914
  1045
        switch (event->window.event) {
slouken@1914
  1046
        case SDL_WINDOWEVENT_CLOSE:
krogoway@5113
  1047
			{
slouken@5463
  1048
                SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
slouken@5463
  1049
                if (window) {
slouken@5463
  1050
					SDL_DestroyWindow(window);
krogoway@5113
  1051
				}
krogoway@5113
  1052
			}
slouken@1914
  1053
            break;
slouken@1914
  1054
        }
slouken@1914
  1055
        break;
slouken@1914
  1056
    case SDL_KEYDOWN:
slouken@1914
  1057
        switch (event->key.keysym.sym) {
slouken@1914
  1058
            /* Add hotkeys here */
slouken@5463
  1059
        case SDLK_PRINTSCREEN: {
slouken@5463
  1060
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
slouken@5463
  1061
                if (window) {
slouken@5463
  1062
                    for (i = 0; i < state->num_windows; ++i) {
slouken@5463
  1063
                        if (window == state->windows[i]) {
slouken@5463
  1064
                            ScreenShot(state->renderers[i]);
slouken@5463
  1065
                        }
slouken@5463
  1066
                    }
slouken@5463
  1067
                }
slouken@5463
  1068
            }
slouken@5463
  1069
            break;
slouken@4497
  1070
        case SDLK_c:
slouken@4497
  1071
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@4497
  1072
                /* Ctrl-C copy awesome text! */
slouken@4497
  1073
                SDL_SetClipboardText("SDL rocks!\nYou know it!");
slouken@4497
  1074
                printf("Copied text to clipboard\n");
slouken@4497
  1075
            }
slouken@4497
  1076
            break;
slouken@4497
  1077
        case SDLK_v:
slouken@4497
  1078
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@4497
  1079
                /* Ctrl-V paste awesome text! */
slouken@4497
  1080
                char *text = SDL_GetClipboardText();
slouken@4497
  1081
                if (*text) {
slouken@4497
  1082
                    printf("Clipboard: %s\n", text);
slouken@4497
  1083
                } else {
slouken@4497
  1084
                    printf("Clipboard is empty\n");
slouken@4497
  1085
                }
slouken@4497
  1086
                SDL_free(text);
slouken@4497
  1087
            }
slouken@4497
  1088
            break;
slouken@1965
  1089
        case SDLK_g:
slouken@1965
  1090
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@1965
  1091
                /* Ctrl-G toggle grab */
slouken@5349
  1092
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
slouken@5349
  1093
                if (window) {
slouken@5349
  1094
                    SDL_SetWindowGrab(window, !SDL_GetWindowGrab(window));
slouken@5349
  1095
                }
slouken@1965
  1096
            }
slouken@1965
  1097
            break;
slouken@4517
  1098
        case SDLK_m:
slouken@4517
  1099
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@4517
  1100
                /* Ctrl-M maximize */
slouken@5349
  1101
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
slouken@5349
  1102
                if (window) {
slouken@5349
  1103
                    Uint32 flags = SDL_GetWindowFlags(window);
slouken@5349
  1104
                    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@5349
  1105
                        SDL_RestoreWindow(window);
slouken@5349
  1106
                    } else {
slouken@5349
  1107
                        SDL_MaximizeWindow(window);
slouken@4517
  1108
                    }
slouken@4517
  1109
                }
slouken@4517
  1110
            }
slouken@4517
  1111
            break;
slouken@5406
  1112
        case SDLK_r:
slouken@5406
  1113
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@5406
  1114
                /* Ctrl-R toggle mouse relative mode */
slouken@5406
  1115
                SDL_SetRelativeMouseMode(!SDL_GetRelativeMouseMode());
slouken@5406
  1116
            }
slouken@5406
  1117
            break;
slouken@3514
  1118
        case SDLK_z:
slouken@3514
  1119
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@3514
  1120
                /* Ctrl-Z minimize */
slouken@5349
  1121
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
slouken@5349
  1122
                if (window) {
slouken@5349
  1123
                    SDL_MinimizeWindow(window);
slouken@5246
  1124
                }
slouken@5246
  1125
            }
slouken@5246
  1126
            break;
slouken@5246
  1127
        case SDLK_RETURN:
slouken@5246
  1128
            if (event->key.keysym.mod & KMOD_CTRL) {
slouken@5246
  1129
                /* Ctrl-Enter toggle fullscreen */
slouken@5349
  1130
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
slouken@5349
  1131
                if (window) {
slouken@5349
  1132
                    Uint32 flags = SDL_GetWindowFlags(window);
slouken@5349
  1133
                    if (flags & SDL_WINDOW_FULLSCREEN) {
slouken@5349
  1134
                        SDL_SetWindowFullscreen(window, SDL_FALSE);
slouken@5349
  1135
                    } else {
slouken@5349
  1136
                        SDL_SetWindowFullscreen(window, SDL_TRUE);
slouken@5246
  1137
                    }
slouken@3514
  1138
                }
slouken@3514
  1139
            }
slouken@3514
  1140
            break;
slouken@1914
  1141
        case SDLK_ESCAPE:
slouken@1914
  1142
            *done = 1;
slouken@1914
  1143
            break;
slouken@1914
  1144
        default:
slouken@1914
  1145
            break;
slouken@1914
  1146
        }
slouken@1914
  1147
        break;
slouken@1914
  1148
    case SDL_QUIT:
slouken@1914
  1149
        *done = 1;
slouken@1914
  1150
        break;
slouken@1914
  1151
    }
slouken@1914
  1152
}
slouken@1914
  1153
slouken@1914
  1154
void
slouken@1914
  1155
CommonQuit(CommonState * state)
slouken@1914
  1156
{
slouken@5147
  1157
    int i;
slouken@5147
  1158
slouken@5147
  1159
    if (state->windows) {
slouken@5147
  1160
        SDL_free(state->windows);
slouken@5147
  1161
    }
slouken@5147
  1162
    if (state->renderers) {
slouken@5147
  1163
        for (i = 0; i < state->num_windows; ++i) {
slouken@5147
  1164
            if (state->renderers[i]) {
slouken@5147
  1165
                SDL_DestroyRenderer(state->renderers[i]);
slouken@5147
  1166
            }
slouken@5147
  1167
        }
slouken@5147
  1168
        SDL_free(state->renderers);
slouken@5147
  1169
    }
slouken@1914
  1170
    if (state->flags & SDL_INIT_VIDEO) {
slouken@1914
  1171
        SDL_VideoQuit();
slouken@1914
  1172
    }
slouken@1914
  1173
    if (state->flags & SDL_INIT_AUDIO) {
slouken@1914
  1174
        SDL_AudioQuit();
slouken@1914
  1175
    }
slouken@1914
  1176
    SDL_free(state);
slouken@1914
  1177
}
slouken@3514
  1178
slouken@3514
  1179
/* vi: set ts=4 sw=4 expandtab: */