[326] | 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 |
|
---|
[297] | 10 | #include "daodan_gl.h"
|
---|
[326] | 11 | #include "oni_gl.h"
|
---|
[297] | 12 |
|
---|
[316] | 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))
|
---|
[297] | 16 |
|
---|
| 17 | const M3tDisplayMode daodan_reslist[] = {
|
---|
[316] | 18 | { 720 , 480, 0, 0 },
|
---|
| 19 | { 720 , 576, 0, 0 },
|
---|
| 20 | { 768 , 480, 0, 0 },
|
---|
| 21 | { 800 , 480, 0, 0 },
|
---|
[340] | 22 | { 800 , 600, 0, 0 },
|
---|
[316] | 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 },
|
---|
[297] | 30 | { 1024, 768, 0, 0 },
|
---|
[316] | 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 },
|
---|
[297] | 37 | { 1280, 1024, 0, 0 },
|
---|
[316] | 38 | { 1366, 768, 0, 0 },
|
---|
| 39 | { 1400, 1050, 0, 0 },
|
---|
| 40 | { 1440, 900, 0, 0 },
|
---|
| 41 | { 1600, 900, 0, 0 },
|
---|
[297] | 42 | { 1600, 1200, 0, 0 },
|
---|
| 43 | { 1920, 1080, 0, 0 },
|
---|
[340] | 44 | { 1920, 1200, 0, 0 },
|
---|
[316] | 45 | { 1920, 1440, 0, 0 },
|
---|
[297] | 46 | };
|
---|
| 47 |
|
---|
[341] | 48 | short daodan_resdepths[] = { 16, 32 };
|
---|
[297] | 49 |
|
---|
[326] | 50 | DEVMODE orig_devmode, cur_devmode, new_devmode;
|
---|
| 51 |
|
---|
| 52 | void init_daodan_gl()
|
---|
| 53 | {
|
---|
[342] | 54 | DDrStartupMessage("initalizing daodan gl");
|
---|
| 55 |
|
---|
[326] | 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 |
|
---|
| 71 | void 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 |
|
---|
[316] | 82 | unsigned int ONICALL daodan_enumerate_valid_display_modes(M3tDisplayMode modes[max_modes])
|
---|
[297] | 83 | {
|
---|
| 84 | unsigned int vmodes = 0;
|
---|
[326] | 85 | unsigned int screen_x = orig_devmode.dmPelsWidth;
|
---|
| 86 | unsigned int screen_y = orig_devmode.dmPelsHeight;
|
---|
[297] | 87 |
|
---|
| 88 | int i, j;
|
---|
| 89 |
|
---|
[342] | 90 | DDrStartupMessage("listing display modes");
|
---|
| 91 |
|
---|
[341] | 92 | if (!M3gResolutionSwitch)
|
---|
| 93 | daodan_resdepths[0] = orig_devmode.dmBitsPerPel;
|
---|
| 94 |
|
---|
[316] | 95 | for (i = 0; i < builtin_depths; i ++)
|
---|
[297] | 96 | {
|
---|
[340] | 97 | bool scrInsert = false;
|
---|
| 98 |
|
---|
[316] | 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) &&
|
---|
[341] | 108 | ((daodan_reslist[j].Width < screen_x && daodan_reslist[j].Height < screen_y) || (M3gResolutionSwitch && daodan_testmode(daodan_reslist[j]))))
|
---|
[297] | 109 | {
|
---|
[340] | 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 |
|
---|
[297] | 122 | modes[vmodes].Width = daodan_reslist[j].Width;
|
---|
| 123 | modes[vmodes].Height = daodan_reslist[j].Height;
|
---|
[316] | 124 | modes[vmodes].Depth = daodan_resdepths[i];
|
---|
[297] | 125 |
|
---|
[316] | 126 | if (++vmodes == max_modes - builtin_modes + i)
|
---|
[297] | 127 | goto modesfull;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[340] | 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 | }
|
---|
[341] | 139 |
|
---|
| 140 | if (!M3gResolutionSwitch)
|
---|
| 141 | goto modesfull;
|
---|
[297] | 142 | }
|
---|
| 143 |
|
---|
| 144 | modesfull:
|
---|
[342] | 145 | DDrStartupMessage("%d modes available", vmodes);
|
---|
[297] | 146 | return vmodes;
|
---|
| 147 | }
|
---|
[316] | 148 |
|
---|
| 149 | bool 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 |
|
---|
[340] | 160 | return (ChangeDisplaySettings(&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL);
|
---|
[316] | 161 | }
|
---|
[326] | 162 |
|
---|
| 163 | int 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 |
|
---|
| 195 | void daodan_set_gamma(float gamma)
|
---|
| 196 | {
|
---|
[473] | 197 | WORD ramp[3][256];
|
---|
[326] | 198 | int i;
|
---|
| 199 |
|
---|
| 200 | if (!gl_gamma_ramp_valid)
|
---|
| 201 | return;
|
---|
| 202 |
|
---|
| 203 | gamma = (1.0f - gamma) * 1.2f + 0.4f;
|
---|
| 204 |
|
---|
[473] | 205 | for (i = 0; i < 256; i++)
|
---|
[326] | 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 |
|
---|
[473] | 214 | ramp[0][i] = ramp[1][i] = ramp[2][i] = value;
|
---|
[326] | 215 | }
|
---|
| 216 |
|
---|
| 217 | if (gl_api->wglSetDeviceGammaRamp3DFX)
|
---|
| 218 | gl_api->wglSetDeviceGammaRamp3DFX(gl->HDC, ramp);
|
---|
| 219 | else
|
---|
| 220 | SetDeviceGammaRamp(gl->HDC, ramp);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | int 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;
|
---|
[476] | 244 | AdjustWindowRect(&Rect, WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_CAPTION |WS_TILEDWINDOW , FALSE);
|
---|
[326] | 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 |
|
---|
[473] | 268 | if (gl_gamma_ramp_valid)
|
---|
| 269 | daodan_set_gamma(ONrPersist_GetGamma());
|
---|
| 270 | else
|
---|
[326] | 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;
|
---|
[473] | 286 | return 1;
|
---|
[326] | 287 |
|
---|
| 288 | exit_err:
|
---|
| 289 | AUrMessageBox(1, "Failed to initialize OpenGL contexts; Oni will now exit.");
|
---|
| 290 | exit(0);
|
---|
[473] | 291 | return 0;
|
---|
[326] | 292 | }
|
---|