Skip to content

Commit 44c7821

Browse files
yuxiaomaonotGlassySundew
authored andcommitted
SDL: prevent options been overwrite by init (HaxeFoundation#649)
* Revert "make sure gl options are set after init()" This reverts commit b4bfd6d. * Fix gl attribute: do not set to default if already set, do not use auto for PROFILE_MASK as it bug on OSX * Restore spaces fix * Keep setGLVersion API
1 parent 9e8216e commit 44c7821

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

libs/sdl/sdl.c

+21-11
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ typedef struct {
9898
vbyte* dropFile;
9999
} event_data;
100100

101+
static bool isGlOptionsSet = false;
102+
101103
HL_PRIM bool HL_NAME(init_once)() {
102104
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
103105
if( SDL_Init(SDL_INIT_EVERYTHING) != 0 ) {
@@ -110,23 +112,26 @@ HL_PRIM bool HL_NAME(init_once)() {
110112
timeBeginPeriod(1);
111113
# endif
112114
// default GL parameters
115+
if (!isGlOptionsSet) {
113116
#ifdef HL_MOBILE
114-
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
115-
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
116-
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
117+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
118+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
119+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
117120
#else
118-
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
119-
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
120-
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
121+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
122+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
123+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
121124
#endif
122-
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
123-
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
124-
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
125+
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
126+
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
127+
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
128+
}
125129

126130
return true;
127131
}
128132

129133
HL_PRIM void HL_NAME(gl_options)( int major, int minor, int depth, int stencil, int flags, int samples ) {
134+
isGlOptionsSet = true;
130135
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
131136
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
132137
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, depth);
@@ -138,8 +143,13 @@ HL_PRIM void HL_NAME(gl_options)( int major, int minor, int depth, int stencil,
138143
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
139144
else if( flags&8 )
140145
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
141-
else
142-
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0); // auto
146+
else {
147+
#ifdef HL_MOBILE
148+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
149+
#else
150+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
151+
#endif
152+
}
143153

144154
if (samples > 1) {
145155
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);

libs/sdl/sdl/Sdl.hx

+10-32
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,30 @@ typedef ScreenMode = {
1616
var framerate : Int;
1717
};
1818

19-
typedef GLOptions = {
20-
final major : Int;
21-
final minor : Int;
22-
final depth : Int;
23-
final stencil : Int;
24-
final flags : Int;
25-
final samples : Int;
26-
}
27-
2819
@:hlNative("sdl")
2920
class Sdl {
3021

3122
static var initDone = false;
3223
static var isWin32 = false;
3324

34-
public static var glOptions(default,null) : GLOptions = {
35-
major : 3,
36-
minor : 2,
37-
depth : 24,
38-
stencil : 8,
39-
flags : 1,
40-
samples : 1,
41-
};
25+
public static var requiredGLMajor(default,null) = 3;
26+
public static var requiredGLMinor(default,null) = 2;
4227

4328
public static function init() {
4429
if( initDone ) return;
4530
initDone = true;
4631
if( !initOnce() ) throw "Failed to init SDL";
4732
isWin32 = detectWin32();
48-
var opt = glOptions;
49-
gl_options(opt.major, opt.minor, opt.depth, opt.stencil, opt.flags, opt.samples);
5033
}
5134

52-
public static function setGLOptions(options) {
53-
glOptions = options;
35+
public static function setGLOptions( major : Int = 3, minor : Int = 2, depth : Int = 24, stencil : Int = 8, flags : Int = 1, samples : Int = 1 ) {
36+
requiredGLMajor = major;
37+
requiredGLMinor = minor;
38+
glOptions(major, minor, depth, stencil, flags, samples);
5439
}
5540

56-
public static function setGLVersion(major, minor) {
57-
glOptions = {
58-
major : major,
59-
minor : minor,
60-
depth : glOptions.depth,
61-
stencil : glOptions.stencil,
62-
flags : glOptions.flags,
63-
samples : glOptions.samples
64-
};
41+
public static function setGLVersion( major : Int, minor : Int) {
42+
setGLOptions(major, minor);
6543
}
6644

6745
public static function setHint(name:String, value:String) {
@@ -78,7 +56,7 @@ class Sdl {
7856
if( device == null ) device = "Unknown";
7957
var flags = new haxe.EnumFlags<hl.UI.DialogFlags>();
8058
flags.set(IsError);
81-
var msg = 'The application was unable to create an OpenGL context\nfor your $device video card.\nOpenGL ${glOptions.major}.${glOptions.minor}+ is required, please update your driver.';
59+
var msg = 'The application was unable to create an OpenGL context\nfor your $device video card.\nOpenGL $requiredGLMajor.$requiredGLMinor+ is required, please update your driver.';
8260
hl.UI.dialog("OpenGL Error", msg, flags);
8361
Sys.exit( -1);
8462
}
@@ -88,7 +66,7 @@ class Sdl {
8866
public static inline var GL_COMPATIBILITY_PROFILE = 1 << 2;
8967
public static inline var GL_ES = 1 << 3;
9068

91-
static function gl_options( major : Int, minor : Int, depth : Int, stencil : Int, flags : Int, samples : Int ) {}
69+
static function glOptions( major : Int, minor : Int, depth : Int, stencil : Int, flags : Int, samples : Int ) {}
9270

9371
static function initOnce() return false;
9472
static function eventLoop( e : Dynamic ) return false;

0 commit comments

Comments
 (0)