Skip to content

Commit

Permalink
Fix loading OpenGL library on Windows
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
winterheart authored and Lgt2x committed Jun 29, 2024
1 parent b394df2 commit 821c573
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 41 deletions.
7 changes: 6 additions & 1 deletion Descent3/descent.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@
#ifndef _DESCENT_H
#define _DESCENT_H

#include <stdlib.h>
#include <cstdlib>
#include <filesystem>

#include "application.h"

// The name of this product
Expand Down Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions Descent3/sdlmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#include <cstdlib>
#include <cstring>
#include <csignal>
#include <filesystem>

#ifndef WIN32
#include <unistd.h>
Expand All @@ -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;

Expand Down Expand Up @@ -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();

Expand Down
28 changes: 12 additions & 16 deletions renderer/HardwareOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "byteswap.h"
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <SDL.h>

#if defined(WIN32)
#include <windows.h>
#endif

#include "byteswap.h"
#include "pserror.h"
#include "mono.h"
#include "3d.h"
Expand All @@ -33,15 +39,9 @@
#include "mem.h"
#include "config.h"
#include "rtperformance.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "HardwareInternal.h"
#include "../Descent3/args.h"
#include "lnxscreenmode.h"
#include <SDL.h>

#include <NewBitmap.h>
#include "NewBitmap.h"

#define DECLARE_OPENGL
#include "dyna_gl.h"
Expand All @@ -50,8 +50,6 @@
#include "win/arb_extensions.h"
#endif

#include <algorithm>

int FindArg(const char *);
void rend_SetLightingState(light_state state);

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
35 changes: 14 additions & 21 deletions renderer/dyna_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

#pragma once

#include <filesystem>

#if defined(WIN32)
#include <GL/gl.h>
#include <gl/GL.h>
#else
#include "SDL_opengl.h"
#include <SDL_opengl.h>
#endif

#include "descent.h"
#include "module.h"

#if defined(WIN32)
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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)
Expand Down

0 comments on commit 821c573

Please sign in to comment.