Remove the need to link to opengl32.dll directly.
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m17s

This commit is contained in:
2025-01-27 17:08:51 -05:00
parent 7593c5f2c7
commit 47a9340eff
2 changed files with 38 additions and 10 deletions

View File

@@ -71,15 +71,13 @@ set_target_properties(ReWindow PROPERTIES LINKER_LANGUAGE CXX)
if(UNIX AND NOT APPLE)
target_link_libraries(ReWindow PUBLIC X11 Event jlog)
target_link_libraries(ReWindow PUBLIC)
add_executable(ReWindowDemo main.cpp)
target_link_libraries(ReWindowDemo PUBLIC ReWindow)
endif()
if(WIN32)
find_package(OPENGL REQUIRED)
target_compile_options(ReWindow PUBLIC /utf-8)
target_link_libraries(ReWindow PUBLIC Event jlog ${OPENGL_LIBRARIES})
target_link_libraries(ReWindow PUBLIC Event jlog)
add_executable(ReWindowDemo main.cpp)
target_link_libraries(ReWindowDemo PUBLIC ReWindow)
endif()

View File

@@ -1,5 +1,6 @@
#include <ReWindow/types/Window.h>
#include <ReWindow/data/WindowsEventLoop.h>
#include <ReWindow/Logger.h>
#include <GL/gl.h>
class RWindow::Platform {
@@ -12,8 +13,28 @@ public:
using namespace ReWindow;
HGLRC glContext;
HMODULE gl_lib = nullptr;
bool constructor_success = true;
namespace OpenGL {
typedef const GLubyte* (APIENTRY *GLGETSTRINGPROC)(GLenum name);
GLGETSTRINGPROC glGetString = nullptr;
typedef BOOL (WINAPI *WGLMAKECURRENTPROC)(HDC hdc, HGLRC hglrc);
WGLMAKECURRENTPROC wglMakeCurrent = nullptr;
typedef FARPROC (WINAPI *WGLGETPROCADDRPROC)(LPCSTR lpszProc);
WGLGETPROCADDRPROC wglGetProcAddress = nullptr;
typedef HGLRC (WINAPI *WGLCREATECONTEXTPROC)(HDC hdc);
WGLCREATECONTEXTPROC wglCreateContext = nullptr;
}
bool OpenGLWindow::Open() {
if (!constructor_success)
return false;
platform->hInstance = GetModuleHandle(nullptr);
WNDCLASS wc = { };
@@ -55,17 +76,15 @@ bool OpenGLWindow::Open() {
platform->hdc = GetDC(platform->hwnd);
int pixelFormat = ChoosePixelFormat(platform->hdc, &pfd);
SetPixelFormat(platform->hdc, pixelFormat, &pfd);
glContext = wglCreateContext(platform->hdc);
wglMakeCurrent(platform->hdc, glContext);
glContext = OpenGL::wglCreateContext(platform->hdc);
OpenGL::wglMakeCurrent(platform->hdc, glContext);
ShowWindow(platform->hwnd, SW_SHOW);
open = true;
// TODO don't link directly to OpenGL.
return true;
}
std::string OpenGLWindow::GetGraphicsDriverVendor() {
return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));
return std::string(reinterpret_cast<const char*>(OpenGL::glGetString(GL_VENDOR)));
}
void OpenGLWindow::SwapBuffers() {
@@ -74,12 +93,23 @@ void OpenGLWindow::SwapBuffers() {
void OpenGLWindow::SetVsyncEnabled(bool b) {
typedef BOOL(WINAPI* PFNWGLSWAPINTERVALEXTPROC)(int interval);
auto wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
auto wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) OpenGL::wglGetProcAddress("wglSwapIntervalEXT");
if (wglSwapIntervalEXT)
wglSwapIntervalEXT(b ? 1 : 0);
}
OpenGLWindow::OpenGLWindow(const std::string& title, int width, int height, uint8_t gl_major, uint8_t gl_minor)
: gl_major(gl_major), gl_minor(gl_minor), RWindow(title, width, height) {}
: gl_major(gl_major), gl_minor(gl_minor), RWindow(title, width, height) {
gl_lib = LoadLibrary("opengl32.dll");
if (!gl_lib)
Logger::Error("opengl32.dll couldn't be found. If you have it, you can try putting it in the same directory as the executable."),
constructor_success = false;
OpenGL::glGetString = (OpenGL::GLGETSTRINGPROC) GetProcAddress(gl_lib, "glGetString");
OpenGL::wglMakeCurrent = (OpenGL::WGLMAKECURRENTPROC) GetProcAddress(gl_lib, "wglMakeCurrent");
OpenGL::wglGetProcAddress = (OpenGL::WGLGETPROCADDRPROC) GetProcAddress(gl_lib, "wglGetProcAddress");
OpenGL::wglCreateContext = (OpenGL::WGLCREATECONTEXTPROC) GetProcAddress(gl_lib, "wglCreateContext");
}