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