7e587776a774ccd1effd145d336c91cd65a3b261
[reactos.git] / reactos / dll / win32 / gdi32 / misc / wingl.c
1 /*
2 * ReactOS Gdi32
3 * Copyright (C) 2003 ReactOS Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 *
20 *
21 *
22 */
23
24 #include "precomp.h"
25
26 #define NDEBUG
27 #include <debug.h>
28
29
30
31 typedef int WINAPI (*CHOOSEPIXELFMT) (HDC, CONST PIXELFORMATDESCRIPTOR *);
32 typedef BOOL WINAPI (*SETPIXELFMT) (HDC, int, CONST PIXELFORMATDESCRIPTOR *);
33 typedef BOOL WINAPI (*SWAPBUFFERS) (HDC hdc);
34 typedef int WINAPI (*DESCRIBEPIXELFMT) (HDC, int, UINT, LPPIXELFORMATDESCRIPTOR);
35 typedef int WINAPI (*GETPIXELFMT) (HDC);
36
37
38 static CHOOSEPIXELFMT glChoosePixelFormat = NULL;
39 static SETPIXELFMT glSetPixelFormat = NULL;
40 static SWAPBUFFERS glSwapBuffers = NULL;
41 static DESCRIBEPIXELFMT glDescribePixelFormat = NULL;
42 static GETPIXELFMT glGetPixelFormat = NULL;
43
44 /*
45 OpenGL Handle.
46 */
47 HINSTANCE hOpenGL = NULL;
48
49 static BOOL OpenGLInitFunction(PCSTR name,
50 FARPROC *funcptr)
51 {
52 PVOID func;
53
54 func = (PVOID)GetProcAddress(hOpenGL, name);
55 if (func != NULL)
56 {
57 (void)InterlockedCompareExchangePointer((PVOID*)funcptr,
58 func,
59 NULL);
60 return TRUE;
61 }
62
63 return FALSE;
64 }
65
66 static BOOL OpenGLEnable(void)
67 {
68 HMODULE hModOpengl32;
69 BOOL Ret = TRUE;
70
71 hModOpengl32 = LoadLibraryW(L"OPENGL32.DLL");
72 if (hModOpengl32 == NULL)
73 return FALSE;
74
75 if (InterlockedCompareExchangePointer((PVOID*)&hOpenGL,
76 (PVOID)hModOpengl32,
77 NULL) != NULL)
78 {
79 FreeLibrary(hModOpengl32);
80
81 /* NOTE: Even though another thread was faster loading the
82 library we can't just bail out here. We really need
83 to *try* to locate every function. This is slow but
84 thread-safe */
85 }
86
87
88 if (!OpenGLInitFunction("wglChoosePixelFormat", &glChoosePixelFormat))
89 Ret = FALSE;
90
91 if (!OpenGLInitFunction("wglSetPixelFormat", &glSetPixelFormat))
92 Ret = FALSE;
93
94 if (!OpenGLInitFunction("wglSwapBuffers", &glSwapBuffers))
95 Ret = FALSE;
96
97 if (!OpenGLInitFunction("wglDescribePixelFormat", &glDescribePixelFormat))
98 Ret = FALSE;
99
100 if (!OpenGLInitFunction("wglGetPixelFormat", &glGetPixelFormat))
101 Ret = FALSE;
102
103 return Ret;
104 }
105
106
107
108 /*
109 * @implemented
110 */
111 INT
112 WINAPI
113 ChoosePixelFormat(HDC hdc,
114 CONST PIXELFORMATDESCRIPTOR * ppfd)
115 {
116 if (glChoosePixelFormat == NULL)
117 if (OpenGLEnable() == FALSE)
118 return(0);
119
120 return(glChoosePixelFormat(hdc, ppfd));
121 }
122
123
124
125 /*
126 * @implemented
127 */
128 INT
129 WINAPI
130 DescribePixelFormat(HDC hdc,
131 INT iPixelFormat,
132 UINT nBytes,
133 LPPIXELFORMATDESCRIPTOR ppfd)
134 {
135 if (glDescribePixelFormat == NULL)
136 if (OpenGLEnable() == FALSE)
137 return(0);
138
139 return(glDescribePixelFormat(hdc, iPixelFormat, nBytes, ppfd));
140 }
141
142
143
144 /*
145 * @implemented
146 */
147 INT
148 WINAPI
149 GetPixelFormat(HDC hdc)
150 {
151 if (glGetPixelFormat == NULL)
152 if (OpenGLEnable() == FALSE)
153 return(0);
154
155 return(glGetPixelFormat(hdc));
156 }
157
158
159
160 /*
161 * @implemented
162 */
163 BOOL
164 WINAPI
165 SetPixelFormat(HDC hdc,
166 INT iPixelFormat,
167 CONST PIXELFORMATDESCRIPTOR * ppfd)
168 {
169 if (glSetPixelFormat == NULL)
170 if (OpenGLEnable() == FALSE)
171 return(0);
172
173 return(glSetPixelFormat(hdc, iPixelFormat, ppfd));
174 }
175
176
177
178 /*
179 * @implemented
180 */
181 BOOL
182 WINAPI
183 SwapBuffers(HDC hdc)
184 {
185 if (glSwapBuffers == NULL)
186 if (OpenGLEnable() == FALSE)
187 return(0);
188
189
190 return(glSwapBuffers(hdc));
191 }
192
193
194 /*
195 Do this here for now.
196 */
197
198 /*
199 * @implemented
200 */
201 UINT
202 WINAPI
203 GetEnhMetaFilePixelFormat(
204 HENHMETAFILE hemf,
205 UINT cbBuffer,
206 PIXELFORMATDESCRIPTOR *ppfd
207 )
208 {
209 ENHMETAHEADER pemh;
210
211 if(GetEnhMetaFileHeader(hemf, sizeof(ENHMETAHEADER), &pemh))
212 {
213 if(pemh.bOpenGL)
214 {
215 if(pemh.cbPixelFormat)
216 {
217 memcpy((void*)ppfd, UlongToPtr(pemh.offPixelFormat), cbBuffer );
218 return(pemh.cbPixelFormat);
219 }
220 }
221 }
222 return(0);
223 }
224
225 /* EOF */