| 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 "Daodan_Win32.h" |
| 10 |
#include "BFW_Utility.h" |
| 11 |
|
| 12 |
#include "daodan_gl.h" |
| 13 |
#include "oni_gl.h" |
| 14 |
|
| 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)) |
| 18 |
|
| 19 |
const M3tDisplayMode daodan_reslist[] = { |
| 20 |
{ 720 , 480, 0, 0 }, |
| 21 |
{ 720 , 576, 0, 0 }, |
| 22 |
{ 768 , 480, 0, 0 }, |
| 23 |
{ 800 , 480, 0, 0 }, |
| 24 |
{ 800 , 600, 0, 0 }, |
| 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 }, |
| 32 |
{ 1024, 768, 0, 0 }, |
| 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 }, |
| 39 |
{ 1280, 1024, 0, 0 }, |
| 40 |
{ 1366, 768, 0, 0 }, |
| 41 |
{ 1400, 1050, 0, 0 }, |
| 42 |
{ 1440, 900, 0, 0 }, |
| 43 |
{ 1600, 900, 0, 0 }, |
| 44 |
{ 1600, 1200, 0, 0 }, |
| 45 |
{ 1920, 1080, 0, 0 }, |
| 46 |
{ 1920, 1200, 0, 0 }, |
| 47 |
{ 1920, 1440, 0, 0 }, |
| 48 |
}; |
| 49 |
//Just going to always use 32 bits for now... |
| 50 |
//short daodan_resdepths[] = { 16, 32 }; |
| 51 |
short daodan_resdepths[] = { 32 }; |
| 52 |
DEVMODE orig_devmode, cur_devmode, new_devmode; |
| 53 |
|
| 54 |
void init_daodan_gl() |
| 55 |
{ |
| 56 |
DDrStartupMessage("initalizing daodan gl"); |
| 57 |
|
| 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 |
|
| 73 |
void 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 |
|
| 84 |
unsigned int ONICALL daodan_enumerate_valid_display_modes(M3tDisplayMode modes[max_modes]) |
| 85 |
{ |
| 86 |
unsigned int vmodes = 0; |
| 87 |
unsigned int screen_x = GetSystemMetrics(SM_CXSCREEN); |
| 88 |
unsigned int screen_y = GetSystemMetrics(SM_CYSCREEN); |
| 89 |
|
| 90 |
uint16_t i, j; |
| 91 |
|
| 92 |
DDrStartupMessage("listing display modes"); |
| 93 |
/* |
| 94 |
if (!M3gResolutionSwitch) |
| 95 |
daodan_resdepths[0] = orig_devmode.dmBitsPerPel; |
| 96 |
*/ |
| 97 |
for (i = 0; i < builtin_depths; i ++) |
| 98 |
{ |
| 99 |
bool scrInsert = false; |
| 100 |
|
| 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) && |
| 110 |
((daodan_reslist[j].Width < screen_x && daodan_reslist[j].Height < screen_y) || (M3gResolutionSwitch && daodan_testmode(daodan_reslist[j])))) |
| 111 |
{ |
| 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 |
|
| 124 |
modes[vmodes].Width = daodan_reslist[j].Width; |
| 125 |
modes[vmodes].Height = daodan_reslist[j].Height; |
| 126 |
modes[vmodes].Depth = daodan_resdepths[i]; |
| 127 |
|
| 128 |
if (++vmodes == max_modes - builtin_modes + i) |
| 129 |
goto modesfull; |
| 130 |
} |
| 131 |
|
| 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 |
} |
| 141 |
|
| 142 |
if (!M3gResolutionSwitch) |
| 143 |
goto modesfull; |
| 144 |
} |
| 145 |
|
| 146 |
modesfull: |
| 147 |
DDrStartupMessage("%d modes available", vmodes); |
| 148 |
return vmodes; |
| 149 |
} |
| 150 |
|
| 151 |
bool 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 |
|
| 162 |
return (ChangeDisplaySettings(&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL); |
| 163 |
} |
| 164 |
|
| 165 |
int 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(); |
| 183 |
gl_eng->DisplayMode.Width = cur_devmode.dmPelsWidth; |
| 184 |
gl_eng->DisplayMode.Height = cur_devmode.dmPelsHeight; |
| 185 |
if (cur_devmode.dmBitsPerPel > depth) |
| 186 |
gl_eng->DisplayMode.Depth = cur_devmode.dmBitsPerPel; |
| 187 |
} |
| 188 |
else |
| 189 |
{ |
| 190 |
update_cdmode(); |
| 191 |
if (cur_devmode.dmBitsPerPel > depth) |
| 192 |
gl_eng->DisplayMode.Depth = cur_devmode.dmBitsPerPel; |
| 193 |
} |
| 194 |
return 1; |
| 195 |
} |
| 196 |
int ONICALL daodangl_platform_initialize() |
| 197 |
{ |
| 198 |
static M3tDisplayMode lastmode = {0, 0, 0, 0}; |
| 199 |
|
| 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) |
| 203 |
{ |
| 204 |
gl_eng->DisplayMode.Width = 640; |
| 205 |
gl_eng->DisplayMode.Height = 480; |
| 206 |
if (!daodan_set_display_mode(640, 480, 16)) |
| 207 |
goto exit_err; |
| 208 |
} |
| 209 |
|
| 210 |
if (lastmode.Width != gl_eng->DisplayMode.Width || lastmode.Height != gl_eng->DisplayMode.Height) |
| 211 |
{ |
| 212 |
RECT Rect; |
| 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; |
| 217 |
AdjustWindowRect(&Rect, WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_CAPTION |WS_TILEDWINDOW , FALSE); |
| 218 |
|
| 219 |
SetWindowPos(ONgPlatformData.Window, NULL, Rect.left, Rect.top, Rect.right - Rect.left, Rect.bottom - Rect.top, SWP_NOACTIVATE | SWP_NOZORDER); |
| 220 |
} |
| 221 |
|
| 222 |
if (gl_eng->HDC == NULL) |
| 223 |
if ((gl_eng->HDC = GetDC(ONgPlatformData.Window)) == NULL) |
| 224 |
goto exit_err; |
| 225 |
|
| 226 |
if (gl_api->wglGetDeviceGammaRamp3DFX != NULL) |
| 227 |
{ |
| 228 |
DDrStartupMessage("Using 3DFX gamma adjustment"); |
| 229 |
|
| 230 |
if (gl_api->wglGetDeviceGammaRamp3DFX(gl_eng->HDC, gl_gamma_ramp)) |
| 231 |
gl_gamma_ramp_valid = 1; |
| 232 |
} |
| 233 |
else |
| 234 |
{ |
| 235 |
DDrStartupMessage("Using standard Windows gamma adjustment"); |
| 236 |
|
| 237 |
if (GetDeviceGammaRamp(gl_eng->HDC, gl_gamma_ramp)) |
| 238 |
gl_gamma_ramp_valid = 1; |
| 239 |
} |
| 240 |
/* |
| 241 |
if (gl_gamma_ramp_valid) |
| 242 |
daodan_set_gamma(ONrPersist_GetGamma()); |
| 243 |
else*/ |
| 244 |
DDrStartupMessage("gamma adjustment not supported"); |
| 245 |
|
| 246 |
if (!gl_platform_set_pixel_format(gl_eng->HDC)) |
| 247 |
if (gl_eng->DisplayMode.Depth != 16) |
| 248 |
{ |
| 249 |
if (!daodan_set_display_mode(gl_eng->DisplayMode.Width, gl_eng->DisplayMode.Height, 16)) |
| 250 |
goto exit_err; |
| 251 |
|
| 252 |
if (!gl_platform_set_pixel_format(gl_eng->HDC)) |
| 253 |
goto exit_err; |
| 254 |
} |
| 255 |
|
| 256 |
lastmode.Width = gl_eng->DisplayMode.Width; |
| 257 |
lastmode.Height = gl_eng->DisplayMode.Height; |
| 258 |
lastmode.Depth = gl_eng->DisplayMode.Depth; |
| 259 |
return 1; |
| 260 |
|
| 261 |
exit_err: |
| 262 |
AUrMessageBox(1, "Failed to initialize OpenGL contexts; Oni will now exit."); |
| 263 |
exit(0); |
| 264 |
return 0; |
| 265 |
} |