Skip to content

Commit

Permalink
SDL: prevent options been overwrite by init (#649)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yuxiaomao authored Feb 2, 2024
1 parent 164f352 commit 062e9be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 43 deletions.
32 changes: 21 additions & 11 deletions libs/sdl/sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ typedef struct {
vbyte* dropFile;
} event_data;

static bool isGlOptionsSet = false;

HL_PRIM bool HL_NAME(init_once)() {
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
if( SDL_Init(SDL_INIT_EVERYTHING) != 0 ) {
Expand All @@ -110,23 +112,26 @@ HL_PRIM bool HL_NAME(init_once)() {
timeBeginPeriod(1);
# endif
// default GL parameters
if (!isGlOptionsSet) {
#ifdef HL_MOBILE
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
#endif
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
}

return true;
}

HL_PRIM void HL_NAME(gl_options)( int major, int minor, int depth, int stencil, int flags, int samples ) {
isGlOptionsSet = true;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, depth);
Expand All @@ -138,8 +143,13 @@ HL_PRIM void HL_NAME(gl_options)( int major, int minor, int depth, int stencil,
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
else if( flags&8 )
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0); // auto
else {
#ifdef HL_MOBILE
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#endif
}

if (samples > 1) {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
Expand Down
42 changes: 10 additions & 32 deletions libs/sdl/sdl/Sdl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,30 @@ typedef ScreenMode = {
var framerate : Int;
};

typedef GLOptions = {
final major : Int;
final minor : Int;
final depth : Int;
final stencil : Int;
final flags : Int;
final samples : Int;
}

@:hlNative("sdl")
class Sdl {

static var initDone = false;
static var isWin32 = false;

public static var glOptions(default,null) : GLOptions = {
major : 3,
minor : 2,
depth : 24,
stencil : 8,
flags : 1,
samples : 1,
};
public static var requiredGLMajor(default,null) = 3;
public static var requiredGLMinor(default,null) = 2;

public static function init() {
if( initDone ) return;
initDone = true;
if( !initOnce() ) throw "Failed to init SDL";
isWin32 = detectWin32();
var opt = glOptions;
gl_options(opt.major, opt.minor, opt.depth, opt.stencil, opt.flags, opt.samples);
}

public static function setGLOptions(options) {
glOptions = options;
public static function setGLOptions( major : Int = 3, minor : Int = 2, depth : Int = 24, stencil : Int = 8, flags : Int = 1, samples : Int = 1 ) {
requiredGLMajor = major;
requiredGLMinor = minor;
glOptions(major, minor, depth, stencil, flags, samples);
}

public static function setGLVersion(major, minor) {
glOptions = {
major : major,
minor : minor,
depth : glOptions.depth,
stencil : glOptions.stencil,
flags : glOptions.flags,
samples : glOptions.samples
};
public static function setGLVersion( major : Int, minor : Int) {
setGLOptions(major, minor);
}

public static function setHint(name:String, value:String) {
Expand All @@ -78,7 +56,7 @@ class Sdl {
if( device == null ) device = "Unknown";
var flags = new haxe.EnumFlags<hl.UI.DialogFlags>();
flags.set(IsError);
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.';
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.';
hl.UI.dialog("OpenGL Error", msg, flags);
Sys.exit( -1);
}
Expand All @@ -88,7 +66,7 @@ class Sdl {
public static inline var GL_COMPATIBILITY_PROFILE = 1 << 2;
public static inline var GL_ES = 1 << 3;

static function gl_options( major : Int, minor : Int, depth : Int, stencil : Int, flags : Int, samples : Int ) {}
static function glOptions( major : Int, minor : Int, depth : Int, stencil : Int, flags : Int, samples : Int ) {}

static function initOnce() return false;
static function eventLoop( e : Dynamic ) return false;
Expand Down

0 comments on commit 062e9be

Please sign in to comment.