Skip to content

Commit 5f9e7c7

Browse files
committed
OpenGL (Windows): simplifies code by removing custom window proc
Adds more cleanups
1 parent aeb1722 commit 5f9e7c7

File tree

1 file changed

+48
-52
lines changed

1 file changed

+48
-52
lines changed

src/detection/opengl/opengl_windows.c

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
typedef struct WGLData
99
{
10-
FFOpenGLResult* result;
11-
const char* error;
12-
1310
FF_LIBRARY_SYMBOL(glGetString)
1411
FF_LIBRARY_SYMBOL(wglMakeCurrent)
1512
FF_LIBRARY_SYMBOL(wglCreateContext)
@@ -18,95 +15,94 @@ typedef struct WGLData
1815

1916
void ffOpenGLHandleResult(FFOpenGLResult* result, __typeof__(&glGetString) ffglGetString);
2017

21-
static const char* wglHandleContext(WGLData* wglData, HDC hdc, HGLRC context)
18+
static const char* wglHandleContext(WGLData* wglData, FFOpenGLResult* result, HDC hdc, HGLRC context)
2219
{
2320
if(wglData->ffwglMakeCurrent(hdc, context) == FALSE)
2421
return "wglMakeCurrent() failed";
25-
ffOpenGLHandleResult(wglData->result, wglData->ffglGetString);
26-
ffStrbufSetStatic(&wglData->result->library, "WGL 1.0");
22+
ffOpenGLHandleResult(result, wglData->ffglGetString);
23+
ffStrbufSetStatic(&result->library, "WGL 1.0");
24+
if(wglData->ffwglMakeCurrent(NULL, NULL) == FALSE)
25+
return "wglMakeCurrent(NULL, NULL) failed";
2726
return NULL;
2827
}
2928

30-
static const char* wglHandlePixelFormat(WGLData* wglData, HWND hWnd)
29+
static const char* wglHandlePixelFormat(WGLData* wglData, FFOpenGLResult* result, HWND hWnd)
3130
{
32-
PIXELFORMATDESCRIPTOR pfd =
33-
{
34-
sizeof(pfd),
35-
1,
36-
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags
37-
PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette.
38-
32, // Colordepth of the framebuffer.
39-
0, 0, 0, 0, 0, 0,
40-
0,
41-
0,
42-
0,
43-
0, 0, 0, 0,
44-
24, // Number of bits for the depthbuffer
45-
8, // Number of bits for the stencilbuffer
46-
0, // Number of Aux buffers in the framebuffer.
47-
PFD_MAIN_PLANE,
48-
0,
49-
0, 0, 0
50-
};
51-
5231
HDC hdc = GetDC(hWnd);
5332

54-
if(SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd) == FALSE)
33+
if(hdc == NULL)
34+
return "GetDC() failed";
35+
36+
PIXELFORMATDESCRIPTOR pfd = {
37+
.nSize = sizeof(PIXELFORMATDESCRIPTOR),
38+
.nVersion = 1,
39+
.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
40+
.iPixelType = PFD_TYPE_RGBA,
41+
.cColorBits = 32,
42+
.cDepthBits = 24,
43+
.iLayerType = PFD_MAIN_PLANE
44+
};
45+
int pixelFormat = ChoosePixelFormat(hdc, &pfd);
46+
if(pixelFormat == 0)
47+
{
48+
ReleaseDC(hWnd, hdc);
49+
return "ChoosePixelFormat() failed";
50+
}
51+
52+
if(SetPixelFormat(hdc, pixelFormat, &pfd) == FALSE)
53+
{
54+
ReleaseDC(hWnd, hdc);
5555
return "SetPixelFormat() failed";
56+
}
5657

5758
HGLRC context = wglData->ffwglCreateContext(hdc);
5859
if(context == NULL)
60+
{
61+
ReleaseDC(hWnd, hdc);
5962
return "wglCreateContext() failed";
63+
}
6064

61-
const char* error = wglHandleContext(wglData, hdc, context);
65+
const char* error = wglHandleContext(wglData, result, hdc, context);
6266
wglData->ffwglDeleteContext(context);
6367

64-
return error;
65-
}
68+
ReleaseDC(hWnd, hdc);
6669

67-
static LRESULT CALLBACK wglHandleWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
68-
{
69-
switch(message)
70-
{
71-
case WM_CREATE: {
72-
WGLData* wglData = (WGLData*)((CREATESTRUCT*)lParam)->lpCreateParams;
73-
wglData->error = wglHandlePixelFormat(wglData, hWnd);
74-
PostQuitMessage(0);
75-
return 0;
76-
}
77-
default:
78-
return DefWindowProcW(hWnd, message, wParam, lParam);
79-
}
70+
return error;
8071
}
8172

8273
static const char* wglDetectOpenGL(FFOpenGLResult* result)
8374
{
8475
FF_LIBRARY_LOAD(opengl32, "dlopen opengl32" FF_LIBRARY_EXTENSION " failed", "opengl32" FF_LIBRARY_EXTENSION, 1);
8576

86-
WGLData data = { .result = result };
77+
WGLData data = {};
8778

8879
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opengl32, data, wglMakeCurrent);
8980
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opengl32, data, wglCreateContext);
9081
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opengl32, data, wglDeleteContext);
9182
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opengl32, data, glGetString);
9283

93-
MSG msg = {0};
84+
HINSTANCE hInstance = GetModuleHandleW(NULL);
85+
9486
WNDCLASSW wc = {
95-
.lpfnWndProc = wglHandleWndProc,
96-
.hInstance = NULL,
87+
.lpfnWndProc = DefWindowProcW,
88+
.hInstance = hInstance,
9789
.hbrBackground = (HBRUSH)COLOR_BACKGROUND,
9890
.lpszClassName = L"ogl_version_check",
9991
.style = CS_OWNDC,
10092
};
10193
if(!RegisterClassW(&wc))
10294
return "RegisterClassW() failed";
10395

104-
HWND hWnd = CreateWindowW(wc.lpszClassName, L"ogl_version_check", 0, 0, 0, FF_OPENGL_BUFFER_WIDTH, FF_OPENGL_BUFFER_HEIGHT, NULL, NULL, NULL, &data);
96+
HWND hWnd = CreateWindowW(wc.lpszClassName, L"ogl_version_check", 0, 0, 0, FF_OPENGL_BUFFER_WIDTH, FF_OPENGL_BUFFER_HEIGHT, NULL, NULL, hInstance, NULL);
97+
if(!hWnd)
98+
return "CreateWindowW() failed";
10599

106-
while(GetMessageW(&msg, hWnd, 0, 0) > 0)
107-
DispatchMessage(&msg);
100+
const char* error = wglHandlePixelFormat(&data, result, hWnd);
108101

109-
return data.error;
102+
DestroyWindow(hWnd);
103+
UnregisterClassW(wc.lpszClassName, hInstance);
104+
105+
return error;
110106
}
111107

112108

0 commit comments

Comments
 (0)