Only load GL library once

And clean up on exit.
This commit is contained in:
Timotej Lazar 2022-12-31 17:35:53 +01:00
parent 2375dff9d7
commit 5de337a347

View file

@ -1,3 +1,4 @@
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -437,6 +438,29 @@ const char *GetVideoModeDescription3()
return buf; 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() int InitSDL()
{ {
#if EMSCRIPTEN #if EMSCRIPTEN
@ -448,9 +472,34 @@ int InitSDL()
fprintf(stderr, "SDL Init failed: %s\n", SDL_GetError()); fprintf(stderr, "SDL Init failed: %s\n", SDL_GetError());
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
atexit(SDL_Quit); 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); SDL_AddEventWatch(SDLEventFilter, NULL);
#if 0 #if 0
@ -689,43 +738,6 @@ static int SetSoftVideoMode(int Width, int Height, int Depth)
return 0; 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) { static int SDLCALL SDLEventFilter(void* userData, SDL_Event* event) {
(void) userData; (void) userData;
@ -741,10 +753,6 @@ static int SDLCALL SDLEventFilter(void* userData, SDL_Event* event) {
return 1; return 1;
} }
static int InitSDLVideo(void) {
return 0;
}
static int SetOGLVideoMode(int Width, int Height) static int SetOGLVideoMode(int Width, int Height)
{ {
GLenum status; GLenum status;
@ -788,32 +796,8 @@ static int SetOGLVideoMode(int Width, int Height)
IngameKeyboardInput_ClearBuffer(); IngameKeyboardInput_ClearBuffer();
// force restart the video system // force restart the video system
SDL_QuitSubSystem(SDL_INIT_VIDEO); //SDL_QuitSubSystem(SDL_INIT_VIDEO);
SDL_InitSubSystem(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);
window = SDL_CreateWindow("Aliens vs Predator", window = SDL_CreateWindow("Aliens vs Predator",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,