source: Daodan/src/daodan_gl.c@ 340

Last change on this file since 340 was 340, checked in by rossy, 15 years ago

fixed resolution list

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