From 0dff3ef144c450d0ef0dbec0461afc8cc22223ba Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 6 May 2021 00:23:46 +0300 Subject: [PATCH] A flag to disable OpenGL, better and more stable handling of no-OpenGL mode --- SDL/gui.c | 7 +++++++ SDL/main.c | 20 ++++++++++++++------ SDL/shader.c | 4 ++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index b8e69cdd..ae39f7ea 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -676,6 +676,7 @@ static void cycle_border_mode_backwards(unsigned index) } } +extern bool uses_gl(void); struct shader_name { const char *file_name; const char *display_name; @@ -699,6 +700,7 @@ struct shader_name { static void cycle_filter(unsigned index) { + if (!uses_gl()) return; unsigned i = 0; for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) { if (strcmp(shaders[i].file_name, configuration.filter) == 0) { @@ -721,6 +723,7 @@ static void cycle_filter(unsigned index) static void cycle_filter_backwards(unsigned index) { + if (!uses_gl()) return; unsigned i = 0; for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) { if (strcmp(shaders[i].file_name, configuration.filter) == 0) { @@ -742,6 +745,7 @@ static void cycle_filter_backwards(unsigned index) } const char *current_filter_name(unsigned index) { + if (!uses_gl()) return "Requires OpenGL 3.2+"; unsigned i = 0; for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) { if (strcmp(shaders[i].file_name, configuration.filter) == 0) { @@ -758,6 +762,7 @@ const char *current_filter_name(unsigned index) static void cycle_blending_mode(unsigned index) { + if (!uses_gl()) return; if (configuration.blending_mode == GB_FRAME_BLENDING_MODE_ACCURATE) { configuration.blending_mode = GB_FRAME_BLENDING_MODE_DISABLED; } @@ -768,6 +773,7 @@ static void cycle_blending_mode(unsigned index) static void cycle_blending_mode_backwards(unsigned index) { + if (!uses_gl()) return; if (configuration.blending_mode == GB_FRAME_BLENDING_MODE_DISABLED) { configuration.blending_mode = GB_FRAME_BLENDING_MODE_ACCURATE; } @@ -778,6 +784,7 @@ static void cycle_blending_mode_backwards(unsigned index) const char *blending_mode_string(unsigned index) { + if (!uses_gl()) return "Requires OpenGL 3.2+"; return (const char *[]){"Disabled", "Simple", "Accurate"} [configuration.blending_mode]; } diff --git a/SDL/main.c b/SDL/main.c index 29398b71..5afc6dea 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -25,8 +25,13 @@ static double clock_mutliplier = 1.0; static char *filename = NULL; static typeof(free) *free_function = NULL; -static char *battery_save_path_ptr; +static char *battery_save_path_ptr = NULL; +static SDL_GLContext gl_context = NULL; +bool uses_gl(void) +{ + return gl_context; +} void set_filename(const char *new_filename, typeof(free) *new_free_function) { @@ -637,9 +642,10 @@ int main(int argc, char **argv) fprintf(stderr, "SameBoy v" xstr(VERSION) "\n"); bool fullscreen = get_arg_flag("--fullscreen", &argc, argv); + bool nogl = get_arg_flag("--nogl", &argc, argv); if (argc > 2) { - fprintf(stderr, "Usage: %s [--fullscreen] [rom]\n", argv[0]); + fprintf(stderr, "Usage: %s [--fullscreen] [--nogl] [rom]\n", argv[0]); exit(1); } @@ -704,13 +710,15 @@ int main(int argc, char **argv) SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP); } - SDL_GLContext gl_context = SDL_GL_CreateContext(window); + gl_context = nogl? NULL : SDL_GL_CreateContext(window); GLint major = 0, minor = 0; - glGetIntegerv(GL_MAJOR_VERSION, &major); - glGetIntegerv(GL_MINOR_VERSION, &minor); + if (gl_context) { + glGetIntegerv(GL_MAJOR_VERSION, &major); + glGetIntegerv(GL_MINOR_VERSION, &minor); + } - if (major * 0x100 + minor < 0x302) { + if (gl_context && major * 0x100 + minor < 0x302) { SDL_GL_DeleteContext(gl_context); gl_context = NULL; } diff --git a/SDL/shader.c b/SDL/shader.c index de2ba564..44de2904 100644 --- a/SDL/shader.c +++ b/SDL/shader.c @@ -62,8 +62,11 @@ static GLuint create_program(const char *vsh, const char *fsh) return program; } +extern bool uses_gl(void); bool init_shader_with_name(shader_t *shader, const char *name) { + if (!uses_gl()) return false; + GLint major = 0, minor = 0; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); @@ -187,6 +190,7 @@ void render_bitmap_with_shader(shader_t *shader, void *bitmap, void *previous, void free_shader(shader_t *shader) { + if (!uses_gl()) return; GLint major = 0, minor = 0; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor);