From 5de337a347706583bfc47ad7ae8d11b11c9c3e38 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sat, 31 Dec 2022 17:35:53 +0100 Subject: [PATCH] Only load GL library once And clean up on exit. --- src/main2.c | 120 +++++++++++++++++++++++----------------------------- 1 file changed, 52 insertions(+), 68 deletions(-) diff --git a/src/main2.c b/src/main2.c index c600d35..c6b1a7f 100644 --- a/src/main2.c +++ b/src/main2.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -437,6 +438,29 @@ const char *GetVideoModeDescription3() return buf; } +static bool load_opengl_library(const char *libs) +{ + if (!libs && SDL_GL_LoadLibrary(NULL) == 0) + return true; + + while (libs && *libs) { + char lib[PATH_MAX]; + size_t len = strcspn(libs, ":"); + size_t copylen = min(len, PATH_MAX-1); + strncpy(lib, libs, copylen); + lib[copylen] = '\0'; + + if (SDL_GL_LoadLibrary(lib) == 0) + return true; + + libs += len; + libs += strspn(libs, ":"); + } + + fprintf(stderr, "error: unable to initialize opengl library: %s\n", SDL_GetError()); + return false; +} + int InitSDL() { #if EMSCRIPTEN @@ -448,9 +472,34 @@ int InitSDL() fprintf(stderr, "SDL Init failed: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } - atexit(SDL_Quit); + // set OpenGL attributes first +#if defined(USE_OPENGL_ES) + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); +#else + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); +#endif + // These should be configurable video options. + // If user requests 8bpp, try that, else fall back to 5. + // Same with depth. Try 32, 24, 16. + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + if (!load_opengl_library(opengl_library)) + return false; + atexit(SDL_GL_UnloadLibrary); + + // These should be configurable video options. + //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); + //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); + SDL_AddEventWatch(SDLEventFilter, NULL); #if 0 @@ -689,43 +738,6 @@ static int SetSoftVideoMode(int Width, int Height, int Depth) return 0; } -/* ** */ -static void load_opengl_library(const char *lib) -{ - char tmppath[PATH_MAX]; - size_t len, copylen; - - if (lib == NULL) { - if (SDL_GL_LoadLibrary(NULL) == 0) { - /* success */ - return; - } - - fprintf( stderr, "ERROR: no opengl libraries given\n" ); - exit( EXIT_FAILURE ); - } - - while (lib != NULL && *lib) { - len = strcspn(lib, ":"); - - copylen = min(len, PATH_MAX-1); - - strncpy(tmppath, lib, copylen); - tmppath[copylen] = 0; - - if (SDL_GL_LoadLibrary(tmppath) == 0) { - /* success */ - return; - } - - lib += len; - lib += strspn(lib, ":"); - } - - fprintf(stderr, "ERROR: unable to initialize opengl library: %s\n", SDL_GetError()); - exit(EXIT_FAILURE); -} - /* ** */ static int SDLCALL SDLEventFilter(void* userData, SDL_Event* event) { (void) userData; @@ -741,10 +753,6 @@ static int SDLCALL SDLEventFilter(void* userData, SDL_Event* event) { return 1; } -static int InitSDLVideo(void) { - return 0; -} - static int SetOGLVideoMode(int Width, int Height) { GLenum status; @@ -788,32 +796,8 @@ static int SetOGLVideoMode(int Width, int Height) IngameKeyboardInput_ClearBuffer(); // force restart the video system - SDL_QuitSubSystem(SDL_INIT_VIDEO); - SDL_InitSubSystem(SDL_INIT_VIDEO); - - // set OpenGL attributes first -#if defined(USE_OPENGL_ES) - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); -#else - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); -#endif - // These should be configurable video options. - // If user requests 8bpp, try that, else fall back to 5. - // Same with depth. Try 32, 24, 16. - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - - load_opengl_library(opengl_library); - - // These should be configurable video options. - //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); - //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); + //SDL_QuitSubSystem(SDL_INIT_VIDEO); + //SDL_InitSubSystem(SDL_INIT_VIDEO); window = SDL_CreateWindow("Aliens vs Predator", SDL_WINDOWPOS_UNDEFINED,