source: Daodan/src/Patches/GL.c@ 1024

Last change on this file since 1024 was 1000, checked in by alloc, 11 years ago

Daodan 3.7

File size: 11.9 KB
RevLine 
[994]1#include <windows.h>
2#include <math.h>
3#include <GL/gl.h>
4#include <GL/glu.h>
5
6#include "../Oni/Oni.h"
7
8#include "../Daodan_Config.h"
9#include "Utility.h"
10#include "Win32.h"
11#include "GL.h"
12
13static const M3tDisplayMode daodan_reslist[] =
14{
15 { 640, 480, 0, 0 },
16 { 720, 480, 0, 0 },
17 { 720, 576, 0, 0 },
18 { 768, 480, 0, 0 },
19 { 800, 480, 0, 0 },
20 { 800, 600, 0, 0 },
21 { 852, 480, 0, 0 },
22 { 856, 480, 0, 0 },
23 { 960, 540, 0, 0 },
24 { 960, 720, 0, 0 },
25 { 1024, 576, 0, 0 },
26 { 1024, 600, 0, 0 },
27 { 1024, 640, 0, 0 },
28 { 1024, 768, 0, 0 },
29 { 1152, 768, 0, 0 },
30 { 1152, 864, 0, 0 },
31 { 1280, 720, 0, 0 },
32 { 1280, 768, 0, 0 },
33 { 1280, 800, 0, 0 },
34 { 1280, 960, 0, 0 },
35 { 1280, 1024, 0, 0 },
36 { 1366, 768, 0, 0 },
37 { 1400, 1050, 0, 0 },
38 { 1440, 900, 0, 0 },
39 { 1600, 900, 0, 0 },
40 { 1600, 1200, 0, 0 },
41 { 1920, 1080, 0, 0 },
42 { 1920, 1200, 0, 0 },
43 { 1920, 1440, 0, 0 },
44};
45
46static DWORD window_style, window_exstyle;
47
48// HACK: use additional device entries to store display modes. It would give us
49// 67 mode slots total (far more than enough). I absolutely have no idea where
50// Rossy got his 104 (it would take up to 0x660 bytes while the whole GL state
51// is only 0x63c bytes). Maybe it was just octal (67 + 1).
52// This hack would break (crash!) "m3_display_list" script command.
53#define DD_MAX_MODES ((offsetof(M3tDrawEngineCaps,__unknown) - \
54 offsetof(M3tDrawEngineCaps,DisplayDevices) - \
55 offsetof(M3tDisplayDevice,Modes)) / sizeof(M3tDisplayMode))
56
57// Former daodan_resdepths.
58#define DD_MIN_DEPTH 16
59
60unsigned short ONICALL DD_GLrEnumerateDisplayModes(M3tDisplayMode* modes)
61{
62 unsigned int vmodes = 0;
63 unsigned int screen_x = GetSystemMetrics(SM_CXSCREEN);
64 unsigned int screen_y = GetSystemMetrics(SM_CYSCREEN);
65
66 unsigned int i;
67 signed int j;
68
69 STARTUPMESSAGE("Listing display modes", 0);
70
71 memset(modes, 0, sizeof(M3tDisplayMode) * DD_MAX_MODES);
72
73 if (M3gResolutionSwitch)
74 {
75 // Enumerate in -switch mode. "67 slots ought to be enough for anybody".
76
77 DEVMODE dm;
78
79 dm.dmSize = sizeof(dm);
80 dm.dmDriverExtra = 0;
81
82 for (i = 0; EnumDisplaySettings(NULL, i, &dm); ++i)
83 {
84 if (dm.dmBitsPerPel < DD_MIN_DEPTH || dm.dmPelsWidth < 640 || dm.dmPelsHeight < 480)
85 continue;
86 if (dm.dmPelsWidth < dm.dmPelsHeight)
87 continue;
88
89 // Already exists? Search backwards as modes are sorted most of the times
90 for (j = vmodes - 1; j >= 0; --j)
91 if (modes[j].Width == dm.dmPelsWidth && modes[j].Height == dm.dmPelsHeight &&
92 modes[j].Depth == dm.dmBitsPerPel)
93 break;
94
95 if (j >= 0)
96 continue; // We've found a match.
97
98 modes[vmodes].Width = dm.dmPelsWidth;
99 modes[vmodes].Height = dm.dmPelsHeight;
100 modes[vmodes].Depth = dm.dmBitsPerPel;
101
102 if (++vmodes >= DD_MAX_MODES)
103 break;
104 }
105 }
106 else
107 {
108 // In -noswitch we put predefined window sizes which don't overlap
109 // deskbar(s) plus one "native" fullscreen mode.
110
111 unsigned int workarea_width, workarea_height, frame_width, frame_height;
112 DWORD style, exstyle;
113 RECT rc;
114
115 if (SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0))
116 {
117 workarea_width = rc.right - rc.left;
118 workarea_height = rc.bottom - rc.top;
119 }
120 else
121 {
122 workarea_width = screen_x;
123 workarea_height = screen_y;
124 }
125
126 style = (DWORD) GetWindowLongPtr(ONgPlatformData.Window, GWL_STYLE);
127 exstyle = (DWORD) GetWindowLongPtr(ONgPlatformData.Window, GWL_EXSTYLE);
128
129 // Calculate additional width and height for window borders. Don't
130 // bother with system metrics. Let Windows calculate this.
131 rc.left = rc.top = 0;
132 rc.right = rc.bottom = 300;
133
134 if (AdjustWindowRectEx(&rc, style, FALSE, exstyle))
135 {
136 frame_width = rc.right - rc.left - 300;
137 frame_height = rc.bottom - rc.top - 300;
138 }
139 else
140 {
141 frame_width = 0;
142 frame_height = 0;
143 }
144
145 for (i = 0; i < sizeof(daodan_reslist) / sizeof(daodan_reslist[0]); ++i)
146 {
147 // Don't check the mode which has the same rect as screen. We would
148 // add it later as a special case.
149 if (daodan_reslist[i].Width == screen_x && daodan_reslist[i].Height == screen_y)
150 continue;
151
152 if (daodan_reslist[i].Width + frame_width <= workarea_width &&
153 daodan_reslist[i].Height + frame_height <= workarea_height)
154 {
155 modes[vmodes] = daodan_reslist[i];
156 modes[vmodes].Depth = GLgInitialMode.dmBitsPerPel;
157
158 if (++vmodes >= DD_MAX_MODES)
159 {
160 --vmodes; // Remove the last mode to make room for "fullscreen" mode.
161 break;
162 }
163 }
164 }
165
166 modes[vmodes].Width = GLgInitialMode.dmPelsWidth;
167 modes[vmodes].Height = GLgInitialMode.dmPelsHeight;
168 modes[vmodes].Depth = GLgInitialMode.dmBitsPerPel;
169 ++vmodes;
170 }
171
172 STARTUPMESSAGE("%u modes available:", vmodes);
173 for (i = 0; i < vmodes; ++i)
174 STARTUPMESSAGE(" %ux%ux%u", modes[i].Width, modes[i].Height, modes[i].Depth);
175
176 return vmodes;
177}
178
179// Sets a new display mode (if it is somehow different from a current mode).
180// NOTE: signature for this function was changed to simplify code.
181UUtBool DD_GLrPlatform_SetDisplayMode(M3tDisplayMode* mode)
182{
183 if (mode->Height < 480)
184 return UUcFalse;
185
186 if (M3gResolutionSwitch)
187 {
188 DEVMODE new_mode, cur_mode;
189
190 cur_mode.dmSize = sizeof(cur_mode);
191 cur_mode.dmDriverExtra = 0;
192
193 // We don't need this check. Windows does this too (see CDS_RESET).
194 if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &cur_mode) ||
195 cur_mode.dmPelsWidth != mode->Width || cur_mode.dmPelsHeight != mode->Height ||
196 cur_mode.dmBitsPerPel != mode->Depth)
197 {
198 new_mode.dmSize = sizeof(new_mode);
199 new_mode.dmFields = DM_BITSPERPEL | DM_PELSHEIGHT | DM_PELSWIDTH;
200 new_mode.dmPelsWidth = mode->Width;
201 new_mode.dmPelsHeight = mode->Height;
202 new_mode.dmBitsPerPel = mode->Depth;
203
204 if (ChangeDisplaySettings(&new_mode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
205 return UUcFalse;
206 }
207
208 // We didn't change window size in DD_GLrPlatform_Initialize so we need
209 // to change it here.
210 SetWindowPos(ONgPlatformData.Window, NULL, 0, 0, mode->Width, mode->Height,
211 SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_NOZORDER);
212
213 return UUcTrue;
214 }
215 else
216 {
217 unsigned screen_x, screen_y;
218 DWORD style, exstyle, new_style, new_exstyle;
219 DWORD flags;
220 RECT rc, workarea_rc;
221 POINT pt;
222
223 screen_x = GetSystemMetrics(SM_CXSCREEN);
224 screen_y = GetSystemMetrics(SM_CYSCREEN);
225
226 GetClientRect(ONgPlatformData.Window, &rc);
227
228 // Don't do anything if the mode was not changed.
229 if (rc.right == mode->Width && rc.bottom == mode->Height)
230 return UUcTrue;
231
232 style = (DWORD) GetWindowLongPtr(ONgPlatformData.Window, GWL_STYLE);
233 exstyle = (DWORD) GetWindowLongPtr(ONgPlatformData.Window, GWL_EXSTYLE);
234 flags = SWP_NOACTIVATE | SWP_NOZORDER;
235
236 // Remember initial window style to correctly restore from fullscreen.
237 if (window_style == 0)
238 {
239 window_style = style;
240 window_exstyle = exstyle;
241 }
242
243 if (mode->Width == screen_x && mode->Height == screen_y)
244 {
245 // "Fullscreen" mode.
246 new_exstyle = exstyle & ~(WS_EX_CLIENTEDGE | WS_EX_DLGMODALFRAME | WS_EX_STATICEDGE | WS_EX_WINDOWEDGE);
247 new_style = style & ~(WS_CAPTION | WS_BORDER | WS_THICKFRAME | WS_DLGFRAME);
248 new_style = new_style | WS_POPUP;
249 rc.left = 0;
250 rc.top = 0;
251 rc.right = mode->Width;
252 rc.bottom = mode->Height;
253 }
254 else
255 {
[1000]256 ConfigOption_t* co = DDrConfig_GetOptOfType("windows.border", C_BOOL);
[994]257 if (co && co->value.intBoolVal)
258 {
259 pt.x = rc.left;
260 pt.y = rc.top;
261 ClientToScreen(ONgPlatformData.Window, &pt);
262 }
263 else
264 {
265 pt.x = screen_x / 2 - mode->Width / 2;
266 pt.y = screen_y / 2 - mode->Height / 2;
267 }
268
269 new_exstyle = window_exstyle;
270 new_style = window_style;
271 rc.left = pt.x;
272 rc.top = pt.y;
273 rc.right = rc.left + mode->Width;
274 rc.bottom = rc.top + mode->Height;
275
276 AdjustWindowRectEx(&rc, new_style, FALSE, new_exstyle);
277
278 // Convert to width and height.
279 rc.right -= rc.left;
280 rc.bottom -= rc.top;
281
282 if (SystemParametersInfo(SPI_GETWORKAREA, 0, &workarea_rc, 0))
283 {
284 // We try to keep window position, but we should prevent window
285 // from going off screen.
286
287 if (rc.left + rc.right > workarea_rc.right)
288 rc.left = workarea_rc.right - rc.right;
289 if (rc.top + rc.bottom > workarea_rc.bottom)
290 rc.top = workarea_rc.bottom - rc.bottom;
291
292 // Titlebar should always be visible.
293
294 if (rc.left < workarea_rc.left)
295 rc.left = workarea_rc.left;
296 if (rc.top < workarea_rc.top)
297 rc.top = workarea_rc.top;
298 }
299 }
300
301 if (new_style != style)
302 {
303 SetWindowLongPtr(ONgPlatformData.Window, GWL_STYLE, (LONG_PTR) new_style);
304 flags |= SWP_FRAMECHANGED | SWP_DRAWFRAME;
305 }
306
307 if (new_exstyle != exstyle)
308 {
309 SetWindowLongPtr(ONgPlatformData.Window, GWL_EXSTYLE, (LONG_PTR) new_exstyle);
310 flags |= SWP_FRAMECHANGED | SWP_DRAWFRAME;
311 }
312
313 SetWindowPos(ONgPlatformData.Window, NULL, rc.left, rc.top, rc.right, rc.bottom, flags);
314 return UUcTrue;
315 }
316}
317
318static void ONICALL DD_GLiGamma_Restore(void)
319{
[1000]320 ConfigOption_t* co = DDrConfig_GetOptOfType("graphics.gamma", C_BOOL);
[994]321 if (co->value.intBoolVal)
322 {
323 if (gl_api->wglSetDeviceGammaRamp3DFX)
324 gl_api->wglSetDeviceGammaRamp3DFX(gl->hDC, GLgInitialGammaRamp);
325 else
326 SetDeviceGammaRamp(gl->hDC, GLgInitialGammaRamp);
327 }
328}
329
330static void ONICALL DD_GLiGamma_Initialize(void)
331{
[1000]332 ConfigOption_t* co = DDrConfig_GetOptOfType("graphics.gamma", C_BOOL);
[994]333 if (co->value.intBoolVal)
334 {
335 if (gl_api->wglSetDeviceGammaRamp3DFX)
336 {
337 STARTUPMESSAGE("Using 3dfx gamma adjustment", 0);
338 GLgGammaRampValid = gl_api->wglGetDeviceGammaRamp3DFX(gl->hDC, GLgInitialGammaRamp);
339 }
340 else
341 {
342 STARTUPMESSAGE("Using Windows gamma adjustment", 0);
343 GLgGammaRampValid = GetDeviceGammaRamp(gl->hDC, GLgInitialGammaRamp);
344 }
345
346 M3rSetGamma(ONrPersist_GetGamma());
347 }
348 else
349 {
350 GLgGammaRampValid = FALSE;
351 }
352}
353
354// Disposes OpenGL engine. Called once.
355void ONICALL DD_GLrPlatform_Dispose(void)
356{
357 DEVMODE dm;
358
359 DD_GLiGamma_Restore();
360
361 gl_api->wglMakeCurrent(NULL, NULL);
362 gl_api->wglDeleteContext(gl->hGLRC);
363 ReleaseDC(ONgPlatformData.Window, gl->hDC);
364
365 // Restore initial display mode if it does not match current mode.
366
367 dm.dmSize = sizeof(dm);
368 dm.dmDriverExtra = 0;
369
370 if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm) ||
371 dm.dmPelsWidth != GLgInitialMode.dmPelsWidth ||
372 dm.dmPelsHeight != GLgInitialMode.dmPelsHeight ||
373 dm.dmBitsPerPel != GLgInitialMode.dmBitsPerPel)
374 {
375 ChangeDisplaySettings(&GLgInitialMode, 0);
376 }
377
378 // (skipping SetWindowPos as it only adds flickering)
379 gl_unload_library();
380}
381
382// Initializes (and re-initializes) OpenGL.
383UUtBool ONICALL DD_GLrPlatform_Initialize(void)
384{
385 static const M3tDisplayMode FallbackMode = { 640, 480, 16, 0 };
386
387 if (!DD_GLrPlatform_SetDisplayMode(&gl->DisplayMode))
388 {
389 gl->DisplayMode = FallbackMode;
390
391 if (!DD_GLrPlatform_SetDisplayMode(&gl->DisplayMode))
392 {
393 goto exit_err;
394 }
395 }
396
397 // (DD_GLrPlatform_SetDisplayMode updates a window rectangle for us)
398
399 if (!gl->hDC && !(gl->hDC = GetDC(ONgPlatformData.Window)))
400 {
401 goto exit_err;
402 }
403
[1000]404 ConfigOption_t* co = DDrConfig_GetOptOfType("graphics.gamma", C_BOOL);
[994]405 if (!M3gResolutionSwitch && co->value.intBoolVal)
406 {
407 STARTUPMESSAGE("Ignoring gamma setting due to windowed mode", 0);
408 co->value.intBoolVal = false;
409 }
410
411 DD_GLiGamma_Initialize();
412
413 // This creates a rendering context too.
414 if (!gl_platform_set_pixel_format(gl->hDC))
415 {
416 if (gl->DisplayMode.Depth != 16)
417 {
418 gl->DisplayMode.Depth = 16;
419 if (!DD_GLrPlatform_SetDisplayMode(&gl->DisplayMode))
420 goto exit_err;
421
422 if (!gl_platform_set_pixel_format(gl->hDC))
423 goto exit_err;
424 }
425 }
426
427 return UUcTrue;
428
429exit_err:
430 AUrMessageBox(1, "Daodan: Failed to initialize OpenGL contexts; Oni will now exit.");
431 exit(0);
432 return 0;
433}
Note: See TracBrowser for help on using the repository browser.