source: Daodan/MSVC/daodan_gl.c@ 919

Last change on this file since 919 was 572, checked in by gumby, 14 years ago

Stuff

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