source: Daodan/src/daodan_gl.c@ 680

Last change on this file since 680 was 677, checked in by alloc, 12 years ago

Daodan: Updated src to work with MinGW

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