Skip to content

Commit

Permalink
Implement optional GLEW support and make graphics utilize it.
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamyCecil committed Nov 27, 2023
1 parent 2e57cf9 commit a1ef45f
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 8 deletions.
10 changes: 10 additions & 0 deletions Sources/Engine/Base/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define SE1_TRUFORM 0
#endif

// GLEW support
#ifndef SE1_GLEW
#define SE1_GLEW 0
#endif

// Utilize GLEW instead of direct OpenGL hooking
#ifndef SE1_USE_GLEW
#define SE1_USE_GLEW 1
#endif

// Don't prioritize SDL functionality over Windows API by default
#if SE1_WIN
#ifndef SE1_SDL
Expand Down
1 change: 0 additions & 1 deletion Sources/Engine/Base/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#endif

#include <Engine/Base/Base.h>
#include <Engine/Graphics/gl_types.h>

// Unsigned integers
typedef unsigned char UBYTE;
Expand Down
9 changes: 9 additions & 0 deletions Sources/Engine/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma comment(lib, "SDL2.lib")
#endif

// [Cecil] GLEW: Include glew in its entirety
#if SE1_GLEW
#include <glew.h>
#include <wglew.h>

#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "opengl32.lib")
#endif

// Engine base
#include <Engine/Base/Base.h>
#include <Engine/Base/Types.h>
Expand Down
8 changes: 4 additions & 4 deletions Sources/Engine/Engine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
</PreBuildEvent>
<PostBuildEvent>
<Message>Copying $(ProjectName) binaries into Bin</Message>
<Command>copy "$(OutDir)$(TargetFileName)" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(SDLLibPath)SDL2.dll" "$(PostBuildCopyDir)" &gt;nul</Command>
<Command>copy "$(OutDir)$(TargetFileName)" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(SDLLibPath)SDL2.dll" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(GLEWBinPath)glew32.dll" "$(PostBuildCopyDir)" &gt;nul</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand Down Expand Up @@ -190,7 +190,7 @@
</PreBuildEvent>
<PostBuildEvent>
<Message>Copying $(ProjectName) binaries into Bin</Message>
<Command>copy "$(OutDir)$(TargetFileName)" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(SDLLibPath)SDL2.dll" "$(PostBuildCopyDir)" &gt;nul</Command>
<Command>copy "$(OutDir)$(TargetFileName)" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(SDLLibPath)SDL2.dll" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(GLEWBinPath)glew32.dll" "$(PostBuildCopyDir)" &gt;nul</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand Down Expand Up @@ -238,7 +238,7 @@
</PreBuildEvent>
<PostBuildEvent>
<Message>Copying $(ProjectName) binaries into Bin</Message>
<Command>copy "$(OutDir)$(TargetFileName)" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(SDLLibPath)SDL2.dll" "$(PostBuildCopyDir)" &gt;nul</Command>
<Command>copy "$(OutDir)$(TargetFileName)" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(SDLLibPath)SDL2.dll" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(GLEWBinPath)glew32.dll" "$(PostBuildCopyDir)" &gt;nul</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down Expand Up @@ -284,7 +284,7 @@
</PreBuildEvent>
<PostBuildEvent>
<Message>Copying $(ProjectName) binaries into Bin</Message>
<Command>copy "$(OutDir)$(TargetFileName)" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(SDLLibPath)SDL2.dll" "$(PostBuildCopyDir)" &gt;nul</Command>
<Command>copy "$(OutDir)$(TargetFileName)" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(SDLLibPath)SDL2.dll" "$(PostBuildCopyDir)" &gt;nul &amp;&amp; copy "$(GLEWBinPath)glew32.dll" "$(PostBuildCopyDir)" &gt;nul</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
77 changes: 77 additions & 0 deletions Sources/Engine/Graphics/GfxLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,78 @@ static void PrepareTables(void)
}
}

#if SE1_GLEW

// [Cecil] GLEW: Initialize GLEW by creating a temporary context
static void InitGLEW(void) {
// Create window and context for it
#if SE1_SDL
SDL_Window *pWnd = SDL_CreateWindow("temp_glew_init", 0, 0, 0, 0, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);

if (pWnd == NULL) {
FatalError(TRANS("Window could not be created! SDL Error:\n%s"), SDL_GetError());
}

SDL_GLContext pContext = SDL_GL_CreateContext(pWnd);

if (pContext == NULL) {
SDL_DestroyWindow(pWnd);
FatalError(TRANS("OpenGL context could not be created! SDL Error:\n%s"), SDL_GetError());
}

#else
HINSTANCE hInstance = GetModuleHandleA(NULL);

WNDCLASSA wc = { 0 };
wc.lpfnWndProc = &DefWindowProcA;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
wc.lpszClassName = "temp_glew_init";
wc.style = CS_OWNDC;

if (!RegisterClassA(&wc)) {
FatalError(TRANS("Error initializing GLEW:\n%s"), "Unable to register window class");
}

HWND hwnd = CreateWindowExA(NULL, wc.lpszClassName, "temp_glew_init", WS_POPUP, 0, 0, 0, 0, 0, 0, hInstance, 0);

PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 8, 0, PFD_MAIN_PLANE, 0, 0, 0, 0
};

HDC hdc = GetDC(hwnd);
SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd);

HGLRC hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hglrc);
#endif

// Initialize GLEW
glewExperimental = GL_TRUE;
GLenum eError = glewInit();

if (eError != GLEW_OK) {
FatalError(TRANS("Error initializing GLEW:\n%s"), glewGetErrorString(eError));
}

// Clear internal errors
glGetError();

// Cleanup
#if SE1_SDL
SDL_GL_DeleteContext(pContext);
SDL_DestroyWindow(pWnd);

#else
wglDeleteContext(hglrc); // Resets current context as well
ReleaseDC(hwnd, hdc);
DestroyWindow(hwnd);
UnregisterClassA(wc.lpszClassName, hInstance);
#endif
};

#endif

/*
* Construct uninitialized gfx library.
Expand All @@ -966,6 +1038,11 @@ CGfxLibrary::CGfxLibrary(void)
// create some internal tables
PrepareTables();

#if SE1_GLEW
// [Cecil] GLEW: Initialize
InitGLEW();
#endif

// no driver loaded
gl_pInterface = NULL; // [Cecil]
gl_hiDriver = NONE;
Expand Down
49 changes: 47 additions & 2 deletions Sources/Engine/Graphics/Gfx_OpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,60 @@ static void FailFunction_t(const char *strName) {
ThrowF_t(TRANS("Required function %s not found."), strName);
}

// [Cecil] Wrappers for non-existent GLEW methods
#if SE1_GLEW && SE1_USE_GLEW

static HINSTANCE _hiForWGL;

static BOOL wglSwapBuffers(HDC hdc) {
typedef BOOL (*CSwapBuffers)(HDC);
static CSwapBuffers pFunc = (CSwapBuffers)OS::GetLibSymbol(_hiForWGL, "wglSwapBuffers");

return pFunc(hdc);
};

static BOOL wglSetPixelFormat(HDC hdc, int iFormat, const PIXELFORMATDESCRIPTOR *ppfd) {
typedef BOOL (*CSetPixelFormat)(HDC, int, const PIXELFORMATDESCRIPTOR *);
static CSetPixelFormat pFunc = (CSetPixelFormat)OS::GetLibSymbol(_hiForWGL, "wglSetPixelFormat");

return pFunc(hdc, iFormat, ppfd);
};

static int wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR *ppfd) {
typedef int (*CChoosePixelFormat)(HDC, const PIXELFORMATDESCRIPTOR *);
static CChoosePixelFormat pFunc = (CChoosePixelFormat)OS::GetLibSymbol(_hiForWGL, "wglChoosePixelFormat");

return pFunc(hdc, ppfd);
};

static int wglDescribePixelFormat(HDC hdc, int iFormat, UINT ctPfdSize, LPPIXELFORMATDESCRIPTOR ppfd) {
typedef int (*CDescribePixelFormat)(HDC, int, UINT, LPPIXELFORMATDESCRIPTOR);
static CDescribePixelFormat pFunc = (CDescribePixelFormat)OS::GetLibSymbol(_hiForWGL, "wglDescribePixelFormat");

return pFunc(hdc, iFormat, ctPfdSize, ppfd);
};

#endif

static void OGL_SetFunctionPointers_t(HINSTANCE hiOGL)
{
const char *strName;
// get gl function pointers
#if SE1_GLEW && SE1_USE_GLEW
_hiForWGL = hiOGL;

#define DLLFUNCTION(dll, output, name, inputs, params, required) \
strName = #name; \
p##name = (output (__stdcall *)inputs)OS::GetLibSymbol(hi##dll, strName); \
p##name = (output (__stdcall *)inputs)&name; \
if( required && p##name == NULL) FailFunction_t(strName);

#else
#define DLLFUNCTION(dll, output, name, inputs, params, required) \
strName = #name; \
p##name = (output (__stdcall *)inputs)OS::GetLibSymbol(hiOGL, strName); \
if( required && p##name == NULL) FailFunction_t(strName);
#endif

#include "gl_functions.h"
#undef DLLFUNCTION
}
Expand Down Expand Up @@ -554,7 +599,7 @@ void CGfxLibrary::InitContext_OGL(void)
pglActiveTextureARB = NULL;
pglClientActiveTextureARB = NULL;
if (HasExtension(go_strExtensions.ConstData(), "GL_ARB_multitexture")) {
pglGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, (int*)&gl_ctRealTextureUnits); // get number of texture units
pglGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, (GLint *)&gl_ctRealTextureUnits); // get number of texture units
if (gl_ctRealTextureUnits > 1 && HasExtension(go_strExtensions.ConstData(), "GL_EXT_texture_env_combine")) {
AddExtension_OGL( NONE, "GL_ARB_multitexture");
AddExtension_OGL( NONE, "GL_EXT_texture_env_combine");
Expand Down
4 changes: 3 additions & 1 deletion Sources/Engine/Graphics/OpenGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#endif

#include "gl_types.h"
#if !SE1_GLEW
#include "gl_types.h"
#endif

/* rcg10042001 wraped for platform. */
#if SE1_WIN
Expand Down

0 comments on commit a1ef45f

Please sign in to comment.