9typedef HGLRC(WINAPI *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC,
const int *);
10typedef const char *(WINAPI *PFNWGLGETEXTENSIONSSTRINGARBPROC)(HDC);
12#ifndef WGL_CONTEXT_MAJOR_VERSION_ARB
13#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
15#ifndef WGL_CONTEXT_MINOR_VERSION_ARB
16#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
18#ifndef WGL_CONTEXT_FLAGS_ARB
19#define WGL_CONTEXT_FLAGS_ARB 0x2094
21#ifndef WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
22#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
24#ifndef WGL_CONTEXT_PROFILE_MASK_ARB
25#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
27#ifndef WGL_CONTEXT_CORE_PROFILE_BIT_ARB
28#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
31static HMODULE g_opengl32 =
nullptr;
33static void *win32GetGLProc(
const char *name)
35 auto *proc = (
void *)wglGetProcAddress(name);
37 if (proc ==
nullptr || proc ==
reinterpret_cast<void *
>(0x1) || proc ==
reinterpret_cast<void *
>(0x2) ||
38 proc ==
reinterpret_cast<void *
>(0x3) || proc ==
reinterpret_cast<void *
>(-1))
40 if (g_opengl32 ==
nullptr)
42 g_opengl32 = LoadLibraryA(
"opengl32.dll");
45 proc = (
void *)GetProcAddress(g_opengl32, name);
51cae::WGLContext::~WGLContext()
53 if (m_hglrc !=
nullptr)
55 wglMakeCurrent(
nullptr,
nullptr);
56 wglDeleteContext(m_hglrc);
58 if ((m_hdc !=
nullptr) && (m_hwnd !=
nullptr))
60 ReleaseDC(m_hwnd, m_hdc);
64void cae::WGLContext::initialize(
const NativeWindowHandle &window)
66 m_hwnd =
static_cast<HWND
>(window.window);
67 m_hdc = GetDC(m_hwnd);
70 throw std::runtime_error(
"Failed to get HDC from HWND");
73 PIXELFORMATDESCRIPTOR pfd{};
74 pfd.nSize =
sizeof(pfd);
76 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
77 pfd.iPixelType = PFD_TYPE_RGBA;
80 pfd.iLayerType = PFD_MAIN_PLANE;
82 const int pf = ChoosePixelFormat(m_hdc, &pfd);
85 throw std::runtime_error(
"Failed to choose pixel format");
87 if (SetPixelFormat(m_hdc, pf, &pfd) == 0)
89 throw std::runtime_error(
"Failed to set pixel format");
92 const HGLRC tempContext = wglCreateContext(m_hdc);
93 if (tempContext ==
nullptr)
95 throw std::runtime_error(
"Failed to create temporary WGL context");
97 if (wglMakeCurrent(m_hdc, tempContext) == 0)
99 throw std::runtime_error(
"Failed to make temporary context current");
102 const auto wglCreateContextAttribsARB =
103 reinterpret_cast<PFNWGLCREATECONTEXTATTRIBSARBPROC
>(wglGetProcAddress(
"wglCreateContextAttribsARB"));
105 if (wglCreateContextAttribsARB !=
nullptr)
107 const int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB,
109 WGL_CONTEXT_MINOR_VERSION_ARB,
111 WGL_CONTEXT_FLAGS_ARB,
112 WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
113 WGL_CONTEXT_PROFILE_MASK_ARB,
114 WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
117 const HGLRC modernContext = wglCreateContextAttribsARB(m_hdc,
nullptr, attribs);
118 if (modernContext ==
nullptr)
120 throw std::runtime_error(
"Failed to create modern WGL context");
123 wglMakeCurrent(
nullptr,
nullptr);
124 wglDeleteContext(tempContext);
126 m_hglrc = modernContext;
127 if (wglMakeCurrent(m_hdc, m_hglrc) == 0)
129 throw std::runtime_error(
"Failed to make modern WGL context current");
135 m_hglrc = tempContext;
138 if (wglGetCurrentContext() != m_hglrc)
140 throw std::runtime_error(
"Current WGL context is not the one just created");
142 if (
const int version = gladLoadGLContext(&gl,
reinterpret_cast<GLADloadfunc
>(win32GetGLProc)); version == 0)
144 throw std::runtime_error(
"Failed to initialize GLAD MX (Windows)");
146 if (gl.Enable !=
nullptr)
148 gl.Enable(GL_DEBUG_OUTPUT);
150 gl.DebugMessageCallback([](GLenum source, GLenum type, GLuint
id, GLenum severity, GLsizei length,
151 const GLchar *message,
const void *userParam)
158void cae::WGLContext::swapBuffers()
160 if (m_hdc !=
nullptr)
166void cae::WGLContext::setVSyncEnabled(
const bool enabled)
168 using PFNWGLSWAPINTERVALEXTPROC = BOOL(WINAPI *)(int);
169 static auto wglSwapIntervalEXT =
170 reinterpret_cast<PFNWGLSWAPINTERVALEXTPROC
>(wglGetProcAddress(
"wglSwapIntervalEXT"));
171 if (wglSwapIntervalEXT !=
nullptr)
173 wglSwapIntervalEXT(enabled ? 1 : 0);
This file contains the Logger class.
This file contains the WGLContext class declaration.
static void log(const std::string &message, const LogLevel &logLevel)
Log a message with a specific log level.