source: Daodan/src/daodan_gl.c@ 341

Last change on this file since 341 was 341, checked in by rossy, 16 years ago
File size: 8.1 KB
Line 
1#include <windows.h>
2#include <math.h>
3
4#include "Oni.h"
5#include "Oni_Persistence.h"
6#include "Daodan_Utility.h"
7
8#include "BFW_Utility.h"
9
10#include "daodan_gl.h"
11#include "oni_gl.h"
12
13#define max_modes (104) // Dirty hack to add more resolutions, it really should only be 16 ^_^
14#define builtin_modes (sizeof(daodan_reslist) / sizeof(M3tDisplayMode))
15#define builtin_depths (sizeof(daodan_resdepths) / sizeof(short))
16
17const M3tDisplayMode daodan_reslist[] = {
18 { 720 , 480, 0, 0 },
19 { 720 , 576, 0, 0 },
20 { 768 , 480, 0, 0 },
21 { 800 , 480, 0, 0 },
22 { 800 , 600, 0, 0 },
23 { 852 , 480, 0, 0 },
24 { 856 , 480, 0, 0 },
25 { 960 , 540, 0, 0 },
26 { 960 , 720, 0, 0 },
27 { 1024, 576, 0, 0 },
28 { 1024, 600, 0, 0 },
29 { 1024, 640, 0, 0 },
30 { 1024, 768, 0, 0 },
31 { 1152, 768, 0, 0 },
32 { 1152, 864, 0, 0 },
33 { 1280, 720, 0, 0 },
34 { 1280, 768, 0, 0 },
35 { 1280, 800, 0, 0 },
36 { 1280, 960, 0, 0 },
37 { 1280, 1024, 0, 0 },
38 { 1366, 768, 0, 0 },
39 { 1400, 1050, 0, 0 },
40 { 1440, 900, 0, 0 },
41 { 1600, 900, 0, 0 },
42 { 1600, 1200, 0, 0 },
43 { 1920, 1080, 0, 0 },
44 { 1920, 1200, 0, 0 },
45 { 1920, 1440, 0, 0 },
46};
47
48short daodan_resdepths[] = { 16, 32 };
49
50DEVMODE orig_devmode, cur_devmode, new_devmode;
51
52void init_daodan_gl()
53{
54 memset(&orig_devmode, 0, sizeof(orig_devmode));
55 orig_devmode.dmSize = sizeof(orig_devmode);
56
57 if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &orig_devmode))
58 {
59 orig_devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
60 orig_devmode.dmBitsPerPel = 32;
61 orig_devmode.dmPelsWidth = GetSystemMetrics(SM_CXSCREEN);
62 orig_devmode.dmPelsHeight = GetSystemMetrics(SM_CYSCREEN);
63 }
64
65 memcpy(&cur_devmode, &orig_devmode, sizeof(orig_devmode));
66 memcpy(&new_devmode, &orig_devmode, sizeof(orig_devmode));
67}
68
69void update_cdmode()
70{
71 if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &cur_devmode))
72 {
73 cur_devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
74 cur_devmode.dmBitsPerPel = 32;
75 cur_devmode.dmPelsWidth = GetSystemMetrics(SM_CXSCREEN);
76 cur_devmode.dmPelsHeight = GetSystemMetrics(SM_CYSCREEN);
77 }
78}
79
80unsigned int ONICALL daodan_enumerate_valid_display_modes(M3tDisplayMode modes[max_modes])
81{
82 unsigned int vmodes = 0;
83 unsigned int screen_x = orig_devmode.dmPelsWidth;
84 unsigned int screen_y = orig_devmode.dmPelsHeight;
85
86 int i, j;
87
88 if (!M3gResolutionSwitch)
89 daodan_resdepths[0] = orig_devmode.dmBitsPerPel;
90
91 for (i = 0; i < builtin_depths; i ++)
92 {
93 bool scrInsert = false;
94
95 modes[vmodes].Width = 640;
96 modes[vmodes].Height = 480;
97 modes[vmodes].Depth = daodan_resdepths[i];
98
99 if (++vmodes == max_modes - builtin_modes + i)
100 goto modesfull;
101
102 for (j = 0; j < builtin_modes; j ++)
103 if (!(daodan_reslist[j].Width == 640 && daodan_reslist[j].Height == 480) && !(daodan_reslist[j].Width == screen_x && daodan_reslist[j].Height == screen_y) &&
104 ((daodan_reslist[j].Width < screen_x && daodan_reslist[j].Height < screen_y) || (M3gResolutionSwitch && daodan_testmode(daodan_reslist[j]))))
105 {
106 if (!scrInsert && (daodan_reslist[j].Width > screen_x || (daodan_reslist[j].Width == screen_x && daodan_reslist[j].Height > screen_y)))
107 {
108 modes[vmodes].Width = screen_x;
109 modes[vmodes].Height = screen_y;
110 modes[vmodes].Depth = daodan_resdepths[i];
111
112 if (++vmodes == max_modes - builtin_modes + i)
113 goto modesfull;
114
115 scrInsert = true;
116 }
117
118 modes[vmodes].Width = daodan_reslist[j].Width;
119 modes[vmodes].Height = daodan_reslist[j].Height;
120 modes[vmodes].Depth = daodan_resdepths[i];
121
122 if (++vmodes == max_modes - builtin_modes + i)
123 goto modesfull;
124 }
125
126 if (!scrInsert)
127 {
128 modes[vmodes].Width = screen_x;
129 modes[vmodes].Height = screen_y;
130 modes[vmodes].Depth = daodan_resdepths[i];
131
132 if (++vmodes == max_modes - builtin_modes + i)
133 goto modesfull;
134 }
135
136 if (!M3gResolutionSwitch)
137 goto modesfull;
138 }
139
140 modesfull:
141 return vmodes;
142}
143
144bool daodan_testmode(M3tDisplayMode mode)
145{
146 DEVMODE devmode;
147 memset(&devmode, 0, sizeof(devmode));
148
149 devmode.dmSize = sizeof(devmode);
150 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
151 devmode.dmBitsPerPel = mode.Depth;
152 devmode.dmPelsWidth = mode.Width;
153 devmode.dmPelsHeight = mode.Height;
154
155 return (ChangeDisplaySettings(&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL);
156}
157
158int daodan_set_display_mode(short width, short height, short depth)
159{
160 if (M3gResolutionSwitch)
161 {
162 DEVMODE new_devmode;
163 new_devmode.dmSize = sizeof(new_devmode);
164 new_devmode.dmFields = DM_BITSPERPEL | DM_PELSHEIGHT | DM_PELSWIDTH;
165 new_devmode.dmPelsWidth = width;
166 new_devmode.dmPelsHeight = height;
167 new_devmode.dmBitsPerPel = depth;
168
169 if (ChangeDisplaySettings(&new_devmode, CDS_TEST) != DISP_CHANGE_SUCCESSFUL)
170 return 0;
171
172 if (ChangeDisplaySettings(&new_devmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
173 return 0;
174
175 update_cdmode();
176 gl->DisplayMode.Width = cur_devmode.dmPelsWidth;
177 gl->DisplayMode.Height = cur_devmode.dmPelsHeight;
178 if (cur_devmode.dmBitsPerPel > depth)
179 gl->DisplayMode.Depth = cur_devmode.dmBitsPerPel;
180 }
181 else
182 {
183 update_cdmode();
184 if (cur_devmode.dmBitsPerPel > depth)
185 gl->DisplayMode.Depth = cur_devmode.dmBitsPerPel;
186 }
187 return 1;
188}
189
190void daodan_set_gamma(float gamma)
191{
192 WORD ramp[3 * 256];
193 int i;
194
195 if (!gl_gamma_ramp_valid)
196 return;
197
198 gamma = (1.0f - gamma) * 1.2f + 0.4f;
199
200 for (i = 0; i < sizeof(ramp); i++)
201 {
202 int value = (int)(pow(gl_gamma_ramp[i] / 65535.0f, gamma) * 65535.0f);
203
204 if (value < 0)
205 value = 0;
206 else if (value > 65535)
207 value = 65535;
208
209 ramp[i] = value;
210 }
211
212 if (gl_api->wglSetDeviceGammaRamp3DFX)
213 gl_api->wglSetDeviceGammaRamp3DFX(gl->HDC, ramp);
214 else
215 SetDeviceGammaRamp(gl->HDC, ramp);
216}
217
218int ONICALL daodangl_platform_initialize()
219{
220 static M3tDisplayMode lastmode = {0, 0, 0, 0};
221
222 if (lastmode.Width != gl->DisplayMode.Width || lastmode.Height != gl->DisplayMode.Height || lastmode.Depth != gl->DisplayMode.Depth)
223 if (!daodan_set_display_mode(gl->DisplayMode.Width, gl->DisplayMode.Height, gl->DisplayMode.Depth))
224 if (gl->DisplayMode.Width != 640 || gl->DisplayMode.Height != 480 || gl->DisplayMode.Depth != 16)
225 {
226 gl->DisplayMode.Width = 640;
227 gl->DisplayMode.Height = 480;
228 if (!daodan_set_display_mode(640, 480, 16))
229 goto exit_err;
230 }
231
232 if (lastmode.Width != gl->DisplayMode.Width || lastmode.Height != gl->DisplayMode.Height)
233 {
234 RECT Rect;
235 Rect.left = (GetSystemMetrics(SM_CXSCREEN) / 2) - (gl->DisplayMode.Width / 2);
236 Rect.top = (GetSystemMetrics(SM_CYSCREEN) / 2) - (gl->DisplayMode.Height / 2);
237 Rect.right = Rect.left + gl->DisplayMode.Width;
238 Rect.bottom = Rect.top + gl->DisplayMode.Height;
239 AdjustWindowRect(&Rect, WS_OVERLAPPED | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, FALSE);
240
241 SetWindowPos(ONgPlatformData.Window, NULL, Rect.left, Rect.top, Rect.right - Rect.left, Rect.bottom - Rect.top, SWP_NOACTIVATE | SWP_NOZORDER);
242 }
243
244 if (gl->HDC == NULL)
245 if ((gl->HDC = GetDC(ONgPlatformData.Window)) == NULL)
246 goto exit_err;
247
248 if (gl_api->wglGetDeviceGammaRamp3DFX != NULL)
249 {
250 DDrStartupMessage("Using 3DFX gamma adjustment");
251
252 if (gl_api->wglGetDeviceGammaRamp3DFX(gl->HDC, gl_gamma_ramp))
253 gl_gamma_ramp_valid = 1;
254 }
255 else
256 {
257 DDrStartupMessage("Using standard Windows gamma adjustment");
258
259 if (GetDeviceGammaRamp(gl->HDC, gl_gamma_ramp))
260 gl_gamma_ramp_valid = 1;
261 }
262
263// if (gl_gamma_ramp_valid)
264// daodan_set_gamma(ONrPersist_GetGamma()); Its not working :(
265// else
266 DDrStartupMessage("gamma adjustment not supported");
267
268 if (!gl_platform_set_pixel_format(gl->HDC))
269 if (gl->DisplayMode.Depth != 16)
270 {
271 if (!daodan_set_display_mode(gl->DisplayMode.Width, gl->DisplayMode.Height, 16))
272 goto exit_err;
273
274 if (!gl_platform_set_pixel_format(gl->HDC))
275 goto exit_err;
276 }
277
278 lastmode.Width = gl->DisplayMode.Width;
279 lastmode.Height = gl->DisplayMode.Height;
280 lastmode.Depth = gl->DisplayMode.Depth;
281
282 return 0;
283
284exit_err:
285 AUrMessageBox(1, "Failed to initialize OpenGL contexts; Oni will now exit.");
286 exit(0);
287 return 1;
288}
Note: See TracBrowser for help on using the repository browser.