source: Daodan/src/Daodan_GL.c@ 985

Last change on this file since 985 was 893, checked in by alloc, 11 years ago

Daodan 3.4:

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