-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
sdlmain.cpp
3325 lines (2974 loc) · 106 KB
/
sdlmain.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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2002-2020 The DOSBox Team
* Copyright (C) 2019-2020 The dosbox-staging team
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "dosbox.h"
#include <array>
#include <cassert>
#include <cstdlib>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <math.h>
#ifdef WIN32
#include <signal.h>
#include <process.h>
#endif
#include <SDL.h>
#if C_OPENGL
#include <SDL_opengl.h>
#endif
#include "cross.h"
#include "video.h"
#include "mouse.h"
#include "joystick.h"
#include "pic.h"
#include "timer.h"
#include "setup.h"
#include "support.h"
#include "debug.h"
#include "mapper.h"
#include "vga.h"
#include "keyboard.h"
#include "cpu.h"
#include "control.h"
#include "render.h"
#include "../libs/ppscale/ppscale.h"
#define MAPPERFILE "mapper-sdl2-" VERSION ".map"
#if C_OPENGL
//Define to disable the usage of the pixel buffer object
//#define DB_DISABLE_DBO
//Define to report opengl errors
//#define DB_OPENGL_ERROR
#ifndef APIENTRY
#define APIENTRY
#endif
#ifndef APIENTRYP
#define APIENTRYP APIENTRY *
#endif
#ifndef GL_ARB_pixel_buffer_object
#define GL_ARB_pixel_buffer_object 1
#define GL_PIXEL_PACK_BUFFER_ARB 0x88EB
#define GL_PIXEL_UNPACK_BUFFER_ARB 0x88EC
#define GL_PIXEL_PACK_BUFFER_BINDING_ARB 0x88ED
#define GL_PIXEL_UNPACK_BUFFER_BINDING_ARB 0x88EF
#endif
#ifndef GL_ARB_vertex_buffer_object
#define GL_ARB_vertex_buffer_object 1
typedef void (APIENTRYP PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
typedef void (APIENTRYP PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
typedef void (APIENTRYP PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
typedef void (APIENTRYP PFNGLBUFFERDATAARBPROC) (GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage);
typedef GLvoid* (APIENTRYP PFNGLMAPBUFFERARBPROC) (GLenum target, GLenum access);
typedef GLboolean (APIENTRYP PFNGLUNMAPBUFFERARBPROC) (GLenum target);
#endif
PFNGLGENBUFFERSARBPROC glGenBuffersARB = NULL;
PFNGLBINDBUFFERARBPROC glBindBufferARB = NULL;
PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = NULL;
PFNGLBUFFERDATAARBPROC glBufferDataARB = NULL;
PFNGLMAPBUFFERARBPROC glMapBufferARB = NULL;
PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = NULL;
/* Don't guard these with GL_VERSION_2_0 - Apple defines it but not these typedefs.
* If they're already defined they should match these definitions, so no conflicts.
*/
typedef void (APIENTRYP PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
typedef void (APIENTRYP PFNGLCOMPILESHADERPROC) (GLuint shader);
typedef GLuint (APIENTRYP PFNGLCREATEPROGRAMPROC) (void);
typedef GLuint (APIENTRYP PFNGLCREATESHADERPROC) (GLenum type);
typedef void (APIENTRYP PFNGLDELETEPROGRAMPROC) (GLuint program);
typedef void (APIENTRYP PFNGLDELETESHADERPROC) (GLuint shader);
typedef void (APIENTRYP PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
typedef GLint (APIENTRYP PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const GLchar *name);
typedef void (APIENTRYP PFNGLGETPROGRAMIVPROC) (GLuint program, GLenum pname, GLint *params);
typedef void (APIENTRYP PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
typedef void (APIENTRYP PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
typedef void (APIENTRYP PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
typedef GLint (APIENTRYP PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const GLchar *name);
typedef void (APIENTRYP PFNGLLINKPROGRAMPROC) (GLuint program);
//Change to NP, as Khronos changes include guard :(
typedef void (APIENTRYP PFNGLSHADERSOURCEPROC_NP) (GLuint shader, GLsizei count, const GLchar **string, const GLint *length);
typedef void (APIENTRYP PFNGLUNIFORM2FPROC) (GLint location, GLfloat v0, GLfloat v1);
typedef void (APIENTRYP PFNGLUNIFORM1IPROC) (GLint location, GLint v0);
typedef void (APIENTRYP PFNGLUSEPROGRAMPROC) (GLuint program);
typedef void (APIENTRYP PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
/* Apple defines these functions in their GL header (as core functions)
* so we can't use their names as function pointers. We can't link
* directly as some platforms may not have them. So they get their own
* namespace here to keep the official names but avoid collisions.
*/
namespace gl2 {
PFNGLATTACHSHADERPROC glAttachShader = NULL;
PFNGLCOMPILESHADERPROC glCompileShader = NULL;
PFNGLCREATEPROGRAMPROC glCreateProgram = NULL;
PFNGLCREATESHADERPROC glCreateShader = NULL;
PFNGLDELETEPROGRAMPROC glDeleteProgram = NULL;
PFNGLDELETESHADERPROC glDeleteShader = NULL;
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray = NULL;
PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation = NULL;
PFNGLGETPROGRAMIVPROC glGetProgramiv = NULL;
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog = NULL;
PFNGLGETSHADERIVPROC glGetShaderiv = NULL;
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog = NULL;
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = NULL;
PFNGLLINKPROGRAMPROC glLinkProgram = NULL;
PFNGLSHADERSOURCEPROC_NP glShaderSource = NULL;
PFNGLUNIFORM2FPROC glUniform2f = NULL;
PFNGLUNIFORM1IPROC glUniform1i = NULL;
PFNGLUSEPROGRAMPROC glUseProgram = NULL;
PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer = NULL;
}
/* "using" is meant to hide identical names declared in outer scope
* but is unreliable, so just redefine instead.
*/
#define glAttachShader gl2::glAttachShader
#define glCompileShader gl2::glCompileShader
#define glCreateProgram gl2::glCreateProgram
#define glCreateShader gl2::glCreateShader
#define glDeleteProgram gl2::glDeleteProgram
#define glDeleteShader gl2::glDeleteShader
#define glEnableVertexAttribArray gl2::glEnableVertexAttribArray
#define glGetAttribLocation gl2::glGetAttribLocation
#define glGetProgramiv gl2::glGetProgramiv
#define glGetProgramInfoLog gl2::glGetProgramInfoLog
#define glGetShaderiv gl2::glGetShaderiv
#define glGetShaderInfoLog gl2::glGetShaderInfoLog
#define glGetUniformLocation gl2::glGetUniformLocation
#define glLinkProgram gl2::glLinkProgram
#define glShaderSource gl2::glShaderSource
#define glUniform2f gl2::glUniform2f
#define glUniform1i gl2::glUniform1i
#define glUseProgram gl2::glUseProgram
#define glVertexAttribPointer gl2::glVertexAttribPointer
#endif // C_OPENGL
#if !(ENVIRON_INCLUDED)
extern char** environ;
#endif
#ifdef WIN32
#include <winuser.h>
#define STDOUT_FILE "stdout.txt"
#define STDERR_FILE "stderr.txt"
#endif
#if C_SET_PRIORITY
#include <sys/resource.h>
#define PRIO_TOTAL (PRIO_MAX-PRIO_MIN)
#endif
SDL_bool mouse_is_captured = SDL_FALSE; // global for mapper
// Masks to be passed when creating SDL_Surface.
// Remove ifndef if they'll be needed for MacOS X builds.
#ifndef MACOSX
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
constexpr uint32_t RMASK = 0xff000000;
constexpr uint32_t GMASK = 0x00ff0000;
constexpr uint32_t BMASK = 0x0000ff00;
constexpr uint32_t AMASK = 0x000000ff;
#else
constexpr uint32_t RMASK = 0x000000ff;
constexpr uint32_t GMASK = 0x0000ff00;
constexpr uint32_t BMASK = 0x00ff0000;
constexpr uint32_t AMASK = 0xff000000;
#endif
#endif // !MACOSX
enum MouseControlType {
CaptureOnClick = 1 << 0,
CaptureOnStart = 1 << 1,
Seamless = 1 << 2,
NoMouse = 1 << 3
};
enum SCREEN_TYPES {
SCREEN_SURFACE,
SCREEN_TEXTURE,
#if C_OPENGL
SCREEN_OPENGL
#endif
};
/* Modes of simple pixel scaling, currently encoded in the output type: */
enum ScalingMode {SmNone, SmNearest, SmPerfect};
enum PRIORITY_LEVELS {
PRIORITY_LEVEL_PAUSE,
PRIORITY_LEVEL_LOWEST,
PRIORITY_LEVEL_LOWER,
PRIORITY_LEVEL_NORMAL,
PRIORITY_LEVEL_HIGHER,
PRIORITY_LEVEL_HIGHEST
};
/* Alias for indicating, that new window should not be user-resizable: */
constexpr bool FIXED_SIZE = false;
struct SDL_Block {
bool initialized = false;
bool active; //If this isn't set don't draw
bool updating;
bool update_display_contents;
bool resizing_window;
ScalingMode scaling_mode;
struct {
int width = 0;
int height = 0;
double scalex = 1.0;
double scaley = 1.0;
double pixel_aspect = 1.0;
GFX_CallBack_t callback = nullptr;
} draw;
bool wait_on_error;
struct {
struct {
Bit16u width = 0;
Bit16u height = 0;
bool fixed = false;
bool display_res = false;
} full;
struct {
uint16_t width = 0; // TODO convert to int
uint16_t height = 0; // TODO convert to int
bool use_original_size = true;
bool resizable = false;
} window;
Bit8u bpp = 0;
bool fullscreen = false;
// This flag indicates, that we are in the process of switching
// between fullscreen or window (as oppososed to changing
// rendering size due to rotating screen, emulation state, or
// user resizing the window).
bool switching_fullscreen = false;
// Lazy window size init triggers updating window size and
// position when leaving fullscreen for the first time.
// See FinalizeWindowState function for details.
bool lazy_init_window_size = false;
bool vsync = false;
bool want_resizable_window = false;
SCREEN_TYPES type;
SCREEN_TYPES want_type;
} desktop;
#if C_OPENGL
struct {
SDL_GLContext context;
int pitch = 0;
void *framebuf = nullptr;
GLuint buffer;
GLuint texture;
GLuint displaylist;
GLint max_texsize;
bool bilinear;
bool packed_pixel;
bool paletted_texture;
bool pixel_buffer_object = false;
bool use_shader;
GLuint program_object;
const char *shader_src;
struct {
GLint texture_size;
GLint input_size;
GLint output_size;
GLint frame_count;
} ruby;
GLuint actual_frame_count;
GLfloat vertex_data[2*3];
} opengl;
#endif // C_OPENGL
struct {
PRIORITY_LEVELS focus;
PRIORITY_LEVELS nofocus;
} priority;
SDL_Rect clip;
SDL_Surface *surface;
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
std::string render_driver = "";
int display_number = 0;
struct {
SDL_Surface *input_surface = nullptr;
SDL_Texture *texture = nullptr;
SDL_PixelFormat *pixelFormat = nullptr;
} texture;
struct {
int xsensitivity = 0;
int ysensitivity = 0;
int sensitivity = 0;
MouseControlType control_choice = Seamless;
bool middle_will_release = true;
bool has_focus = false;
} mouse;
SDL_Point pp_scale = {1, 1};
bool double_h, double_w; /* double-height and double-width flags */
SDL_Rect updateRects[1024];
#if defined (WIN32)
// Time when sdl regains focus (alt-tab) in windowed mode
Bit32u focus_ticks;
#endif
// state of alt-keys for certain special handlings
SDL_EventType laltstate;
SDL_EventType raltstate;
};
static SDL_Block sdl;
static SDL_Point calc_pp_scale(int width, int heigth);
static SDL_Rect calc_viewport(int width, int height);
static void CleanupSDLResources();
static void HandleVideoResize(int width, int height);
static constexpr char version_msg[] = R"(dosbox (dosbox-staging), version %s
Copyright (C) 2020 The dosbox-staging team.
License GPLv2+: GNU GPL version 2 or later <https://www.gnu.org/licenses/gpl-2.0.html>
This is free software, and you are welcome to change and redistribute it
under certain conditions; please read the COPYING file thoroughly before
doing so. There is NO WARRANTY, to the extent permitted by law.
This program (dosbox-staging) is modified version of DOSBox.
Copyright (C) 2020 The DOSBox Team, published under GNU GPLv2+
Read AUTHORS file for more details.
)";
#if C_OPENGL
static char const shader_src_default[] = R"GLSL(
varying vec2 v_texCoord;
#if defined(VERTEX)
uniform vec2 rubyTextureSize;
uniform vec2 rubyInputSize;
attribute vec4 a_position;
void main() {
gl_Position = a_position;
v_texCoord = vec2(a_position.x+1.0,1.0-a_position.y)/2.0*rubyInputSize/rubyTextureSize;
}
#elif defined(FRAGMENT)
uniform sampler2D rubyTexture;
void main() {
gl_FragColor = texture2D(rubyTexture, v_texCoord);
}
#endif
)GLSL";
#ifdef DB_OPENGL_ERROR
void OPENGL_ERROR(const char* message) {
GLenum r = glGetError();
if (r == GL_NO_ERROR) return;
LOG_MSG("errors from %s",message);
do {
LOG_MSG("%X",r);
} while ( (r=glGetError()) != GL_NO_ERROR);
}
#else
void OPENGL_ERROR(const char*) {
return;
}
#endif
#endif
static void QuitSDL()
{
if (sdl.initialized)
SDL_Quit();
}
extern const char* RunningProgram;
extern bool CPU_CycleAutoAdjust;
//Globals for keyboard initialisation
bool startup_state_numlock=false;
bool startup_state_capslock=false;
void GFX_SetTitle(Bit32s cycles, int /*frameskip*/, bool paused)
{
char title[200] = {0};
static Bit32s internal_cycles = 0;
if (cycles != -1)
internal_cycles = cycles;
const char *msg = CPU_CycleAutoAdjust
? "%8s - max %d%% - dosbox-staging%s"
: "%8s - %d cycles/ms - dosbox-staging%s";
snprintf(title, sizeof(title), msg, RunningProgram, internal_cycles,
paused ? " (PAUSED)" : "");
SDL_SetWindowTitle(sdl.window, title);
}
#if SDL_VERSION_ATLEAST(2, 0, 5)
/* This function is SDL_EventFilter which is being called when event is
* pushed into the SDL event queue.
*
* WARNING: Be very careful of what you do in this function, as it may run in
* a different thread!
*
* Read documentation for SDL_AddEventWatch for more details.
*/
static int watch_sdl_events(void *userdata, SDL_Event *e)
{
/* There's a significant difference in handling of window resize
* events in different OSes. When handling resize in main event loop
* we receive continuous stream of events (as expected) on Linux,
* but only single event after user stopped dragging cursor on Windows
* and macOS.
*
* Watching resize events here gives us continuous stream on
* every OS.
*/
if (e->type == SDL_WINDOWEVENT && e->window.event == SDL_WINDOWEVENT_RESIZED) {
SDL_Window *win = SDL_GetWindowFromID(e->window.windowID);
if (win == (SDL_Window *)userdata) {
const int w = e->window.data1;
const int h = e->window.data2;
const int id = e->window.windowID;
DEBUG_LOG_MSG("SDL: Resizing window %d to %dx%d", id, w, h);
HandleVideoResize(w, h);
}
}
return 0;
}
#endif
/* On macOS, as we use a nicer external icon packaged in App bundle.
*
* Visual Studio bundles .ico file specified in winres.rc into the
* dosbox.exe file.
*
* Other OSes will either use svg icon bundled in the native package, or
* the window uploaded via SDL_SetWindowIcon below.
*/
#if defined(MACOSX) || defined(_MSC_VER)
static void SetIcon() {}
#else
#include "icon.c"
static void SetIcon()
{
constexpr int src_w = icon_data.width;
constexpr int src_h = icon_data.height;
constexpr int src_bpp = icon_data.bytes_per_pixel;
static_assert(src_bpp == 4, "Source image expected in RGBA format.");
std::array<uint8_t, (src_w * src_h * src_bpp)> icon;
ICON_DATA_RUN_LENGTH_DECODE(icon.data(), icon_data.rle_pixel_data,
src_w * src_h, src_bpp);
SDL_Surface *s = SDL_CreateRGBSurfaceFrom(icon.data(), src_w, src_h,
src_bpp * 8, src_w * src_bpp,
RMASK, GMASK, BMASK, AMASK);
SDL_SetWindowIcon(sdl.window, s);
SDL_FreeSurface(s);
}
#endif
static void KillSwitch(bool pressed) {
if (!pressed)
return;
throw 1;
}
MAYBE_UNUSED static void PauseDOSBox(bool pressed)
{
if (!pressed)
return;
GFX_SetTitle(-1,-1,true);
bool paused = true;
KEYBOARD_ClrBuffer();
SDL_Delay(500);
SDL_Event event;
while (SDL_PollEvent(&event)) {
// flush event queue.
}
/* NOTE: This is one of the few places where we use SDL key codes
with SDL 2.0, rather than scan codes. Is that the correct behavior? */
while (paused) {
SDL_WaitEvent(&event); // since we're not polling, cpu usage drops to 0.
switch (event.type) {
case SDL_QUIT: KillSwitch(true); break;
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESTORED) {
// We may need to re-create a texture and more
GFX_ResetScreen();
}
break;
case SDL_KEYDOWN: // Must use Pause/Break Key to resume.
case SDL_KEYUP:
if(event.key.keysym.sym == SDLK_PAUSE) {
paused = false;
GFX_SetTitle(-1,-1,false);
break;
}
#if defined (MACOSX)
if (event.key.keysym.sym == SDLK_q &&
(event.key.keysym.mod == KMOD_RGUI || event.key.keysym.mod == KMOD_LGUI)) {
/* On macs, all aps exit when pressing cmd-q */
KillSwitch(true);
break;
}
#endif
}
}
}
/* Reset the screen with current values in the sdl structure */
Bitu GFX_GetBestMode(Bitu flags)
{
if (sdl.scaling_mode == SmPerfect)
flags |= GFX_UNITY_SCALE;
switch (sdl.desktop.want_type) {
case SCREEN_SURFACE:
check_surface:
flags &= ~GFX_LOVE_8; //Disable love for 8bpp modes
switch (sdl.desktop.bpp) {
case 8:
if (flags & GFX_CAN_8) flags&=~(GFX_CAN_15|GFX_CAN_16|GFX_CAN_32);
break;
case 15:
if (flags & GFX_CAN_15) flags&=~(GFX_CAN_8|GFX_CAN_16|GFX_CAN_32);
break;
case 16:
if (flags & GFX_CAN_16) flags&=~(GFX_CAN_8|GFX_CAN_15|GFX_CAN_32);
break;
case 24:
case 32:
if (flags & GFX_CAN_32) flags&=~(GFX_CAN_8|GFX_CAN_15|GFX_CAN_16);
break;
}
flags |= GFX_CAN_RANDOM;
break;
#if C_OPENGL
case SCREEN_OPENGL:
#endif
case SCREEN_TEXTURE:
// We only accept 32bit output from the scalers here
if (!(flags&GFX_CAN_32)) goto check_surface;
flags|=GFX_SCALING;
flags&=~(GFX_CAN_8|GFX_CAN_15|GFX_CAN_16);
break;
default:
goto check_surface;
break;
}
return flags;
}
void GFX_ResetScreen(void) {
GFX_Stop();
if (sdl.draw.callback)
(sdl.draw.callback)( GFX_CallBackReset );
GFX_Start();
CPU_Reset_AutoAdjust();
}
void GFX_ForceFullscreenExit()
{
sdl.desktop.fullscreen = false;
GFX_ResetScreen();
}
static int int_log2 (int val) {
int log = 0;
while ((val >>= 1) != 0)
log++;
return log;
}
// This is a hack to prevent SDL2 from re-creating window internally. Prevents
// crashes on Windows and Linux, and prevents initial window from being visibly
// destroyed (for window managers that show animations while creating window,
// e.g. Gnome 3).
static uint32_t opengl_driver_crash_workaround(SCREEN_TYPES type)
{
if (type != SCREEN_TEXTURE)
return 0;
if (starts_with("opengl", sdl.render_driver))
return SDL_WINDOW_OPENGL;
if (sdl.render_driver != "auto")
return 0;
static int default_driver_is_opengl = -1;
if (default_driver_is_opengl >= 0)
return (default_driver_is_opengl ? SDL_WINDOW_OPENGL : 0);
// According to SDL2 documentation, the first driver
// in the list is the default one.
int i = 0;
SDL_RendererInfo info;
while (SDL_GetRenderDriverInfo(i++, &info) == 0) {
if (info.flags & SDL_RENDERER_TARGETTEXTURE)
break;
}
default_driver_is_opengl = starts_with("opengl", info.name);
return (default_driver_is_opengl ? SDL_WINDOW_OPENGL : 0);
}
static SDL_Window *SetWindowMode(SCREEN_TYPES screen_type,
int width,
int height,
bool fullscreen,
bool resizable)
{
static SCREEN_TYPES last_type = SCREEN_SURFACE;
CleanupSDLResources();
int currWidth, currHeight;
if (sdl.window && sdl.resizing_window) {
SDL_GetWindowSize(sdl.window, &currWidth, &currHeight);
sdl.update_display_contents = ((width <= currWidth) && (height <= currHeight));
return sdl.window;
}
if (!sdl.window || (last_type != screen_type)) {
last_type = screen_type;
if (sdl.window) {
SDL_DestroyWindow(sdl.window);
}
uint32_t flags = opengl_driver_crash_workaround(screen_type);
#if C_OPENGL
if (screen_type == SCREEN_OPENGL)
flags |= SDL_WINDOW_OPENGL;
#endif
// Using undefined position will take care of placing and
// restoring the window by WM.
const int pos = SDL_WINDOWPOS_UNDEFINED_DISPLAY(sdl.display_number);
sdl.window = SDL_CreateWindow("", pos, pos, width, height, flags);
if (!sdl.window) {
LOG_MSG("SDL: %s", SDL_GetError());
return nullptr;
}
#if SDL_VERSION_ATLEAST(2, 0, 5)
if (resizable) {
SDL_AddEventWatch(watch_sdl_events, sdl.window);
SDL_SetWindowResizable(sdl.window, SDL_TRUE);
}
sdl.desktop.window.resizable = resizable;
#endif
GFX_SetTitle(-1, -1, false); // refresh title.
if (!fullscreen) {
goto finish;
}
}
/* Fullscreen mode switching has its limits, and is also problematic on
* some window managers. For now, the following may work up to some
* level. On X11, SDL_VIDEO_X11_LEGACY_FULLSCREEN=1 can also help,
* although it has its own issues.
*/
if (fullscreen) {
SDL_DisplayMode displayMode;
SDL_GetWindowDisplayMode(sdl.window, &displayMode);
displayMode.w = width;
displayMode.h = height;
SDL_SetWindowDisplayMode(sdl.window, &displayMode);
SDL_SetWindowFullscreen(sdl.window,
sdl.desktop.full.display_res ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_FULLSCREEN);
} else {
// If you feel inclined to remove this SDL_SetWindowSize call
// (or further modify when it is being invoked), then make sure
// window size is updated AND content is correctly clipped when
// emulated program changes resolution with various
// windowresolution settings (!).
if (sdl.desktop.window.use_original_size &&
!sdl.desktop.window.resizable && !sdl.desktop.switching_fullscreen)
SDL_SetWindowSize(sdl.window, width, height);
SDL_SetWindowFullscreen(sdl.window, 0);
}
// Maybe some requested fullscreen resolution is unsupported?
finish:
if (sdl.desktop.window.resizable && !sdl.desktop.switching_fullscreen) {
const int w = iround(sdl.draw.width * sdl.draw.scalex);
const int h = iround(sdl.draw.height * sdl.draw.scaley);
SDL_SetWindowMinimumSize(sdl.window, w, h);
}
sdl.update_display_contents = true;
return sdl.window;
}
// Used for the mapper UI and more: Creates a fullscreen window with desktop res
// on Android, and a non-fullscreen window with the input dimensions otherwise.
SDL_Window * GFX_SetSDLSurfaceWindow(Bit16u width, Bit16u height)
{
constexpr bool fullscreen = false;
return SetWindowMode(SCREEN_SURFACE, width, height, fullscreen, FIXED_SIZE);
}
// Returns the rectangle in the current window to be used for scaling a
// sub-window with the given dimensions, like the mapper UI.
SDL_Rect GFX_GetSDLSurfaceSubwindowDims(Bit16u width, Bit16u height)
{
SDL_Rect rect;
rect.x = rect.y = 0;
rect.w = width;
rect.h = height;
return rect;
}
static SDL_Window *setup_window_pp(SCREEN_TYPES screen_type, bool resizable)
{
assert(sdl.window);
int w = 0;
int h = 0;
SDL_GetWindowSize(sdl.window, &w, &h);
sdl.pp_scale = calc_pp_scale(w, h);
LOG_MSG("MAIN: Pixel-perfect scaling (%dx%d): %dx%d (PAR %#.3g) -> %dx%d (PAR %#.3g)",
sdl.pp_scale.x, sdl.pp_scale.y, sdl.draw.width, sdl.draw.height,
sdl.draw.pixel_aspect, sdl.pp_scale.x * sdl.draw.width,
sdl.pp_scale.y * sdl.draw.height,
static_cast<double>(sdl.pp_scale.y) / sdl.pp_scale.x);
const int imgw = sdl.pp_scale.x * sdl.draw.width;
const int imgh = sdl.pp_scale.y * sdl.draw.height;
const int wndw = (sdl.desktop.fullscreen ? w : imgw);
const int wndh = (sdl.desktop.fullscreen ? h : imgh);
sdl.clip.w = imgw;
sdl.clip.h = imgh;
sdl.clip.x = (wndw - imgw) / 2;
sdl.clip.y = (wndh - imgh) / 2;
sdl.window = SetWindowMode(screen_type, wndw, wndh,
sdl.desktop.fullscreen, resizable);
return sdl.window;
}
static SDL_Window *SetupWindowScaled(SCREEN_TYPES screen_type, bool resizable)
{
if (sdl.scaling_mode == SmPerfect)
return setup_window_pp(screen_type, resizable);
Bit16u fixedWidth;
Bit16u fixedHeight;
if (sdl.desktop.fullscreen) {
fixedWidth = sdl.desktop.full.fixed ? sdl.desktop.full.width : 0;
fixedHeight = sdl.desktop.full.fixed ? sdl.desktop.full.height : 0;
} else {
fixedWidth = sdl.desktop.window.width;
fixedHeight = sdl.desktop.window.height;
}
if (fixedWidth && fixedHeight) {
double ratio_w=(double)fixedWidth/(sdl.draw.width*sdl.draw.scalex);
double ratio_h=(double)fixedHeight/(sdl.draw.height*sdl.draw.scaley);
if ( ratio_w < ratio_h) {
sdl.clip.w=fixedWidth;
sdl.clip.h=(Bit16u)(sdl.draw.height*sdl.draw.scaley*ratio_w + 0.1); //possible rounding issues
} else {
/*
* The 0.4 is there to correct for rounding issues.
* (partly caused by the rounding issues fix in RENDER_SetSize)
*/
sdl.clip.w=(Bit16u)(sdl.draw.width*sdl.draw.scalex*ratio_h + 0.4);
sdl.clip.h = fixedHeight;
}
if (sdl.desktop.fullscreen) {
sdl.window = SetWindowMode(screen_type, fixedWidth,
fixedHeight, sdl.desktop.fullscreen,
resizable);
} else {
sdl.window = SetWindowMode(screen_type, sdl.clip.w,
sdl.clip.h, sdl.desktop.fullscreen,
resizable);
}
if (sdl.window && SDL_GetWindowFlags(sdl.window) & SDL_WINDOW_FULLSCREEN) {
int windowWidth;
SDL_GetWindowSize(sdl.window, &windowWidth, NULL);
sdl.clip.x = (Sint16)((windowWidth - sdl.clip.w) / 2);
sdl.clip.y = (Sint16)((fixedHeight - sdl.clip.h) / 2);
} else {
sdl.clip.x = 0;
sdl.clip.y = 0;
}
return sdl.window;
} else {
sdl.clip.x = 0;
sdl.clip.y = 0;
sdl.clip.w = iround(sdl.draw.width * sdl.draw.scalex);
sdl.clip.h = iround(sdl.draw.height * sdl.draw.scaley);
sdl.window = SetWindowMode(screen_type, sdl.clip.w, sdl.clip.h,
sdl.desktop.fullscreen, resizable);
return sdl.window;
}
}
#if C_OPENGL
/* Create a GLSL shader object, load the shader source, and compile the shader. */
static GLuint BuildShader ( GLenum type, const char *shaderSrc ) {
GLuint shader;
GLint compiled;
const char* src_strings[2];
std::string top;
// look for "#version" because it has to occur first
const char *ver = strstr(shaderSrc, "#version ");
if (ver) {
const char *endline = strchr(ver+9, '\n');
if (endline) {
top.assign(shaderSrc, endline-shaderSrc+1);
shaderSrc = endline+1;
}
}
top += (type==GL_VERTEX_SHADER) ? "#define VERTEX 1\n":"#define FRAGMENT 1\n";
if (!sdl.opengl.bilinear)
top += "#define OPENGLNB 1\n";
src_strings[0] = top.c_str();
src_strings[1] = shaderSrc;
// Create the shader object
shader = glCreateShader(type);
if (shader == 0) return 0;
// Load the shader source
glShaderSource(shader, 2, src_strings, NULL);
// Compile the shader
glCompileShader(shader);
// Check the compile status
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint info_len = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len);
if (info_len > 1) {
std::vector<GLchar> info_log(info_len);
glGetShaderInfoLog(shader, info_len, NULL, info_log.data());
LOG_MSG("Error compiling shader: %s", info_log.data());
}
glDeleteShader(shader);
return 0;
}
return shader;
}
static bool LoadGLShaders(const char *src, GLuint *vertex, GLuint *fragment) {
GLuint s = BuildShader(GL_VERTEX_SHADER, src);
if (s) {
*vertex = s;
s = BuildShader(GL_FRAGMENT_SHADER, src);
if (s) {
*fragment = s;
return true;
}
glDeleteShader(*vertex);
}
return false;
}
#endif
static SDL_Point calc_pp_scale(int avw, int avh)
{
assert(sdl.draw.width > 0);
assert(sdl.draw.height > 0);
assert(sdl.draw.pixel_aspect > 0.0);
int x = 0;
int y = 0;
constexpr double aspect_weight = 1.14;
const int err = pp_getscale(sdl.draw.width, sdl.draw.height,
sdl.draw.pixel_aspect, avw, avh,
aspect_weight, &x, &y);
if (err == 0)
return {x, y};
else
return {1, 1};
}
Bitu GFX_SetSize(Bitu width, Bitu height, Bitu flags,
double scalex, double scaley,
GFX_CallBack_t callback,
double pixel_aspect)
{
Bitu retFlags = 0;
if (sdl.updating)
GFX_EndUpdate( 0 );
sdl.draw.width = static_cast<int>(width);
sdl.draw.height = static_cast<int>(height);
sdl.draw.scalex = scalex;
sdl.draw.scaley = scaley;
sdl.draw.pixel_aspect = pixel_aspect;
sdl.draw.callback = callback;
sdl.double_h = (flags & GFX_DBL_H) > 0;
sdl.double_w = (flags & GFX_DBL_W) > 0;
LOG_MSG("MAIN: Draw resolution: %dx%d,%s%s pixel aspect ratio: %#.2f",
sdl.draw.width, sdl.draw.height,
sdl.double_w ? " double-width," : "",
sdl.double_h ? " double-height," : "",
pixel_aspect);
switch (sdl.desktop.want_type) {
dosurface:
case SCREEN_SURFACE:
sdl.clip.w = width;
sdl.clip.h = height;
if (sdl.desktop.fullscreen) {
if (sdl.desktop.full.fixed) {
sdl.clip.x = (sdl.desktop.full.width - width) / 2;
sdl.clip.y = (sdl.desktop.full.height - height) / 2;
sdl.window = SetWindowMode(SCREEN_SURFACE,
sdl.desktop.full.width,
sdl.desktop.full.height,
sdl.desktop.fullscreen,
FIXED_SIZE);
if (sdl.window == NULL)
E_Exit("Could not set fullscreen video mode %ix%i-%i: %s",
sdl.desktop.full.width,
sdl.desktop.full.height,
sdl.desktop.bpp,
SDL_GetError());
/* This may be required after an ALT-TAB leading to a window
minimize, which further effectively shrinks its size */
if ((sdl.clip.x < 0) || (sdl.clip.y < 0)) {
sdl.update_display_contents = false;
}
} else {
sdl.clip.x = 0;
sdl.clip.y = 0;
sdl.window = SetWindowMode(SCREEN_SURFACE,
width, height,
sdl.desktop.fullscreen,
FIXED_SIZE);
if (sdl.window == NULL)
E_Exit("Could not set fullscreen video mode %ix%i-%i: %s",
(int)width,
(int)height,