7
7
8
8
typedef struct WGLData
9
9
{
10
- FFOpenGLResult * result ;
11
- const char * error ;
12
-
13
10
FF_LIBRARY_SYMBOL (glGetString )
14
11
FF_LIBRARY_SYMBOL (wglMakeCurrent )
15
12
FF_LIBRARY_SYMBOL (wglCreateContext )
@@ -18,95 +15,94 @@ typedef struct WGLData
18
15
19
16
void ffOpenGLHandleResult (FFOpenGLResult * result , __typeof__ (& glGetString ) ffglGetString );
20
17
21
- static const char * wglHandleContext (WGLData * wglData , HDC hdc , HGLRC context )
18
+ static const char * wglHandleContext (WGLData * wglData , FFOpenGLResult * result , HDC hdc , HGLRC context )
22
19
{
23
20
if (wglData -> ffwglMakeCurrent (hdc , context ) == FALSE)
24
21
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" ;
27
26
return NULL ;
28
27
}
29
28
30
- static const char * wglHandlePixelFormat (WGLData * wglData , HWND hWnd )
29
+ static const char * wglHandlePixelFormat (WGLData * wglData , FFOpenGLResult * result , HWND hWnd )
31
30
{
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
-
52
31
HDC hdc = GetDC (hWnd );
53
32
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 );
55
55
return "SetPixelFormat() failed" ;
56
+ }
56
57
57
58
HGLRC context = wglData -> ffwglCreateContext (hdc );
58
59
if (context == NULL )
60
+ {
61
+ ReleaseDC (hWnd , hdc );
59
62
return "wglCreateContext() failed" ;
63
+ }
60
64
61
- const char * error = wglHandleContext (wglData , hdc , context );
65
+ const char * error = wglHandleContext (wglData , result , hdc , context );
62
66
wglData -> ffwglDeleteContext (context );
63
67
64
- return error ;
65
- }
68
+ ReleaseDC (hWnd , hdc );
66
69
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 ;
80
71
}
81
72
82
73
static const char * wglDetectOpenGL (FFOpenGLResult * result )
83
74
{
84
75
FF_LIBRARY_LOAD (opengl32 , "dlopen opengl32" FF_LIBRARY_EXTENSION " failed" , "opengl32" FF_LIBRARY_EXTENSION , 1 );
85
76
86
- WGLData data = { . result = result };
77
+ WGLData data = {};
87
78
88
79
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE (opengl32 , data , wglMakeCurrent );
89
80
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE (opengl32 , data , wglCreateContext );
90
81
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE (opengl32 , data , wglDeleteContext );
91
82
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE (opengl32 , data , glGetString );
92
83
93
- MSG msg = {0 };
84
+ HINSTANCE hInstance = GetModuleHandleW (NULL );
85
+
94
86
WNDCLASSW wc = {
95
- .lpfnWndProc = wglHandleWndProc ,
96
- .hInstance = NULL ,
87
+ .lpfnWndProc = DefWindowProcW ,
88
+ .hInstance = hInstance ,
97
89
.hbrBackground = (HBRUSH )COLOR_BACKGROUND ,
98
90
.lpszClassName = L"ogl_version_check" ,
99
91
.style = CS_OWNDC ,
100
92
};
101
93
if (!RegisterClassW (& wc ))
102
94
return "RegisterClassW() failed" ;
103
95
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" ;
105
99
106
- while (GetMessageW (& msg , hWnd , 0 , 0 ) > 0 )
107
- DispatchMessage (& msg );
100
+ const char * error = wglHandlePixelFormat (& data , result , hWnd );
108
101
109
- return data .error ;
102
+ DestroyWindow (hWnd );
103
+ UnregisterClassW (wc .lpszClassName , hInstance );
104
+
105
+ return error ;
110
106
}
111
107
112
108
0 commit comments