source: Daodan/src/daodan_gl.c@ 373

Last change on this file since 373 was 342, checked in by rossy, 15 years ago
File size: 8.3 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 DDrStartupMessage("initalizing daodan gl");
55
56 memset(&orig_devmode, 0, sizeof(orig_devmode));
57 orig_devmode.dmSize = sizeof(orig_devmode);
58
59 if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &orig_devmode))
60 {
61 orig_devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
62 orig_devmode.dmBitsPerPel = 32;
63 orig_devmode.dmPelsWidth = GetSystemMetrics(SM_CXSCREEN);
64 orig_devmode.dmPelsHeight = GetSystemMetrics(SM_CYSCREEN);
65 }
66
67 memcpy(&cur_devmode, &orig_devmode, sizeof(orig_devmode));
68 memcpy(&new_devmode, &orig_devmode, sizeof(orig_devmode));
69}
70
71void update_cdmode()
72{
73 if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &cur_devmode))
74 {
75 cur_devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
76 cur_devmode.dmBitsPerPel = 32;
77 cur_devmode.dmPelsWidth = GetSystemMetrics(SM_CXSCREEN);
78 cur_devmode.dmPelsHeight = GetSystemMetrics(SM_CYSCREEN);
79 }
80}
81
82unsigned int ONICALL daodan_enumerate_valid_display_modes(M3tDisplayMode modes[max_modes])
83{
84 unsigned int vmodes = 0;
85 unsigned int screen_x = orig_devmode.dmPelsWidth;
86 unsigned int screen_y = orig_devmode.dmPelsHeight;
87
88 int i, j;
89
90 DDrStartupMessage("listing display modes");
91
92 if (!M3gResolutionSwitch)
93 daodan_resdepths[0] = orig_devmode.dmBitsPerPel;
94
95 for (i = 0; i < builtin_depths; i ++)
96 {
97 bool scrInsert = false;
98
99 modes[vmodes].Width = 640;
100 modes[vmodes].Height = 480;
101 modes[vmodes].Depth = daodan_resdepths[i];
102
103 if (++vmodes == max_modes - builtin_modes + i)
104 goto modesfull;
105
106 for (j = 0; j < builtin_modes; j ++)
107 if (!(daodan_reslist[j].Width == 640 && daodan_reslist[j].Height == 480) && !(daodan_reslist[j].Width == screen_x && daodan_reslist[j].Height == screen_y) &&
108 ((daodan_reslist[j].Width < screen_x && daodan_reslist[j].Height < screen_y) || (M3gResolutionSwitch && daodan_testmode(daodan_reslist[j]))))
109 {
110 if (!scrInsert && (daodan_reslist[j].Width > screen_x || (daodan_reslist[j].Width == screen_x && daodan_reslist[j].Height > screen_y)))
111 {
112 modes[vmodes].Width = screen_x;
113 modes[vmodes].Height = screen_y;
114 modes[vmodes].Depth = daodan_resdepths[i];
115
116 if (++vmodes == max_modes - builtin_modes + i)
117 goto modesfull;
118
119 scrInsert = true;
120 }
121
122 modes[vmodes].Width = daodan_reslist[j].Width;
123 modes[vmodes].Height = daodan_reslist[j].Height;
124 modes[vmodes].Depth = daodan_resdepths[i];
125
126 if (++vmodes == max_modes - builtin_modes + i)
127 goto modesfull;
128 }
129
130 if (!scrInsert)
131 {
132 modes[vmodes].Width = screen_x;
133 modes[vmodes].Height = screen_y;
134 modes[vmodes].Depth = daodan_resdepths[i];
135
136 if (++vmodes == max_modes - builtin_modes + i)
137 goto modesfull;
138 }
139
140 if (!M3gResolutionSwitch)
141 goto modesfull;
142 }
143
144 modesfull:
145 DDrStartupMessage("%d modes available", vmodes);
146 return vmodes;
147}
148
149bool daodan_testmode(M3tDisplayMode mode)
150{
151 DEVMODE devmode;
152 memset(&devmode, 0, sizeof(devmode));
153
154 devmode.dmSize = sizeof(devmode);
155 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
156 devmode.dmBitsPerPel = mode.Depth;
157 devmode.dmPelsWidth = mode.Width;
158 devmode.dmPelsHeight = mode.Height;
159
160 return (ChangeDisplaySettings(&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL);
161}
162
163int daodan_set_display_mode(short width, short height, short depth)
164{
165 if (M3gResolutionSwitch)
166 {
167 DEVMODE new_devmode;
168 new_devmode.dmSize = sizeof(new_devmode);
169 new_devmode.dmFields = DM_BITSPERPEL | DM_PELSHEIGHT | DM_PELSWIDTH;
170 new_devmode.dmPelsWidth = width;
171 new_devmode.dmPelsHeight = height;
172 new_devmode.dmBitsPerPel = depth;
173
174 if (ChangeDisplaySettings(&new_devmode, CDS_TEST) != DISP_CHANGE_SUCCESSFUL)
175 return 0;
176
177 if (ChangeDisplaySettings(&new_devmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
178 return 0;
179
180 update_cdmode();
181 gl->DisplayMode.Width = cur_devmode.dmPelsWidth;
182 gl->DisplayMode.Height = cur_devmode.dmPelsHeight;
183 if (cur_devmode.dmBitsPerPel > depth)
184 gl->DisplayMode.Depth = cur_devmode.dmBitsPerPel;
185 }
186 else
187 {
188 update_cdmode();
189 if (cur_devmode.dmBitsPerPel > depth)
190 gl->DisplayMode.Depth = cur_devmode.dmBitsPerPel;
191 }
192 return 1;
193}
194
195void daodan_set_gamma(float gamma)
196{
197 WORD ramp[3 * 256];
198 int i;
199
200 if (!gl_gamma_ramp_valid)
201 return;
202
203 gamma = (1.0f - gamma) * 1.2f + 0.4f;
204
205 for (i = 0; i < sizeof(ramp); i++)
206 {
207 int value = (int)(pow(gl_gamma_ramp[i] / 65535.0f, gamma) * 65535.0f);
208
209 if (value < 0)
210 value = 0;
211 else if (value > 65535)
212 value = 65535;
213
214 ramp[i] = value;
215 }
216
217 if (gl_api->wglSetDeviceGammaRamp3DFX)
218 gl_api->wglSetDeviceGammaRamp3DFX(gl->HDC, ramp);
219 else
220 SetDeviceGammaRamp(gl->HDC, ramp);
221}
222
223int ONICALL daodangl_platform_initialize()
224{
225 static M3tDisplayMode lastmode = {0, 0, 0, 0};
226
227 if (lastmode.Width != gl->DisplayMode.Width || lastmode.Height != gl->DisplayMode.Height || lastmode.Depth != gl->DisplayMode.Depth)
228 if (!daodan_set_display_mode(gl->DisplayMode.Width, gl->DisplayMode.Height, gl->DisplayMode.Depth))
229 if (gl->DisplayMode.Width != 640 || gl->DisplayMode.Height != 480 || gl->DisplayMode.Depth != 16)
230 {
231 gl->DisplayMode.Width = 640;
232 gl->DisplayMode.Height = 480;
233 if (!daodan_set_display_mode(640, 480, 16))
234 goto exit_err;
235 }
236
237 if (lastmode.Width != gl->DisplayMode.Width || lastmode.Height != gl->DisplayMode.Height)
238 {
239 RECT Rect;
240 Rect.left = (GetSystemMetrics(SM_CXSCREEN) / 2) - (gl->DisplayMode.Width / 2);
241 Rect.top = (GetSystemMetrics(SM_CYSCREEN) / 2) - (gl->DisplayMode.Height / 2);
242 Rect.right = Rect.left + gl->DisplayMode.Width;
243 Rect.bottom = Rect.top + gl->DisplayMode.Height;
244 AdjustWindowRect(&Rect, WS_OVERLAPPED | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, FALSE);
245
246 SetWindowPos(ONgPlatformData.Window, NULL, Rect.left, Rect.top, Rect.right - Rect.left, Rect.bottom - Rect.top, SWP_NOACTIVATE | SWP_NOZORDER);
247 }
248
249 if (gl->HDC == NULL)
250 if ((gl->HDC = GetDC(ONgPlatformData.Window)) == NULL)
251 goto exit_err;
252
253 if (gl_api->wglGetDeviceGammaRamp3DFX != NULL)
254 {
255 DDrStartupMessage("Using 3DFX gamma adjustment");
256
257 if (gl_api->wglGetDeviceGammaRamp3DFX(gl->HDC, gl_gamma_ramp))
258 gl_gamma_ramp_valid = 1;
259 }
260 else
261 {
262 DDrStartupMessage("Using standard Windows gamma adjustment");
263
264 if (GetDeviceGammaRamp(gl->HDC, gl_gamma_ramp))
265 gl_gamma_ramp_valid = 1;
266 }
267
268// if (gl_gamma_ramp_valid)
269// daodan_set_gamma(ONrPersist_GetGamma()); Its not working :(
270// else
271 DDrStartupMessage("gamma adjustment not supported");
272
273 if (!gl_platform_set_pixel_format(gl->HDC))
274 if (gl->DisplayMode.Depth != 16)
275 {
276 if (!daodan_set_display_mode(gl->DisplayMode.Width, gl->DisplayMode.Height, 16))
277 goto exit_err;
278
279 if (!gl_platform_set_pixel_format(gl->HDC))
280 goto exit_err;
281 }
282
283 lastmode.Width = gl->DisplayMode.Width;
284 lastmode.Height = gl->DisplayMode.Height;
285 lastmode.Depth = gl->DisplayMode.Depth;
286
287 return 0;
288
289exit_err:
290 AUrMessageBox(1, "Failed to initialize OpenGL contexts; Oni will now exit.");
291 exit(0);
292 return 1;
293}
Note: See TracBrowser for help on using the repository browser.