/
SDL_test_common.c
1715 lines (1633 loc) · 59.4 KB
1
2
/*
Simple DirectMedia Layer
3
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/* Ported from original test\common.c file. */
#include "SDL_config.h"
#include "SDL_test.h"
#include <stdio.h>
#define VIDEO_USAGE \
"[--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]"
#define AUDIO_USAGE \
"[--rate N] [--format U8|S8|U16|U16LE|U16BE|S16|S16LE|S16BE] [--channels N] [--samples N]"
35
36
37
38
39
40
41
42
43
44
45
46
static void SDL_snprintfcat(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ... )
{
size_t length = SDL_strlen(text);
va_list ap;
va_start(ap, fmt);
text += length;
maxlen -= length;
SDL_vsnprintf(text, maxlen, fmt, ap);
va_end(ap);
}
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
SDLTest_CommonState *
SDLTest_CommonCreateState(char **argv, Uint32 flags)
{
SDLTest_CommonState *state = (SDLTest_CommonState *)SDL_calloc(1, sizeof(*state));
if (!state) {
SDL_OutOfMemory();
return NULL;
}
/* Initialize some defaults */
state->argv = argv;
state->flags = flags;
state->window_title = argv[0];
state->window_flags = 0;
state->window_x = SDL_WINDOWPOS_UNDEFINED;
state->window_y = SDL_WINDOWPOS_UNDEFINED;
state->window_w = DEFAULT_WINDOW_WIDTH;
state->window_h = DEFAULT_WINDOW_HEIGHT;
state->num_windows = 1;
state->audiospec.freq = 22050;
state->audiospec.format = AUDIO_S16;
state->audiospec.channels = 2;
state->audiospec.samples = 2048;
/* Set some very sane GL defaults */
state->gl_red_size = 3;
state->gl_green_size = 3;
state->gl_blue_size = 2;
state->gl_alpha_size = 0;
state->gl_buffer_size = 0;
state->gl_depth_size = 16;
state->gl_stencil_size = 0;
state->gl_double_buffer = 1;
state->gl_accum_red_size = 0;
state->gl_accum_green_size = 0;
state->gl_accum_blue_size = 0;
state->gl_accum_alpha_size = 0;
state->gl_stereo = 0;
state->gl_multisamplebuffers = 0;
state->gl_multisamplesamples = 0;
state->gl_retained_backing = 1;
state->gl_accelerated = -1;
state->gl_debug = 0;
return state;
}
int
SDLTest_CommonArg(SDLTest_CommonState * state, int index)
{
char **argv = state->argv;
if (SDL_strcasecmp(argv[index], "--video") == 0) {
++index;
if (!argv[index]) {
return -1;
}
state->videodriver = argv[index];
return 2;
}
if (SDL_strcasecmp(argv[index], "--renderer") == 0) {
++index;
if (!argv[index]) {
return -1;
}
state->renderdriver = argv[index];
return 2;
}
if (SDL_strcasecmp(argv[index], "--gldebug") == 0) {
state->gl_debug = 1;
return 1;
}
if (SDL_strcasecmp(argv[index], "--info") == 0) {
++index;
if (!argv[index]) {
return -1;
}
if (SDL_strcasecmp(argv[index], "all") == 0) {
state->verbose |=
(VERBOSE_VIDEO | VERBOSE_MODES | VERBOSE_RENDER |
VERBOSE_EVENT);
return 2;
}
if (SDL_strcasecmp(argv[index], "video") == 0) {
state->verbose |= VERBOSE_VIDEO;
return 2;
}
if (SDL_strcasecmp(argv[index], "modes") == 0) {
state->verbose |= VERBOSE_MODES;
return 2;
}
if (SDL_strcasecmp(argv[index], "render") == 0) {
state->verbose |= VERBOSE_RENDER;
return 2;
}
if (SDL_strcasecmp(argv[index], "event") == 0) {
state->verbose |= VERBOSE_EVENT;
return 2;
}
return -1;
}
if (SDL_strcasecmp(argv[index], "--log") == 0) {
++index;
if (!argv[index]) {
return -1;
}
if (SDL_strcasecmp(argv[index], "all") == 0) {
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
return 2;
}
if (SDL_strcasecmp(argv[index], "error") == 0) {
SDL_LogSetPriority(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_VERBOSE);
return 2;
}
if (SDL_strcasecmp(argv[index], "system") == 0) {
SDL_LogSetPriority(SDL_LOG_CATEGORY_SYSTEM, SDL_LOG_PRIORITY_VERBOSE);
return 2;
}
if (SDL_strcasecmp(argv[index], "audio") == 0) {
SDL_LogSetPriority(SDL_LOG_CATEGORY_AUDIO, SDL_LOG_PRIORITY_VERBOSE);
return 2;
}
if (SDL_strcasecmp(argv[index], "video") == 0) {
SDL_LogSetPriority(SDL_LOG_CATEGORY_VIDEO, SDL_LOG_PRIORITY_VERBOSE);
return 2;
}
if (SDL_strcasecmp(argv[index], "render") == 0) {
SDL_LogSetPriority(SDL_LOG_CATEGORY_RENDER, SDL_LOG_PRIORITY_VERBOSE);
return 2;
}
if (SDL_strcasecmp(argv[index], "input") == 0) {
SDL_LogSetPriority(SDL_LOG_CATEGORY_INPUT, SDL_LOG_PRIORITY_VERBOSE);
return 2;
}
return -1;
}
if (SDL_strcasecmp(argv[index], "--display") == 0) {
++index;
if (!argv[index]) {
return -1;
}
state->display = SDL_atoi(argv[index]);
if (SDL_WINDOWPOS_ISUNDEFINED(state->window_x)) {
state->window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(state->display);
state->window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(state->display);
}
if (SDL_WINDOWPOS_ISCENTERED(state->window_x)) {
state->window_x = SDL_WINDOWPOS_CENTERED_DISPLAY(state->display);
state->window_y = SDL_WINDOWPOS_CENTERED_DISPLAY(state->display);
}
return 2;
}
if (SDL_strcasecmp(argv[index], "--fullscreen") == 0) {
state->window_flags |= SDL_WINDOW_FULLSCREEN;
state->num_windows = 1;
return 1;
}
if (SDL_strcasecmp(argv[index], "--fullscreen-desktop") == 0) {
state->window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
state->num_windows = 1;
return 1;
}
if (SDL_strcasecmp(argv[index], "--allow-highdpi") == 0) {
state->window_flags |= SDL_WINDOW_ALLOW_HIGHDPI;
return 1;
}
if (SDL_strcasecmp(argv[index], "--windows") == 0) {
++index;
if (!argv[index] || !SDL_isdigit(*argv[index])) {
return -1;
}
if (!(state->window_flags & SDL_WINDOW_FULLSCREEN)) {
state->num_windows = SDL_atoi(argv[index]);
}
return 2;
}
if (SDL_strcasecmp(argv[index], "--title") == 0) {
++index;
if (!argv[index]) {
return -1;
}
state->window_title = argv[index];
return 2;
}
if (SDL_strcasecmp(argv[index], "--icon") == 0) {
++index;
if (!argv[index]) {
return -1;
}
state->window_icon = argv[index];
return 2;
}
if (SDL_strcasecmp(argv[index], "--center") == 0) {
state->window_x = SDL_WINDOWPOS_CENTERED;
state->window_y = SDL_WINDOWPOS_CENTERED;
return 1;
}
if (SDL_strcasecmp(argv[index], "--position") == 0) {
char *x, *y;
++index;
if (!argv[index]) {
return -1;
}
x = argv[index];
y = argv[index];
while (*y && *y != ',') {
++y;
}
if (!*y) {
return -1;
}
*y++ = '\0';
state->window_x = SDL_atoi(x);
state->window_y = SDL_atoi(y);
return 2;
}
if (SDL_strcasecmp(argv[index], "--geometry") == 0) {
char *w, *h;
++index;
if (!argv[index]) {
return -1;
}
w = argv[index];
h = argv[index];
while (*h && *h != 'x') {
++h;
}
if (!*h) {
return -1;
}
*h++ = '\0';
state->window_w = SDL_atoi(w);
state->window_h = SDL_atoi(h);
return 2;
}
if (SDL_strcasecmp(argv[index], "--min-geometry") == 0) {
char *w, *h;
++index;
if (!argv[index]) {
return -1;
}
w = argv[index];
h = argv[index];
while (*h && *h != 'x') {
++h;
}
if (!*h) {
return -1;
}
*h++ = '\0';
state->window_minW = SDL_atoi(w);
state->window_minH = SDL_atoi(h);
return 2;
}
if (SDL_strcasecmp(argv[index], "--max-geometry") == 0) {
char *w, *h;
++index;
if (!argv[index]) {
return -1;
}
w = argv[index];
h = argv[index];
while (*h && *h != 'x') {
++h;
}
if (!*h) {
return -1;
}
*h++ = '\0';
state->window_maxW = SDL_atoi(w);
state->window_maxH = SDL_atoi(h);
return 2;
}
if (SDL_strcasecmp(argv[index], "--logical") == 0) {
char *w, *h;
++index;
if (!argv[index]) {
return -1;
}
w = argv[index];
h = argv[index];
while (*h && *h != 'x') {
++h;
}
if (!*h) {
return -1;
}
*h++ = '\0';
state->logical_w = SDL_atoi(w);
state->logical_h = SDL_atoi(h);
return 2;
}
if (SDL_strcasecmp(argv[index], "--scale") == 0) {
++index;
if (!argv[index]) {
return -1;
}
state->scale = (float)SDL_atof(argv[index]);
return 2;
}
if (SDL_strcasecmp(argv[index], "--depth") == 0) {
++index;
if (!argv[index]) {
return -1;
}
state->depth = SDL_atoi(argv[index]);
return 2;
}
if (SDL_strcasecmp(argv[index], "--refresh") == 0) {
++index;
if (!argv[index]) {
return -1;
}
state->refresh_rate = SDL_atoi(argv[index]);
return 2;
}
if (SDL_strcasecmp(argv[index], "--vsync") == 0) {
state->render_flags |= SDL_RENDERER_PRESENTVSYNC;
return 1;
}
if (SDL_strcasecmp(argv[index], "--noframe") == 0) {
state->window_flags |= SDL_WINDOW_BORDERLESS;
return 1;
}
if (SDL_strcasecmp(argv[index], "--resize") == 0) {
state->window_flags |= SDL_WINDOW_RESIZABLE;
return 1;
}
if (SDL_strcasecmp(argv[index], "--minimize") == 0) {
state->window_flags |= SDL_WINDOW_MINIMIZED;
return 1;
}
if (SDL_strcasecmp(argv[index], "--maximize") == 0) {
state->window_flags |= SDL_WINDOW_MAXIMIZED;
return 1;
}
if (SDL_strcasecmp(argv[index], "--grab") == 0) {
state->window_flags |= SDL_WINDOW_INPUT_GRABBED;
return 1;
}
if (SDL_strcasecmp(argv[index], "--rate") == 0) {
++index;
if (!argv[index]) {
return -1;
}
state->audiospec.freq = SDL_atoi(argv[index]);
return 2;
}
if (SDL_strcasecmp(argv[index], "--format") == 0) {
++index;
if (!argv[index]) {
return -1;
}
if (SDL_strcasecmp(argv[index], "U8") == 0) {
state->audiospec.format = AUDIO_U8;
return 2;
}
if (SDL_strcasecmp(argv[index], "S8") == 0) {
state->audiospec.format = AUDIO_S8;
return 2;
}
if (SDL_strcasecmp(argv[index], "U16") == 0) {
state->audiospec.format = AUDIO_U16;
return 2;
}
if (SDL_strcasecmp(argv[index], "U16LE") == 0) {
state->audiospec.format = AUDIO_U16LSB;
return 2;
}
if (SDL_strcasecmp(argv[index], "U16BE") == 0) {
state->audiospec.format = AUDIO_U16MSB;
return 2;
}
if (SDL_strcasecmp(argv[index], "S16") == 0) {
state->audiospec.format = AUDIO_S16;
return 2;
}
if (SDL_strcasecmp(argv[index], "S16LE") == 0) {
state->audiospec.format = AUDIO_S16LSB;
return 2;
}
if (SDL_strcasecmp(argv[index], "S16BE") == 0) {
state->audiospec.format = AUDIO_S16MSB;
return 2;
}
return -1;
}
if (SDL_strcasecmp(argv[index], "--channels") == 0) {
++index;
if (!argv[index]) {
return -1;
}
state->audiospec.channels = (Uint8) SDL_atoi(argv[index]);
return 2;
}
if (SDL_strcasecmp(argv[index], "--samples") == 0) {
++index;
if (!argv[index]) {
return -1;
}
state->audiospec.samples = (Uint16) SDL_atoi(argv[index]);
return 2;
}
if ((SDL_strcasecmp(argv[index], "-h") == 0)
|| (SDL_strcasecmp(argv[index], "--help") == 0)) {
/* Print the usage message */
return -1;
}
if (SDL_strcmp(argv[index], "-NSDocumentRevisionsDebugMode") == 0) {
/* Debug flag sent by Xcode */
return 2;
}
return 0;
}
const char *
SDLTest_CommonUsage(SDLTest_CommonState * state)
{
switch (state->flags & (SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
case SDL_INIT_VIDEO:
return VIDEO_USAGE;
case SDL_INIT_AUDIO:
return AUDIO_USAGE;
case (SDL_INIT_VIDEO | SDL_INIT_AUDIO):
return VIDEO_USAGE " " AUDIO_USAGE;
default:
return "";
}
}
static void
478
SDLTest_PrintRendererFlag(char *text, size_t maxlen, Uint32 flag)
479
480
{
switch (flag) {
481
482
case SDL_RENDERER_SOFTWARE:
SDL_snprintfcat(text, maxlen, "Software");
483
484
break;
case SDL_RENDERER_ACCELERATED:
485
SDL_snprintfcat(text, maxlen, "Accelerated");
486
break;
487
488
489
490
491
492
case SDL_RENDERER_PRESENTVSYNC:
SDL_snprintfcat(text, maxlen, "PresentVSync");
break;
case SDL_RENDERER_TARGETTEXTURE:
SDL_snprintfcat(text, maxlen, "TargetTexturesSupported");
break;
493
default:
494
SDL_snprintfcat(text, maxlen, "0x%8.8x", flag);
495
496
497
498
499
break;
}
}
static void
500
SDLTest_PrintPixelFormat(char *text, size_t maxlen, Uint32 format)
501
502
503
{
switch (format) {
case SDL_PIXELFORMAT_UNKNOWN:
504
SDL_snprintfcat(text, maxlen, "Unknown");
505
506
break;
case SDL_PIXELFORMAT_INDEX1LSB:
507
SDL_snprintfcat(text, maxlen, "Index1LSB");
508
509
break;
case SDL_PIXELFORMAT_INDEX1MSB:
510
SDL_snprintfcat(text, maxlen, "Index1MSB");
511
512
break;
case SDL_PIXELFORMAT_INDEX4LSB:
513
SDL_snprintfcat(text, maxlen, "Index4LSB");
514
515
break;
case SDL_PIXELFORMAT_INDEX4MSB:
516
SDL_snprintfcat(text, maxlen, "Index4MSB");
517
518
break;
case SDL_PIXELFORMAT_INDEX8:
519
SDL_snprintfcat(text, maxlen, "Index8");
520
521
break;
case SDL_PIXELFORMAT_RGB332:
522
SDL_snprintfcat(text, maxlen, "RGB332");
523
524
break;
case SDL_PIXELFORMAT_RGB444:
525
SDL_snprintfcat(text, maxlen, "RGB444");
526
527
break;
case SDL_PIXELFORMAT_RGB555:
528
SDL_snprintfcat(text, maxlen, "RGB555");
529
530
break;
case SDL_PIXELFORMAT_BGR555:
531
SDL_snprintfcat(text, maxlen, "BGR555");
532
533
break;
case SDL_PIXELFORMAT_ARGB4444:
534
SDL_snprintfcat(text, maxlen, "ARGB4444");
535
536
break;
case SDL_PIXELFORMAT_ABGR4444:
537
SDL_snprintfcat(text, maxlen, "ABGR4444");
538
539
break;
case SDL_PIXELFORMAT_ARGB1555:
540
SDL_snprintfcat(text, maxlen, "ARGB1555");
541
542
break;
case SDL_PIXELFORMAT_ABGR1555:
543
SDL_snprintfcat(text, maxlen, "ABGR1555");
544
545
break;
case SDL_PIXELFORMAT_RGB565:
546
SDL_snprintfcat(text, maxlen, "RGB565");
547
548
break;
case SDL_PIXELFORMAT_BGR565:
549
SDL_snprintfcat(text, maxlen, "BGR565");
550
551
break;
case SDL_PIXELFORMAT_RGB24:
552
SDL_snprintfcat(text, maxlen, "RGB24");
553
554
break;
case SDL_PIXELFORMAT_BGR24:
555
SDL_snprintfcat(text, maxlen, "BGR24");
556
557
break;
case SDL_PIXELFORMAT_RGB888:
558
SDL_snprintfcat(text, maxlen, "RGB888");
559
560
break;
case SDL_PIXELFORMAT_BGR888:
561
SDL_snprintfcat(text, maxlen, "BGR888");
562
563
break;
case SDL_PIXELFORMAT_ARGB8888:
564
SDL_snprintfcat(text, maxlen, "ARGB8888");
565
566
break;
case SDL_PIXELFORMAT_RGBA8888:
567
SDL_snprintfcat(text, maxlen, "RGBA8888");
568
569
break;
case SDL_PIXELFORMAT_ABGR8888:
570
SDL_snprintfcat(text, maxlen, "ABGR8888");
571
572
break;
case SDL_PIXELFORMAT_BGRA8888:
573
SDL_snprintfcat(text, maxlen, "BGRA8888");
574
575
break;
case SDL_PIXELFORMAT_ARGB2101010:
576
SDL_snprintfcat(text, maxlen, "ARGB2101010");
577
578
break;
case SDL_PIXELFORMAT_YV12:
579
SDL_snprintfcat(text, maxlen, "YV12");
580
581
break;
case SDL_PIXELFORMAT_IYUV:
582
SDL_snprintfcat(text, maxlen, "IYUV");
583
584
break;
case SDL_PIXELFORMAT_YUY2:
585
SDL_snprintfcat(text, maxlen, "YUY2");
586
587
break;
case SDL_PIXELFORMAT_UYVY:
588
SDL_snprintfcat(text, maxlen, "UYVY");
589
590
break;
case SDL_PIXELFORMAT_YVYU:
591
SDL_snprintfcat(text, maxlen, "YVYU");
592
593
break;
case SDL_PIXELFORMAT_NV12:
594
SDL_snprintfcat(text, maxlen, "NV12");
595
596
break;
case SDL_PIXELFORMAT_NV21:
597
SDL_snprintfcat(text, maxlen, "NV21");
598
599
break;
default:
600
SDL_snprintfcat(text, maxlen, "0x%8.8x", format);
601
602
603
604
605
606
607
608
break;
}
}
static void
SDLTest_PrintRenderer(SDL_RendererInfo * info)
{
int i, count;
609
char text[1024];
610
611
SDL_Log(" Renderer %s:\n", info->name);
612
613
614
SDL_snprintf(text, sizeof(text), " Flags: 0x%8.8X", info->flags);
SDL_snprintfcat(text, sizeof(text), " (");
615
616
617
618
619
count = 0;
for (i = 0; i < sizeof(info->flags) * 8; ++i) {
Uint32 flag = (1 << i);
if (info->flags & flag) {
if (count > 0) {
620
SDL_snprintfcat(text, sizeof(text), " | ");
621
}
622
SDLTest_PrintRendererFlag(text, sizeof(text), flag);
623
624
625
++count;
}
}
626
627
SDL_snprintfcat(text, sizeof(text), ")");
SDL_Log("%s\n", text);
628
629
SDL_snprintf(text, sizeof(text), " Texture formats (%d): ", info->num_texture_formats);
630
631
for (i = 0; i < (int) info->num_texture_formats; ++i) {
if (i > 0) {
632
SDL_snprintfcat(text, sizeof(text), ", ");
633
}
634
SDLTest_PrintPixelFormat(text, sizeof(text), info->texture_formats[i]);
635
}
636
SDL_Log("%s\n", text);
637
638
if (info->max_texture_width || info->max_texture_height) {
639
SDL_Log(" Max Texture Size: %dx%d\n",
640
641
642
643
644
645
646
647
648
649
650
651
info->max_texture_width, info->max_texture_height);
}
}
static SDL_Surface *
SDLTest_LoadIcon(const char *file)
{
SDL_Surface *icon;
/* Load the icon surface */
icon = SDL_LoadBMP(file);
if (icon == NULL) {
652
SDL_Log("Couldn't load %s: %s\n", file, SDL_GetError());
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
return (NULL);
}
if (icon->format->palette) {
/* Set the colorkey */
SDL_SetColorKey(icon, 1, *((Uint8 *) icon->pixels));
}
return (icon);
}
SDL_bool
SDLTest_CommonInit(SDLTest_CommonState * state)
{
int i, j, m, n, w, h;
SDL_DisplayMode fullscreen_mode;
669
char text[1024];
670
671
672
673
674
if (state->flags & SDL_INIT_VIDEO) {
if (state->verbose & VERBOSE_VIDEO) {
n = SDL_GetNumVideoDrivers();
if (n == 0) {
675
SDL_Log("No built-in video drivers\n");
676
} else {
677
SDL_snprintf(text, sizeof(text), "Built-in video drivers:");
678
679
for (i = 0; i < n; ++i) {
if (i > 0) {
680
SDL_snprintfcat(text, sizeof(text), ",");
681
}
682
SDL_snprintfcat(text, sizeof(text), " %s", SDL_GetVideoDriver(i));
683
}
684
SDL_Log("%s\n", text);
685
686
687
}
}
if (SDL_VideoInit(state->videodriver) < 0) {
688
SDL_Log("Couldn't initialize video driver: %s\n",
689
690
691
692
SDL_GetError());
return SDL_FALSE;
}
if (state->verbose & VERBOSE_VIDEO) {
693
SDL_Log("Video driver: %s\n",
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
SDL_GetCurrentVideoDriver());
}
/* Upload GL settings */
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, state->gl_red_size);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, state->gl_green_size);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, state->gl_blue_size);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, state->gl_alpha_size);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, state->gl_double_buffer);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, state->gl_buffer_size);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, state->gl_depth_size);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, state->gl_stencil_size);
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, state->gl_accum_red_size);
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, state->gl_accum_green_size);
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, state->gl_accum_blue_size);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, state->gl_accum_alpha_size);
SDL_GL_SetAttribute(SDL_GL_STEREO, state->gl_stereo);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, state->gl_multisamplebuffers);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, state->gl_multisamplesamples);
if (state->gl_accelerated >= 0) {
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL,
state->gl_accelerated);
}
SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, state->gl_retained_backing);
if (state->gl_major_version) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, state->gl_major_version);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, state->gl_minor_version);
}
if (state->gl_debug) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
}
if (state->gl_profile_mask) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, state->gl_profile_mask);
}
if (state->verbose & VERBOSE_MODES) {
730
731
732
SDL_Rect bounds, usablebounds;
float hdpi = 0;
float vdpi = 0;
733
734
735
736
737
738
739
740
SDL_DisplayMode mode;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
#if SDL_VIDEO_DRIVER_WINDOWS
int adapterIndex = 0;
int outputIndex = 0;
#endif
n = SDL_GetNumVideoDisplays();
741
SDL_Log("Number of displays: %d\n", n);
742
for (i = 0; i < n; ++i) {
743
SDL_Log("Display %d: %s\n", i, SDL_GetDisplayName(i));
744
745
746
SDL_zero(bounds);
SDL_GetDisplayBounds(i, &bounds);
747
748
749
750
751
752
SDL_zero(usablebounds);
SDL_GetDisplayUsableBounds(i, &usablebounds);
SDL_GetDisplayDPI(i, NULL, &hdpi, &vdpi);
753
SDL_Log("Bounds: %dx%d at %d,%d\n", bounds.w, bounds.h, bounds.x, bounds.y);
754
755
SDL_Log("Usable bounds: %dx%d at %d,%d\n", usablebounds.w, usablebounds.h, usablebounds.x, usablebounds.y);
SDL_Log("DPI: %fx%f\n", hdpi, vdpi);
756
757
758
759
SDL_GetDesktopDisplayMode(i, &mode);
SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask,
&Bmask, &Amask);
760
SDL_Log(" Current mode: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
761
762
763
mode.w, mode.h, mode.refresh_rate, bpp,
SDL_GetPixelFormatName(mode.format));
if (Rmask || Gmask || Bmask) {
764
765
766
SDL_Log(" Red Mask = 0x%.8x\n", Rmask);
SDL_Log(" Green Mask = 0x%.8x\n", Gmask);
SDL_Log(" Blue Mask = 0x%.8x\n", Bmask);
767
if (Amask)
768
SDL_Log(" Alpha Mask = 0x%.8x\n", Amask);
769
770
771
772
773
}
/* Print available fullscreen video modes */
m = SDL_GetNumDisplayModes(i);
if (m == 0) {
774
SDL_Log("No available fullscreen video modes\n");
775
} else {
776
SDL_Log(" Fullscreen video modes:\n");
777
778
779
780
for (j = 0; j < m; ++j) {
SDL_GetDisplayMode(i, j, &mode);
SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask,
&Gmask, &Bmask, &Amask);
781
SDL_Log(" Mode %d: %dx%d@%dHz, %d bits-per-pixel (%s)\n",
782
783
784
j, mode.w, mode.h, mode.refresh_rate, bpp,
SDL_GetPixelFormatName(mode.format));
if (Rmask || Gmask || Bmask) {
785
SDL_Log(" Red Mask = 0x%.8x\n",
786
Rmask);
787
SDL_Log(" Green Mask = 0x%.8x\n",
788
Gmask);
789
SDL_Log(" Blue Mask = 0x%.8x\n",
790
791
Bmask);
if (Amask)
792
SDL_Log(" Alpha Mask = 0x%.8x\n",
793
794
795
796
797
798
799
800
Amask);
}
}
}
#if SDL_VIDEO_DRIVER_WINDOWS
/* Print the D3D9 adapter index */
adapterIndex = SDL_Direct3D9GetAdapterIndex( i );
801
SDL_Log("D3D9 Adapter Index: %d", adapterIndex);
802
803
804
/* Print the DXGI adapter and output indices */
SDL_DXGIGetOutputInfo(i, &adapterIndex, &outputIndex);
805
SDL_Log("DXGI Adapter Index: %d Output Index: %d", adapterIndex, outputIndex);
806
807
808
809
810
811
812
813
814
#endif
}
}
if (state->verbose & VERBOSE_RENDER) {
SDL_RendererInfo info;
n = SDL_GetNumRenderDrivers();
if (n == 0) {
815
SDL_Log("No built-in render drivers\n");
816
} else {
817
SDL_Log("Built-in render drivers:\n");
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
for (i = 0; i < n; ++i) {
SDL_GetRenderDriverInfo(i, &info);
SDLTest_PrintRenderer(&info);
}
}
}
SDL_zero(fullscreen_mode);
switch (state->depth) {
case 8:
fullscreen_mode.format = SDL_PIXELFORMAT_INDEX8;
break;
case 15:
fullscreen_mode.format = SDL_PIXELFORMAT_RGB555;
break;
case 16:
fullscreen_mode.format = SDL_PIXELFORMAT_RGB565;
break;
case 24:
fullscreen_mode.format = SDL_PIXELFORMAT_RGB24;
break;
default:
fullscreen_mode.format = SDL_PIXELFORMAT_RGB888;
break;
}
fullscreen_mode.refresh_rate = state->refresh_rate;
state->windows =
846
(SDL_Window **) SDL_calloc(state->num_windows,
847
848
sizeof(*state->windows));
state->renderers =
849
(SDL_Renderer **) SDL_calloc(state->num_windows,
850
851
sizeof(*state->renderers));
state->targets =
852
(SDL_Texture **) SDL_calloc(state->num_windows,
853
854
sizeof(*state->targets));
if (!state->windows || !state->renderers) {
855
SDL_Log("Out of memory!\n");
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
return SDL_FALSE;
}
for (i = 0; i < state->num_windows; ++i) {
char title[1024];
if (state->num_windows > 1) {
SDL_snprintf(title, SDL_arraysize(title), "%s %d",
state->window_title, i + 1);
} else {
SDL_strlcpy(title, state->window_title, SDL_arraysize(title));
}
state->windows[i] =
SDL_CreateWindow(title, state->window_x, state->window_y,
state->window_w, state->window_h,
state->window_flags);
if (!state->windows[i]) {
872
SDL_Log("Couldn't create window: %s\n",
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
SDL_GetError());
return SDL_FALSE;
}
if (state->window_minW || state->window_minH) {
SDL_SetWindowMinimumSize(state->windows[i], state->window_minW, state->window_minH);
}
if (state->window_maxW || state->window_maxH) {
SDL_SetWindowMaximumSize(state->windows[i], state->window_maxW, state->window_maxH);
}
SDL_GetWindowSize(state->windows[i], &w, &h);
if (!(state->window_flags & SDL_WINDOW_RESIZABLE) &&
(w != state->window_w || h != state->window_h)) {
printf("Window requested size %dx%d, got %dx%d\n", state->window_w, state->window_h, w, h);
state->window_w = w;
state->window_h = h;
}
if (SDL_SetWindowDisplayMode(state->windows[i], &fullscreen_mode) < 0) {
890
SDL_Log("Can't set up fullscreen display mode: %s\n",
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
SDL_GetError());
return SDL_FALSE;
}
if (state->window_icon) {
SDL_Surface *icon = SDLTest_LoadIcon(state->window_icon);
if (icon) {
SDL_SetWindowIcon(state->windows[i], icon);
SDL_FreeSurface(icon);
}
}
SDL_ShowWindow(state->windows[i]);
if (!state->skip_renderer
&& (state->renderdriver
907
|| !(state->window_flags & (SDL_WINDOW_OPENGL | SDL_WINDOW_VULKAN)))) {
908
909
910
911
912
913
914
915
916
917
918
919
920
m = -1;
if (state->renderdriver) {
SDL_RendererInfo info;
n = SDL_GetNumRenderDrivers();
for (j = 0; j < n; ++j) {
SDL_GetRenderDriverInfo(j, &info);
if (SDL_strcasecmp(info.name, state->renderdriver) ==
0) {
m = j;
break;
}
}
if (m == -1) {
921
SDL_Log("Couldn't find render driver named %s",
922
923
924
925
926
927
928
state->renderdriver);
return SDL_FALSE;
}
}
state->renderers[i] = SDL_CreateRenderer(state->windows[i],
m, state->render_flags);
if (!state->renderers[i]) {
929
SDL_Log("Couldn't create renderer: %s\n",
930
931
932
933
934
935
936
937
938
939
940
SDL_GetError());
return SDL_FALSE;
}
if (state->logical_w && state->logical_h) {
SDL_RenderSetLogicalSize(state->renderers[i], state->logical_w, state->logical_h);
} else if (state->scale) {
SDL_RenderSetScale(state->renderers[i], state->scale, state->scale);
}
if (state->verbose & VERBOSE_RENDER) {
SDL_RendererInfo info;
941
SDL_Log("Current renderer:\n");
942
943
944
945
946
947
948
949
950
951
952
SDL_GetRendererInfo(state->renderers[i], &info);
SDLTest_PrintRenderer(&info);
}
}
}
}
if (state->flags & SDL_INIT_AUDIO) {
if (state->verbose & VERBOSE_AUDIO) {
n = SDL_GetNumAudioDrivers();
if (n == 0) {
953
SDL_Log("No built-in audio drivers\n");
954
} else {
955
SDL_snprintf(text, sizeof(text), "Built-in audio drivers:");
956
957
for (i = 0; i < n; ++i) {
if (i > 0) {
958
SDL_snprintfcat(text, sizeof(text), ",");
959
}
960
SDL_snprintfcat(text, sizeof(text), " %s", SDL_GetAudioDriver(i));
961
}
962
SDL_Log("%s\n", text);
963
964
965
}
}
if (SDL_AudioInit(state->audiodriver) < 0) {
966
SDL_Log("Couldn't initialize audio driver: %s\n",
967
968
969
970
SDL_GetError());
return SDL_FALSE;
}
if (state->verbose & VERBOSE_VIDEO) {
971
SDL_Log("Audio driver: %s\n",
972
973
974
975
SDL_GetCurrentAudioDriver());
}
if (SDL_OpenAudio(&state->audiospec, NULL) < 0) {
976
SDL_Log("Couldn't open audio: %s\n", SDL_GetError());
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
return SDL_FALSE;
}
}
return SDL_TRUE;
}
static const char *
ControllerAxisName(const SDL_GameControllerAxis axis)
{
switch (axis)
{
#define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
AXIS_CASE(INVALID);
AXIS_CASE(LEFTX);
AXIS_CASE(LEFTY);
AXIS_CASE(RIGHTX);
AXIS_CASE(RIGHTY);
AXIS_CASE(TRIGGERLEFT);
AXIS_CASE(TRIGGERRIGHT);
#undef AXIS_CASE
default: return "???";
}
}