-
Notifications
You must be signed in to change notification settings - Fork 571
/
Copy pathsdlglvideo.cpp
790 lines (671 loc) · 18.6 KB
/
sdlglvideo.cpp
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
** sdlglvideo.cpp
**
**---------------------------------------------------------------------------
** Copyright 2005-2016 Christoph Oelckers et.al.
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
// HEADER FILES ------------------------------------------------------------
#include "i_module.h"
#include "i_soundinternal.h"
#include "i_system.h"
#include "i_video.h"
#include "m_argv.h"
#include "v_video.h"
#include "version.h"
#include "c_console.h"
#include "c_dispatch.h"
#include "printf.h"
#include "hardware.h"
#include "gl_sysfb.h"
#include "gl_system.h"
#include "gl_renderer.h"
#include "gl_framebuffer.h"
#ifdef HAVE_VULKAN
#include "vulkan/system/vk_framebuffer.h"
#endif
#ifdef HAVE_SOFTPOLY
#include "poly_framebuffer.h"
#endif
// MACROS ------------------------------------------------------------------
// Requires SDL 2.0.6 or newer
//#define SDL2_STATIC_LIBRARY
#if defined SDL2_STATIC_LIBRARY && defined HAVE_VULKAN
#include <SDL_vulkan.h>
#endif // SDL2_STATIC_LIBRARY && HAVE_VULKAN
// TYPES -------------------------------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
extern IVideo *Video;
EXTERN_CVAR (Int, vid_adapter)
EXTERN_CVAR (Int, vid_displaybits)
EXTERN_CVAR (Int, vid_defwidth)
EXTERN_CVAR (Int, vid_defheight)
EXTERN_CVAR (Int, vid_preferbackend)
EXTERN_CVAR (Bool, cl_capfps)
// PUBLIC DATA DEFINITIONS -------------------------------------------------
CUSTOM_CVAR(Bool, gl_debug, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL)
{
Printf("This won't take effect until " GAMENAME " is restarted.\n");
}
CUSTOM_CVAR(Bool, gl_es, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL)
{
Printf("This won't take effect until " GAMENAME " is restarted.\n");
}
CVAR(Bool, i_soundinbackground, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
CVAR (Int, vid_adapter, 0, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
CUSTOM_CVAR(String, vid_sdl_render_driver, "", CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL)
{
Printf("This won't take effect until " GAMENAME " is restarted.\n");
}
CCMD(vid_list_sdl_render_drivers)
{
for (int i = 0; i < SDL_GetNumRenderDrivers(); ++i)
{
SDL_RendererInfo info;
if (SDL_GetRenderDriverInfo(i, &info) == 0)
Printf("%s\n", info.name);
}
}
// PRIVATE DATA DEFINITIONS ------------------------------------------------
namespace Priv
{
#ifdef SDL2_STATIC_LIBRARY
#define SDL2_OPTIONAL_FUNCTION(RESULT, NAME, ...) \
RESULT(*NAME)(__VA_ARGS__) = SDL_ ## NAME
#else // !SDL2_STATIC_LIBRARY
FModule library("SDL2");
#define SDL2_OPTIONAL_FUNCTION(RESULT, NAME, ...) \
static TOptProc<library, RESULT(*)(__VA_ARGS__)> NAME("SDL_" #NAME)
#endif // SDL2_STATIC_LIBRARY
SDL2_OPTIONAL_FUNCTION(int, GetWindowBordersSize, SDL_Window *window, int *top, int *left, int *bottom, int *right);
#ifdef HAVE_VULKAN
SDL2_OPTIONAL_FUNCTION(void, Vulkan_GetDrawableSize, SDL_Window *window, int *width, int *height);
SDL2_OPTIONAL_FUNCTION(SDL_bool, Vulkan_GetInstanceExtensions, SDL_Window *window, unsigned int *count, const char **names);
SDL2_OPTIONAL_FUNCTION(SDL_bool, Vulkan_CreateSurface, SDL_Window *window, VkInstance instance, VkSurfaceKHR *surface);
#endif
#undef SDL2_OPTIONAL_FUNCTION
static const uint32_t VulkanWindowFlag = 0x1000'0000;
SDL_Window *window;
bool vulkanEnabled;
bool softpolyEnabled;
bool fullscreenSwitch;
void CreateWindow(uint32_t extraFlags)
{
assert(Priv::window == nullptr);
// Set default size
SDL_Rect bounds;
SDL_GetDisplayBounds(vid_adapter, &bounds);
if (win_w <= 0 || win_h <= 0)
{
win_w = bounds.w * 8 / 10;
win_h = bounds.h * 8 / 10;
}
FString caption;
caption.Format(GAMENAME " %s (%s)", GetVersionString(), GetGitTime());
const uint32_t windowFlags = (win_maximized ? SDL_WINDOW_MAXIMIZED : 0) | SDL_WINDOW_RESIZABLE | extraFlags;
Priv::window = SDL_CreateWindow(caption,
(win_x <= 0) ? SDL_WINDOWPOS_CENTERED_DISPLAY(vid_adapter) : win_x,
(win_y <= 0) ? SDL_WINDOWPOS_CENTERED_DISPLAY(vid_adapter) : win_y,
win_w, win_h, windowFlags);
if (Priv::window != nullptr)
{
// Enforce minimum size limit
SDL_SetWindowMinimumSize(Priv::window, VID_MIN_WIDTH, VID_MIN_HEIGHT);
}
}
void DestroyWindow()
{
assert(Priv::window != nullptr);
SDL_DestroyWindow(Priv::window);
Priv::window = nullptr;
}
void SetupPixelFormat(int multisample, const int *glver)
{
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if (multisample > 0) {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, multisample);
}
if (gl_debug)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
if (gl_es)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
}
else if (glver[0] > 2)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, glver[0]);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, glver[1]);
}
else
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
}
}
}
class SDLVideo : public IVideo
{
public:
SDLVideo ();
~SDLVideo ();
DFrameBuffer *CreateFrameBuffer ();
private:
#ifdef HAVE_VULKAN
VulkanDevice *device = nullptr;
#endif
};
// CODE --------------------------------------------------------------------
#ifdef HAVE_VULKAN
void I_GetVulkanDrawableSize(int *width, int *height)
{
assert(Priv::vulkanEnabled);
assert(Priv::window != nullptr);
assert(Priv::Vulkan_GetDrawableSize);
Priv::Vulkan_GetDrawableSize(Priv::window, width, height);
}
bool I_GetVulkanPlatformExtensions(unsigned int *count, const char **names)
{
assert(Priv::vulkanEnabled);
assert(Priv::window != nullptr);
return Priv::Vulkan_GetInstanceExtensions(Priv::window, count, names) == SDL_TRUE;
}
bool I_CreateVulkanSurface(VkInstance instance, VkSurfaceKHR *surface)
{
assert(Priv::vulkanEnabled);
assert(Priv::window != nullptr);
return Priv::Vulkan_CreateSurface(Priv::window, instance, surface) == SDL_TRUE;
}
#endif
#ifdef HAVE_SOFTPOLY
namespace
{
SDL_Renderer* polyrendertarget = nullptr;
SDL_Texture* polytexture = nullptr;
int polytexturew = 0;
int polytextureh = 0;
bool polyvsync = false;
bool polyfirstinit = true;
}
void I_PolyPresentInit()
{
assert(Priv::softpolyEnabled);
assert(Priv::window != nullptr);
if (strcmp(vid_sdl_render_driver, "") != 0)
{
SDL_SetHint(SDL_HINT_RENDER_DRIVER, vid_sdl_render_driver);
}
}
uint8_t *I_PolyPresentLock(int w, int h, bool vsync, int &pitch)
{
// When vsync changes we need to reinitialize
if (polyrendertarget && polyvsync != vsync)
{
I_PolyPresentDeinit();
}
if (!polyrendertarget)
{
polyvsync = vsync;
polyrendertarget = SDL_CreateRenderer(Priv::window, -1, vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
if (!polyrendertarget)
{
I_FatalError("Could not create render target for softpoly: %s\n", SDL_GetError());
}
// Tell the user which render driver is being used, but don't repeat
// outselves if we're just changing vsync.
if (polyfirstinit)
{
polyfirstinit = false;
SDL_RendererInfo rendererInfo;
if (SDL_GetRendererInfo(polyrendertarget, &rendererInfo) == 0)
{
Printf("Using render driver %s\n", rendererInfo.name);
}
else
{
Printf("Failed to query render driver\n");
}
}
// Mask color
SDL_SetRenderDrawColor(polyrendertarget, 0, 0, 0, 255);
}
if (!polytexture || polytexturew != w || polytextureh != h)
{
if (polytexture)
{
SDL_DestroyTexture(polytexture);
polytexture = nullptr;
polytexturew = polytextureh = 0;
}
if ((polytexture = SDL_CreateTexture(polyrendertarget, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, w, h)) == nullptr)
I_Error("Failed to create %dx%d render target texture.", w, h);
polytexturew = w;
polytextureh = h;
}
uint8_t* pixels;
SDL_LockTexture(polytexture, nullptr, (void**)&pixels, &pitch);
return pixels;
}
void I_PolyPresentUnlock(int x, int y, int width, int height)
{
SDL_UnlockTexture(polytexture);
int ClientWidth, ClientHeight;
SDL_GetRendererOutputSize(polyrendertarget, &ClientWidth, &ClientHeight);
SDL_Rect clearrects[4];
int count = 0;
if (y > 0)
{
clearrects[count].x = 0;
clearrects[count].y = 0;
clearrects[count].w = ClientWidth;
clearrects[count].h = y;
count++;
}
if (y + height < ClientHeight)
{
clearrects[count].x = 0;
clearrects[count].y = y + height;
clearrects[count].w = ClientWidth;
clearrects[count].h = ClientHeight - clearrects[count].y;
count++;
}
if (x > 0)
{
clearrects[count].x = 0;
clearrects[count].y = y;
clearrects[count].w = x;
clearrects[count].h = height;
count++;
}
if (x + width < ClientWidth)
{
clearrects[count].x = x + width;
clearrects[count].y = y;
clearrects[count].w = ClientWidth - clearrects[count].x;
clearrects[count].h = height;
count++;
}
if (count > 0)
SDL_RenderFillRects(polyrendertarget, clearrects, count);
SDL_Rect dstrect;
dstrect.x = x;
dstrect.y = y;
dstrect.w = width;
dstrect.h = height;
SDL_RenderCopy(polyrendertarget, polytexture, nullptr, &dstrect);
SDL_RenderPresent(polyrendertarget);
}
void I_PolyPresentDeinit()
{
if (polytexture)
{
SDL_DestroyTexture(polytexture);
polytexture = nullptr;
}
if (polyrendertarget)
{
SDL_DestroyRenderer(polyrendertarget);
polyrendertarget = nullptr;
}
}
#endif
SDLVideo::SDLVideo ()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
return;
}
#ifndef SDL2_STATIC_LIBRARY
// Load optional SDL functions
if (!Priv::library.IsLoaded())
{
Priv::library.Load({ "libSDL2-2.0.so.0", "libSDL2-2.0.so", "libSDL2.so" });
}
#endif // !SDL2_STATIC_LIBRARY
#ifdef HAVE_SOFTPOLY
Priv::softpolyEnabled = vid_preferbackend == 2;
#endif
#ifdef HAVE_VULKAN
Priv::vulkanEnabled = vid_preferbackend == 1
&& Priv::Vulkan_GetDrawableSize && Priv::Vulkan_GetInstanceExtensions && Priv::Vulkan_CreateSurface;
if (Priv::vulkanEnabled)
{
Priv::CreateWindow(Priv::VulkanWindowFlag | SDL_WINDOW_HIDDEN);
if (Priv::window == nullptr)
{
Priv::vulkanEnabled = false;
}
}
#endif
#ifdef HAVE_SOFTPOLY
if (Priv::softpolyEnabled)
{
Priv::CreateWindow(SDL_WINDOW_HIDDEN);
if (Priv::window == nullptr)
{
I_FatalError("Could not create SoftPoly window:\n%s\n",SDL_GetError());
}
}
#endif
}
SDLVideo::~SDLVideo ()
{
#ifdef HAVE_VULKAN
delete device;
#endif
}
DFrameBuffer *SDLVideo::CreateFrameBuffer ()
{
SystemBaseFrameBuffer *fb = nullptr;
// first try Vulkan, if that fails OpenGL
#ifdef HAVE_VULKAN
if (Priv::vulkanEnabled)
{
try
{
assert(device == nullptr);
device = new VulkanDevice();
fb = new VulkanFrameBuffer(nullptr, vid_fullscreen, device);
}
catch (CVulkanError const&)
{
if (Priv::window != nullptr)
{
Priv::DestroyWindow();
}
Priv::vulkanEnabled = false;
}
}
#endif
#ifdef HAVE_SOFTPOLY
if (Priv::softpolyEnabled)
{
fb = new PolyFrameBuffer(nullptr, vid_fullscreen);
}
#endif
if (fb == nullptr)
{
fb = new OpenGLRenderer::OpenGLFrameBuffer(0, vid_fullscreen);
}
return fb;
}
IVideo *gl_CreateVideo()
{
return new SDLVideo();
}
// FrameBuffer Implementation -----------------------------------------------
SystemBaseFrameBuffer::SystemBaseFrameBuffer (void *, bool fullscreen)
: DFrameBuffer (vid_defwidth, vid_defheight)
{
if (Priv::window != nullptr)
{
SDL_SetWindowFullscreen(Priv::window, fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
SDL_ShowWindow(Priv::window);
}
}
int SystemBaseFrameBuffer::GetClientWidth()
{
int width = 0;
#ifdef HAVE_SOFTPOLY
if (Priv::softpolyEnabled)
{
if (polyrendertarget)
SDL_GetRendererOutputSize(polyrendertarget, &width, nullptr);
else
SDL_GetWindowSize(Priv::window, &width, nullptr);
return width;
}
#endif
#ifdef HAVE_VULKAN
assert(Priv::vulkanEnabled);
Priv::Vulkan_GetDrawableSize(Priv::window, &width, nullptr);
#endif
return width;
}
int SystemBaseFrameBuffer::GetClientHeight()
{
int height = 0;
#ifdef HAVE_SOFTPOLY
if (Priv::softpolyEnabled)
{
if (polyrendertarget)
SDL_GetRendererOutputSize(polyrendertarget, nullptr, &height);
else
SDL_GetWindowSize(Priv::window, nullptr, &height);
return height;
}
#endif
#ifdef HAVE_VULKAN
assert(Priv::vulkanEnabled);
Priv::Vulkan_GetDrawableSize(Priv::window, nullptr, &height);
#endif
return height;
}
bool SystemBaseFrameBuffer::IsFullscreen ()
{
return (SDL_GetWindowFlags(Priv::window) & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0;
}
void SystemBaseFrameBuffer::ToggleFullscreen(bool yes)
{
SDL_SetWindowFullscreen(Priv::window, yes ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
if ( !yes )
{
if ( !Priv::fullscreenSwitch )
{
Priv::fullscreenSwitch = true;
vid_fullscreen = false;
}
else
{
Priv::fullscreenSwitch = false;
SetWindowSize(win_w, win_h);
}
}
}
void SystemBaseFrameBuffer::SetWindowSize(int w, int h)
{
if (w < VID_MIN_WIDTH || h < VID_MIN_HEIGHT)
{
w = VID_MIN_WIDTH;
h = VID_MIN_HEIGHT;
}
win_w = w;
win_h = h;
if (vid_fullscreen)
{
vid_fullscreen = false;
}
else
{
win_maximized = false;
SDL_SetWindowSize(Priv::window, w, h);
SDL_SetWindowPosition(Priv::window, SDL_WINDOWPOS_CENTERED_DISPLAY(vid_adapter), SDL_WINDOWPOS_CENTERED_DISPLAY(vid_adapter));
SetSize(GetClientWidth(), GetClientHeight());
int x, y;
SDL_GetWindowPosition(Priv::window, &x, &y);
win_x = x;
win_y = y;
}
}
SystemGLFrameBuffer::SystemGLFrameBuffer(void *hMonitor, bool fullscreen)
: SystemBaseFrameBuffer(hMonitor, fullscreen)
{
// NOTE: Core profiles were added with GL 3.2, so there's no sense trying
// to set core 3.1 or 3.0. We could try a forward-compatible context
// instead, but that would be too restrictive (w.r.t. shaders).
static const int glvers[][2] = {
{ 4, 6 }, { 4, 5 }, { 4, 4 }, { 4, 3 }, { 4, 2 }, { 4, 1 }, { 4, 0 },
{ 3, 3 }, { 3, 2 }, { 2, 0 },
{ 0, 0 },
};
int glveridx = 0;
int i;
const char *version = Args->CheckValue("-glversion");
if (version != NULL)
{
double gl_version = strtod(version, NULL) + 0.01;
int vermaj = (int)gl_version;
int vermin = (int)(gl_version*10.0) % 10;
while (glvers[glveridx][0] > vermaj || (glvers[glveridx][0] == vermaj &&
glvers[glveridx][1] > vermin))
{
glveridx++;
if (glvers[glveridx][0] == 0)
{
glveridx = 0;
break;
}
}
}
for ( ; glvers[glveridx][0] > 0; ++glveridx)
{
Priv::SetupPixelFormat(0, glvers[glveridx]);
Priv::CreateWindow(SDL_WINDOW_OPENGL | (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
if (Priv::window == nullptr)
{
continue;
}
GLContext = SDL_GL_CreateContext(Priv::window);
if (GLContext == nullptr)
{
Priv::DestroyWindow();
}
else
{
break;
}
}
if (Priv::window == nullptr)
{
I_FatalError("Could not create OpenGL window:\n%s\n",SDL_GetError());
}
}
SystemGLFrameBuffer::~SystemGLFrameBuffer ()
{
if (Priv::window)
{
if (GLContext)
{
SDL_GL_DeleteContext(GLContext);
}
Priv::DestroyWindow();
}
}
int SystemGLFrameBuffer::GetClientWidth()
{
int width = 0;
SDL_GL_GetDrawableSize(Priv::window, &width, nullptr);
return width;
}
int SystemGLFrameBuffer::GetClientHeight()
{
int height = 0;
SDL_GL_GetDrawableSize(Priv::window, nullptr, &height);
return height;
}
void SystemGLFrameBuffer::SetVSync( bool vsync )
{
#if defined (__APPLE__)
const GLint value = vsync ? 1 : 0;
CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval, &value );
#else
if (vsync)
{
if (SDL_GL_SetSwapInterval(-1) == -1)
SDL_GL_SetSwapInterval(1);
}
else
{
SDL_GL_SetSwapInterval(0);
}
#endif
}
void SystemGLFrameBuffer::SwapBuffers()
{
SDL_GL_SwapWindow(Priv::window);
}
void ProcessSDLWindowEvent(const SDL_WindowEvent &event)
{
switch (event.event)
{
extern bool AppActive;
case SDL_WINDOWEVENT_FOCUS_GAINED:
S_SetSoundPaused(1);
AppActive = true;
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
S_SetSoundPaused(i_soundinbackground);
AppActive = false;
break;
case SDL_WINDOWEVENT_MOVED:
if (!vid_fullscreen && Priv::GetWindowBordersSize)
{
int top = 0, left = 0;
Priv::GetWindowBordersSize(Priv::window, &top, &left, nullptr, nullptr);
win_x = event.data1-left;
win_y = event.data2-top;
}
break;
case SDL_WINDOWEVENT_RESIZED:
if (!vid_fullscreen && !Priv::fullscreenSwitch)
{
win_w = event.data1;
win_h = event.data2;
}
break;
case SDL_WINDOWEVENT_MAXIMIZED:
win_maximized = true;
break;
case SDL_WINDOWEVENT_RESTORED:
win_maximized = false;
break;
}
}
// each platform has its own specific version of this function.
void I_SetWindowTitle(const char* caption)
{
if (caption)
{
SDL_SetWindowTitle(Priv::window, caption);
}
else
{
FString default_caption;
default_caption.Format(GAMENAME " %s (%s)", GetVersionString(), GetGitTime());
SDL_SetWindowTitle(Priv::window, default_caption);
}
}