Skip to content

Commit 7b7ffde

Browse files
winterheartLgt2x
authored andcommitted
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.
1 parent b394df2 commit 7b7ffde

4 files changed

Lines changed: 37 additions & 41 deletions

File tree

Descent3/descent.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@
128128
#ifndef _DESCENT_H
129129
#define _DESCENT_H
130130

131-
#include <stdlib.h>
131+
#include <cstdlib>
132+
#include <filesystem>
133+
132134
#include "application.h"
133135

134136
// The name of this product
@@ -181,6 +183,9 @@ extern bool Descent_overrided_intro;
181183
// The "root" directory of the D3 file tree
182184
extern char Base_directory[];
183185

186+
// Variable to preserve current path. TODO: redundant?
187+
extern std::filesystem::path orig_pwd;
188+
184189
// ---------------------------------------------------------------------------
185190
// Globals
186191

Descent3/sdlmain.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525

2626
#include <cstdlib>
2727
#include <cstring>
28-
#include <csignal>
28+
#include <filesystem>
2929

3030
#ifndef WIN32
3131
#include <unistd.h>
32+
#include <csignal>
3233
#endif
3334

3435
#include <SDL.h>
@@ -50,7 +51,7 @@ extern bool ddio_mouseGrabbed;
5051
const char *DMFCGetString(int d);
5152
// void *x = (void *) DMFCGetString; // just force a reference to dmfc.so ...
5253

53-
char *__orig_pwd = NULL;
54+
std::filesystem::path orig_pwd;
5455

5556
bool linux_permit_gamma = false;
5657

@@ -361,10 +362,11 @@ int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR szCmdLine, int nC
361362
GatherArgs(szCmdLine);
362363
#else
363364
int main(int argc, char *argv[]) {
364-
__orig_pwd = getcwd(NULL, 0);
365365
GatherArgs(argv);
366366
#endif
367367

368+
orig_pwd = std::filesystem::current_path();
369+
368370
/* Setup the logging system */
369371
InitLog();
370372

renderer/HardwareOpenGL.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
#include "byteswap.h"
19+
#include <algorithm>
20+
#include <cstdlib>
21+
#include <cstdio>
22+
#include <cstring>
23+
#include <SDL.h>
24+
2025
#if defined(WIN32)
2126
#include <windows.h>
2227
#endif
2328

29+
#include "byteswap.h"
2430
#include "pserror.h"
2531
#include "mono.h"
2632
#include "3d.h"
@@ -33,15 +39,9 @@
3339
#include "mem.h"
3440
#include "config.h"
3541
#include "rtperformance.h"
36-
#include <stdlib.h>
37-
#include <stdio.h>
38-
#include <string.h>
3942
#include "HardwareInternal.h"
4043
#include "../Descent3/args.h"
41-
#include "lnxscreenmode.h"
42-
#include <SDL.h>
43-
44-
#include <NewBitmap.h>
44+
#include "NewBitmap.h"
4545

4646
#define DECLARE_OPENGL
4747
#include "dyna_gl.h"
@@ -50,8 +50,6 @@
5050
#include "win/arb_extensions.h"
5151
#endif
5252

53-
#include <algorithm>
54-
5553
int FindArg(const char *);
5654
void rend_SetLightingState(light_state state);
5755

@@ -66,9 +64,6 @@ extern uint8_t Renderer_initted;
6664
renderer_type Renderer_type = RENDERER_OPENGL;
6765
int WindowGL = 0;
6866

69-
extern matrix Unscaled_matrix;
70-
extern vector View_position;
71-
7267
#ifndef GL_UNSIGNED_SHORT_5_5_5_1
7368
#define GL_UNSIGNED_SHORT_5_5_5_1 0x8034
7469
#endif
@@ -452,16 +447,17 @@ int opengl_Setup(oeApplication *app, int *width, int *height) {
452447
if (!(OpenGLDLLHandle)) {
453448
// rcg07072000 last ditch effort...
454449
#ifdef __LINUX__
455-
OpenGLDLLHandle = LoadOpenGLDLL("libGL.so.1");
450+
strcpy(gl_library, "libGL.so.1");
456451
#else
457-
OpenGLDLLHandle = LoadOpenGLDLL("opengl32.dll");
452+
strcpy(gl_library, "opengl32.dll");
458453
#endif
454+
OpenGLDLLHandle = LoadOpenGLDLL(gl_library);
459455
if (!(OpenGLDLLHandle)) {
460456
success = false;
461457
}
462458
} // if
463459

464-
if (success == false) {
460+
if (!success) {
465461
char buffer[512];
466462
snprintf(buffer, sizeof(buffer), "Failed to load library [%s].\n", gl_library);
467463
fprintf(stderr, "%s", buffer);

renderer/dyna_gl.h

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818

1919
#pragma once
2020

21+
#include <filesystem>
22+
2123
#if defined(WIN32)
22-
#include <GL/gl.h>
24+
#include <gl/GL.h>
2325
#else
24-
#include "SDL_opengl.h"
26+
#include <SDL_opengl.h>
2527
#endif
2628

29+
#include "descent.h"
2730
#include "module.h"
2831

2932
#if defined(WIN32)
@@ -220,14 +223,14 @@ static module OpenGLDLLInst;
220223
static void *__SDL_mod_GetSymbol(const char *funcStr) {
221224
void *retVal = NULL;
222225

223-
mprintf(0, "Looking up GL function [%s]...", funcStr);
226+
mprintf(0, "Looking up GL function [%s]... ", funcStr);
224227

225228
retVal = SDL_GL_GetProcAddress(funcStr);
226229

227230
if (retVal == NULL)
228231
fprintf(stderr, " Could not find GL symbol [%s]!\n\n", funcStr);
229232
else {
230-
mprintf(0, "Found at (%p).", retVal);
233+
mprintf(0, "Found at (%p).\n", retVal);
231234
} // else
232235

233236
return (retVal);
@@ -237,19 +240,16 @@ static void *__SDL_mod_GetSymbol(const char *funcStr) {
237240
#define mod_GetSymbol(x, funcStr, y) __SDL_mod_GetSymbol(funcStr)
238241
/****************** WARNING: NASTY HACK! ***********************/
239242

240-
241-
#ifdef __LINUX__
242-
extern char *__orig_pwd;
243243
extern char loadedLibrary[_MAX_PATH];
244-
#endif
244+
245245
module *LoadOpenGLDLL(const char *dllname) {
246246
mprintf(0, "Loading OpenGL dll...\n");
247-
#ifdef __LINUX__
248-
char *tmp = getcwd(NULL, 0);
249-
chdir(__orig_pwd);
250-
int rc = SDL_GL_LoadLibrary(dllname[0] ? dllname : NULL);
251-
chdir(tmp);
252-
free(tmp);
247+
248+
std::filesystem::path tmp = std::filesystem::current_path();
249+
std::filesystem::current_path(orig_pwd);
250+
int rc = SDL_GL_LoadLibrary(dllname[0] ? dllname : nullptr);
251+
std::filesystem::current_path(tmp);
252+
253253
if (rc < 0) {
254254
const char *sdlErr = SDL_GetError();
255255
mprintf(0, "OpenGL: Couldn't open library [%s].\n", dllname[0] ? dllname : "system default");
@@ -259,13 +259,6 @@ module *LoadOpenGLDLL(const char *dllname) {
259259

260260
strcpy(loadedLibrary, dllname);
261261

262-
#else
263-
if (!mod_LoadModule(&OpenGLDLLInst, dllname, MODF_LAZY | MODF_GLOBAL)) {
264-
int err = mod_GetLastError();
265-
mprintf(0, "Couldn't open module called %s\n", dllname);
266-
return NULL;
267-
}
268-
#endif
269262

270263
dglAlphaFunc = (glAlphaFunc_fp)mod_GetSymbol(&OpenGLDLLInst, "glAlphaFunc", 255);
271264
if (!dglAlphaFunc)

0 commit comments

Comments
 (0)