src/test/SDL_test_common.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 12 Oct 2017 13:44:28 -0700
changeset 11610 6dea196ecbcb
parent 11604 634c34c79fbc
child 11615 f5b657276413
permissions -rw-r--r--
Added functions to query and set the SDL memory allocation functions:
SDL_GetMemoryFunctions()
SDL_SetMemoryFunctions()
SDL_GetNumAllocations()
aschiffler@6727
     1
/*
aschiffler@6727
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
aschiffler@6727
     4
aschiffler@6727
     5
  This software is provided 'as-is', without any express or implied
aschiffler@6727
     6
  warranty.  In no event will the authors be held liable for any damages
aschiffler@6727
     7
  arising from the use of this software.
aschiffler@6727
     8
aschiffler@6727
     9
  Permission is granted to anyone to use this software for any purpose,
aschiffler@6727
    10
  including commercial applications, and to alter it and redistribute it
aschiffler@6727
    11
  freely, subject to the following restrictions:
aschiffler@6727
    12
aschiffler@6727
    13
  1. The origin of this software must not be misrepresented; you must not
aschiffler@6727
    14
     claim that you wrote the original software. If you use this software
aschiffler@6727
    15
     in a product, an acknowledgment in the product documentation would be
aschiffler@6727
    16
     appreciated but is not required.
aschiffler@6727
    17
  2. Altered source versions must be plainly marked as such, and must not be
aschiffler@6727
    18
     misrepresented as being the original software.
aschiffler@6727
    19
  3. This notice may not be removed or altered from any source distribution.
aschiffler@6727
    20
*/
aschiffler@6727
    21
aschiffler@6727
    22
/* Ported from original test\common.c file. */
aschiffler@6727
    23
aschiffler@6727
    24
#include "SDL_config.h"
aschiffler@6727
    25
#include "SDL_test.h"
aschiffler@6727
    26
aschiffler@6727
    27
#include <stdio.h>
aschiffler@6727
    28
aschiffler@6727
    29
#define VIDEO_USAGE \
icculus@7895
    30
"[--video driver] [--renderer driver] [--gldebug] [--info all|video|modes|render|event] [--log all|error|system|audio|video|render|input] [--display N] [--fullscreen | --fullscreen-desktop | --windows N] [--title title] [--icon icon.bmp] [--center | --position X,Y] [--geometry WxH] [--min-geometry WxH] [--max-geometry WxH] [--logical WxH] [--scale N] [--depth N] [--refresh R] [--vsync] [--noframe] [--resize] [--minimize] [--maximize] [--grab] [--allow-highdpi]"
aschiffler@6727
    31
aschiffler@6727
    32
#define AUDIO_USAGE \
aschiffler@6727
    33
"[--rate N] [--format U8|S8|U16|U16LE|U16BE|S16|S16LE|S16BE] [--channels N] [--samples N]"
aschiffler@6727
    34
slouken@11425
    35
static void SDL_snprintfcat(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ... )
slouken@11425
    36
{
slouken@11425
    37
    size_t length = SDL_strlen(text);
slouken@11425
    38
    va_list ap;
slouken@11425
    39
slouken@11425
    40
    va_start(ap, fmt);
slouken@11425
    41
    text += length;
slouken@11425
    42
    maxlen -= length;
slouken@11425
    43
    SDL_vsnprintf(text, maxlen, fmt, ap);
slouken@11425
    44
    va_end(ap);
slouken@11425
    45
}
slouken@11425
    46
aschiffler@6727
    47
SDLTest_CommonState *
aschiffler@6727
    48
SDLTest_CommonCreateState(char **argv, Uint32 flags)
aschiffler@6727
    49
{
slouken@11610
    50
    int i;
slouken@11610
    51
    SDLTest_CommonState *state;
slouken@11610
    52
slouken@11610
    53
    /* Do this first so we catch all allocations */
slouken@11610
    54
    for (i = 1; argv[i]; ++i) {
slouken@11610
    55
        if (SDL_strcasecmp(argv[i], "--trackmem") == 0) {
slouken@11610
    56
            SDLTest_TrackAllocations();
slouken@11610
    57
            break;
slouken@11610
    58
        }
slouken@11610
    59
    }
slouken@11610
    60
slouken@11610
    61
    state = (SDLTest_CommonState *)SDL_calloc(1, sizeof(*state));
aschiffler@6727
    62
    if (!state) {
aschiffler@6727
    63
        SDL_OutOfMemory();
aschiffler@6727
    64
        return NULL;
aschiffler@6727
    65
    }
aschiffler@6727
    66
aschiffler@6727
    67
    /* Initialize some defaults */
aschiffler@6727
    68
    state->argv = argv;
aschiffler@6727
    69
    state->flags = flags;
aschiffler@6727
    70
    state->window_title = argv[0];
aschiffler@6727
    71
    state->window_flags = 0;
aschiffler@6727
    72
    state->window_x = SDL_WINDOWPOS_UNDEFINED;
aschiffler@6727
    73
    state->window_y = SDL_WINDOWPOS_UNDEFINED;
aschiffler@6727
    74
    state->window_w = DEFAULT_WINDOW_WIDTH;
aschiffler@6727
    75
    state->window_h = DEFAULT_WINDOW_HEIGHT;
aschiffler@6727
    76
    state->num_windows = 1;
aschiffler@6727
    77
    state->audiospec.freq = 22050;
aschiffler@6727
    78
    state->audiospec.format = AUDIO_S16;
aschiffler@6727
    79
    state->audiospec.channels = 2;
aschiffler@6727
    80
    state->audiospec.samples = 2048;
aschiffler@6727
    81
aschiffler@6727
    82
    /* Set some very sane GL defaults */
aschiffler@6727
    83
    state->gl_red_size = 3;
aschiffler@6727
    84
    state->gl_green_size = 3;
aschiffler@6727
    85
    state->gl_blue_size = 2;
aschiffler@6727
    86
    state->gl_alpha_size = 0;
aschiffler@6727
    87
    state->gl_buffer_size = 0;
aschiffler@6727
    88
    state->gl_depth_size = 16;
aschiffler@6727
    89
    state->gl_stencil_size = 0;
aschiffler@6727
    90
    state->gl_double_buffer = 1;
aschiffler@6727
    91
    state->gl_accum_red_size = 0;
aschiffler@6727
    92
    state->gl_accum_green_size = 0;
aschiffler@6727
    93
    state->gl_accum_blue_size = 0;
aschiffler@6727
    94
    state->gl_accum_alpha_size = 0;
aschiffler@6727
    95
    state->gl_stereo = 0;
aschiffler@6727
    96
    state->gl_multisamplebuffers = 0;
aschiffler@6727
    97
    state->gl_multisamplesamples = 0;
aschiffler@6727
    98
    state->gl_retained_backing = 1;
aschiffler@6727
    99
    state->gl_accelerated = -1;
slouken@7194
   100
    state->gl_debug = 0;
aschiffler@6727
   101
aschiffler@6727
   102
    return state;
aschiffler@6727
   103
}
aschiffler@6727
   104
aschiffler@6727
   105
int
aschiffler@6727
   106
SDLTest_CommonArg(SDLTest_CommonState * state, int index)
aschiffler@6727
   107
{
aschiffler@6727
   108
    char **argv = state->argv;
aschiffler@6727
   109
aschiffler@6727
   110
    if (SDL_strcasecmp(argv[index], "--video") == 0) {
aschiffler@6727
   111
        ++index;
aschiffler@6727
   112
        if (!argv[index]) {
aschiffler@6727
   113
            return -1;
aschiffler@6727
   114
        }
aschiffler@6727
   115
        state->videodriver = argv[index];
aschiffler@6727
   116
        return 2;
aschiffler@6727
   117
    }
aschiffler@6727
   118
    if (SDL_strcasecmp(argv[index], "--renderer") == 0) {
aschiffler@6727
   119
        ++index;
aschiffler@6727
   120
        if (!argv[index]) {
aschiffler@6727
   121
            return -1;
aschiffler@6727
   122
        }
aschiffler@6727
   123
        state->renderdriver = argv[index];
aschiffler@6727
   124
        return 2;
aschiffler@6727
   125
    }
slouken@7194
   126
    if (SDL_strcasecmp(argv[index], "--gldebug") == 0) {
slouken@7194
   127
        state->gl_debug = 1;
slouken@7194
   128
        return 1;
slouken@7194
   129
    }
aschiffler@6727
   130
    if (SDL_strcasecmp(argv[index], "--info") == 0) {
aschiffler@6727
   131
        ++index;
aschiffler@6727
   132
        if (!argv[index]) {
aschiffler@6727
   133
            return -1;
aschiffler@6727
   134
        }
aschiffler@6727
   135
        if (SDL_strcasecmp(argv[index], "all") == 0) {
aschiffler@6727
   136
            state->verbose |=
aschiffler@6727
   137
                (VERBOSE_VIDEO | VERBOSE_MODES | VERBOSE_RENDER |
aschiffler@6727
   138
                 VERBOSE_EVENT);
aschiffler@6727
   139
            return 2;
aschiffler@6727
   140
        }
aschiffler@6727
   141
        if (SDL_strcasecmp(argv[index], "video") == 0) {
aschiffler@6727
   142
            state->verbose |= VERBOSE_VIDEO;
aschiffler@6727
   143
            return 2;
aschiffler@6727
   144
        }
aschiffler@6727
   145
        if (SDL_strcasecmp(argv[index], "modes") == 0) {
aschiffler@6727
   146
            state->verbose |= VERBOSE_MODES;
aschiffler@6727
   147
            return 2;
aschiffler@6727
   148
        }
aschiffler@6727
   149
        if (SDL_strcasecmp(argv[index], "render") == 0) {
aschiffler@6727
   150
            state->verbose |= VERBOSE_RENDER;
aschiffler@6727
   151
            return 2;
aschiffler@6727
   152
        }
aschiffler@6727
   153
        if (SDL_strcasecmp(argv[index], "event") == 0) {
aschiffler@6727
   154
            state->verbose |= VERBOSE_EVENT;
aschiffler@6727
   155
            return 2;
aschiffler@6727
   156
        }
aschiffler@6727
   157
        return -1;
aschiffler@6727
   158
    }
aschiffler@6727
   159
    if (SDL_strcasecmp(argv[index], "--log") == 0) {
aschiffler@6727
   160
        ++index;
aschiffler@6727
   161
        if (!argv[index]) {
aschiffler@6727
   162
            return -1;
aschiffler@6727
   163
        }
aschiffler@6727
   164
        if (SDL_strcasecmp(argv[index], "all") == 0) {
aschiffler@6727
   165
            SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
aschiffler@6727
   166
            return 2;
aschiffler@6727
   167
        }
aschiffler@6727
   168
        if (SDL_strcasecmp(argv[index], "error") == 0) {
aschiffler@6727
   169
            SDL_LogSetPriority(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_VERBOSE);
aschiffler@6727
   170
            return 2;
aschiffler@6727
   171
        }
aschiffler@6727
   172
        if (SDL_strcasecmp(argv[index], "system") == 0) {
aschiffler@6727
   173
            SDL_LogSetPriority(SDL_LOG_CATEGORY_SYSTEM, SDL_LOG_PRIORITY_VERBOSE);
aschiffler@6727
   174
            return 2;
aschiffler@6727
   175
        }
aschiffler@6727
   176
        if (SDL_strcasecmp(argv[index], "audio") == 0) {
aschiffler@6727
   177
            SDL_LogSetPriority(SDL_LOG_CATEGORY_AUDIO, SDL_LOG_PRIORITY_VERBOSE);
aschiffler@6727
   178
            return 2;
aschiffler@6727
   179
        }
aschiffler@6727
   180
        if (SDL_strcasecmp(argv[index], "video") == 0) {
aschiffler@6727
   181
            SDL_LogSetPriority(SDL_LOG_CATEGORY_VIDEO, SDL_LOG_PRIORITY_VERBOSE);
aschiffler@6727
   182
            return 2;
aschiffler@6727
   183
        }
aschiffler@6727
   184
        if (SDL_strcasecmp(argv[index], "render") == 0) {
aschiffler@6727
   185
            SDL_LogSetPriority(SDL_LOG_CATEGORY_RENDER, SDL_LOG_PRIORITY_VERBOSE);
aschiffler@6727
   186
            return 2;
aschiffler@6727
   187
        }
aschiffler@6727
   188
        if (SDL_strcasecmp(argv[index], "input") == 0) {
aschiffler@6727
   189
            SDL_LogSetPriority(SDL_LOG_CATEGORY_INPUT, SDL_LOG_PRIORITY_VERBOSE);
aschiffler@6727
   190
            return 2;
aschiffler@6727
   191
        }
aschiffler@6727
   192
        return -1;
aschiffler@6727
   193
    }
aschiffler@6727
   194
    if (SDL_strcasecmp(argv[index], "--display") == 0) {
aschiffler@6727
   195
        ++index;
aschiffler@6727
   196
        if (!argv[index]) {
aschiffler@6727
   197
            return -1;
aschiffler@6727
   198
        }
aschiffler@6727
   199
        state->display = SDL_atoi(argv[index]);
aschiffler@6727
   200
        if (SDL_WINDOWPOS_ISUNDEFINED(state->window_x)) {
aschiffler@6727
   201
            state->window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(state->display);
aschiffler@6727
   202
            state->window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(state->display);
aschiffler@6727
   203
        }
aschiffler@6727
   204
        if (SDL_WINDOWPOS_ISCENTERED(state->window_x)) {
aschiffler@6727
   205
            state->window_x = SDL_WINDOWPOS_CENTERED_DISPLAY(state->display);
aschiffler@6727
   206
            state->window_y = SDL_WINDOWPOS_CENTERED_DISPLAY(state->display);
aschiffler@6727
   207
        }
aschiffler@6727
   208
        return 2;
aschiffler@6727
   209
    }
aschiffler@6727
   210
    if (SDL_strcasecmp(argv[index], "--fullscreen") == 0) {
aschiffler@6727
   211
        state->window_flags |= SDL_WINDOW_FULLSCREEN;
aschiffler@6727
   212
        state->num_windows = 1;
aschiffler@6727
   213
        return 1;
aschiffler@6727
   214
    }
slouken@6783
   215
    if (SDL_strcasecmp(argv[index], "--fullscreen-desktop") == 0) {
slouken@6783
   216
        state->window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
slouken@6783
   217
        state->num_windows = 1;
slouken@6783
   218
        return 1;
slouken@6783
   219
    }
urkle@7746
   220
    if (SDL_strcasecmp(argv[index], "--allow-highdpi") == 0) {
urkle@7746
   221
        state->window_flags |= SDL_WINDOW_ALLOW_HIGHDPI;
urkle@7746
   222
        return 1;
urkle@7746
   223
    }
aschiffler@6727
   224
    if (SDL_strcasecmp(argv[index], "--windows") == 0) {
aschiffler@6727
   225
        ++index;
aschiffler@6727
   226
        if (!argv[index] || !SDL_isdigit(*argv[index])) {
aschiffler@6727
   227
            return -1;
aschiffler@6727
   228
        }
aschiffler@6727
   229
        if (!(state->window_flags & SDL_WINDOW_FULLSCREEN)) {
aschiffler@6727
   230
            state->num_windows = SDL_atoi(argv[index]);
aschiffler@6727
   231
        }
aschiffler@6727
   232
        return 2;
aschiffler@6727
   233
    }
aschiffler@6727
   234
    if (SDL_strcasecmp(argv[index], "--title") == 0) {
aschiffler@6727
   235
        ++index;
aschiffler@6727
   236
        if (!argv[index]) {
aschiffler@6727
   237
            return -1;
aschiffler@6727
   238
        }
aschiffler@6727
   239
        state->window_title = argv[index];
aschiffler@6727
   240
        return 2;
aschiffler@6727
   241
    }
aschiffler@6727
   242
    if (SDL_strcasecmp(argv[index], "--icon") == 0) {
aschiffler@6727
   243
        ++index;
aschiffler@6727
   244
        if (!argv[index]) {
aschiffler@6727
   245
            return -1;
aschiffler@6727
   246
        }
aschiffler@6727
   247
        state->window_icon = argv[index];
aschiffler@6727
   248
        return 2;
aschiffler@6727
   249
    }
aschiffler@6727
   250
    if (SDL_strcasecmp(argv[index], "--center") == 0) {
aschiffler@6727
   251
        state->window_x = SDL_WINDOWPOS_CENTERED;
aschiffler@6727
   252
        state->window_y = SDL_WINDOWPOS_CENTERED;
aschiffler@6727
   253
        return 1;
aschiffler@6727
   254
    }
aschiffler@6727
   255
    if (SDL_strcasecmp(argv[index], "--position") == 0) {
aschiffler@6727
   256
        char *x, *y;
aschiffler@6727
   257
        ++index;
aschiffler@6727
   258
        if (!argv[index]) {
aschiffler@6727
   259
            return -1;
aschiffler@6727
   260
        }
aschiffler@6727
   261
        x = argv[index];
aschiffler@6727
   262
        y = argv[index];
aschiffler@6727
   263
        while (*y && *y != ',') {
aschiffler@6727
   264
            ++y;
aschiffler@6727
   265
        }
aschiffler@6727
   266
        if (!*y) {
aschiffler@6727
   267
            return -1;
aschiffler@6727
   268
        }
aschiffler@6727
   269
        *y++ = '\0';
aschiffler@6727
   270
        state->window_x = SDL_atoi(x);
aschiffler@6727
   271
        state->window_y = SDL_atoi(y);
aschiffler@6727
   272
        return 2;
aschiffler@6727
   273
    }
aschiffler@6727
   274
    if (SDL_strcasecmp(argv[index], "--geometry") == 0) {
aschiffler@6727
   275
        char *w, *h;
aschiffler@6727
   276
        ++index;
aschiffler@6727
   277
        if (!argv[index]) {
aschiffler@6727
   278
            return -1;
aschiffler@6727
   279
        }
aschiffler@6727
   280
        w = argv[index];
aschiffler@6727
   281
        h = argv[index];
aschiffler@6727
   282
        while (*h && *h != 'x') {
aschiffler@6727
   283
            ++h;
aschiffler@6727
   284
        }
aschiffler@6727
   285
        if (!*h) {
aschiffler@6727
   286
            return -1;
aschiffler@6727
   287
        }
aschiffler@6727
   288
        *h++ = '\0';
aschiffler@6727
   289
        state->window_w = SDL_atoi(w);
aschiffler@6727
   290
        state->window_h = SDL_atoi(h);
aschiffler@6727
   291
        return 2;
aschiffler@6727
   292
    }
slouken@6788
   293
    if (SDL_strcasecmp(argv[index], "--min-geometry") == 0) {
slouken@6788
   294
        char *w, *h;
slouken@6788
   295
        ++index;
slouken@6788
   296
        if (!argv[index]) {
slouken@6788
   297
            return -1;
slouken@6788
   298
        }
slouken@6788
   299
        w = argv[index];
slouken@6788
   300
        h = argv[index];
slouken@6788
   301
        while (*h && *h != 'x') {
slouken@6788
   302
            ++h;
slouken@6788
   303
        }
slouken@6788
   304
        if (!*h) {
slouken@6788
   305
            return -1;
slouken@6788
   306
        }
slouken@6788
   307
        *h++ = '\0';
slouken@6788
   308
        state->window_minW = SDL_atoi(w);
slouken@6788
   309
        state->window_minH = SDL_atoi(h);
slouken@6788
   310
        return 2;
slouken@6788
   311
    }
slouken@6788
   312
    if (SDL_strcasecmp(argv[index], "--max-geometry") == 0) {
slouken@6788
   313
        char *w, *h;
slouken@6788
   314
        ++index;
slouken@6788
   315
        if (!argv[index]) {
slouken@6788
   316
            return -1;
slouken@6788
   317
        }
slouken@6788
   318
        w = argv[index];
slouken@6788
   319
        h = argv[index];
slouken@6788
   320
        while (*h && *h != 'x') {
slouken@6788
   321
            ++h;
slouken@6788
   322
        }
slouken@6788
   323
        if (!*h) {
slouken@6788
   324
            return -1;
slouken@6788
   325
        }
slouken@6788
   326
        *h++ = '\0';
slouken@6788
   327
        state->window_maxW = SDL_atoi(w);
slouken@6788
   328
        state->window_maxH = SDL_atoi(h);
slouken@6788
   329
        return 2;
slouken@6788
   330
    }
slouken@7240
   331
    if (SDL_strcasecmp(argv[index], "--logical") == 0) {
slouken@7240
   332
        char *w, *h;
slouken@7240
   333
        ++index;
slouken@7240
   334
        if (!argv[index]) {
slouken@7240
   335
            return -1;
slouken@7240
   336
        }
slouken@7240
   337
        w = argv[index];
slouken@7240
   338
        h = argv[index];
slouken@7240
   339
        while (*h && *h != 'x') {
slouken@7240
   340
            ++h;
slouken@7240
   341
        }
slouken@7240
   342
        if (!*h) {
slouken@7240
   343
            return -1;
slouken@7240
   344
        }
slouken@7240
   345
        *h++ = '\0';
slouken@7240
   346
        state->logical_w = SDL_atoi(w);
slouken@7240
   347
        state->logical_h = SDL_atoi(h);
slouken@7240
   348
        return 2;
slouken@7240
   349
    }
slouken@7240
   350
    if (SDL_strcasecmp(argv[index], "--scale") == 0) {
slouken@7240
   351
        ++index;
slouken@7240
   352
        if (!argv[index]) {
slouken@7240
   353
            return -1;
slouken@7240
   354
        }
aschiffler@7249
   355
        state->scale = (float)SDL_atof(argv[index]);
slouken@7240
   356
        return 2;
slouken@7240
   357
    }
aschiffler@6727
   358
    if (SDL_strcasecmp(argv[index], "--depth") == 0) {
aschiffler@6727
   359
        ++index;
aschiffler@6727
   360
        if (!argv[index]) {
aschiffler@6727
   361
            return -1;
aschiffler@6727
   362
        }
aschiffler@6727
   363
        state->depth = SDL_atoi(argv[index]);
aschiffler@6727
   364
        return 2;
aschiffler@6727
   365
    }
aschiffler@6727
   366
    if (SDL_strcasecmp(argv[index], "--refresh") == 0) {
aschiffler@6727
   367
        ++index;
aschiffler@6727
   368
        if (!argv[index]) {
aschiffler@6727
   369
            return -1;
aschiffler@6727
   370
        }
aschiffler@6727
   371
        state->refresh_rate = SDL_atoi(argv[index]);
aschiffler@6727
   372
        return 2;
aschiffler@6727
   373
    }
aschiffler@6727
   374
    if (SDL_strcasecmp(argv[index], "--vsync") == 0) {
aschiffler@6727
   375
        state->render_flags |= SDL_RENDERER_PRESENTVSYNC;
aschiffler@6727
   376
        return 1;
aschiffler@6727
   377
    }
aschiffler@6727
   378
    if (SDL_strcasecmp(argv[index], "--noframe") == 0) {
aschiffler@6727
   379
        state->window_flags |= SDL_WINDOW_BORDERLESS;
aschiffler@6727
   380
        return 1;
aschiffler@6727
   381
    }
aschiffler@6727
   382
    if (SDL_strcasecmp(argv[index], "--resize") == 0) {
aschiffler@6727
   383
        state->window_flags |= SDL_WINDOW_RESIZABLE;
aschiffler@6727
   384
        return 1;
aschiffler@6727
   385
    }
aschiffler@6727
   386
    if (SDL_strcasecmp(argv[index], "--minimize") == 0) {
aschiffler@6727
   387
        state->window_flags |= SDL_WINDOW_MINIMIZED;
aschiffler@6727
   388
        return 1;
aschiffler@6727
   389
    }
aschiffler@6727
   390
    if (SDL_strcasecmp(argv[index], "--maximize") == 0) {
aschiffler@6727
   391
        state->window_flags |= SDL_WINDOW_MAXIMIZED;
aschiffler@6727
   392
        return 1;
aschiffler@6727
   393
    }
aschiffler@6727
   394
    if (SDL_strcasecmp(argv[index], "--grab") == 0) {
aschiffler@6727
   395
        state->window_flags |= SDL_WINDOW_INPUT_GRABBED;
aschiffler@6727
   396
        return 1;
aschiffler@6727
   397
    }
aschiffler@6727
   398
    if (SDL_strcasecmp(argv[index], "--rate") == 0) {
aschiffler@6727
   399
        ++index;
aschiffler@6727
   400
        if (!argv[index]) {
aschiffler@6727
   401
            return -1;
aschiffler@6727
   402
        }
aschiffler@6727
   403
        state->audiospec.freq = SDL_atoi(argv[index]);
aschiffler@6727
   404
        return 2;
aschiffler@6727
   405
    }
aschiffler@6727
   406
    if (SDL_strcasecmp(argv[index], "--format") == 0) {
aschiffler@6727
   407
        ++index;
aschiffler@6727
   408
        if (!argv[index]) {
aschiffler@6727
   409
            return -1;
aschiffler@6727
   410
        }
aschiffler@6727
   411
        if (SDL_strcasecmp(argv[index], "U8") == 0) {
aschiffler@6727
   412
            state->audiospec.format = AUDIO_U8;
aschiffler@6727
   413
            return 2;
aschiffler@6727
   414
        }
aschiffler@6727
   415
        if (SDL_strcasecmp(argv[index], "S8") == 0) {
aschiffler@6727
   416
            state->audiospec.format = AUDIO_S8;
aschiffler@6727
   417
            return 2;
aschiffler@6727
   418
        }
aschiffler@6727
   419
        if (SDL_strcasecmp(argv[index], "U16") == 0) {
aschiffler@6727
   420
            state->audiospec.format = AUDIO_U16;
aschiffler@6727
   421
            return 2;
aschiffler@6727
   422
        }
aschiffler@6727
   423
        if (SDL_strcasecmp(argv[index], "U16LE") == 0) {
aschiffler@6727
   424
            state->audiospec.format = AUDIO_U16LSB;
aschiffler@6727
   425
            return 2;
aschiffler@6727
   426
        }
aschiffler@6727
   427
        if (SDL_strcasecmp(argv[index], "U16BE") == 0) {
aschiffler@6727
   428
            state->audiospec.format = AUDIO_U16MSB;
aschiffler@6727
   429
            return 2;
aschiffler@6727
   430
        }
aschiffler@6727
   431
        if (SDL_strcasecmp(argv[index], "S16") == 0) {
aschiffler@6727
   432
            state->audiospec.format = AUDIO_S16;
aschiffler@6727
   433
            return 2;
aschiffler@6727
   434
        }
aschiffler@6727
   435
        if (SDL_strcasecmp(argv[index], "S16LE") == 0) {
aschiffler@6727
   436
            state->audiospec.format = AUDIO_S16LSB;
aschiffler@6727
   437
            return 2;
aschiffler@6727
   438
        }
aschiffler@6727
   439
        if (SDL_strcasecmp(argv[index], "S16BE") == 0) {
aschiffler@6727
   440
            state->audiospec.format = AUDIO_S16MSB;
aschiffler@6727
   441
            return 2;
aschiffler@6727
   442
        }
aschiffler@6727
   443
        return -1;
aschiffler@6727
   444
    }
aschiffler@6727
   445
    if (SDL_strcasecmp(argv[index], "--channels") == 0) {
aschiffler@6727
   446
        ++index;
aschiffler@6727
   447
        if (!argv[index]) {
aschiffler@6727
   448
            return -1;
aschiffler@6727
   449
        }
aschiffler@6727
   450
        state->audiospec.channels = (Uint8) SDL_atoi(argv[index]);
aschiffler@6727
   451
        return 2;
aschiffler@6727
   452
    }
aschiffler@6727
   453
    if (SDL_strcasecmp(argv[index], "--samples") == 0) {
aschiffler@6727
   454
        ++index;
aschiffler@6727
   455
        if (!argv[index]) {
aschiffler@6727
   456
            return -1;
aschiffler@6727
   457
        }
aschiffler@6727
   458
        state->audiospec.samples = (Uint16) SDL_atoi(argv[index]);
aschiffler@6727
   459
        return 2;
aschiffler@6727
   460
    }
slouken@11610
   461
    if (SDL_strcasecmp(argv[index], "--trackmem") == 0) {
slouken@11610
   462
        /* Already handled in SDLTest_CommonCreateState() */
slouken@11610
   463
        return 1;
slouken@11610
   464
    }
aschiffler@6727
   465
    if ((SDL_strcasecmp(argv[index], "-h") == 0)
aschiffler@6727
   466
        || (SDL_strcasecmp(argv[index], "--help") == 0)) {
aschiffler@6727
   467
        /* Print the usage message */
aschiffler@6727
   468
        return -1;
aschiffler@6727
   469
    }
aschiffler@6727
   470
    if (SDL_strcmp(argv[index], "-NSDocumentRevisionsDebugMode") == 0) {
slouken@7191
   471
    /* Debug flag sent by Xcode */
aschiffler@6727
   472
        return 2;
aschiffler@6727
   473
    }
aschiffler@6727
   474
    return 0;
aschiffler@6727
   475
}
aschiffler@6727
   476
aschiffler@6727
   477
const char *
aschiffler@6727
   478
SDLTest_CommonUsage(SDLTest_CommonState * state)
aschiffler@6727
   479
{
aschiffler@6727
   480
    switch (state->flags & (SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
aschiffler@6727
   481
    case SDL_INIT_VIDEO:
slouken@11610
   482
        return "[--trackmem] " VIDEO_USAGE;
aschiffler@6727
   483
    case SDL_INIT_AUDIO:
slouken@11610
   484
        return "[--trackmem] " AUDIO_USAGE;
aschiffler@6727
   485
    case (SDL_INIT_VIDEO | SDL_INIT_AUDIO):
slouken@11610
   486
        return "[--trackmem] " VIDEO_USAGE " " AUDIO_USAGE;
aschiffler@6727
   487
    default:
slouken@11610
   488
        return "[--trackmem]";
aschiffler@6727
   489
    }
aschiffler@6727
   490
}
aschiffler@6727
   491
aschiffler@6727
   492
static void
slouken@11425
   493
SDLTest_PrintRendererFlag(char *text, size_t maxlen, Uint32 flag)
aschiffler@6727
   494
{
aschiffler@6727
   495
    switch (flag) {
slouken@11426
   496
    case SDL_RENDERER_SOFTWARE:
slouken@11426
   497
        SDL_snprintfcat(text, maxlen, "Software");
slouken@11426
   498
        break;
slouken@11426
   499
    case SDL_RENDERER_ACCELERATED:
slouken@11426
   500
        SDL_snprintfcat(text, maxlen, "Accelerated");
slouken@11426
   501
        break;
aschiffler@6727
   502
    case SDL_RENDERER_PRESENTVSYNC:
slouken@11425
   503
        SDL_snprintfcat(text, maxlen, "PresentVSync");
aschiffler@6727
   504
        break;
slouken@11426
   505
    case SDL_RENDERER_TARGETTEXTURE:
slouken@11426
   506
        SDL_snprintfcat(text, maxlen, "TargetTexturesSupported");
aschiffler@6727
   507
        break;
aschiffler@6727
   508
    default:
slouken@11425
   509
        SDL_snprintfcat(text, maxlen, "0x%8.8x", flag);
aschiffler@6727
   510
        break;
aschiffler@6727
   511
    }
aschiffler@6727
   512
}
aschiffler@6727
   513
aschiffler@6727
   514
static void
slouken@11425
   515
SDLTest_PrintPixelFormat(char *text, size_t maxlen, Uint32 format)
aschiffler@6727
   516
{
aschiffler@6727
   517
    switch (format) {
aschiffler@6727
   518
    case SDL_PIXELFORMAT_UNKNOWN:
slouken@11425
   519
        SDL_snprintfcat(text, maxlen, "Unknown");
aschiffler@6727
   520
        break;
aschiffler@6727
   521
    case SDL_PIXELFORMAT_INDEX1LSB:
slouken@11425
   522
        SDL_snprintfcat(text, maxlen, "Index1LSB");
aschiffler@6727
   523
        break;
aschiffler@6727
   524
    case SDL_PIXELFORMAT_INDEX1MSB:
slouken@11425
   525
        SDL_snprintfcat(text, maxlen, "Index1MSB");
aschiffler@6727
   526
        break;
aschiffler@6727
   527
    case SDL_PIXELFORMAT_INDEX4LSB:
slouken@11425
   528
        SDL_snprintfcat(text, maxlen, "Index4LSB");
aschiffler@6727
   529
        break;
aschiffler@6727
   530
    case SDL_PIXELFORMAT_INDEX4MSB:
slouken@11425
   531
        SDL_snprintfcat(text, maxlen, "Index4MSB");
aschiffler@6727
   532
        break;
aschiffler@6727
   533
    case SDL_PIXELFORMAT_INDEX8:
slouken@11425
   534
        SDL_snprintfcat(text, maxlen, "Index8");
aschiffler@6727
   535
        break;
aschiffler@6727
   536
    case SDL_PIXELFORMAT_RGB332:
slouken@11425
   537
        SDL_snprintfcat(text, maxlen, "RGB332");
aschiffler@6727
   538
        break;
aschiffler@6727
   539
    case SDL_PIXELFORMAT_RGB444:
slouken@11425
   540
        SDL_snprintfcat(text, maxlen, "RGB444");
aschiffler@6727
   541
        break;
aschiffler@6727
   542
    case SDL_PIXELFORMAT_RGB555:
slouken@11425
   543
        SDL_snprintfcat(text, maxlen, "RGB555");
aschiffler@6727
   544
        break;
aschiffler@6727
   545
    case SDL_PIXELFORMAT_BGR555:
slouken@11425
   546
        SDL_snprintfcat(text, maxlen, "BGR555");
aschiffler@6727
   547
        break;
aschiffler@6727
   548
    case SDL_PIXELFORMAT_ARGB4444:
slouken@11425
   549
        SDL_snprintfcat(text, maxlen, "ARGB4444");
aschiffler@6727
   550
        break;
aschiffler@6727
   551
    case SDL_PIXELFORMAT_ABGR4444:
slouken@11425
   552
        SDL_snprintfcat(text, maxlen, "ABGR4444");
aschiffler@6727
   553
        break;
aschiffler@6727
   554
    case SDL_PIXELFORMAT_ARGB1555:
slouken@11425
   555
        SDL_snprintfcat(text, maxlen, "ARGB1555");
aschiffler@6727
   556
        break;
aschiffler@6727
   557
    case SDL_PIXELFORMAT_ABGR1555:
slouken@11425
   558
        SDL_snprintfcat(text, maxlen, "ABGR1555");
aschiffler@6727
   559
        break;
aschiffler@6727
   560
    case SDL_PIXELFORMAT_RGB565:
slouken@11425
   561
        SDL_snprintfcat(text, maxlen, "RGB565");
aschiffler@6727
   562
        break;
aschiffler@6727
   563
    case SDL_PIXELFORMAT_BGR565:
slouken@11425
   564
        SDL_snprintfcat(text, maxlen, "BGR565");
aschiffler@6727
   565
        break;
aschiffler@6727
   566
    case SDL_PIXELFORMAT_RGB24:
slouken@11425
   567
        SDL_snprintfcat(text, maxlen, "RGB24");
aschiffler@6727
   568
        break;
aschiffler@6727
   569
    case SDL_PIXELFORMAT_BGR24:
slouken@11425
   570
        SDL_snprintfcat(text, maxlen, "BGR24");
aschiffler@6727
   571
        break;
aschiffler@6727
   572
    case SDL_PIXELFORMAT_RGB888:
slouken@11425
   573
        SDL_snprintfcat(text, maxlen, "RGB888");
aschiffler@6727
   574
        break;
aschiffler@6727
   575
    case SDL_PIXELFORMAT_BGR888:
slouken@11425
   576
        SDL_snprintfcat(text, maxlen, "BGR888");
aschiffler@6727
   577
        break;
aschiffler@6727
   578
    case SDL_PIXELFORMAT_ARGB8888:
slouken@11425
   579
        SDL_snprintfcat(text, maxlen, "ARGB8888");
aschiffler@6727
   580
        break;
aschiffler@6727
   581
    case SDL_PIXELFORMAT_RGBA8888:
slouken@11425
   582
        SDL_snprintfcat(text, maxlen, "RGBA8888");
aschiffler@6727
   583
        break;
aschiffler@6727
   584
    case SDL_PIXELFORMAT_ABGR8888:
slouken@11425
   585
        SDL_snprintfcat(text, maxlen, "ABGR8888");
aschiffler@6727
   586
        break;
aschiffler@6727
   587
    case SDL_PIXELFORMAT_BGRA8888:
slouken@11425
   588
        SDL_snprintfcat(text, maxlen, "BGRA8888");
aschiffler@6727
   589
        break;
aschiffler@6727
   590
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@11425
   591
        SDL_snprintfcat(text, maxlen, "ARGB2101010");
aschiffler@6727
   592
        break;
aschiffler@6727
   593
    case SDL_PIXELFORMAT_YV12:
slouken@11425
   594
        SDL_snprintfcat(text, maxlen, "YV12");
aschiffler@6727
   595
        break;
aschiffler@6727
   596
    case SDL_PIXELFORMAT_IYUV:
slouken@11425
   597
        SDL_snprintfcat(text, maxlen, "IYUV");
aschiffler@6727
   598
        break;
aschiffler@6727
   599
    case SDL_PIXELFORMAT_YUY2:
slouken@11425
   600
        SDL_snprintfcat(text, maxlen, "YUY2");
aschiffler@6727
   601
        break;
aschiffler@6727
   602
    case SDL_PIXELFORMAT_UYVY:
slouken@11425
   603
        SDL_snprintfcat(text, maxlen, "UYVY");
aschiffler@6727
   604
        break;
aschiffler@6727
   605
    case SDL_PIXELFORMAT_YVYU:
slouken@11425
   606
        SDL_snprintfcat(text, maxlen, "YVYU");
aschiffler@6727
   607
        break;
slouken@9046
   608
    case SDL_PIXELFORMAT_NV12:
slouken@11425
   609
        SDL_snprintfcat(text, maxlen, "NV12");
slouken@9046
   610
        break;
slouken@9046
   611
    case SDL_PIXELFORMAT_NV21:
slouken@11425
   612
        SDL_snprintfcat(text, maxlen, "NV21");
slouken@9046
   613
        break;
aschiffler@6727
   614
    default:
slouken@11425
   615
        SDL_snprintfcat(text, maxlen, "0x%8.8x", format);
aschiffler@6727
   616
        break;
aschiffler@6727
   617
    }
aschiffler@6727
   618
}
aschiffler@6727
   619
aschiffler@6727
   620
static void
aschiffler@6727
   621
SDLTest_PrintRenderer(SDL_RendererInfo * info)
aschiffler@6727
   622
{
aschiffler@6727
   623
    int i, count;
slouken@11425
   624
    char text[1024];
aschiffler@6727
   625
slouken@10866
   626
    SDL_Log("  Renderer %s:\n", info->name);
aschiffler@6727
   627
slouken@11425
   628
    SDL_snprintf(text, sizeof(text), "    Flags: 0x%8.8X", info->flags);
slouken@11425
   629
    SDL_snprintfcat(text, sizeof(text), " (");
aschiffler@6727
   630
    count = 0;
aschiffler@6727
   631
    for (i = 0; i < sizeof(info->flags) * 8; ++i) {
aschiffler@6727
   632
        Uint32 flag = (1 << i);
aschiffler@6727
   633
        if (info->flags & flag) {
aschiffler@6727
   634
            if (count > 0) {
slouken@11425
   635
                SDL_snprintfcat(text, sizeof(text), " | ");
aschiffler@6727
   636
            }
slouken@11425
   637
            SDLTest_PrintRendererFlag(text, sizeof(text), flag);
aschiffler@6727
   638
            ++count;
aschiffler@6727
   639
        }
aschiffler@6727
   640
    }
slouken@11425
   641
    SDL_snprintfcat(text, sizeof(text), ")");
slouken@11425
   642
    SDL_Log("%s\n", text);
aschiffler@6727
   643
slouken@11425
   644
    SDL_snprintf(text, sizeof(text), "    Texture formats (%d): ", info->num_texture_formats);
aschiffler@6727
   645
    for (i = 0; i < (int) info->num_texture_formats; ++i) {
aschiffler@6727
   646
        if (i > 0) {
slouken@11425
   647
            SDL_snprintfcat(text, sizeof(text), ", ");
aschiffler@6727
   648
        }
slouken@11425
   649
        SDLTest_PrintPixelFormat(text, sizeof(text), info->texture_formats[i]);
aschiffler@6727
   650
    }
slouken@11425
   651
    SDL_Log("%s\n", text);
aschiffler@6727
   652
aschiffler@6727
   653
    if (info->max_texture_width || info->max_texture_height) {
slouken@10866
   654
        SDL_Log("    Max Texture Size: %dx%d\n",
aschiffler@6727
   655
                info->max_texture_width, info->max_texture_height);
aschiffler@6727
   656
    }
aschiffler@6727
   657
}
aschiffler@6727
   658
aschiffler@6727
   659
static SDL_Surface *
aschiffler@6727
   660
SDLTest_LoadIcon(const char *file)
aschiffler@6727
   661
{
aschiffler@6727
   662
    SDL_Surface *icon;
aschiffler@6727
   663
aschiffler@6727
   664
    /* Load the icon surface */
aschiffler@6727
   665
    icon = SDL_LoadBMP(file);
aschiffler@6727
   666
    if (icon == NULL) {
slouken@10866
   667
        SDL_Log("Couldn't load %s: %s\n", file, SDL_GetError());
aschiffler@6727
   668
        return (NULL);
aschiffler@6727
   669
    }
aschiffler@6727
   670
aschiffler@6727
   671
    if (icon->format->palette) {
aschiffler@6727
   672
        /* Set the colorkey */
aschiffler@6727
   673
        SDL_SetColorKey(icon, 1, *((Uint8 *) icon->pixels));
aschiffler@6727
   674
    }
aschiffler@6727
   675
aschiffler@6727
   676
    return (icon);
aschiffler@6727
   677
}
aschiffler@6727
   678
slouken@11539
   679
static SDL_HitTestResult
slouken@11539
   680
SDLTest_ExampleHitTestCallback(SDL_Window *win, const SDL_Point *area, void *data)
slouken@11539
   681
{
slouken@11539
   682
    int w, h;
slouken@11539
   683
    const int RESIZE_BORDER = 8;
slouken@11539
   684
    const int DRAGGABLE_TITLE = 32;
slouken@11539
   685
slouken@11539
   686
    /*SDL_Log("Hit test point %d,%d\n", area->x, area->y);*/
slouken@11539
   687
slouken@11539
   688
    SDL_GetWindowSize(win, &w, &h);
slouken@11539
   689
slouken@11539
   690
    if (area->x < RESIZE_BORDER) {
slouken@11539
   691
        if (area->y < RESIZE_BORDER) {
slouken@11539
   692
            SDL_Log("SDL_HITTEST_RESIZE_TOPLEFT\n");
slouken@11539
   693
            return SDL_HITTEST_RESIZE_TOPLEFT;
slouken@11539
   694
        } else if (area->y >= (h-RESIZE_BORDER)) {
slouken@11539
   695
            SDL_Log("SDL_HITTEST_RESIZE_BOTTOMLEFT\n");
slouken@11539
   696
            return SDL_HITTEST_RESIZE_BOTTOMLEFT;
slouken@11539
   697
        } else {
slouken@11539
   698
            SDL_Log("SDL_HITTEST_RESIZE_LEFT\n");
slouken@11539
   699
            return SDL_HITTEST_RESIZE_LEFT;
slouken@11539
   700
        }
slouken@11539
   701
    } else if (area->x >= (w-RESIZE_BORDER)) {
slouken@11539
   702
        if (area->y < RESIZE_BORDER) {
slouken@11539
   703
            SDL_Log("SDL_HITTEST_RESIZE_TOPRIGHT\n");
slouken@11539
   704
            return SDL_HITTEST_RESIZE_TOPRIGHT;
slouken@11539
   705
        } else if (area->y >= (h-RESIZE_BORDER)) {
slouken@11539
   706
            SDL_Log("SDL_HITTEST_RESIZE_BOTTOMRIGHT\n");
slouken@11539
   707
            return SDL_HITTEST_RESIZE_BOTTOMRIGHT;
slouken@11539
   708
        } else {
slouken@11539
   709
            SDL_Log("SDL_HITTEST_RESIZE_RIGHT\n");
slouken@11539
   710
            return SDL_HITTEST_RESIZE_RIGHT;
slouken@11539
   711
        }
slouken@11539
   712
    } else if (area->y >= (h-RESIZE_BORDER)) {
slouken@11539
   713
        SDL_Log("SDL_HITTEST_RESIZE_BOTTOM\n");
slouken@11539
   714
        return SDL_HITTEST_RESIZE_BOTTOM;
slouken@11539
   715
    } else if (area->y < RESIZE_BORDER) {
slouken@11539
   716
        SDL_Log("SDL_HITTEST_RESIZE_TOP\n");
slouken@11539
   717
        return SDL_HITTEST_RESIZE_TOP;
slouken@11539
   718
    } else if (area->y < DRAGGABLE_TITLE) {
slouken@11539
   719
        SDL_Log("SDL_HITTEST_DRAGGABLE\n");
slouken@11539
   720
        return SDL_HITTEST_DRAGGABLE;
slouken@11539
   721
    }
slouken@11539
   722
    return SDL_HITTEST_NORMAL;
slouken@11539
   723
}
slouken@11539
   724
aschiffler@6727
   725
SDL_bool
aschiffler@6727
   726
SDLTest_CommonInit(SDLTest_CommonState * state)
aschiffler@6727
   727
{
aschiffler@6727
   728
    int i, j, m, n, w, h;
aschiffler@6727
   729
    SDL_DisplayMode fullscreen_mode;
slouken@11425
   730
    char text[1024];
aschiffler@6727
   731
aschiffler@6727
   732
    if (state->flags & SDL_INIT_VIDEO) {
aschiffler@6727
   733
        if (state->verbose & VERBOSE_VIDEO) {
aschiffler@6727
   734
            n = SDL_GetNumVideoDrivers();
aschiffler@6727
   735
            if (n == 0) {
slouken@10866
   736
                SDL_Log("No built-in video drivers\n");
aschiffler@6727
   737
            } else {
slouken@11425
   738
                SDL_snprintf(text, sizeof(text), "Built-in video drivers:");
aschiffler@6727
   739
                for (i = 0; i < n; ++i) {
aschiffler@6727
   740
                    if (i > 0) {
slouken@11425
   741
                        SDL_snprintfcat(text, sizeof(text), ",");
aschiffler@6727
   742
                    }
slouken@11425
   743
                    SDL_snprintfcat(text, sizeof(text), " %s", SDL_GetVideoDriver(i));
aschiffler@6727
   744
                }
slouken@11425
   745
                SDL_Log("%s\n", text);
aschiffler@6727
   746
            }
aschiffler@6727
   747
        }
aschiffler@6727
   748
        if (SDL_VideoInit(state->videodriver) < 0) {
slouken@10866
   749
            SDL_Log("Couldn't initialize video driver: %s\n",
aschiffler@6727
   750
                    SDL_GetError());
aschiffler@6727
   751
            return SDL_FALSE;
aschiffler@6727
   752
        }
aschiffler@6727
   753
        if (state->verbose & VERBOSE_VIDEO) {
slouken@10866
   754
            SDL_Log("Video driver: %s\n",
aschiffler@6727
   755
                    SDL_GetCurrentVideoDriver());
aschiffler@6727
   756
        }
aschiffler@6727
   757
aschiffler@6727
   758
        /* Upload GL settings */
aschiffler@6727
   759
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, state->gl_red_size);
aschiffler@6727
   760
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, state->gl_green_size);
aschiffler@6727
   761
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, state->gl_blue_size);
aschiffler@6727
   762
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, state->gl_alpha_size);
aschiffler@6727
   763
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, state->gl_double_buffer);
aschiffler@6727
   764
        SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, state->gl_buffer_size);
aschiffler@6727
   765
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, state->gl_depth_size);
aschiffler@6727
   766
        SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, state->gl_stencil_size);
aschiffler@6727
   767
        SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, state->gl_accum_red_size);
aschiffler@6727
   768
        SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, state->gl_accum_green_size);
aschiffler@6727
   769
        SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, state->gl_accum_blue_size);
aschiffler@6727
   770
        SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, state->gl_accum_alpha_size);
aschiffler@6727
   771
        SDL_GL_SetAttribute(SDL_GL_STEREO, state->gl_stereo);
aschiffler@6727
   772
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, state->gl_multisamplebuffers);
aschiffler@6727
   773
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, state->gl_multisamplesamples);
aschiffler@6727
   774
        if (state->gl_accelerated >= 0) {
aschiffler@6727
   775
            SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL,
aschiffler@6727
   776
                                state->gl_accelerated);
aschiffler@6727
   777
        }
aschiffler@6727
   778
        SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, state->gl_retained_backing);
aschiffler@6727
   779
        if (state->gl_major_version) {
aschiffler@6727
   780
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, state->gl_major_version);
aschiffler@6727
   781
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, state->gl_minor_version);
aschiffler@6727
   782
        }
slouken@7194
   783
        if (state->gl_debug) {
slouken@7194
   784
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
slouken@7194
   785
        }
gabomdq@7796
   786
        if (state->gl_profile_mask) {
gabomdq@7796
   787
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, state->gl_profile_mask);
gabomdq@7796
   788
        }
aschiffler@6727
   789
aschiffler@6727
   790
        if (state->verbose & VERBOSE_MODES) {
icculus@11012
   791
            SDL_Rect bounds, usablebounds;
icculus@11012
   792
            float hdpi = 0;
icculus@11012
   793
            float vdpi = 0;
aschiffler@6727
   794
            SDL_DisplayMode mode;
aschiffler@6727
   795
            int bpp;
aschiffler@6727
   796
            Uint32 Rmask, Gmask, Bmask, Amask;
slouken@7756
   797
#if SDL_VIDEO_DRIVER_WINDOWS
slouken@11539
   798
            int adapterIndex = 0;
slouken@11539
   799
            int outputIndex = 0;
slouken@7756
   800
#endif
aschiffler@6727
   801
            n = SDL_GetNumVideoDisplays();
slouken@10866
   802
            SDL_Log("Number of displays: %d\n", n);
aschiffler@6727
   803
            for (i = 0; i < n; ++i) {
slouken@10866
   804
                SDL_Log("Display %d: %s\n", i, SDL_GetDisplayName(i));
aschiffler@6727
   805
aschiffler@6727
   806
                SDL_zero(bounds);
aschiffler@6727
   807
                SDL_GetDisplayBounds(i, &bounds);
icculus@11012
   808
icculus@11012
   809
                SDL_zero(usablebounds);
icculus@11012
   810
                SDL_GetDisplayUsableBounds(i, &usablebounds);
icculus@11012
   811
icculus@11012
   812
                SDL_GetDisplayDPI(i, NULL, &hdpi, &vdpi);
icculus@11012
   813
slouken@10866
   814
                SDL_Log("Bounds: %dx%d at %d,%d\n", bounds.w, bounds.h, bounds.x, bounds.y);
icculus@11012
   815
                SDL_Log("Usable bounds: %dx%d at %d,%d\n", usablebounds.w, usablebounds.h, usablebounds.x, usablebounds.y);
icculus@11012
   816
                SDL_Log("DPI: %fx%f\n", hdpi, vdpi);
aschiffler@6727
   817
aschiffler@6727
   818
                SDL_GetDesktopDisplayMode(i, &mode);
aschiffler@6727
   819
                SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask,
aschiffler@6727
   820
                                           &Bmask, &Amask);
slouken@10866
   821
                SDL_Log("  Current mode: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
aschiffler@6727
   822
                        mode.w, mode.h, mode.refresh_rate, bpp,
aschiffler@6727
   823
                        SDL_GetPixelFormatName(mode.format));
aschiffler@6727
   824
                if (Rmask || Gmask || Bmask) {
slouken@10866
   825
                    SDL_Log("      Red Mask   = 0x%.8x\n", Rmask);
slouken@10866
   826
                    SDL_Log("      Green Mask = 0x%.8x\n", Gmask);
slouken@10866
   827
                    SDL_Log("      Blue Mask  = 0x%.8x\n", Bmask);
aschiffler@6727
   828
                    if (Amask)
slouken@10866
   829
                        SDL_Log("      Alpha Mask = 0x%.8x\n", Amask);
aschiffler@6727
   830
                }
aschiffler@6727
   831
aschiffler@6727
   832
                /* Print available fullscreen video modes */
aschiffler@6727
   833
                m = SDL_GetNumDisplayModes(i);
aschiffler@6727
   834
                if (m == 0) {
slouken@10866
   835
                    SDL_Log("No available fullscreen video modes\n");
aschiffler@6727
   836
                } else {
slouken@10866
   837
                    SDL_Log("  Fullscreen video modes:\n");
aschiffler@6727
   838
                    for (j = 0; j < m; ++j) {
aschiffler@6727
   839
                        SDL_GetDisplayMode(i, j, &mode);
aschiffler@6727
   840
                        SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask,
aschiffler@6727
   841
                                                   &Gmask, &Bmask, &Amask);
slouken@11539
   842
                        SDL_Log("    Mode %d: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
aschiffler@6727
   843
                                j, mode.w, mode.h, mode.refresh_rate, bpp,
aschiffler@6727
   844
                                SDL_GetPixelFormatName(mode.format));
aschiffler@6727
   845
                        if (Rmask || Gmask || Bmask) {
slouken@10866
   846
                            SDL_Log("        Red Mask   = 0x%.8x\n",
aschiffler@6727
   847
                                    Rmask);
slouken@10866
   848
                            SDL_Log("        Green Mask = 0x%.8x\n",
aschiffler@6727
   849
                                    Gmask);
slouken@10866
   850
                            SDL_Log("        Blue Mask  = 0x%.8x\n",
aschiffler@6727
   851
                                    Bmask);
aschiffler@6727
   852
                            if (Amask)
slouken@11539
   853
                                SDL_Log("        Alpha Mask = 0x%.8x\n",
aschiffler@6727
   854
                                        Amask);
aschiffler@6727
   855
                        }
aschiffler@6727
   856
                    }
aschiffler@6727
   857
                }
slouken@7756
   858
slouken@7756
   859
#if SDL_VIDEO_DRIVER_WINDOWS
slouken@11539
   860
                /* Print the D3D9 adapter index */
slouken@11539
   861
                adapterIndex = SDL_Direct3D9GetAdapterIndex( i );
slouken@11539
   862
                SDL_Log("D3D9 Adapter Index: %d", adapterIndex);
slouken@8213
   863
slouken@11539
   864
                /* Print the DXGI adapter and output indices */
slouken@11539
   865
                SDL_DXGIGetOutputInfo(i, &adapterIndex, &outputIndex);
slouken@11539
   866
                SDL_Log("DXGI Adapter Index: %d  Output Index: %d", adapterIndex, outputIndex);
slouken@7756
   867
#endif
aschiffler@6727
   868
            }
aschiffler@6727
   869
        }
aschiffler@6727
   870
aschiffler@6727
   871
        if (state->verbose & VERBOSE_RENDER) {
aschiffler@6727
   872
            SDL_RendererInfo info;
aschiffler@6727
   873
aschiffler@6727
   874
            n = SDL_GetNumRenderDrivers();
aschiffler@6727
   875
            if (n == 0) {
slouken@10866
   876
                SDL_Log("No built-in render drivers\n");
aschiffler@6727
   877
            } else {
slouken@10866
   878
                SDL_Log("Built-in render drivers:\n");
aschiffler@6727
   879
                for (i = 0; i < n; ++i) {
aschiffler@6727
   880
                    SDL_GetRenderDriverInfo(i, &info);
aschiffler@6727
   881
                    SDLTest_PrintRenderer(&info);
aschiffler@6727
   882
                }
aschiffler@6727
   883
            }
aschiffler@6727
   884
        }
aschiffler@6727
   885
aschiffler@6727
   886
        SDL_zero(fullscreen_mode);
aschiffler@6727
   887
        switch (state->depth) {
aschiffler@6727
   888
        case 8:
aschiffler@6727
   889
            fullscreen_mode.format = SDL_PIXELFORMAT_INDEX8;
aschiffler@6727
   890
            break;
aschiffler@6727
   891
        case 15:
aschiffler@6727
   892
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB555;
aschiffler@6727
   893
            break;
aschiffler@6727
   894
        case 16:
aschiffler@6727
   895
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB565;
aschiffler@6727
   896
            break;
aschiffler@6727
   897
        case 24:
aschiffler@6727
   898
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB24;
aschiffler@6727
   899
            break;
aschiffler@6727
   900
        default:
aschiffler@6727
   901
            fullscreen_mode.format = SDL_PIXELFORMAT_RGB888;
aschiffler@6727
   902
            break;
aschiffler@6727
   903
        }
aschiffler@6727
   904
        fullscreen_mode.refresh_rate = state->refresh_rate;
aschiffler@6727
   905
aschiffler@6727
   906
        state->windows =
slouken@11370
   907
            (SDL_Window **) SDL_calloc(state->num_windows,
aschiffler@6727
   908
                                        sizeof(*state->windows));
aschiffler@6727
   909
        state->renderers =
slouken@11370
   910
            (SDL_Renderer **) SDL_calloc(state->num_windows,
aschiffler@6727
   911
                                        sizeof(*state->renderers));
slouken@7993
   912
        state->targets =
slouken@11370
   913
            (SDL_Texture **) SDL_calloc(state->num_windows,
slouken@7993
   914
                                        sizeof(*state->targets));
aschiffler@6727
   915
        if (!state->windows || !state->renderers) {
slouken@10866
   916
            SDL_Log("Out of memory!\n");
aschiffler@6727
   917
            return SDL_FALSE;
aschiffler@6727
   918
        }
aschiffler@6727
   919
        for (i = 0; i < state->num_windows; ++i) {
aschiffler@6727
   920
            char title[1024];
aschiffler@6727
   921
aschiffler@6727
   922
            if (state->num_windows > 1) {
aschiffler@6727
   923
                SDL_snprintf(title, SDL_arraysize(title), "%s %d",
aschiffler@6727
   924
                             state->window_title, i + 1);
aschiffler@6727
   925
            } else {
aschiffler@6727
   926
                SDL_strlcpy(title, state->window_title, SDL_arraysize(title));
aschiffler@6727
   927
            }
aschiffler@6727
   928
            state->windows[i] =
aschiffler@6727
   929
                SDL_CreateWindow(title, state->window_x, state->window_y,
aschiffler@6727
   930
                                 state->window_w, state->window_h,
aschiffler@6727
   931
                                 state->window_flags);
aschiffler@6727
   932
            if (!state->windows[i]) {
slouken@10866
   933
                SDL_Log("Couldn't create window: %s\n",
aschiffler@6727
   934
                        SDL_GetError());
aschiffler@6727
   935
                return SDL_FALSE;
aschiffler@6727
   936
            }
slouken@6788
   937
            if (state->window_minW || state->window_minH) {
slouken@6788
   938
                SDL_SetWindowMinimumSize(state->windows[i], state->window_minW, state->window_minH);
slouken@6788
   939
            }
slouken@6788
   940
            if (state->window_maxW || state->window_maxH) {
slouken@6788
   941
                SDL_SetWindowMaximumSize(state->windows[i], state->window_maxW, state->window_maxH);
slouken@6788
   942
            }
aschiffler@6727
   943
            SDL_GetWindowSize(state->windows[i], &w, &h);
aschiffler@6727
   944
            if (!(state->window_flags & SDL_WINDOW_RESIZABLE) &&
aschiffler@6727
   945
                (w != state->window_w || h != state->window_h)) {
aschiffler@6727
   946
                printf("Window requested size %dx%d, got %dx%d\n", state->window_w, state->window_h, w, h);
aschiffler@6727
   947
                state->window_w = w;
aschiffler@6727
   948
                state->window_h = h;
aschiffler@6727
   949
            }
aschiffler@6727
   950
            if (SDL_SetWindowDisplayMode(state->windows[i], &fullscreen_mode) < 0) {
slouken@10866
   951
                SDL_Log("Can't set up fullscreen display mode: %s\n",
aschiffler@6727
   952
                        SDL_GetError());
aschiffler@6727
   953
                return SDL_FALSE;
aschiffler@6727
   954
            }
aschiffler@6727
   955
slouken@11539
   956
            /* Add resize/drag areas for windows that are borderless and resizable */
slouken@11604
   957
            if ((state->window_flags & (SDL_WINDOW_RESIZABLE|SDL_WINDOW_BORDERLESS)) ==
slouken@11539
   958
                (SDL_WINDOW_RESIZABLE|SDL_WINDOW_BORDERLESS)) {
slouken@11539
   959
                SDL_SetWindowHitTest(state->windows[i], SDLTest_ExampleHitTestCallback, NULL);
slouken@11539
   960
            }
slouken@11539
   961
aschiffler@6727
   962
            if (state->window_icon) {
aschiffler@6727
   963
                SDL_Surface *icon = SDLTest_LoadIcon(state->window_icon);
aschiffler@6727
   964
                if (icon) {
aschiffler@6727
   965
                    SDL_SetWindowIcon(state->windows[i], icon);
aschiffler@6727
   966
                    SDL_FreeSurface(icon);
aschiffler@6727
   967
                }
aschiffler@6727
   968
            }
aschiffler@6727
   969
aschiffler@6727
   970
            SDL_ShowWindow(state->windows[i]);
aschiffler@6727
   971
aschiffler@6727
   972
            if (!state->skip_renderer
aschiffler@6727
   973
                && (state->renderdriver
icculus@11365
   974
                    || !(state->window_flags & (SDL_WINDOW_OPENGL | SDL_WINDOW_VULKAN)))) {
aschiffler@6727
   975
                m = -1;
aschiffler@6727
   976
                if (state->renderdriver) {
aschiffler@6727
   977
                    SDL_RendererInfo info;
aschiffler@6727
   978
                    n = SDL_GetNumRenderDrivers();
aschiffler@6727
   979
                    for (j = 0; j < n; ++j) {
aschiffler@6727
   980
                        SDL_GetRenderDriverInfo(j, &info);
aschiffler@6727
   981
                        if (SDL_strcasecmp(info.name, state->renderdriver) ==
aschiffler@6727
   982
                            0) {
aschiffler@6727
   983
                            m = j;
aschiffler@6727
   984
                            break;
aschiffler@6727
   985
                        }
aschiffler@6727
   986
                    }
slouken@8697
   987
                    if (m == -1) {
slouken@11539
   988
                        SDL_Log("Couldn't find render driver named %s",
aschiffler@6727
   989
                                state->renderdriver);
aschiffler@6727
   990
                        return SDL_FALSE;
aschiffler@6727
   991
                    }
aschiffler@6727
   992
                }
aschiffler@6727
   993
                state->renderers[i] = SDL_CreateRenderer(state->windows[i],
aschiffler@6727
   994
                                            m, state->render_flags);
aschiffler@6727
   995
                if (!state->renderers[i]) {
slouken@10866
   996
                    SDL_Log("Couldn't create renderer: %s\n",
aschiffler@6727
   997
                            SDL_GetError());
aschiffler@6727
   998
                    return SDL_FALSE;
aschiffler@6727
   999
                }
slouken@7240
  1000
                if (state->logical_w && state->logical_h) {
slouken@7240
  1001
                    SDL_RenderSetLogicalSize(state->renderers[i], state->logical_w, state->logical_h);
slouken@7240
  1002
                } else if (state->scale) {
slouken@7240
  1003
                    SDL_RenderSetScale(state->renderers[i], state->scale, state->scale);
slouken@7240
  1004
                }
aschiffler@6727
  1005
                if (state->verbose & VERBOSE_RENDER) {
aschiffler@6727
  1006
                    SDL_RendererInfo info;
aschiffler@6727
  1007
slouken@10866
  1008
                    SDL_Log("Current renderer:\n");
aschiffler@6727
  1009
                    SDL_GetRendererInfo(state->renderers[i], &info);
aschiffler@6727
  1010
                    SDLTest_PrintRenderer(&info);
aschiffler@6727
  1011
                }
aschiffler@6727
  1012
            }
aschiffler@6727
  1013
        }
aschiffler@6727
  1014
    }
aschiffler@6727
  1015
aschiffler@6727
  1016
    if (state->flags & SDL_INIT_AUDIO) {
aschiffler@6727
  1017
        if (state->verbose & VERBOSE_AUDIO) {
aschiffler@6727
  1018
            n = SDL_GetNumAudioDrivers();
aschiffler@6727
  1019
            if (n == 0) {
slouken@10866
  1020
                SDL_Log("No built-in audio drivers\n");
aschiffler@6727
  1021
            } else {
slouken@11425
  1022
                SDL_snprintf(text, sizeof(text), "Built-in audio drivers:");
aschiffler@6727
  1023
                for (i = 0; i < n; ++i) {
aschiffler@6727
  1024
                    if (i > 0) {
slouken@11425
  1025
                        SDL_snprintfcat(text, sizeof(text), ",");
aschiffler@6727
  1026
                    }
slouken@11425
  1027
                    SDL_snprintfcat(text, sizeof(text), " %s", SDL_GetAudioDriver(i));
aschiffler@6727
  1028
                }
slouken@11425
  1029
                SDL_Log("%s\n", text);
aschiffler@6727
  1030
            }
aschiffler@6727
  1031
        }
aschiffler@6727
  1032
        if (SDL_AudioInit(state->audiodriver) < 0) {
slouken@10866
  1033
            SDL_Log("Couldn't initialize audio driver: %s\n",
aschiffler@6727
  1034
                    SDL_GetError());
aschiffler@6727
  1035
            return SDL_FALSE;
aschiffler@6727
  1036
        }
aschiffler@6727
  1037
        if (state->verbose & VERBOSE_VIDEO) {
slouken@10866
  1038
            SDL_Log("Audio driver: %s\n",
aschiffler@6727
  1039
                    SDL_GetCurrentAudioDriver());
aschiffler@6727
  1040
        }
aschiffler@6727
  1041
aschiffler@6727
  1042
        if (SDL_OpenAudio(&state->audiospec, NULL) < 0) {
slouken@10866
  1043
            SDL_Log("Couldn't open audio: %s\n", SDL_GetError());
aschiffler@6727
  1044
            return SDL_FALSE;
aschiffler@6727
  1045
        }
aschiffler@6727
  1046
    }
aschiffler@6727
  1047
aschiffler@6727
  1048
    return SDL_TRUE;
aschiffler@6727
  1049
}
aschiffler@6727
  1050
slouken@8069
  1051
static const char *
slouken@8069
  1052
ControllerAxisName(const SDL_GameControllerAxis axis)
slouken@8069
  1053
{
slouken@8069
  1054
    switch (axis)
slouken@8069
  1055
    {
slouken@8069
  1056
#define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
slouken@8069
  1057
        AXIS_CASE(INVALID);
slouken@8069
  1058
        AXIS_CASE(LEFTX);
slouken@8069
  1059
        AXIS_CASE(LEFTY);
slouken@8069
  1060
        AXIS_CASE(RIGHTX);
slouken@8069
  1061
        AXIS_CASE(RIGHTY);
slouken@8069
  1062
        AXIS_CASE(TRIGGERLEFT);
slouken@8069
  1063
        AXIS_CASE(TRIGGERRIGHT);
slouken@8069
  1064
#undef AXIS_CASE
slouken@8069
  1065
default: return "???";
slouken@8069
  1066
    }
slouken@8069
  1067
}
slouken@8069
  1068
slouken@8069
  1069
static const char *
slouken@8069
  1070
ControllerButtonName(const SDL_GameControllerButton button)
slouken@8069
  1071
{
slouken@8069
  1072
    switch (button)
slouken@8069
  1073
    {
slouken@8069
  1074
#define BUTTON_CASE(btn) case SDL_CONTROLLER_BUTTON_##btn: return #btn
slouken@8069
  1075
        BUTTON_CASE(INVALID);
slouken@8069
  1076
        BUTTON_CASE(A);
slouken@8069
  1077
        BUTTON_CASE(B);
slouken@8069
  1078
        BUTTON_CASE(X);
slouken@8069
  1079
        BUTTON_CASE(Y);
slouken@8069
  1080
        BUTTON_CASE(BACK);
slouken@8069
  1081
        BUTTON_CASE(GUIDE);
slouken@8069
  1082
        BUTTON_CASE(START);
slouken@8069
  1083
        BUTTON_CASE(LEFTSTICK);
slouken@8069
  1084
        BUTTON_CASE(RIGHTSTICK);
slouken@8069
  1085
        BUTTON_CASE(LEFTSHOULDER);
slouken@8069
  1086
        BUTTON_CASE(RIGHTSHOULDER);
slouken@8069
  1087
        BUTTON_CASE(DPAD_UP);
slouken@8069
  1088
        BUTTON_CASE(DPAD_DOWN);
slouken@8069
  1089
        BUTTON_CASE(DPAD_LEFT);
slouken@8069
  1090
        BUTTON_CASE(DPAD_RIGHT);
slouken@8069
  1091
#undef BUTTON_CASE
slouken@8069
  1092
default: return "???";
slouken@8069
  1093
    }
slouken@8069
  1094
}
slouken@8069
  1095
aschiffler@6727
  1096
static void
aschiffler@6727
  1097
SDLTest_PrintEvent(SDL_Event * event)
aschiffler@6727
  1098
{
icculus@7470
  1099
    if ((event->type == SDL_MOUSEMOTION) || (event->type == SDL_FINGERMOTION)) {
icculus@7470
  1100
        /* Mouse and finger motion are really spammy */
slouken@8012
  1101
        return;
aschiffler@6727
  1102
    }
aschiffler@6727
  1103
aschiffler@6727
  1104
    switch (event->type) {
aschiffler@6727
  1105
    case SDL_WINDOWEVENT:
aschiffler@6727
  1106
        switch (event->window.event) {
aschiffler@6727
  1107
        case SDL_WINDOWEVENT_SHOWN:
slouken@8069
  1108
            SDL_Log("SDL EVENT: Window %d shown", event->window.windowID);
aschiffler@6727
  1109
            break;
aschiffler@6727
  1110
        case SDL_WINDOWEVENT_HIDDEN:
slouken@8069
  1111
            SDL_Log("SDL EVENT: Window %d hidden", event->window.windowID);
aschiffler@6727
  1112
            break;
aschiffler@6727
  1113
        case SDL_WINDOWEVENT_EXPOSED:
slouken@8069
  1114
            SDL_Log("SDL EVENT: Window %d exposed", event->window.windowID);
aschiffler@6727
  1115
            break;
aschiffler@6727
  1116
        case SDL_WINDOWEVENT_MOVED:
slouken@8069
  1117
            SDL_Log("SDL EVENT: Window %d moved to %d,%d",
aschiffler@6727
  1118
                    event->window.windowID, event->window.data1,
aschiffler@6727
  1119
                    event->window.data2);
aschiffler@6727
  1120
            break;
aschiffler@6727
  1121
        case SDL_WINDOWEVENT_RESIZED:
slouken@8069
  1122
            SDL_Log("SDL EVENT: Window %d resized to %dx%d",
aschiffler@6727
  1123
                    event->window.windowID, event->window.data1,
aschiffler@6727
  1124
                    event->window.data2);
aschiffler@6727
  1125
            break;
aschiffler@6727
  1126
        case SDL_WINDOWEVENT_SIZE_CHANGED:
slouken@8069
  1127
            SDL_Log("SDL EVENT: Window %d changed size to %dx%d",
aschiffler@6727
  1128
                    event->window.windowID, event->window.data1,
aschiffler@6727
  1129
                    event->window.data2);
aschiffler@6727
  1130
            break;
aschiffler@6727
  1131
        case SDL_WINDOWEVENT_MINIMIZED:
slouken@8069
  1132
            SDL_Log("SDL EVENT: Window %d minimized", event->window.windowID);
aschiffler@6727
  1133
            break;
aschiffler@6727
  1134
        case SDL_WINDOWEVENT_MAXIMIZED:
slouken@8069
  1135
            SDL_Log("SDL EVENT: Window %d maximized", event->window.windowID);
aschiffler@6727
  1136
            break;
aschiffler@6727
  1137
        case SDL_WINDOWEVENT_RESTORED:
slouken@8069
  1138
            SDL_Log("SDL EVENT: Window %d restored", event->window.windowID);
aschiffler@6727
  1139
            break;
aschiffler@6727
  1140
        case SDL_WINDOWEVENT_ENTER:
slouken@8069
  1141
            SDL_Log("SDL EVENT: Mouse entered window %d",
aschiffler@6727
  1142
                    event->window.windowID);
aschiffler@6727
  1143
            break;
aschiffler@6727
  1144
        case SDL_WINDOWEVENT_LEAVE:
slouken@8069
  1145
            SDL_Log("SDL EVENT: Mouse left window %d", event->window.windowID);
aschiffler@6727
  1146
            break;
aschiffler@6727
  1147
        case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@8069
  1148
            SDL_Log("SDL EVENT: Window %d gained keyboard focus",
aschiffler@6727
  1149
                    event->window.windowID);
aschiffler@6727
  1150
            break;
aschiffler@6727
  1151
        case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@8069
  1152
            SDL_Log("SDL EVENT: Window %d lost keyboard focus",
aschiffler@6727
  1153
                    event->window.windowID);
aschiffler@6727
  1154
            break;
aschiffler@6727
  1155
        case SDL_WINDOWEVENT_CLOSE:
slouken@8069
  1156
            SDL_Log("SDL EVENT: Window %d closed", event->window.windowID);
aschiffler@6727
  1157
            break;
slouken@10381
  1158
        case SDL_WINDOWEVENT_TAKE_FOCUS:
slouken@10381
  1159
            SDL_Log("SDL EVENT: Window %d take focus", event->window.windowID);
slouken@10381
  1160
            break;
slouken@10381
  1161
        case SDL_WINDOWEVENT_HIT_TEST:
slouken@10381
  1162
            SDL_Log("SDL EVENT: Window %d hit test", event->window.windowID);
slouken@10381
  1163
            break;
aschiffler@6727
  1164
        default:
slouken@11257
  1165
            SDL_Log("SDL EVENT: Window %d got unknown event 0x%4.4x",
aschiffler@6727
  1166
                    event->window.windowID, event->window.event);
aschiffler@6727
  1167
            break;
aschiffler@6727
  1168
        }
aschiffler@6727
  1169
        break;
aschiffler@6727
  1170
    case SDL_KEYDOWN:
slouken@8069
  1171
        SDL_Log("SDL EVENT: Keyboard: key pressed  in window %d: scancode 0x%08X = %s, keycode 0x%08X = %s",
aschiffler@6727
  1172
                event->key.windowID,
aschiffler@6727
  1173
                event->key.keysym.scancode,
aschiffler@6727
  1174
                SDL_GetScancodeName(event->key.keysym.scancode),
aschiffler@6727
  1175
                event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym));
aschiffler@6727
  1176
        break;
aschiffler@6727
  1177
    case SDL_KEYUP:
slouken@8069
  1178
        SDL_Log("SDL EVENT: Keyboard: key released in window %d: scancode 0x%08X = %s, keycode 0x%08X = %s",
aschiffler@6727
  1179
                event->key.windowID,
aschiffler@6727
  1180
                event->key.keysym.scancode,
aschiffler@6727
  1181
                SDL_GetScancodeName(event->key.keysym.scancode),
aschiffler@6727
  1182
                event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym));
aschiffler@6727
  1183
        break;
slouken@11424
  1184
    case SDL_TEXTEDITING:
slouken@11424
  1185
        SDL_Log("SDL EVENT: Keyboard: text editing \"%s\" in window %d",
slouken@11424
  1186
                event->edit.text, event->edit.windowID);
slouken@11424
  1187
        break;
aschiffler@6727
  1188
    case SDL_TEXTINPUT:
slouken@8069
  1189
        SDL_Log("SDL EVENT: Keyboard: text input \"%s\" in window %d",
aschiffler@6727
  1190
                event->text.text, event->text.windowID);
aschiffler@6727
  1191
        break;
slouken@11424
  1192
    case SDL_KEYMAPCHANGED:
slouken@11424
  1193
        SDL_Log("SDL EVENT: Keymap changed");
slouken@11424
  1194
        break;
aschiffler@6727
  1195
    case SDL_MOUSEMOTION:
slouken@8069
  1196
        SDL_Log("SDL EVENT: Mouse: moved to %d,%d (%d,%d) in window %d",
aschiffler@6727
  1197
                event->motion.x, event->motion.y,
aschiffler@6727
  1198
                event->motion.xrel, event->motion.yrel,
aschiffler@6727
  1199
                event->motion.windowID);
aschiffler@6727
  1200
        break;
aschiffler@6727
  1201
    case SDL_MOUSEBUTTONDOWN:
slouken@8069
  1202
        SDL_Log("SDL EVENT: Mouse: button %d pressed at %d,%d with click count %d in window %d",
slouken@8066
  1203
                event->button.button, event->button.x, event->button.y, event->button.clicks,
aschiffler@6727
  1204
                event->button.windowID);
aschiffler@6727
  1205
        break;
aschiffler@6727
  1206
    case SDL_MOUSEBUTTONUP:
slouken@8069
  1207
        SDL_Log("SDL EVENT: Mouse: button %d released at %d,%d with click count %d in window %d",
slouken@8066
  1208
                event->button.button, event->button.x, event->button.y, event->button.clicks,
aschiffler@6727
  1209
                event->button.windowID);
aschiffler@6727
  1210
        break;
aschiffler@6727
  1211
    case SDL_MOUSEWHEEL:
urkle@9258
  1212
        SDL_Log("SDL EVENT: Mouse: wheel scrolled %d in x and %d in y (reversed: %d) in window %d",
urkle@9259
  1213
                event->wheel.x, event->wheel.y, event->wheel.direction, event->wheel.windowID);
aschiffler@6727
  1214
        break;
slouken@8069
  1215
    case SDL_JOYDEVICEADDED:
slouken@8069
  1216
        SDL_Log("SDL EVENT: Joystick index %d attached",
slouken@8069
  1217
            event->jdevice.which);
slouken@8069
  1218
        break;
slouken@8069
  1219
    case SDL_JOYDEVICEREMOVED:
slouken@8069
  1220
        SDL_Log("SDL EVENT: Joystick %d removed",
slouken@8069
  1221
            event->jdevice.which);
slouken@8069
  1222
        break;
aschiffler@6727
  1223
    case SDL_JOYBALLMOTION:
slouken@8069
  1224
        SDL_Log("SDL EVENT: Joystick %d: ball %d moved by %d,%d",
aschiffler@6727
  1225
                event->jball.which, event->jball.ball, event->jball.xrel,
aschiffler@6727
  1226
                event->jball.yrel);
aschiffler@6727
  1227
        break;
aschiffler@6727
  1228
    case SDL_JOYHATMOTION:
slouken@8069
  1229
        {
slouken@8069
  1230
            const char *position = "UNKNOWN";
slouken@8069
  1231
            switch (event->jhat.value) {
slouken@8069
  1232
            case SDL_HAT_CENTERED:
slouken@8069
  1233
                position = "CENTER";
slouken@8069
  1234
                break;
slouken@8069
  1235
            case SDL_HAT_UP:
slouken@8069
  1236
                position = "UP";
slouken@8069
  1237
                break;
slouken@8069
  1238
            case SDL_HAT_RIGHTUP:
slouken@8069
  1239
                position = "RIGHTUP";
slouken@8069
  1240
                break;
slouken@8069
  1241
            case SDL_HAT_RIGHT:
slouken@8069
  1242
                position = "RIGHT";
slouken@8069
  1243
                break;
slouken@8069
  1244
            case SDL_HAT_RIGHTDOWN:
slouken@8069
  1245
                position = "RIGHTDOWN";
slouken@8069
  1246
                break;
slouken@8069
  1247
            case SDL_HAT_DOWN:
slouken@8069
  1248
                position = "DOWN";
slouken@8069
  1249
                break;
slouken@8069
  1250
            case SDL_HAT_LEFTDOWN:
slouken@8069
  1251
                position = "LEFTDOWN";
slouken@8069
  1252
                break;
slouken@8069
  1253
            case SDL_HAT_LEFT:
slouken@8069
  1254
                position = "LEFT";
slouken@8069
  1255
                break;
slouken@8069
  1256
            case SDL_HAT_LEFTUP:
slouken@8069
  1257
                position = "LEFTUP";
slouken@8069
  1258
                break;
slouken@8069
  1259
            }
slouken@8069
  1260
            SDL_Log("SDL EVENT: Joystick %d: hat %d moved to %s", event->jhat.which,
slouken@8069
  1261
                event->jhat.hat, position);
aschiffler@6727
  1262
        }
aschiffler@6727
  1263
        break;
aschiffler@6727
  1264
    case SDL_JOYBUTTONDOWN:
slouken@8069
  1265
        SDL_Log("SDL EVENT: Joystick %d: button %d pressed",
aschiffler@6727
  1266
                event->jbutton.which, event->jbutton.button);
aschiffler@6727
  1267
        break;
aschiffler@6727
  1268
    case SDL_JOYBUTTONUP:
slouken@8069
  1269
        SDL_Log("SDL EVENT: Joystick %d: button %d released",
aschiffler@6727
  1270
                event->jbutton.which, event->jbutton.button);
aschiffler@6727
  1271
        break;
slouken@8069
  1272
    case SDL_CONTROLLERDEVICEADDED:
slouken@8069
  1273
        SDL_Log("SDL EVENT: Controller index %d attached",
slouken@8069
  1274
            event->cdevice.which);
slouken@8069
  1275
        break;
slouken@8069
  1276
    case SDL_CONTROLLERDEVICEREMOVED:
slouken@8069
  1277
        SDL_Log("SDL EVENT: Controller %d removed",
slouken@8069
  1278
            event->cdevice.which);
slouken@8069
  1279
        break;
slouken@8069
  1280
    case SDL_CONTROLLERAXISMOTION:
slouken@8069
  1281
        SDL_Log("SDL EVENT: Controller %d axis %d ('%s') value: %d",
slouken@8069
  1282
            event->caxis.which,
slouken@8069
  1283
            event->caxis.axis,
slouken@8069
  1284
            ControllerAxisName((SDL_GameControllerAxis)event->caxis.axis),
slouken@8069
  1285
            event->caxis.value);
slouken@8069
  1286
        break;
slouken@8069
  1287
    case SDL_CONTROLLERBUTTONDOWN:
slouken@8069
  1288
        SDL_Log("SDL EVENT: Controller %d button %d ('%s') down",
slouken@8069
  1289
            event->cbutton.which, event->cbutton.button,
slouken@8069
  1290
            ControllerButtonName((SDL_GameControllerButton)event->cbutton.button));
slouken@8069
  1291
        break;
slouken@8069
  1292
    case SDL_CONTROLLERBUTTONUP:
slouken@8069
  1293
        SDL_Log("SDL EVENT: Controller %d button %d ('%s') up",
slouken@8069
  1294
            event->cbutton.which, event->cbutton.button,
slouken@8069
  1295
            ControllerButtonName((SDL_GameControllerButton)event->cbutton.button));
slouken@8069
  1296
        break;
aschiffler@6727
  1297
    case SDL_CLIPBOARDUPDATE:
slouken@8069
  1298
        SDL_Log("SDL EVENT: Clipboard updated");
aschiffler@6727
  1299
        break;
icculus@7470
  1300
slouken@11257
  1301
    case SDL_FINGERMOTION:
slouken@11257
  1302
        SDL_Log("SDL EVENT: Finger: motion touch=%ld, finger=%ld, x=%f, y=%f, dx=%f, dy=%f, pressure=%f",
slouken@11257
  1303
                (long) event->tfinger.touchId,
slouken@11257
  1304
                (long) event->tfinger.fingerId,
slouken@11257
  1305
                event->tfinger.x, event->tfinger.y,
slouken@11257
  1306
                event->tfinger.dx, event->tfinger.dy, event->tfinger.pressure);
slouken@11257
  1307
        break;
icculus@7470
  1308
    case SDL_FINGERDOWN:
icculus@7470
  1309
    case SDL_FINGERUP:
slouken@8069
  1310
        SDL_Log("SDL EVENT: Finger: %s touch=%ld, finger=%ld, x=%f, y=%f, dx=%f, dy=%f, pressure=%f",
icculus@7470
  1311
                (event->type == SDL_FINGERDOWN) ? "down" : "up",
slouken@7828
  1312
                (long) event->tfinger.touchId,
slouken@7828
  1313
                (long) event->tfinger.fingerId,
icculus@7470
  1314
                event->tfinger.x, event->tfinger.y,
icculus@7470
  1315
                event->tfinger.dx, event->tfinger.dy, event->tfinger.pressure);
icculus@7470
  1316
        break;
urkle@9258
  1317
    case SDL_DOLLARGESTURE:
icculus@10644
  1318
        SDL_Log("SDL_EVENT: Dollar gesture detect: %ld", (long) event->dgesture.gestureId);
urkle@9258
  1319
        break;
urkle@9258
  1320
    case SDL_DOLLARRECORD:
icculus@10644
  1321
        SDL_Log("SDL_EVENT: Dollar gesture record: %ld", (long) event->dgesture.gestureId);
urkle@9258
  1322
        break;
urkle@9258
  1323
    case SDL_MULTIGESTURE:
urkle@9258
  1324
        SDL_Log("SDL_EVENT: Multi gesture fingers: %d", event->mgesture.numFingers);
urkle@9258
  1325
        break;
icculus@7470
  1326
slouken@8674
  1327
    case SDL_RENDER_DEVICE_RESET:
slouken@8674
  1328
        SDL_Log("SDL EVENT: render device reset");
slouken@8674
  1329
        break;
slouken@8674
  1330
    case SDL_RENDER_TARGETS_RESET:
slouken@8674
  1331
        SDL_Log("SDL EVENT: render targets reset");
slouken@8674
  1332
        break;
slouken@8674
  1333
slouken@11257
  1334
    case SDL_APP_TERMINATING:
slouken@11257
  1335
        SDL_Log("SDL EVENT: App terminating");
slouken@11257
  1336
        break;
slouken@11257
  1337
    case SDL_APP_LOWMEMORY:
slouken@11257
  1338
        SDL_Log("SDL EVENT: App running low on memory");
slouken@11257
  1339
        break;
slouken@11257
  1340
    case SDL_APP_WILLENTERBACKGROUND:
slouken@11257
  1341
        SDL_Log("SDL EVENT: App will enter the background");
slouken@11257
  1342
        break;
slouken@11257
  1343
    case SDL_APP_DIDENTERBACKGROUND:
slouken@11257
  1344
        SDL_Log("SDL EVENT: App entered the background");
slouken@11257
  1345
        break;
slouken@11257
  1346
    case SDL_APP_WILLENTERFOREGROUND:
slouken@11257
  1347
        SDL_Log("SDL EVENT: App will enter the foreground");
slouken@11257
  1348
        break;
slouken@11257
  1349
    case SDL_APP_DIDENTERFOREGROUND:
slouken@11257
  1350
        SDL_Log("SDL EVENT: App entered the foreground");
slouken@11257
  1351
        break;
slouken@11257
  1352
aschiffler@6727
  1353
    case SDL_QUIT:
slouken@8069
  1354
        SDL_Log("SDL EVENT: Quit requested");
aschiffler@6727
  1355
        break;
aschiffler@6727
  1356
    case SDL_USEREVENT:
slouken@8069
  1357
        SDL_Log("SDL EVENT: User event %d", event->user.code);
aschiffler@6727
  1358
        break;
aschiffler@6727
  1359
    default:
slouken@11257
  1360
        SDL_Log("Unknown event 0x%4.4x", event->type);
aschiffler@6727
  1361
        break;
aschiffler@6727
  1362
    }
aschiffler@6727
  1363
}
aschiffler@6727
  1364
aschiffler@6727
  1365
static void
aschiffler@6727
  1366
SDLTest_ScreenShot(SDL_Renderer *renderer)
aschiffler@6727
  1367
{
aschiffler@6727
  1368
    SDL_Rect viewport;
aschiffler@6727
  1369
    SDL_Surface *surface;
aschiffler@6727
  1370
aschiffler@6727
  1371
    if (!renderer) {
aschiffler@6727
  1372
        return;
aschiffler@6727
  1373
    }
aschiffler@6727
  1374
aschiffler@6727
  1375
    SDL_RenderGetViewport(renderer, &viewport);
aschiffler@6727
  1376
    surface = SDL_CreateRGBSurface(0, viewport.w, viewport.h, 24,
aschiffler@6727
  1377
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
aschiffler@6727
  1378
                    0x00FF0000, 0x0000FF00, 0x000000FF,
aschiffler@6727
  1379
#else
aschiffler@6727
  1380
                    0x000000FF, 0x0000FF00, 0x00FF0000,
aschiffler@6727
  1381
#endif
aschiffler@6727
  1382
                    0x00000000);
aschiffler@6727
  1383
    if (!surface) {
slouken@10866
  1384
        SDL_Log("Couldn't create surface: %s\n", SDL_GetError());
aschiffler@6727
  1385
        return;
aschiffler@6727
  1386
    }
aschiffler@6727
  1387
aschiffler@6727
  1388
    if (SDL_RenderReadPixels(renderer, NULL, surface->format->format,
aschiffler@6727
  1389
                             surface->pixels, surface->pitch) < 0) {
slouken@10866
  1390
        SDL_Log("Couldn't read screen: %s\n", SDL_GetError());
slouken@7869
  1391
        SDL_free(surface);
aschiffler@6727
  1392
        return;
aschiffler@6727
  1393
    }
aschiffler@6727
  1394
aschiffler@6727
  1395
    if (SDL_SaveBMP(surface, "screenshot.bmp") < 0) {
slouken@10866
  1396
        SDL_Log("Couldn't save screenshot.bmp: %s\n", SDL_GetError());
slouken@7869
  1397
        SDL_free(surface);
aschiffler@6727
  1398
        return;
aschiffler@6727
  1399
    }
aschiffler@6727
  1400
}
aschiffler@6727
  1401
slouken@7191
  1402
static void
jorgen@7092
  1403
FullscreenTo(int index, int windowId)
jorgen@7092
  1404
{
jorgen@7092
  1405
    Uint32 flags;
jorgen@7092
  1406
    struct SDL_Rect rect = { 0, 0, 0, 0 };
jorgen@7092
  1407
    SDL_Window *window = SDL_GetWindowFromID(windowId);
jorgen@7092
  1408
    if (!window) {
jorgen@7092
  1409
        return;
jorgen@7092
  1410
    }
jorgen@7092
  1411
jorgen@7092
  1412
    SDL_GetDisplayBounds( index, &rect );
jorgen@7092
  1413
jorgen@7092
  1414
    flags = SDL_GetWindowFlags(window);
jorgen@7092
  1415
    if (flags & SDL_WINDOW_FULLSCREEN) {
jorgen@7092
  1416
        SDL_SetWindowFullscreen( window, SDL_FALSE );
jorgen@7092
  1417
        SDL_Delay( 15 );
jorgen@7092
  1418
    }
jorgen@7092
  1419
jorgen@7092
  1420
    SDL_SetWindowPosition( window, rect.x, rect.y );
jorgen@7092
  1421
    SDL_SetWindowFullscreen( window, SDL_TRUE );
jorgen@7092
  1422
}
jorgen@7092
  1423
aschiffler@6727
  1424
void
aschiffler@6727
  1425
SDLTest_CommonEvent(SDLTest_CommonState * state, SDL_Event * event, int *done)
aschiffler@6727
  1426
{
aschiffler@6727
  1427
    int i;
jorgen@7092
  1428
    static SDL_MouseMotionEvent lastEvent;
aschiffler@6727
  1429
aschiffler@6727
  1430
    if (state->verbose & VERBOSE_EVENT) {
aschiffler@6727
  1431
        SDLTest_PrintEvent(event);
aschiffler@6727
  1432
    }
aschiffler@6727
  1433
aschiffler@6727
  1434
    switch (event->type) {
aschiffler@6727
  1435
    case SDL_WINDOWEVENT:
aschiffler@6727
  1436
        switch (event->window.event) {
aschiffler@6727
  1437
        case SDL_WINDOWEVENT_CLOSE:
slouken@7191
  1438
            {
aschiffler@6727
  1439
                SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
aschiffler@6727
  1440
                if (window) {
slouken@7787
  1441
                    for (i = 0; i < state->num_windows; ++i) {
slouken@7787
  1442
                        if (window == state->windows[i]) {
slouken@8115
  1443
                            if (state->targets[i]) {
slouken@8115
  1444
                                SDL_DestroyTexture(state->targets[i]);
slouken@8115
  1445
                                state->targets[i] = NULL;
slouken@8115
  1446
                            }
slouken@8115
  1447
                            if (state->renderers[i]) {
slouken@8115
  1448
                                SDL_DestroyRenderer(state->renderers[i]);
slouken@8115
  1449
                                state->renderers[i] = NULL;
slouken@8115
  1450
                            }
slouken@8115
  1451
                            SDL_DestroyWindow(state->windows[i]);
slouken@7787
  1452
                            state->windows[i] = NULL;
slouken@7787
  1453
                            break;
slouken@7787
  1454
                        }
slouken@7787
  1455
                    }
slouken@7191
  1456
                }
slouken@7191
  1457
            }
aschiffler@6727
  1458
            break;
aschiffler@6727
  1459
        }
aschiffler@6727
  1460
        break;
jorgen@8108
  1461
    case SDL_KEYDOWN: {
jorgen@8108
  1462
        SDL_bool withControl = !!(event->key.keysym.mod & KMOD_CTRL);
jorgen@8108
  1463
        SDL_bool withShift = !!(event->key.keysym.mod & KMOD_SHIFT);
jorgen@8108
  1464
        SDL_bool withAlt = !!(event->key.keysym.mod & KMOD_ALT);
jorgen@8108
  1465
aschiffler@6727
  1466
        switch (event->key.keysym.sym) {
aschiffler@6727
  1467
            /* Add hotkeys here */
aschiffler@6727
  1468
        case SDLK_PRINTSCREEN: {
aschiffler@6727
  1469
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
aschiffler@6727
  1470
                if (window) {
aschiffler@6727
  1471
                    for (i = 0; i < state->num_windows; ++i) {
aschiffler@6727
  1472
                        if (window == state->windows[i]) {
aschiffler@6727
  1473
                            SDLTest_ScreenShot(state->renderers[i]);
aschiffler@6727
  1474
                        }
aschiffler@6727
  1475
                    }
aschiffler@6727
  1476
                }
aschiffler@6727
  1477
            }
aschiffler@6727
  1478
            break;
aschiffler@6727
  1479
        case SDLK_EQUALS:
jorgen@8108
  1480
            if (withControl) {
philipp@7474
  1481
                /* Ctrl-+ double the size of the window */
aschiffler@6727
  1482
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
aschiffler@6727
  1483
                if (window) {
aschiffler@6727
  1484
                    int w, h;
aschiffler@6727
  1485
                    SDL_GetWindowSize(window, &w, &h);
aschiffler@6727
  1486
                    SDL_SetWindowSize(window, w*2, h*2);
aschiffler@6727
  1487
                }
aschiffler@6727
  1488
            }
aschiffler@6727
  1489
            break;
aschiffler@6727
  1490
        case SDLK_MINUS:
jorgen@8108
  1491
            if (withControl) {
philipp@7474
  1492
                /* Ctrl-- half the size of the window */
aschiffler@6727
  1493
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
aschiffler@6727
  1494
                if (window) {
aschiffler@6727
  1495
                    int w, h;
aschiffler@6727
  1496
                    SDL_GetWindowSize(window, &w, &h);
aschiffler@6727
  1497
                    SDL_SetWindowSize(window, w/2, h/2);
aschiffler@6727
  1498
                }
aschiffler@6727
  1499
            }
aschiffler@6727
  1500
            break;
slouken@11221
  1501
        case SDLK_UP:
slouken@11221
  1502
        case SDLK_DOWN:
slouken@11221
  1503
        case SDLK_LEFT:
slouken@11221
  1504
        case SDLK_RIGHT:
slouken@11221
  1505
            if (withAlt) {
slouken@11221
  1506
                /* Alt-Up/Down/Left/Right switches between displays */
slouken@11221
  1507
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
slouken@11221
  1508
                if (window) {
slouken@11221
  1509
                    int currentIndex = SDL_GetWindowDisplayIndex(window);
slouken@11221
  1510
                    int numDisplays = SDL_GetNumVideoDisplays();
slouken@11221
  1511
slouken@11221
  1512
                    if (currentIndex >= 0 && numDisplays >= 1) {
slouken@11221
  1513
                        int dest;
slouken@11221
  1514
                        if (event->key.keysym.sym == SDLK_UP || event->key.keysym.sym == SDLK_LEFT) {
slouken@11221
  1515
                            dest = (currentIndex + numDisplays - 1) % numDisplays;
slouken@11221
  1516
                        } else {
slouken@11221
  1517
                            dest = (currentIndex + numDisplays + 1) % numDisplays;
slouken@11221
  1518
                        }
slouken@11221
  1519
                        SDL_Log("Centering on display %d\n", dest);
slouken@11221
  1520
                        SDL_SetWindowPosition(window,
slouken@11221
  1521
                            SDL_WINDOWPOS_CENTERED_DISPLAY(dest),
slouken@11221
  1522
                            SDL_WINDOWPOS_CENTERED_DISPLAY(dest));
slouken@11221
  1523
                    }
slouken@11221
  1524
                }
slouken@11221
  1525
            }
slouken@11221
  1526
            if (withShift) {
slouken@11221
  1527
                /* Shift-Up/Down/Left/Right shift the window by 100px */
slouken@11221
  1528
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
slouken@11221
  1529
                if (window) {
slouken@11221
  1530
                    const int delta = 100;
slouken@11221
  1531
                    int x, y;
slouken@11221
  1532
                    SDL_GetWindowPosition(window, &x, &y);
slouken@11221
  1533
                    
slouken@11221
  1534
                    if (event->key.keysym.sym == SDLK_UP)    y -= delta;
slouken@11221
  1535
                    if (event->key.keysym.sym == SDLK_DOWN)  y += delta;
slouken@11221
  1536
                    if (event->key.keysym.sym == SDLK_LEFT)  x -= delta;
slouken@11221
  1537
                    if (event->key.keysym.sym == SDLK_RIGHT) x += delta;
slouken@11221
  1538
slouken@11221
  1539
                    SDL_Log("Setting position to (%d, %d)\n", x, y);
slouken@11221
  1540
                    SDL_SetWindowPosition(window, x, y);
slouken@11221
  1541
                }
slouken@11221
  1542
            }
slouken@11221
  1543
            break;
icculus@10025
  1544
        case SDLK_o:
icculus@10025
  1545
            if (withControl) {
icculus@10025
  1546
                /* Ctrl-O (or Ctrl-Shift-O) changes window opacity. */
icculus@10025
  1547
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
icculus@10025
  1548
                if (window) {
icculus@10025
  1549
                    float opacity;
icculus@10025
  1550
                    if (SDL_GetWindowOpacity(window, &opacity) == 0) {
icculus@10025
  1551
                        if (withShift) {
icculus@10025
  1552
                            opacity += 0.20f;
icculus@10025
  1553
                        } else {
icculus@10025
  1554
                            opacity -= 0.20f;
icculus@10025
  1555
                        }
icculus@10025
  1556
                        SDL_SetWindowOpacity(window, opacity);
icculus@10025
  1557
                    }
icculus@10025
  1558
                }
icculus@10025
  1559
            }
icculus@10025
  1560
            break;
icculus@10025
  1561
aschiffler@6727
  1562
        case SDLK_c:
jorgen@8108
  1563
            if (withControl) {
aschiffler@6727
  1564
                /* Ctrl-C copy awesome text! */
aschiffler@6727
  1565
                SDL_SetClipboardText("SDL rocks!\nYou know it!");
aschiffler@6727
  1566
                printf("Copied text to clipboard\n");
aschiffler@6727
  1567
            }
jorgen@8108
  1568
            if (withAlt) {
slouken@7141
  1569
                /* Alt-C toggle a render clip rectangle */
slouken@7141
  1570
                for (i = 0; i < state->num_windows; ++i) {
slouken@7141
  1571
                    int w, h;
slouken@7141
  1572
                    if (state->renderers[i]) {
slouken@7141
  1573
                        SDL_Rect clip;
slouken@7141
  1574
                        SDL_GetWindowSize(state->windows[i], &w, &h);
slouken@7141
  1575
                        SDL_RenderGetClipRect(state->renderers[i], &clip);
slouken@7141
  1576
                        if (SDL_RectEmpty(&clip)) {
slouken@7141
  1577
                            clip.x = w/4;
slouken@7141
  1578
                            clip.y = h/4;
slouken@7141
  1579
                            clip.w = w/2;
slouken@7141
  1580
                            clip.h = h/2;
slouken@7141
  1581
                            SDL_RenderSetClipRect(state->renderers[i], &clip);
slouken@7141
  1582
                        } else {
slouken@7141
  1583
                            SDL_RenderSetClipRect(state->renderers[i], NULL);
slouken@7141
  1584
                        }
slouken@7141
  1585
                    }
slouken@7141
  1586
                }
slouken@7141
  1587
            }
icculus@8927
  1588
            if (withShift) {
icculus@8927
  1589
                SDL_Window *current_win = SDL_GetKeyboardFocus();
icculus@8927
  1590
                if (current_win) {
icculus@8927
  1591
                    const SDL_bool shouldCapture = (SDL_GetWindowFlags(current_win) & SDL_WINDOW_MOUSE_CAPTURE) == 0;
icculus@8927
  1592
                    const int rc = SDL_CaptureMouse(shouldCapture);
icculus@8939
  1593
                    SDL_Log("%sapturing mouse %s!\n", shouldCapture ? "C" : "Unc", (rc == 0) ? "succeeded" : "failed");
icculus@8927
  1594
                }
icculus@8927
  1595
            }
aschiffler@6727
  1596
            break;
aschiffler@6727
  1597
        case SDLK_v:
jorgen@8108
  1598
            if (withControl) {
aschiffler@6727
  1599
                /* Ctrl-V paste awesome text! */
aschiffler@6727
  1600
                char *text = SDL_GetClipboardText();
aschiffler@6727
  1601
                if (*text) {
aschiffler@6727
  1602
                    printf("Clipboard: %s\n", text);
aschiffler@6727
  1603
                } else {
aschiffler@6727
  1604
                    printf("Clipboard is empty\n");
aschiffler@6727
  1605
                }
aschiffler@6727
  1606
                SDL_free(text);
aschiffler@6727
  1607
            }
aschiffler@6727
  1608
            break;
aschiffler@6727
  1609
        case SDLK_g:
jorgen@8108
  1610
            if (withControl) {
aschiffler@6727
  1611
                /* Ctrl-G toggle grab */
aschiffler@6727
  1612
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
aschiffler@6727
  1613
                if (window) {
aschiffler@7249
  1614
                    SDL_SetWindowGrab(window, !SDL_GetWindowGrab(window) ? SDL_TRUE : SDL_FALSE);
aschiffler@6727
  1615
                }
aschiffler@6727
  1616
            }
aschiffler@6727
  1617
            break;
aschiffler@6727
  1618
        case SDLK_m:
jorgen@8108
  1619
            if (withControl) {
aschiffler@6727
  1620
                /* Ctrl-M maximize */
aschiffler@6727
  1621
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
aschiffler@6727
  1622
                if (window) {
aschiffler@6727
  1623
                    Uint32 flags = SDL_GetWindowFlags(window);
aschiffler@6727
  1624
                    if (flags & SDL_WINDOW_MAXIMIZED) {
aschiffler@6727
  1625
                        SDL_RestoreWindow(window);
aschiffler@6727
  1626
                    } else {
aschiffler@6727
  1627
                        SDL_MaximizeWindow(window);
aschiffler@6727
  1628
                    }
aschiffler@6727
  1629
                }
aschiffler@6727
  1630
            }
aschiffler@6727
  1631
            break;
aschiffler@6727
  1632
        case SDLK_r:
jorgen@8108
  1633
            if (withControl) {
aschiffler@6727
  1634
                /* Ctrl-R toggle mouse relative mode */
aschiffler@7249
  1635
                SDL_SetRelativeMouseMode(!SDL_GetRelativeMouseMode() ? SDL_TRUE : SDL_FALSE);
aschiffler@6727
  1636
            }
aschiffler@6727
  1637
            break;
aschiffler@6727
  1638
        case SDLK_z:
jorgen@8108
  1639
            if (withControl) {
aschiffler@6727
  1640
                /* Ctrl-Z minimize */
aschiffler@6727
  1641
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
aschiffler@6727
  1642
                if (window) {
aschiffler@6727
  1643
                    SDL_MinimizeWindow(window);
aschiffler@6727
  1644
                }
aschiffler@6727
  1645
            }
aschiffler@6727
  1646
            break;
aschiffler@6727
  1647
        case SDLK_RETURN:
jorgen@8108
  1648
            if (withControl) {
aschiffler@6727
  1649
                /* Ctrl-Enter toggle fullscreen */
aschiffler@6727
  1650
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
aschiffler@6727
  1651
                if (window) {
aschiffler@6727
  1652
                    Uint32 flags = SDL_GetWindowFlags(window);
aschiffler@6727
  1653
                    if (flags & SDL_WINDOW_FULLSCREEN) {
aschiffler@6727
  1654
                        SDL_SetWindowFullscreen(window, SDL_FALSE);
aschiffler@6727
  1655
                    } else {
slouken@7949
  1656
                        SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
aschiffler@6727
  1657
                    }
aschiffler@6727
  1658
                }
jorgen@8108
  1659
            } else if (withAlt) {
philipp@7474
  1660
                /* Alt-Enter toggle fullscreen desktop */
urkle@7340
  1661
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
urkle@7340
  1662
                if (window) {
urkle@7340
  1663
                    Uint32 flags = SDL_GetWindowFlags(window);
urkle@7340
  1664
                    if (flags & SDL_WINDOW_FULLSCREEN) {
urkle@7340
  1665
                        SDL_SetWindowFullscreen(window, SDL_FALSE);
urkle@7340
  1666
                    } else {
urkle@7340
  1667
                        SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
urkle@7340
  1668
                    }
urkle@7340
  1669
                }
jorgen@8109
  1670
            } else if (withShift) {
jorgen@8109
  1671
                /* Shift-Enter toggle fullscreen desktop / fullscreen */
jorgen@8109
  1672
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
jorgen@8109
  1673
                if (window) {
jorgen@8109
  1674
                    Uint32 flags = SDL_GetWindowFlags(window);
jorgen@8109
  1675
                    if ((flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
jorgen@8109
  1676
                        SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
jorgen@8109
  1677
                    } else {
jorgen@8109
  1678
                        SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
jorgen@8109
  1679
                    }
jorgen@8109
  1680
                }
aschiffler@6727
  1681
            }
jorgen@8109
  1682
aschiffler@6727
  1683
            break;
aschiffler@6727
  1684
        case SDLK_b:
jorgen@8108
  1685
            if (withControl) {
aschiffler@6727
  1686
                /* Ctrl-B toggle window border */
aschiffler@6727
  1687
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
aschiffler@6727
  1688
                if (window) {
aschiffler@6727
  1689
                    const Uint32 flags = SDL_GetWindowFlags(window);
aschiffler@7249
  1690
                    const SDL_bool b = ((flags & SDL_WINDOW_BORDERLESS) != 0) ? SDL_TRUE : SDL_FALSE;
aschiffler@6727
  1691
                    SDL_SetWindowBordered(window, b);
aschiffler@6727
  1692
                }
aschiffler@6727
  1693
            }
aschiffler@6727
  1694
            break;
icculus@8945
  1695
        case SDLK_a:
icculus@8945
  1696
            if (withControl) {
icculus@8945
  1697
                /* Ctrl-A reports absolute mouse position. */
icculus@8945
  1698
                int x, y;
icculus@8952
  1699
                const Uint32 mask = SDL_GetGlobalMouseState(&x, &y);
icculus@8945
  1700
                SDL_Log("ABSOLUTE MOUSE: (%d, %d)%s%s%s%s%s\n", x, y,
icculus@8945
  1701
                        (mask & SDL_BUTTON_LMASK) ? " [LBUTTON]" : "",
icculus@8945
  1702
                        (mask & SDL_BUTTON_MMASK) ? " [MBUTTON]" : "",
icculus@8945
  1703
                        (mask & SDL_BUTTON_RMASK) ? " [RBUTTON]" : "",
icculus@8945
  1704
                        (mask & SDL_BUTTON_X1MASK) ? " [X2BUTTON]" : "",
icculus@8945
  1705
                        (mask & SDL_BUTTON_X2MASK) ? " [X2BUTTON]" : "");
icculus@8945
  1706
            }
icculus@8945
  1707
            break;
slouken@7191
  1708
        case SDLK_0:
jorgen@8108
  1709
            if (withControl) {
aschiffler@6727
  1710
                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
aschiffler@6727
  1711
                SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Test Message", "You're awesome!", window);
aschiffler@6727
  1712
            }
aschiffler@6727
  1713
            break;
jorgen@7092
  1714
        case SDLK_1:
jorgen@8108
  1715
            if (withControl) {
jorgen@7092
  1716
                FullscreenTo(0, event->key.windowID);
jorgen@7092
  1717
            }
jorgen@7092
  1718
            break;
jorgen@7092
  1719
        case SDLK_2:
jorgen@8108
  1720
            if (withControl) {
jorgen@7092
  1721
                FullscreenTo(1, event->key.windowID);
jorgen@7092
  1722
            }
jorgen@7092
  1723
            break;
aschiffler@6727
  1724
        case SDLK_ESCAPE:
aschiffler@6727
  1725
            *done = 1;
aschiffler@6727
  1726
            break;
jorgen@7092
  1727
        case SDLK_SPACE:
jorgen@7092
  1728
        {
jorgen@7092
  1729
            char message[256];
jorgen@7092
  1730
            SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
jorgen@7092
  1731
jorgen@7092
  1732
            SDL_snprintf(message, sizeof(message), "(%i, %i), rel (%i, %i)\n", lastEvent.x, lastEvent.y, lastEvent.xrel, lastEvent.yrel);
jorgen@7092
  1733
            SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Last mouse position", message, window);
jorgen@7092
  1734
            break;
jorgen@7092
  1735
        }
aschiffler@6727
  1736
        default:
aschiffler@6727
  1737
            break;
aschiffler@6727
  1738
        }
aschiffler@6727
  1739
        break;
jorgen@8108
  1740
    }
aschiffler@6727
  1741
    case SDL_QUIT:
aschiffler@6727
  1742
        *done = 1;
aschiffler@6727
  1743
        break;
jorgen@7092
  1744
    case SDL_MOUSEMOTION:
jorgen@7092
  1745
        lastEvent = event->motion;
slouken@7191
  1746
        break;
aschiffler@6727
  1747
    }
aschiffler@6727
  1748
}
aschiffler@6727
  1749
aschiffler@6727
  1750
void
aschiffler@6727
  1751
SDLTest_CommonQuit(SDLTest_CommonState * state)
aschiffler@6727
  1752
{
aschiffler@6727
  1753
    int i;
aschiffler@6727
  1754
slouken@7719
  1755
    SDL_free(state->windows);
slouken@8011
  1756
    if (state->targets) {
slouken@8011
  1757
        for (i = 0; i < state->num_windows; ++i) {
slouken@8011
  1758
            if (state->targets[i]) {
slouken@8011
  1759
                SDL_DestroyTexture(state->targets[i]);
slouken@8011
  1760
            }
slouken@8011
  1761
        }
slouken@8011
  1762
        SDL_free(state->targets);
slouken@8011
  1763
    }
aschiffler@6727
  1764
    if (state->renderers) {
aschiffler@6727
  1765
        for (i = 0; i < state->num_windows; ++i) {
aschiffler@6727
  1766
            if (state->renderers[i]) {
aschiffler@6727
  1767
                SDL_DestroyRenderer(state->renderers[i]);
aschiffler@6727
  1768
            }
aschiffler@6727
  1769
        }
aschiffler@6727
  1770
        SDL_free(state->renderers);
aschiffler@6727
  1771
    }
aschiffler@6727
  1772
    if (state->flags & SDL_INIT_VIDEO) {
aschiffler@6727
  1773
        SDL_VideoQuit();
aschiffler@6727
  1774
    }
aschiffler@6727
  1775
    if (state->flags & SDL_INIT_AUDIO) {
aschiffler@6727
  1776
        SDL_AudioQuit();
aschiffler@6727
  1777
    }
aschiffler@6727
  1778
    SDL_free(state);
slouken@8268
  1779
    SDL_Quit();
slouken@11610
  1780
    SDLTest_LogAllocations();
aschiffler@6727
  1781
}
aschiffler@6727
  1782
aschiffler@6727
  1783
/* vi: set ts=4 sw=4 expandtab: */