A flag to disable OpenGL, better and more stable handling of no-OpenGL mode

This commit is contained in:
Lior Halphon 2021-05-06 00:23:46 +03:00
parent 1d0366052d
commit 0dff3ef144
3 changed files with 25 additions and 6 deletions

View File

@ -676,6 +676,7 @@ static void cycle_border_mode_backwards(unsigned index)
} }
} }
extern bool uses_gl(void);
struct shader_name { struct shader_name {
const char *file_name; const char *file_name;
const char *display_name; const char *display_name;
@ -699,6 +700,7 @@ struct shader_name {
static void cycle_filter(unsigned index) static void cycle_filter(unsigned index)
{ {
if (!uses_gl()) return;
unsigned i = 0; unsigned i = 0;
for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) { for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) {
if (strcmp(shaders[i].file_name, configuration.filter) == 0) { 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) static void cycle_filter_backwards(unsigned index)
{ {
if (!uses_gl()) return;
unsigned i = 0; unsigned i = 0;
for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) { for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) {
if (strcmp(shaders[i].file_name, configuration.filter) == 0) { 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) const char *current_filter_name(unsigned index)
{ {
if (!uses_gl()) return "Requires OpenGL 3.2+";
unsigned i = 0; unsigned i = 0;
for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) { for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) {
if (strcmp(shaders[i].file_name, configuration.filter) == 0) { 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) static void cycle_blending_mode(unsigned index)
{ {
if (!uses_gl()) return;
if (configuration.blending_mode == GB_FRAME_BLENDING_MODE_ACCURATE) { if (configuration.blending_mode == GB_FRAME_BLENDING_MODE_ACCURATE) {
configuration.blending_mode = GB_FRAME_BLENDING_MODE_DISABLED; 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) static void cycle_blending_mode_backwards(unsigned index)
{ {
if (!uses_gl()) return;
if (configuration.blending_mode == GB_FRAME_BLENDING_MODE_DISABLED) { if (configuration.blending_mode == GB_FRAME_BLENDING_MODE_DISABLED) {
configuration.blending_mode = GB_FRAME_BLENDING_MODE_ACCURATE; 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) const char *blending_mode_string(unsigned index)
{ {
if (!uses_gl()) return "Requires OpenGL 3.2+";
return (const char *[]){"Disabled", "Simple", "Accurate"} return (const char *[]){"Disabled", "Simple", "Accurate"}
[configuration.blending_mode]; [configuration.blending_mode];
} }

View File

@ -25,8 +25,13 @@ static double clock_mutliplier = 1.0;
static char *filename = NULL; static char *filename = NULL;
static typeof(free) *free_function = 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) 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"); fprintf(stderr, "SameBoy v" xstr(VERSION) "\n");
bool fullscreen = get_arg_flag("--fullscreen", &argc, argv); bool fullscreen = get_arg_flag("--fullscreen", &argc, argv);
bool nogl = get_arg_flag("--nogl", &argc, argv);
if (argc > 2) { if (argc > 2) {
fprintf(stderr, "Usage: %s [--fullscreen] [rom]\n", argv[0]); fprintf(stderr, "Usage: %s [--fullscreen] [--nogl] [rom]\n", argv[0]);
exit(1); exit(1);
} }
@ -704,13 +710,15 @@ int main(int argc, char **argv)
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP); 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; GLint major = 0, minor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &major); if (gl_context) {
glGetIntegerv(GL_MINOR_VERSION, &minor); 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); SDL_GL_DeleteContext(gl_context);
gl_context = NULL; gl_context = NULL;
} }

View File

@ -62,8 +62,11 @@ static GLuint create_program(const char *vsh, const char *fsh)
return program; return program;
} }
extern bool uses_gl(void);
bool init_shader_with_name(shader_t *shader, const char *name) bool init_shader_with_name(shader_t *shader, const char *name)
{ {
if (!uses_gl()) return false;
GLint major = 0, minor = 0; GLint major = 0, minor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor); 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) void free_shader(shader_t *shader)
{ {
if (!uses_gl()) return;
GLint major = 0, minor = 0; GLint major = 0, minor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor); glGetIntegerv(GL_MINOR_VERSION, &minor);