/
glimp_sdl.c
730 lines (607 loc) · 16.9 KB
/
glimp_sdl.c
1
2
3
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
35
36
37
38
39
40
41
42
43
44
45
46
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
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
730
/*
* Copyright (C) 2010 Yamagi Burmeister
* Copyright (C) 1997-2001 Id Software, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* =======================================================================
*
* This is the client side of the render backend, implemented trough SDL.
* The SDL window and related functrion (mouse grap, fullscreen switch)
* are implemented here, everything else is in the renderers.
*
* =======================================================================
*/
#include "../../common/header/common.h"
#include "header/ref.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
int glimp_refreshRate = -1;
static cvar_t *vid_displayrefreshrate;
static cvar_t *vid_displayindex;
static cvar_t *vid_rate;
static int last_flags = 0;
static int last_display = 0;
static int last_position_x = SDL_WINDOWPOS_UNDEFINED;
static int last_position_y = SDL_WINDOWPOS_UNDEFINED;
static SDL_Window* window = NULL;
static qboolean initSuccessful = false;
static char **displayindices = NULL;
static int num_displays = 0;
/*
* Resets the display index Cvar if out of bounds
*/
static void
ClampDisplayIndexCvar(void)
{
if (vid_displayindex->value < 0 || vid_displayindex->value >= num_displays)
{
Cvar_SetValue("vid_displayindex", 0);
}
}
static void
ClearDisplayIndices(void)
{
if ( displayindices )
{
for ( int i = 0; i < num_displays; i++ )
{
free( displayindices[ i ] );
}
free( displayindices );
displayindices = NULL;
}
}
static qboolean
CreateSDLWindow(int flags, int w, int h)
{
if (SDL_WINDOWPOS_ISUNDEFINED(last_position_x) || SDL_WINDOWPOS_ISUNDEFINED(last_position_y) || last_position_x < 0 ||last_position_y < 24)
{
last_position_x = last_position_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY((int)vid_displayindex->value);
}
window = SDL_CreateWindow("Yamagi Quake II", last_position_x, last_position_y, w, h, flags);
if (window)
{
/* save current display as default */
last_display = SDL_GetWindowDisplayIndex(window);
SDL_GetWindowPosition(window, &last_position_x, &last_position_y);
/* Check if we're really in the requested diplay mode. There is
(or was) an SDL bug were SDL switched into the wrong mode
without giving an error code. See the bug report for details:
https://bugzilla.libsdl.org/show_bug.cgi?id=4700 */
SDL_DisplayMode real_mode;
if (SDL_GetWindowDisplayMode(window, &real_mode) != 0)
{
SDL_DestroyWindow(window);
window = NULL;
Com_Printf("Can't get display mode: %s\n", SDL_GetError());
return false;
}
/* SDL_WINDOW_FULLSCREEN_DESKTOP implies SDL_WINDOW_FULLSCREEN! */
if (((flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP)) == SDL_WINDOW_FULLSCREEN)
&& ((real_mode.w != w) || (real_mode.h != h)))
{
Com_Printf("Current display mode isn't requested display mode\n");
Com_Printf("Likely SDL bug #4700, trying to work around it\n");
/* Mkay, try to hack around that. */
SDL_DisplayMode wanted_mode = {};
wanted_mode.w = w;
wanted_mode.h = h;
if (SDL_SetWindowDisplayMode(window, &wanted_mode) != 0)
{
SDL_DestroyWindow(window);
window = NULL;
Com_Printf("Can't force resolution to %ix%i: %s\n", w, h, SDL_GetError());
return false;
}
/* The SDL doku says, that SDL_SetWindowSize() shouldn't be
used on fullscreen windows. But at least in my test with
SDL 2.0.9 the subsequent SDL_GetWindowDisplayMode() fails
if I don't call it. */
SDL_SetWindowSize(window, wanted_mode.w, wanted_mode.h);
if (SDL_GetWindowDisplayMode(window, &real_mode) != 0)
{
SDL_DestroyWindow(window);
window = NULL;
Com_Printf("Can't get display mode: %s\n", SDL_GetError());
return false;
}
if ((real_mode.w != w) || (real_mode.h != h))
{
SDL_DestroyWindow(window);
window = NULL;
Com_Printf("Can't get display mode: %s\n", SDL_GetError());
return false;
}
}
/* Normally SDL stays at desktop refresh rate or chooses something
sane. Some player may want to override that.
Reminder: SDL_WINDOW_FULLSCREEN_DESKTOP implies SDL_WINDOW_FULLSCREEN! */
if ((flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP)) == SDL_WINDOW_FULLSCREEN)
{
if (vid_rate->value > 0)
{
SDL_DisplayMode closest_mode;
SDL_DisplayMode requested_mode = real_mode;
requested_mode.refresh_rate = (int)vid_rate->value;
if (SDL_GetClosestDisplayMode(last_display, &requested_mode, &closest_mode) == NULL)
{
Com_Printf("SDL was unable to find a mode close to %ix%i@%i\n", w, h, requested_mode.refresh_rate);
Cvar_SetValue("vid_rate", -1);
}
else
{
Com_Printf("User requested %ix%i@%i, setting closest mode %ix%i@%i\n",
w, h, requested_mode.refresh_rate, w, h, closest_mode.refresh_rate);
if (SDL_SetWindowDisplayMode(window, &closest_mode) != 0)
{
Com_Printf("Couldn't switch to mode %ix%i@%i, staying at current mode\n",
w, h, closest_mode.refresh_rate);
Cvar_SetValue("vid_rate", -1);
}
else
{
Cvar_SetValue("vid_rate", closest_mode.refresh_rate);
}
}
}
}
}
else
{
return false;
}
return true;
}
static int
GetFullscreenType()
{
if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP)
{
return 1;
}
else if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)
{
return 2;
}
else
{
return 0;
}
}
static qboolean
GetWindowSize(int* w, int* h)
{
if (window == NULL || w == NULL || h == NULL)
{
return false;
}
SDL_DisplayMode m;
if (SDL_GetWindowDisplayMode(window, &m) != 0)
{
Com_Printf("Can't get Displaymode: %s\n", SDL_GetError());
return false;
}
*w = m.w;
*h = m.h;
return true;
}
static void
InitDisplayIndices()
{
displayindices = malloc((num_displays + 1) * sizeof(char *));
for ( int i = 0; i < num_displays; i++ )
{
/* There are a maximum of 10 digits in 32 bit int + 1 for the NULL terminator. */
displayindices[ i ] = malloc(11 * sizeof( char ));
YQ2_COM_CHECK_OOM(displayindices[i], "malloc()", 11 * sizeof( char ))
snprintf( displayindices[ i ], 11, "%d", i );
}
/* The last entry is NULL to indicate the list of strings ends. */
displayindices[ num_displays ] = 0;
}
/*
* Lists all available display modes.
*/
static void
PrintDisplayModes(void)
{
int curdisplay = window ? SDL_GetWindowDisplayIndex(window) : 0;
// On X11 (at least for me)
// curdisplay is always -1.
// DG: probably because window was NULL?
if (curdisplay < 0) {
curdisplay = 0;
}
int nummodes = SDL_GetNumDisplayModes(curdisplay);
if (nummodes < 1)
{
Com_Printf("Can't get display modes: %s\n", SDL_GetError());
return;
}
for (int i = 0; i < nummodes; i++)
{
SDL_DisplayMode mode;
if (SDL_GetDisplayMode(curdisplay, i, &mode) != 0)
{
Com_Printf("Can't get display mode: %s\n", SDL_GetError());
return;
}
Com_Printf(" - Mode %2i: %ix%i@%i\n", i, mode.w, mode.h, mode.refresh_rate);
}
}
/*
* Sets the window icon
*/
static void
SetSDLIcon()
{
#include "icon/q2icon64.h" // 64x64 32 Bit
/* these masks are needed to tell SDL_CreateRGBSurface(From)
to assume the data it gets is byte-wise RGB(A) data */
Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
int shift = (q2icon64.bytes_per_pixel == 3) ? 8 : 0;
rmask = 0xff000000 >> shift;
gmask = 0x00ff0000 >> shift;
bmask = 0x0000ff00 >> shift;
amask = 0x000000ff >> shift;
#else /* little endian, like x86 */
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = (q2icon64.bytes_per_pixel == 3) ? 0 : 0xff000000;
#endif
SDL_Surface* icon = SDL_CreateRGBSurfaceFrom((void*)q2icon64.pixel_data, q2icon64.width,
q2icon64.height, q2icon64.bytes_per_pixel*8, q2icon64.bytes_per_pixel*q2icon64.width,
rmask, gmask, bmask, amask);
SDL_SetWindowIcon(window, icon);
SDL_FreeSurface(icon);
}
// FIXME: We need a header for this.
// Maybe we could put it in vid.h.
void GLimp_GrabInput(qboolean grab);
/*
* Shuts the SDL render backend down
*/
static void
ShutdownGraphics(void)
{
ClampDisplayIndexCvar();
if (window)
{
/* save current display as default */
last_display = SDL_GetWindowDisplayIndex(window);
/* or if current display isn't the desired default */
if (last_display != vid_displayindex->value) {
last_position_x = last_position_y = SDL_WINDOWPOS_UNDEFINED;
last_display = vid_displayindex->value;
}
else {
SDL_GetWindowPosition(window,
&last_position_x, &last_position_y);
}
/* cleanly ungrab input (needs window) */
GLimp_GrabInput(false);
SDL_DestroyWindow(window);
window = NULL;
}
// make sure that after vid_restart the refreshrate will be queried from SDL2 again.
glimp_refreshRate = -1;
initSuccessful = false; // not initialized anymore
}
// --------
/*
* Initializes the SDL video subsystem. Must
* be called before anything else.
*/
qboolean
GLimp_Init(void)
{
vid_displayrefreshrate = Cvar_Get("vid_displayrefreshrate", "-1", CVAR_ARCHIVE);
vid_displayindex = Cvar_Get("vid_displayindex", "0", CVAR_ARCHIVE);
vid_rate = Cvar_Get("vid_rate", "-1", CVAR_ARCHIVE);
if (!SDL_WasInit(SDL_INIT_VIDEO))
{
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
Com_Printf("Couldn't init SDL video: %s.\n", SDL_GetError());
return false;
}
SDL_version version;
SDL_GetVersion(&version);
Com_Printf("SDL version is: %i.%i.%i\n", (int)version.major, (int)version.minor, (int)version.patch);
Com_Printf("SDL video driver is \"%s\".\n", SDL_GetCurrentVideoDriver());
num_displays = SDL_GetNumVideoDisplays();
InitDisplayIndices();
ClampDisplayIndexCvar();
Com_Printf("SDL didplay modes:\n");
PrintDisplayModes();
}
return true;
}
/*
* Shuts the SDL video subsystem down. Must
* be called after evrything's finished and
* clean up.
*/
void
GLimp_Shutdown(void)
{
ShutdownGraphics();
if (SDL_WasInit(SDL_INIT_EVERYTHING) == SDL_INIT_VIDEO)
{
SDL_Quit();
}
else
{
SDL_QuitSubSystem(SDL_INIT_VIDEO);
}
ClearDisplayIndices();
}
/*
* (Re)initializes the actual window.
*/
qboolean
GLimp_InitGraphics(int fullscreen, int *pwidth, int *pheight)
{
int flags;
int curWidth, curHeight;
int width = *pwidth;
int height = *pheight;
unsigned int fs_flag = 0;
if (fullscreen == 1)
{
fs_flag = SDL_WINDOW_FULLSCREEN_DESKTOP;
}
else if (fullscreen == 2)
{
fs_flag = SDL_WINDOW_FULLSCREEN;
}
/* Only do this if we already have a working window and a fully
initialized rendering backend GLimp_InitGraphics() is also
called when recovering if creating GL context fails or the
one we got is unusable. */
if (initSuccessful && GetWindowSize(&curWidth, &curHeight)
&& (curWidth == width) && (curHeight == height))
{
/* If we want fullscreen, but aren't */
if (GetFullscreenType())
{
SDL_SetWindowFullscreen(window, fs_flag);
Cvar_SetValue("vid_fullscreen", fullscreen);
}
/* Are we now? */
if (GetFullscreenType())
{
return true;
}
}
/* Is the surface used? */
if (window)
{
re.ShutdownContext();
ShutdownGraphics();
window = NULL;
}
/* We need the window size for the menu, the HUD, etc. */
viddef.width = width;
viddef.height = height;
if(last_flags != -1 && (last_flags & SDL_WINDOW_OPENGL))
{
/* Reset SDL. */
SDL_GL_ResetAttributes();
}
/* Let renderer prepare things (set OpenGL attributes).
FIXME: This is no longer necessary, the renderer
could and should pass the flags when calling this
function. */
flags = re.PrepareForWindow();
if (flags == -1)
{
/* It's PrepareForWindow() job to log an error */
return false;
}
if (fs_flag)
{
flags |= fs_flag;
}
/* Mkay, now the hard work. Let's create the window. */
cvar_t *gl_msaa_samples = Cvar_Get("gl_msaa_samples", "0", CVAR_ARCHIVE);
while (1)
{
if (!CreateSDLWindow(flags, width, height))
{
if((flags & SDL_WINDOW_OPENGL) && gl_msaa_samples->value)
{
Com_Printf("SDL SetVideoMode failed: %s\n", SDL_GetError());
Com_Printf("Reverting to %s r_mode %i (%ix%i) without MSAA.\n",
(flags & fs_flag) ? "fullscreen" : "windowed",
(int) Cvar_VariableValue("r_mode"), width, height);
/* Try to recover */
Cvar_SetValue("gl_msaa_samples", 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
}
else if (width != 640 || height != 480 || (flags & fs_flag))
{
Com_Printf("SDL SetVideoMode failed: %s\n", SDL_GetError());
Com_Printf("Reverting to windowed r_mode 4 (640x480).\n");
/* Try to recover */
Cvar_SetValue("r_mode", 4);
Cvar_SetValue("vid_fullscreen", 0);
Cvar_SetValue("vid_rate", -1);
fullscreen = 0;
*pwidth = width = 640;
*pheight = height = 480;
flags &= ~fs_flag;
}
else
{
Com_Error(ERR_FATAL, "Failed to revert to r_mode 4. Exiting...\n");
return false;
}
}
else
{
break;
}
}
last_flags = flags;
/* Now that we've got a working window print it's mode. */
int curdisplay = SDL_GetWindowDisplayIndex(window);
if (curdisplay < 0) {
curdisplay = 0;
}
SDL_DisplayMode mode;
if (SDL_GetCurrentDisplayMode(curdisplay, &mode) != 0)
{
Com_Printf("Can't get current display mode: %s\n", SDL_GetError());
}
else
{
Com_Printf("Real display mode: %ix%i@%i (vid_fullscreen: %i)\n", mode.w, mode.h,
mode.refresh_rate, fullscreen);
}
/* Initialize rendering context. */
if (!re.InitContext(window))
{
/* InitContext() should have logged an error. */
return false;
}
/* Set the window icon - For SDL2, this must be done after creating the window */
SetSDLIcon();
/* No cursor */
SDL_ShowCursor(0);
initSuccessful = true;
return true;
}
/*
* Shuts the window down.
*/
void
GLimp_ShutdownGraphics(void)
{
SDL_GL_ResetAttributes();
ShutdownGraphics();
}
/*
* (Un)grab Input
*/
void
GLimp_GrabInput(qboolean grab)
{
if(window != NULL)
{
SDL_SetWindowGrab(window, grab ? SDL_TRUE : SDL_FALSE);
}
if(SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE) < 0)
{
Com_Printf("WARNING: Setting Relative Mousemode failed, reason: %s\n", SDL_GetError());
Com_Printf(" You should probably update to SDL 2.0.3 or newer!\n");
}
}
/*
* Returns the current display refresh rate. There're 2 limitations:
*
* * The timing code in frame.c only understands full integers, so
* values given by vid_displayrefreshrate are always round up. For
* example 59.95 become 60. Rounding up is the better choice for
* most users because assuming a too high display refresh rate
* avoids micro stuttering caused by missed frames if the vsync
* is enabled. The price are small and hard to notice timing
* problems.
*
* * SDL returns only full integers. In most cases they're rounded
* up, but in some cases - likely depending on the GPU driver -
* they're rounded down. If the value is rounded up, we'll see
* some small and nard to notice timing problems. If the value
* is rounded down frames will be missed. Both is only relevant
* if the vsync is enabled.
*/
int
GLimp_GetRefreshRate(void)
{
if (vid_displayrefreshrate->value > -1 ||
vid_displayrefreshrate->modified)
{
glimp_refreshRate = ceil(vid_displayrefreshrate->value);
vid_displayrefreshrate->modified = false;
}
if (glimp_refreshRate == -1)
{
SDL_DisplayMode mode;
int i = SDL_GetWindowDisplayIndex(window);
if (i >= 0 && SDL_GetCurrentDisplayMode(i, &mode) == 0)
{
glimp_refreshRate = mode.refresh_rate;
}
// Something went wrong, use default.
if (glimp_refreshRate <= 0)
{
glimp_refreshRate = 60;
}
}
return glimp_refreshRate;
}
/*
* Detect current desktop mode
*/
qboolean
GLimp_GetDesktopMode(int *pwidth, int *pheight)
{
// Declare display mode structure to be filled in.
SDL_DisplayMode mode;
if (window)
{
/* save current display as default */
last_display = SDL_GetWindowDisplayIndex(window);
SDL_GetWindowPosition(window, &last_position_x, &last_position_y);
}
if (last_display < 0)
{
// In case of error...
Com_Printf("Can't detect current desktop.\n");
last_display = 0;
}
// We can't get desktop where we start, so use first desktop
if(SDL_GetDesktopDisplayMode(last_display, &mode) != 0)
{
// In case of error...
Com_Printf("Can't detect default desktop mode: %s\n",
SDL_GetError());
return false;
}
*pwidth = mode.w;
*pheight = mode.h;
return true;
}
const char**
GLimp_GetDisplayIndices(void)
{
return (const char**)displayindices;
}
int
GLimp_GetNumVideoDisplays(void)
{
return num_displays;
}
int
GLimp_GetWindowDisplayIndex(void)
{
return last_display;
}