From 821c57324d8417983a8cac71f82d16cbc439de27 Mon Sep 17 00:00:00 2001 From: "Azamat H. Hackimov" Date: Sat, 29 Jun 2024 18:36:04 +0300 Subject: [PATCH] Fix loading OpenGL library on Windows On loading GL symbols we are using SDL_GL_GetProcAddress(), which only valid if we previously loaded OpenGL library with SDL_GL_LoadLibrary(). We cannot use mod_LoadModule() here, otherwise finding symbols will fail. --- Descent3/descent.h | 7 ++++++- Descent3/sdlmain.cpp | 7 ++++--- renderer/HardwareOpenGL.cpp | 28 ++++++++++++---------------- renderer/dyna_gl.h | 35 ++++++++++++++--------------------- 4 files changed, 36 insertions(+), 41 deletions(-) diff --git a/Descent3/descent.h b/Descent3/descent.h index 81c27db0b..65e0fcd32 100644 --- a/Descent3/descent.h +++ b/Descent3/descent.h @@ -128,7 +128,9 @@ #ifndef _DESCENT_H #define _DESCENT_H -#include +#include +#include + #include "application.h" // The name of this product @@ -181,6 +183,9 @@ extern bool Descent_overrided_intro; // The "root" directory of the D3 file tree extern char Base_directory[]; +// Variable to preserve current path. TODO: redundant? +extern std::filesystem::path orig_pwd; + // --------------------------------------------------------------------------- // Globals diff --git a/Descent3/sdlmain.cpp b/Descent3/sdlmain.cpp index 9eed37d21..2ebf5ecbb 100644 --- a/Descent3/sdlmain.cpp +++ b/Descent3/sdlmain.cpp @@ -25,7 +25,7 @@ #include #include -#include +#include #ifndef WIN32 #include @@ -50,7 +50,7 @@ extern bool ddio_mouseGrabbed; const char *DMFCGetString(int d); // void *x = (void *) DMFCGetString; // just force a reference to dmfc.so ... -char *__orig_pwd = NULL; +std::filesystem::path orig_pwd; bool linux_permit_gamma = false; @@ -361,10 +361,11 @@ int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR szCmdLine, int nC GatherArgs(szCmdLine); #else int main(int argc, char *argv[]) { - __orig_pwd = getcwd(NULL, 0); GatherArgs(argv); #endif + orig_pwd = std::filesystem::current_path(); + /* Setup the logging system */ InitLog(); diff --git a/renderer/HardwareOpenGL.cpp b/renderer/HardwareOpenGL.cpp index 37c2f4f4a..8e85bc894 100644 --- a/renderer/HardwareOpenGL.cpp +++ b/renderer/HardwareOpenGL.cpp @@ -16,11 +16,17 @@ * along with this program. If not, see . */ -#include "byteswap.h" +#include +#include +#include +#include +#include + #if defined(WIN32) #include #endif +#include "byteswap.h" #include "pserror.h" #include "mono.h" #include "3d.h" @@ -33,15 +39,9 @@ #include "mem.h" #include "config.h" #include "rtperformance.h" -#include -#include -#include #include "HardwareInternal.h" #include "../Descent3/args.h" -#include "lnxscreenmode.h" -#include - -#include +#include "NewBitmap.h" #define DECLARE_OPENGL #include "dyna_gl.h" @@ -50,8 +50,6 @@ #include "win/arb_extensions.h" #endif -#include - int FindArg(const char *); void rend_SetLightingState(light_state state); @@ -66,9 +64,6 @@ extern uint8_t Renderer_initted; renderer_type Renderer_type = RENDERER_OPENGL; int WindowGL = 0; -extern matrix Unscaled_matrix; -extern vector View_position; - #ifndef GL_UNSIGNED_SHORT_5_5_5_1 #define GL_UNSIGNED_SHORT_5_5_5_1 0x8034 #endif @@ -452,16 +447,17 @@ int opengl_Setup(oeApplication *app, int *width, int *height) { if (!(OpenGLDLLHandle)) { // rcg07072000 last ditch effort... #ifdef __LINUX__ - OpenGLDLLHandle = LoadOpenGLDLL("libGL.so.1"); + strcpy(gl_library, "libGL.so.1"); #else - OpenGLDLLHandle = LoadOpenGLDLL("opengl32.dll"); + strcpy(gl_library, "opengl32.dll"); #endif + OpenGLDLLHandle = LoadOpenGLDLL(gl_library); if (!(OpenGLDLLHandle)) { success = false; } } // if - if (success == false) { + if (!success) { char buffer[512]; snprintf(buffer, sizeof(buffer), "Failed to load library [%s].\n", gl_library); fprintf(stderr, "%s", buffer); diff --git a/renderer/dyna_gl.h b/renderer/dyna_gl.h index dd0357861..2e7aa32b3 100644 --- a/renderer/dyna_gl.h +++ b/renderer/dyna_gl.h @@ -18,12 +18,15 @@ #pragma once +#include + #if defined(WIN32) -#include +#include #else -#include "SDL_opengl.h" +#include #endif +#include "descent.h" #include "module.h" #if defined(WIN32) @@ -220,14 +223,14 @@ static module OpenGLDLLInst; static void *__SDL_mod_GetSymbol(const char *funcStr) { void *retVal = NULL; - mprintf(0, "Looking up GL function [%s]...", funcStr); + mprintf(0, "Looking up GL function [%s]... ", funcStr); retVal = SDL_GL_GetProcAddress(funcStr); if (retVal == NULL) fprintf(stderr, " Could not find GL symbol [%s]!\n\n", funcStr); else { - mprintf(0, "Found at (%p).", retVal); + mprintf(0, "Found at (%p).\n", retVal); } // else return (retVal); @@ -237,19 +240,16 @@ static void *__SDL_mod_GetSymbol(const char *funcStr) { #define mod_GetSymbol(x, funcStr, y) __SDL_mod_GetSymbol(funcStr) /****************** WARNING: NASTY HACK! ***********************/ - -#ifdef __LINUX__ -extern char *__orig_pwd; extern char loadedLibrary[_MAX_PATH]; -#endif + module *LoadOpenGLDLL(const char *dllname) { mprintf(0, "Loading OpenGL dll...\n"); -#ifdef __LINUX__ - char *tmp = getcwd(NULL, 0); - chdir(__orig_pwd); - int rc = SDL_GL_LoadLibrary(dllname[0] ? dllname : NULL); - chdir(tmp); - free(tmp); + + std::filesystem::path tmp = std::filesystem::current_path(); + std::filesystem::current_path(orig_pwd); + int rc = SDL_GL_LoadLibrary(dllname[0] ? dllname : nullptr); + std::filesystem::current_path(tmp); + if (rc < 0) { const char *sdlErr = SDL_GetError(); mprintf(0, "OpenGL: Couldn't open library [%s].\n", dllname[0] ? dllname : "system default"); @@ -259,13 +259,6 @@ module *LoadOpenGLDLL(const char *dllname) { strcpy(loadedLibrary, dllname); -#else - if (!mod_LoadModule(&OpenGLDLLInst, dllname, MODF_LAZY | MODF_GLOBAL)) { - int err = mod_GetLastError(); - mprintf(0, "Couldn't open module called %s\n", dllname); - return NULL; - } -#endif dglAlphaFunc = (glAlphaFunc_fp)mod_GetSymbol(&OpenGLDLLInst, "glAlphaFunc", 255); if (!dglAlphaFunc)