winOGLVideo.cc 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformWin32/platformGL.h"
  23. #include "platformWin32/platformWin32.h"
  24. #include "platformWin32/winOGLVideo.h"
  25. #include "console/console.h"
  26. #include "math/mPoint.h"
  27. #include "platform/event.h"
  28. #include "game/gameInterface.h"
  29. #include "console/consoleInternal.h"
  30. #include "console/ast.h"
  31. #include "io/fileStream.h"
  32. //------------------------------------------------------------------------------
  33. struct CardProfile
  34. {
  35. const char *vendor; // manufacturer
  36. const char *renderer; // driver name
  37. bool safeMode; // destroy rendering context for resolution change
  38. bool lockArray; // allow compiled vertex arrays
  39. bool subImage; // allow glTexSubImage*
  40. bool fogTexture; // require bound texture for combine extension
  41. bool noEnvColor; // no texture environment color
  42. bool clipHigh; // clip high resolutions
  43. bool deleteContext; // delete rendering context
  44. bool texCompress; // allow texture compression
  45. bool interiorLock; // lock arrays for Interior render
  46. bool skipFirstFog; // skip first two-pass fogging (dumb 3Dfx hack)
  47. bool only16; // inhibit 32-bit resolutions
  48. bool noArraysAlpha; // don't use glDrawArrays with a GL_ALPHA texture
  49. const char *proFile; // explicit profile of graphic settings
  50. };
  51. struct OSCardProfile
  52. {
  53. const char *vendor; // manufacturer
  54. const char *renderer; // driver name
  55. bool allowOpenGL; // allow OpenGL driver
  56. bool preferOpenGL; // prefer OpenGL driver
  57. };
  58. static Vector<CardProfile> sCardProfiles(__FILE__, __LINE__);
  59. static Vector<OSCardProfile> sOSCardProfiles(__FILE__, __LINE__);
  60. //------------------------------------------------------------------------------
  61. #include "winOGLVideo_ScriptBinding.h"
  62. //------------------------------------------------------------------------------
  63. struct ProcessorProfile
  64. {
  65. U16 clock; // clock range max
  66. U16 adjust; // CPU adjust
  67. };
  68. static U8 sNumProcessors = 4;
  69. static ProcessorProfile sProcessorProfiles[] =
  70. {
  71. { 400, 0 },
  72. { 600, 5 },
  73. { 800, 10 },
  74. { 1000, 15 },
  75. };
  76. struct SettingProfile
  77. {
  78. U16 performance; // metric range max
  79. const char *settings; // default file
  80. };
  81. static U8 sNumSettings = 3;
  82. static SettingProfile sSettingProfiles[] =
  83. {
  84. { 33, "LowProfile.cs" },
  85. { 66, "MediumProfile.cs" },
  86. { 100, "HighProfile.cs" },
  87. };
  88. //------------------------------------------------------------------------------
  89. static void clearCardProfiles()
  90. {
  91. while (sCardProfiles.size())
  92. {
  93. dFree((char *) sCardProfiles.last().vendor);
  94. dFree((char *) sCardProfiles.last().renderer);
  95. dFree((char *) sCardProfiles.last().proFile);
  96. sCardProfiles.decrement();
  97. }
  98. }
  99. static void clearOSCardProfiles()
  100. {
  101. while (sOSCardProfiles.size())
  102. {
  103. dFree((char *) sOSCardProfiles.last().vendor);
  104. dFree((char *) sOSCardProfiles.last().renderer);
  105. sOSCardProfiles.decrement();
  106. }
  107. }
  108. static void execScript(const char *scriptFile)
  109. {
  110. // execute the script
  111. FileStream str;
  112. if (!str.open(scriptFile, FileStream::Read))
  113. return;
  114. U32 size = str.getStreamSize();
  115. char *script = new char[size + 1];
  116. str.read(size, script);
  117. str.close();
  118. script[size] = 0;
  119. Con::executef(2, "eval", script);
  120. delete[] script;
  121. }
  122. static void profileSystem(const char *vendor, const char *renderer)
  123. {
  124. //Con::executef(2, "exec", "scripts/CardProfiles.cs");
  125. execScript("CardProfiles.cs");
  126. const char *arch;
  127. OSVERSIONINFO OSVersionInfo;
  128. const char *os = NULL;
  129. char osProfiles[64];
  130. if (dStrstr(PlatformSystemInfo.processor.name,"AMD") != NULL)
  131. arch = "AMD";
  132. else
  133. arch = "Intel";
  134. dMemset( &OSVersionInfo, 0, sizeof( OSVERSIONINFO ) );
  135. OSVersionInfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  136. if ( GetVersionEx( &OSVersionInfo ) )
  137. {
  138. if (OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  139. if ( OSVersionInfo.dwMinorVersion == 0 )
  140. {
  141. if (dStrcmp(arch,"Intel") == 0)
  142. os = "W95";
  143. }
  144. else if ( OSVersionInfo.dwMinorVersion == 10 )
  145. if ( OSVersionInfo.szCSDVersion[1] != 'A' )
  146. os = "W98";
  147. else
  148. os = "W98SE";
  149. else
  150. os = "WME";
  151. else
  152. if ( OSVersionInfo.dwMajorVersion >= 5 )
  153. os = "W2K";
  154. if ( os != NULL )
  155. {
  156. dSprintf(osProfiles,64,"%s%sCardProfiles.cs",arch,os);
  157. //Con::executef(2, "exec", osProfiles);
  158. execScript(osProfiles);
  159. }
  160. }
  161. const char *proFile = NULL;
  162. U32 i;
  163. for (i = 0; i < (U32)sCardProfiles.size(); ++i)
  164. if (dStrstr(vendor, sCardProfiles[i].vendor) &&
  165. (!dStrcmp(sCardProfiles[i].renderer, "*") ||
  166. dStrstr(renderer, sCardProfiles[i].renderer)))
  167. {
  168. Con::setBoolVariable("$pref::Video::safeModeOn", sCardProfiles[i].safeMode);
  169. Con::setBoolVariable("$pref::OpenGL::disableEXTCompiledVertexArray", !sCardProfiles[i].lockArray);
  170. Con::setBoolVariable("$pref::OpenGL::disableSubImage", !sCardProfiles[i].subImage);
  171. Con::setBoolVariable("$pref::TS::fogTexture", sCardProfiles[i].fogTexture);
  172. Con::setBoolVariable("$pref::OpenGL::noEnvColor", sCardProfiles[i].noEnvColor);
  173. Con::setBoolVariable("$pref::Video::clipHigh", sCardProfiles[i].clipHigh);
  174. if (!sCardProfiles[i].deleteContext)
  175. {
  176. OSVERSIONINFO OSVersionInfo;
  177. // HACK: The Voodoo3/5 on W2K crash when deleting a rendering context
  178. // So we're not deleting it.
  179. // Oh, and the Voodoo3 returns a Banshee renderer string under W2K
  180. dMemset( &OSVersionInfo, 0, sizeof( OSVERSIONINFO ) );
  181. OSVersionInfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  182. if ( GetVersionEx( &OSVersionInfo ) &&
  183. OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  184. OSVersionInfo.dwMajorVersion == 5)
  185. Con::setBoolVariable("$pref::Video::deleteContext", false);
  186. else
  187. Con::setBoolVariable("$pref::Video::deleteContext", true);
  188. }
  189. else
  190. Con::setBoolVariable("$pref::Video::deleteContext", true);
  191. Con::setBoolVariable("$pref::OpenGL::disableARBTextureCompression", !sCardProfiles[i].texCompress);
  192. Con::setBoolVariable("$pref::Interior::lockArrays", sCardProfiles[i].interiorLock);
  193. Con::setBoolVariable("$pref::TS::skipFirstFog", sCardProfiles[i].skipFirstFog);
  194. Con::setBoolVariable("$pref::Video::only16", sCardProfiles[i].only16);
  195. Con::setBoolVariable("$pref::OpenGL::noDrawArraysAlpha", sCardProfiles[i].noArraysAlpha);
  196. proFile = sCardProfiles[i].proFile;
  197. break;
  198. }
  199. // defaults
  200. U16 glProfile;
  201. if (!proFile)
  202. {
  203. // no driver GL profile -- make one via weighting GL extensions
  204. glProfile = 25;
  205. glProfile += gGLState.suppARBMultitexture * 25;
  206. glProfile += gGLState.suppLockedArrays * 15;
  207. glProfile += gGLState.suppVertexArrayRange * 10;
  208. glProfile += gGLState.suppTextureEnvCombine * 5;
  209. glProfile += gGLState.suppPackedPixels * 5;
  210. glProfile += gGLState.suppTextureCompression * 5;
  211. glProfile += gGLState.suppS3TC * 5;
  212. glProfile += gGLState.suppFXT1 * 5;
  213. Con::setBoolVariable("$pref::Video::safeModeOn", true);
  214. Con::setBoolVariable("$pref::OpenGL::disableEXTCompiledVertexArray", false);
  215. Con::setBoolVariable("$pref::OpenGL::disableSubImage", false);
  216. Con::setBoolVariable("$pref::TS::fogTexture", false);
  217. Con::setBoolVariable("$pref::OpenGL::noEnvColor", false);
  218. Con::setBoolVariable("$pref::Video::clipHigh", false);
  219. Con::setBoolVariable("$pref::Video::deleteContext", true);
  220. Con::setBoolVariable("$pref::OpenGL::disableARBTextureCompression", false);
  221. Con::setBoolVariable("$pref::Interior::lockArrays", true);
  222. Con::setBoolVariable("$pref::TS::skipFirstFog", false);
  223. Con::setBoolVariable("$pref::Video::only16", false);
  224. Con::setBoolVariable("$pref::OpenGL::noDrawArraysAlpha", false);
  225. }
  226. Con::setVariable("$pref::Video::profiledVendor", vendor);
  227. Con::setVariable("$pref::Video::profiledRenderer", renderer);
  228. if (!Con::getBoolVariable("$DisableSystemProfiling") &&
  229. ( dStrcmp(vendor, Con::getVariable("$pref::Video::defaultsVendor")) ||
  230. dStrcmp(renderer, Con::getVariable("$pref::Video::defaultsRenderer")) ))
  231. {
  232. if (proFile)
  233. {
  234. char settings[64];
  235. dSprintf(settings,64,"%s.cs",proFile);
  236. //Con::executef(2, "exec", settings);
  237. execScript(settings);
  238. }
  239. else
  240. {
  241. U16 adjust;
  242. // match clock with profile
  243. for (i = 0; i < sNumProcessors; ++i)
  244. {
  245. adjust = sProcessorProfiles[i].adjust;
  246. if (PlatformSystemInfo.processor.mhz < sProcessorProfiles[i].clock) break;
  247. }
  248. const char *settings;
  249. // match performance metric with profile
  250. for (i = 0; i < sNumSettings; ++i)
  251. {
  252. settings = sSettingProfiles[i].settings;
  253. if (glProfile+adjust <= sSettingProfiles[i].performance) break;
  254. }
  255. //Con::executef(2, "exec", settings);
  256. execScript(settings);
  257. }
  258. bool match = false;
  259. for (i = 0; i < (U32)sOSCardProfiles.size(); ++i)
  260. if (dStrstr(vendor, sOSCardProfiles[i].vendor) &&
  261. (!dStrcmp(sOSCardProfiles[i].renderer, "*") ||
  262. dStrstr(renderer, sOSCardProfiles[i].renderer)))
  263. {
  264. Con::setBoolVariable("$pref::Video::allowOpenGL", sOSCardProfiles[i].allowOpenGL);
  265. Con::setBoolVariable("$pref::Video::preferOpenGL", sOSCardProfiles[i].preferOpenGL);
  266. match = true;
  267. break;
  268. }
  269. if (!match)
  270. {
  271. Con::setBoolVariable("$pref::Video::allowOpenGL", true);
  272. Con::setBoolVariable("$pref::Video::preferOpenGL", true);
  273. }
  274. Con::setVariable("$pref::Video::defaultsVendor", vendor);
  275. Con::setVariable("$pref::Video::defaultsRenderer", renderer);
  276. }
  277. clearCardProfiles();
  278. clearOSCardProfiles();
  279. }
  280. //------------------------------------------------------------------------------
  281. OpenGLDevice::OpenGLDevice()
  282. {
  283. initDevice();
  284. }
  285. //------------------------------------------------------------------------------
  286. void OpenGLDevice::initDevice()
  287. {
  288. // Set the device name:
  289. mDeviceName = "OpenGL";
  290. // Let Parent enumerate valid resolutions.
  291. Parent::initDevice();
  292. }
  293. //------------------------------------------------------------------------------
  294. bool OpenGLDevice::activate( U32 width, U32 height, U32 bpp, bool fullScreen )
  295. {
  296. Con::printf( "Activating the OpenGL display device..." );
  297. bool needResurrect = false;
  298. // If the rendering context exists, delete it:
  299. if ( winState.hGLRC )
  300. {
  301. Con::printf( "Killing the texture manager..." );
  302. Game->textureKill();
  303. needResurrect = true;
  304. Con::printf( "Making the rendering context not current..." );
  305. if ( !dwglMakeCurrent( NULL, NULL ) )
  306. {
  307. AssertFatal( false, "OpenGLDevice::activate\ndwglMakeCurrent( NULL, NULL ) failed!" );
  308. return false;
  309. }
  310. Con::printf( "Deleting the rendering context ..." );
  311. if ( !dwglDeleteContext( winState.hGLRC ) )
  312. {
  313. AssertFatal( false, "OpenGLDevice::activate\ndwglDeleteContext failed!" );
  314. return false;
  315. }
  316. winState.hGLRC = NULL;
  317. }
  318. // [neo, 5/31/2007 - #3174]
  319. if( winState.appMenu )
  320. {
  321. DestroyMenu( winState.appMenu );
  322. winState.appMenu = NULL;
  323. }
  324. // If the window already exists, kill it so we can start fresh:
  325. if ( winState.appWindow )
  326. {
  327. if ( winState.appDC )
  328. {
  329. Con::printf( "Releasing the device context..." );
  330. ReleaseDC( winState.appWindow, winState.appDC );
  331. winState.appDC = NULL;
  332. }
  333. Con::printf( "Destroying the window..." );
  334. DestroyWindow( winState.appWindow );
  335. winState.appWindow = NULL;
  336. }
  337. // If OpenGL library already loaded, shut it down and reload it:
  338. if ( winState.hinstOpenGL )
  339. GL_Shutdown();
  340. GL_Init( "opengl32", "glu32" );
  341. static bool onceAlready = false;
  342. bool profiled = false;
  343. if ( !mFullScreenOnly && fullScreen && !onceAlready )
  344. {
  345. OSVERSIONINFO OSVersionInfo;
  346. // HACK: The Voodoo5 on W2K will only work if the initial rendering
  347. // context is windowed. Can you believe this crap?
  348. dMemset( &OSVersionInfo, 0, sizeof( OSVERSIONINFO ) );
  349. OSVersionInfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  350. if ( GetVersionEx( &OSVersionInfo ) &&
  351. OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  352. OSVersionInfo.dwMajorVersion == 5 )
  353. //MIN_RESOLUTION defined in platformWin32/platformGL.h
  354. if ( !setScreenMode( MIN_RESOLUTION_X, MIN_RESOLUTION_Y, bpp, false, true, false ) )
  355. return false;
  356. else
  357. {
  358. const char* vendorString = (const char*) glGetString( GL_VENDOR );
  359. const char* rendererString = (const char*) glGetString( GL_RENDERER );
  360. // only do this for the first session
  361. if (!Con::getBoolVariable("$DisableSystemProfiling") &&
  362. ( dStrcmp(vendorString, Con::getVariable("$pref::Video::profiledVendor")) ||
  363. dStrcmp(rendererString, Con::getVariable("$pref::Video::profiledRenderer")) ))
  364. {
  365. profileSystem(vendorString, rendererString);
  366. profiled = true;
  367. }
  368. }
  369. onceAlready = true;
  370. }
  371. // Set the resolution:
  372. if ( !setScreenMode( width, height, bpp, ( fullScreen || mFullScreenOnly ), true, false ) )
  373. return false;
  374. // Get original gamma ramp
  375. mRestoreGamma = GetDeviceGammaRamp(winState.appDC, mOriginalRamp);
  376. // Output some driver info to the console:
  377. const char* vendorString = (const char*) glGetString( GL_VENDOR );
  378. const char* rendererString = (const char*) glGetString( GL_RENDERER );
  379. const char* versionString = (const char*) glGetString( GL_VERSION );
  380. Con::printf( "OpenGL driver information:" );
  381. if ( vendorString )
  382. Con::printf( " Vendor: %s", vendorString );
  383. if ( rendererString )
  384. Con::printf( " Renderer: %s", rendererString );
  385. if ( versionString )
  386. Con::printf( " Version: %s", versionString );
  387. if ( needResurrect )
  388. {
  389. // Reload the textures:
  390. Con::printf( "Resurrecting the texture manager..." );
  391. Game->textureResurrect();
  392. }
  393. GL_EXT_Init();
  394. Con::setVariable( "$pref::Video::displayDevice", mDeviceName );
  395. // only do this for the first session
  396. if (!profiled &&
  397. !Con::getBoolVariable("$DisableSystemProfiling") &&
  398. ( dStrcmp(vendorString, Con::getVariable("$pref::Video::profiledVendor")) ||
  399. dStrcmp(rendererString, Con::getVariable("$pref::Video::profiledRenderer")) ))
  400. {
  401. profileSystem(vendorString, rendererString);
  402. profiled = true;
  403. }
  404. if (profiled)
  405. {
  406. U32 width, height, bpp;
  407. if (Con::getBoolVariable("$pref::Video::clipHigh", false))
  408. for (S32 i = mResolutionList.size()-1; i >= 0; --i)
  409. if (mResolutionList[i].w > 1152 || mResolutionList[i].h > 864)
  410. mResolutionList.erase(i);
  411. if (Con::getBoolVariable("$pref::Video::only16", false))
  412. for (S32 i = mResolutionList.size()-1; i >= 0; --i)
  413. if (mResolutionList[i].bpp == 32)
  414. mResolutionList.erase(i);
  415. bool bPrefFullScreen = Con::getBoolVariable("$pref::Video::fullScreen", true);
  416. if( bPrefFullScreen || fullScreen )
  417. dSscanf(Con::getVariable("$pref::Video::resolution"), "%d %d %d", &width, &height, &bpp);
  418. else
  419. dSscanf(Con::getVariable("$pref::Video::windowedRes"), "%d %d %d", &width, &height, &bpp);
  420. setScreenMode(width, height, bpp,
  421. bPrefFullScreen || fullScreen, false, false);
  422. }
  423. // Do this here because we now know about the extensions:
  424. if ( gGLState.suppSwapInterval )
  425. setVerticalSync( !Con::getBoolVariable( "$pref::Video::disableVerticalSync" ) );
  426. Con::setBoolVariable("$pref::OpenGL::allowTexGen", true);
  427. return true;
  428. }
  429. //------------------------------------------------------------------------------
  430. void OpenGLDevice::shutdown()
  431. {
  432. Con::printf( "Shutting down the OpenGL display device..." );
  433. if ( winState.hGLRC )
  434. {
  435. if (mRestoreGamma)
  436. SetDeviceGammaRamp(winState.appDC, mOriginalRamp);
  437. Con::printf( "Making the GL rendering context not current..." );
  438. dwglMakeCurrent( NULL, NULL );
  439. if ( Con::getBoolVariable("$pref::Video::deleteContext", true) )
  440. {
  441. Con::printf( "Deleting the GL rendering context..." );
  442. dwglDeleteContext( winState.hGLRC );
  443. }
  444. winState.hGLRC = NULL;
  445. }
  446. if ( winState.appDC )
  447. {
  448. Con::printf( "Releasing the device context..." );
  449. ReleaseDC( winState.appWindow, winState.appDC );
  450. winState.appDC = NULL;
  451. }
  452. if ( smIsFullScreen )
  453. {
  454. Con::printf( "Restoring the desktop display settings (%dx%dx%d)...", winState.desktopWidth, winState.desktopHeight, winState.desktopBitsPixel );
  455. ChangeDisplaySettings( NULL, 0 );
  456. }
  457. }
  458. //------------------------------------------------------------------------------
  459. // This is the real workhorse function of the DisplayDevice...
  460. //
  461. bool OpenGLDevice::setScreenMode( U32 width, U32 height, U32 bpp, bool fullScreen, bool forceIt, bool repaint )
  462. {
  463. HWND curtain = NULL;
  464. char errorMessage[256];
  465. Resolution newRes( width, height, bpp );
  466. bool newFullScreen = fullScreen;
  467. bool safeModeOn = Con::getBoolVariable( "$pref::Video::safeModeOn" );
  468. if ( !newFullScreen && mFullScreenOnly )
  469. {
  470. Con::warnf( ConsoleLogEntry::General, "OpenGLDevice::setScreenMode - device or desktop color depth does not allow windowed mode!" );
  471. newFullScreen = true;
  472. }
  473. if ( !newFullScreen && ( (S32)newRes.w >= winState.desktopClientWidth || (S32)newRes.h >= winState.desktopClientHeight ) )
  474. {
  475. Con::warnf( ConsoleLogEntry::General, "OpenGLDevice::setScreenMode -- can't switch to resolution larger than desktop in windowed mode!" );
  476. S32 resIndex = 0;
  477. S32 bestScore = 0, thisScore = 0;
  478. for ( S32 i = 0; i < mResolutionList.size(); i++ )
  479. {
  480. if ( newRes == mResolutionList[i] )
  481. {
  482. resIndex = i;
  483. break;
  484. }
  485. else if( (S32)newRes.h <= winState.desktopClientHeight && (S32)newRes.w <= winState.desktopClientWidth )
  486. {
  487. thisScore = abs( S32( newRes.w ) - S32( mResolutionList[i].w ) )
  488. + abs( S32( newRes.h ) - S32( mResolutionList[i].h ) )
  489. + ( newRes.bpp == mResolutionList[i].bpp ? 0 : 1 );
  490. if ( !bestScore || ( thisScore < bestScore ) )
  491. {
  492. bestScore = thisScore;
  493. resIndex = i;
  494. }
  495. }
  496. }
  497. }
  498. //MIN_RESOLUTION defined in platformWin32/platformGL.h
  499. if ( newRes.w < MIN_RESOLUTION_X || newRes.h < MIN_RESOLUTION_Y )
  500. {
  501. Con::warnf( ConsoleLogEntry::General, "OpenGLDevice::setScreenMode -- can't go smaller than 640x480!" );
  502. return false;
  503. }
  504. if ( newFullScreen )
  505. {
  506. if (newRes.bpp != 16 && mFullScreenOnly)
  507. newRes.bpp = 16;
  508. // Match the new resolution to one in the list:
  509. U32 resIndex = 0;
  510. U32 bestScore = 0, thisScore = 0;
  511. for ( int i = 0; i < mResolutionList.size(); i++ )
  512. {
  513. if ( newRes == mResolutionList[i] )
  514. {
  515. resIndex = i;
  516. break;
  517. }
  518. else
  519. {
  520. thisScore = abs( S32( newRes.w ) - S32( mResolutionList[i].w ) )
  521. + abs( S32( newRes.h ) - S32( mResolutionList[i].h ) )
  522. + ( newRes.bpp == mResolutionList[i].bpp ? 0 : 1 );
  523. if ( !bestScore || ( thisScore < bestScore ) )
  524. {
  525. bestScore = thisScore;
  526. resIndex = i;
  527. }
  528. }
  529. }
  530. newRes = mResolutionList[resIndex];
  531. }
  532. else
  533. {
  534. // Basically ignore the bit depth parameter:
  535. newRes.bpp = winState.desktopBitsPixel;
  536. }
  537. // Return if already at this resolution:
  538. if ( !forceIt && newRes == smCurrentRes && newFullScreen == smIsFullScreen )
  539. return true;
  540. Con::printf( "Setting screen mode to %dx%dx%d (%s)...", newRes.w, newRes.h, newRes.bpp, ( newFullScreen ? "fs" : "w" ) );
  541. bool needResurrect = false;
  542. if ( ( newRes.bpp != smCurrentRes.bpp ) || ( safeModeOn && ( ( smIsFullScreen != newFullScreen ) || newFullScreen ) ) )
  543. {
  544. // Delete the rendering context:
  545. if ( winState.hGLRC )
  546. {
  547. if (!Video::smNeedResurrect)
  548. {
  549. Con::printf( "Killing the texture manager..." );
  550. Game->textureKill();
  551. needResurrect = true;
  552. }
  553. Con::printf( "Making the rendering context not current..." );
  554. if ( !dwglMakeCurrent( NULL, NULL ) )
  555. {
  556. AssertFatal( false, "OpenGLDevice::setScreenMode\ndwglMakeCurrent( NULL, NULL ) failed!" );
  557. return false;
  558. }
  559. Con::printf( "Deleting the rendering context..." );
  560. if ( Con::getBoolVariable("$pref::Video::deleteContext",true) &&
  561. !dwglDeleteContext( winState.hGLRC ) )
  562. {
  563. AssertFatal( false, "OpenGLDevice::setScreenMode\ndwglDeleteContext failed!" );
  564. return false;
  565. }
  566. winState.hGLRC = NULL;
  567. }
  568. // Release the device context:
  569. if ( winState.appDC )
  570. {
  571. Con::printf( "Releasing the device context..." );
  572. ReleaseDC( winState.appWindow, winState.appDC );
  573. winState.appDC = NULL;
  574. }
  575. // [neo, 5/31/2007 - #3174]
  576. if( winState.appMenu )
  577. {
  578. DestroyMenu( winState.appMenu );
  579. winState.appMenu = NULL;
  580. }
  581. // Destroy the window:
  582. if ( winState.appWindow )
  583. {
  584. Con::printf( "Destroying the window..." );
  585. DestroyWindow( winState.appWindow );
  586. winState.appWindow = NULL;
  587. }
  588. }
  589. else if ( smIsFullScreen != newFullScreen )
  590. {
  591. // Change the window style:
  592. Con::printf( "Changing the window style..." );
  593. S32 windowStyle = WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  594. if ( newFullScreen )
  595. windowStyle |= ( WS_MAXIMIZE | WS_VISIBLE);
  596. else
  597. windowStyle |= ( WS_OVERLAPPEDWINDOW );
  598. if ( winState.appWindow && !SetWindowLong( winState.appWindow, GWL_STYLE, windowStyle ) )
  599. Con::errorf( "SetWindowLong failed to change the window style!" );
  600. }
  601. if( winState.appWindow && !newFullScreen )
  602. ShowWindow( winState.appWindow, SW_NORMAL );
  603. else
  604. ShowWindow( winState.appWindow, SW_MAXIMIZE );
  605. U32 test;
  606. if ( newFullScreen )
  607. {
  608. // Change the display settings:
  609. DEVMODE devMode;
  610. dMemset( &devMode, 0, sizeof( devMode ) );
  611. devMode.dmSize = sizeof( devMode );
  612. devMode.dmPelsWidth = newRes.w;
  613. devMode.dmPelsHeight = newRes.h;
  614. devMode.dmBitsPerPel = newRes.bpp;
  615. devMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
  616. Con::printf( "Changing the display settings to %dx%dx%d...", newRes.w, newRes.h, newRes.bpp );
  617. curtain = CreateCurtain( newRes.w, newRes.h );
  618. test = ChangeDisplaySettings( &devMode, CDS_FULLSCREEN );
  619. if ( test != DISP_CHANGE_SUCCESSFUL )
  620. {
  621. smIsFullScreen = false;
  622. Con::setBoolVariable( "$pref::Video::fullScreen", false );
  623. ChangeDisplaySettings( NULL, 0 );
  624. Con::errorf( ConsoleLogEntry::General, "OpenGLDevice::setScreenMode - ChangeDisplaySettings failed." );
  625. switch( test )
  626. {
  627. case DISP_CHANGE_RESTART:
  628. Platform::AlertOK( "Display Change Failed", "You must restart your machine to get the specified mode." );
  629. break;
  630. case DISP_CHANGE_BADMODE:
  631. Platform::AlertOK( "Display Change Failed", "The specified mode is not supported by this device." );
  632. break;
  633. default:
  634. Platform::AlertOK( "Display Change Failed", "Hardware failed to switch to the specified mode." );
  635. break;
  636. };
  637. DestroyWindow( curtain );
  638. return false;
  639. }
  640. else
  641. smIsFullScreen = true;
  642. }
  643. else if ( smIsFullScreen )
  644. {
  645. Con::printf( "Changing to the desktop display settings (%dx%dx%d)...", winState.desktopWidth, winState.desktopHeight, winState.desktopBitsPixel );
  646. ChangeDisplaySettings( NULL, 0 );
  647. smIsFullScreen = false;
  648. }
  649. Con::setBoolVariable( "$pref::Video::fullScreen", smIsFullScreen );
  650. bool newWindow = false;
  651. if ( !winState.appWindow )
  652. {
  653. Con::printf( "Creating a new %swindow...", ( fullScreen ? "full-screen " : "" ) );
  654. winState.appWindow = CreateOpenGLWindow( newRes.w, newRes.h, newFullScreen, true );
  655. if ( !winState.appWindow )
  656. {
  657. AssertFatal( false, "OpenGLDevice::setScreenMode\nFailed to create a new window!" );
  658. return false;
  659. }
  660. newWindow = true;
  661. }
  662. // Move the window:
  663. if ( !newFullScreen )
  664. {
  665. // Adjust the window rect to compensate for the window style:
  666. RECT windowRect;
  667. windowRect.left = windowRect.top = 0;
  668. windowRect.right = newRes.w;
  669. windowRect.bottom = newRes.h;
  670. AdjustWindowRect( &windowRect, GetWindowLong( winState.appWindow, GWL_STYLE ), false );
  671. U32 adjWidth = windowRect.right - windowRect.left;
  672. U32 adjHeight = windowRect.bottom - windowRect.top;
  673. // Center the window on the desktop:
  674. U32 xPos = 0, yPos = 0;
  675. if(adjWidth < (U32)winState.desktopClientWidth)
  676. xPos = ( winState.desktopClientWidth - adjWidth ) / 2;
  677. if(adjHeight < (U32)winState.desktopClientHeight)
  678. yPos = ( winState.desktopClientHeight - adjHeight ) / 2;
  679. test = SetWindowPos( winState.appWindow, 0, xPos, yPos, adjWidth, adjHeight, SWP_NOZORDER );
  680. if ( !test )
  681. {
  682. dSprintf( errorMessage, 255, "OpenGLDevice::setScreenMode\nSetWindowPos failed trying to move the window to (%d,%d) and size it to %dx%d.", xPos, yPos, newRes.w, newRes.h );
  683. AssertFatal( false, errorMessage );
  684. return false;
  685. }
  686. AnimateWindow( winState.appWindow, 90, AW_BLEND );
  687. }
  688. else if ( !newWindow )
  689. {
  690. // Move and size the window to take up the whole screen:
  691. if ( !SetWindowPos( winState.appWindow, HWND_TOPMOST, 0, 0, newRes.w, newRes.h, SWP_NOACTIVATE)
  692. || !SetWindowPos( winState.appWindow, HWND_TOPMOST, 0, 0, newRes.w, newRes.h, SWP_NOZORDER) )
  693. {
  694. dSprintf( errorMessage, 255, "OpenGLDevice::setScreenMode\nSetWindowPos failed to move the window to (0,0) and size it to %dx%d.", newRes.w, newRes.h );
  695. AssertFatal( false, errorMessage );
  696. return false;
  697. }
  698. }
  699. bool newDeviceContext = false;
  700. if ( !winState.appDC )
  701. {
  702. // Get a new device context:
  703. Con::printf( "Acquiring a new device context..." );
  704. winState.appDC = GetDC( winState.appWindow );
  705. if ( !winState.appDC )
  706. {
  707. AssertFatal( false, "OpenGLDevice::setScreenMode\nGetDC failed to get a valid device context!" );
  708. return false;
  709. }
  710. newDeviceContext = true;
  711. }
  712. if ( newWindow )
  713. {
  714. // Set the pixel format of the new window:
  715. PIXELFORMATDESCRIPTOR pfd;
  716. CreatePixelFormat( &pfd, newRes.bpp, 24, 8, false );
  717. S32 chosenFormat = ChooseBestPixelFormat( winState.appDC, &pfd );
  718. if ( !chosenFormat )
  719. {
  720. AssertFatal( false, "OpenGLDevice::setScreenMode\nNo valid pixel formats found!" );
  721. return false;
  722. }
  723. dwglDescribePixelFormat( winState.appDC, chosenFormat, sizeof( pfd ), &pfd );
  724. if ( !SetPixelFormat( winState.appDC, chosenFormat, &pfd ) )
  725. {
  726. AssertFatal( false, "OpenGLDevice::setScreenMode\nFailed to set the pixel format!" );
  727. return false;
  728. }
  729. Con::printf( "Pixel format set:" );
  730. Con::printf( " %d color bits, %d depth bits, %d stencil bits", pfd.cColorBits, pfd.cDepthBits, pfd.cStencilBits );
  731. }
  732. if ( !winState.hGLRC )
  733. {
  734. // Create a new rendering context:
  735. Con::printf( "Creating a new rendering context..." );
  736. winState.hGLRC = dwglCreateContext( winState.appDC );
  737. if ( !winState.hGLRC )
  738. {
  739. AssertFatal( false, "OpenGLDevice::setScreenMode\ndwglCreateContext failed to create an OpenGL rendering context!" );
  740. return false;
  741. }
  742. // Make the new rendering context current:
  743. Con::printf( "Making the new rendering context current..." );
  744. if ( !dwglMakeCurrent( winState.appDC, winState.hGLRC ) )
  745. {
  746. AssertFatal( false, "OpenGLDevice::setScreenMode\ndwglMakeCurrent failed to make the rendering context current!" );
  747. return false;
  748. }
  749. // Just for kicks. Seems a relatively central place to put this...
  750. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  751. if ( needResurrect )
  752. {
  753. // Reload the textures:
  754. Con::printf( "Resurrecting the texture manager..." );
  755. Game->textureResurrect();
  756. }
  757. }
  758. // Just for kicks. Seems a relatively central place to put this...
  759. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  760. if ( newDeviceContext && gGLState.suppSwapInterval )
  761. setVerticalSync( !Con::getBoolVariable( "$pref::Video::disableVerticalSync" ) );
  762. smCurrentRes = newRes;
  763. Platform::setWindowSize( newRes.w, newRes.h );
  764. char tempBuf[15];
  765. dSprintf( tempBuf, sizeof( tempBuf ), "%d %d %d", smCurrentRes.w, smCurrentRes.h, smCurrentRes.bpp );
  766. if( fullScreen )
  767. Con::setVariable( "$pref::Video::resolution", tempBuf );
  768. else
  769. Con::setVariable( "$pref::Video::windowedRes", tempBuf );
  770. if ( curtain )
  771. DestroyWindow( curtain );
  772. // Doesn't hurt to do this even it isn't necessary:
  773. ShowWindow( winState.appWindow, SW_SHOW );
  774. SetForegroundWindow( winState.appWindow );
  775. SetFocus( winState.appWindow );
  776. if (newFullScreen)
  777. {
  778. // Doesn't hurt to do this an extra time, and seems to help in some cases
  779. if ( !SetWindowPos( winState.appWindow, HWND_TOPMOST, 0, 0, newRes.w, newRes.h, SWP_NOACTIVATE)
  780. || !SetWindowPos( winState.appWindow, HWND_TOPMOST, 0, 0, newRes.w, newRes.h, SWP_NOZORDER) )
  781. {
  782. dSprintf( errorMessage, 255, "OpenGLDevice::setScreenMode\nSetWindowPos failed to move the window to (0,0) and size it to %dx%d.", newRes.w, newRes.h );
  783. AssertFatal( false, errorMessage );
  784. return false;
  785. }
  786. }
  787. //Luma: Clear window at first, as it is showing previous gl color buffer stuff.
  788. glClearColor(0.0f,0.0f,0.0f,0.0f);
  789. glClear(GL_COLOR_BUFFER_BIT);
  790. if ( repaint )
  791. Con::evaluate( "resetCanvas();" );
  792. return true;
  793. }
  794. //------------------------------------------------------------------------------
  795. void OpenGLDevice::swapBuffers()
  796. {
  797. dwglSwapBuffers( winState.appDC );
  798. }
  799. //------------------------------------------------------------------------------
  800. const char* OpenGLDevice::getDriverInfo()
  801. {
  802. // Output some driver info to the console:
  803. const char* vendorString = (const char*) glGetString( GL_VENDOR );
  804. const char* rendererString = (const char*) glGetString( GL_RENDERER );
  805. const char* versionString = (const char*) glGetString( GL_VERSION );
  806. const char* extensionsString = (const char*) glGetString( GL_EXTENSIONS );
  807. U32 bufferLen = ( vendorString ? dStrlen( vendorString ) : 0 )
  808. + ( rendererString ? dStrlen( rendererString ) : 0 )
  809. + ( versionString ? dStrlen( versionString ) : 0 )
  810. + ( extensionsString ? dStrlen( extensionsString ) : 0 )
  811. + 4;
  812. char* returnString = Con::getReturnBuffer( bufferLen );
  813. dSprintf( returnString, bufferLen, "%s\t%s\t%s\t%s",
  814. ( vendorString ? vendorString : "" ),
  815. ( rendererString ? rendererString : "" ),
  816. ( versionString ? versionString : "" ),
  817. ( extensionsString ? extensionsString : "" ) );
  818. return( returnString );
  819. }
  820. //------------------------------------------------------------------------------
  821. bool OpenGLDevice::getGammaCorrection(F32 &g)
  822. {
  823. U16 ramp[256*3];
  824. if (!GetDeviceGammaRamp(winState.appDC, ramp))
  825. return false;
  826. F32 csum = 0.0;
  827. U32 ccount = 0;
  828. for (U16 i = 0; i < 256; ++i)
  829. {
  830. if (i != 0 && ramp[i] != 0 && ramp[i] != 65535)
  831. {
  832. F64 b = (F64) i/256.0;
  833. F64 a = (F64) ramp[i]/65535.0;
  834. F32 c = (F32) (mLog(a)/mLog(b));
  835. csum += c;
  836. ++ccount;
  837. }
  838. }
  839. g = csum/ccount;
  840. return true;
  841. }
  842. //------------------------------------------------------------------------------
  843. bool OpenGLDevice::setGammaCorrection(F32 g)
  844. {
  845. U16 ramp[256*3];
  846. for (U16 i = 0; i < 256; ++i)
  847. ramp[i] = (U16)((U16)mPow((F32) i/256.0f, g) * 65535.0f);
  848. dMemcpy(&ramp[256],ramp,256*sizeof(U16));
  849. dMemcpy(&ramp[512],ramp,256*sizeof(U16));
  850. return SetDeviceGammaRamp(winState.appDC, ramp);
  851. }
  852. //------------------------------------------------------------------------------
  853. bool OpenGLDevice::setVerticalSync( bool on )
  854. {
  855. if ( !gGLState.suppSwapInterval )
  856. return( false );
  857. return( dwglSwapIntervalEXT( on ? 1 : 0 ) );
  858. }
  859. //------------------------------------------------------------------------------
  860. DisplayDevice* OpenGLDevice::create()
  861. {
  862. // Get Bit Depth Switching Info.
  863. enumerateBitDepths();
  864. bool result = false;
  865. bool fullScreenOnly = false;
  866. //------------------------------------------------------------------------------
  867. // This shouldn't happen, but just to be safe...
  868. //------------------------------------------------------------------------------
  869. if ( winState.hinstOpenGL )
  870. GL_Shutdown();
  871. //------------------------------------------------------------------------------
  872. // Initialize GL
  873. //------------------------------------------------------------------------------
  874. if (!GL_Init( "opengl32", "glu32" ))
  875. return NULL;
  876. //------------------------------------------------------------------------------
  877. // Create a test window to see if OpenGL hardware acceleration is available:
  878. //------------------------------------------------------------------------------
  879. WNDCLASS wc;
  880. dMemset(&wc, 0, sizeof(wc));
  881. wc.style = CS_OWNDC;
  882. wc.lpfnWndProc = DefWindowProc;
  883. wc.hInstance = winState.appInstance;
  884. wc.lpszClassName = dT("OGLTest");
  885. RegisterClass( &wc );
  886. //------------------------------------------------------------------------------
  887. // Create the Test Window
  888. //------------------------------------------------------------------------------
  889. //MIN_RESOLUTION defined in platformWin32/platformGL.h
  890. HWND testWindow = CreateWindow( dT("OGLTest"),dT(""), WS_POPUP, 0, 0, MIN_RESOLUTION_X, MIN_RESOLUTION_Y, NULL, NULL, winState.appInstance, NULL );
  891. if ( !testWindow )
  892. {
  893. // Unregister the Window Class
  894. UnregisterClass( dT("OGLTest"), winState.appInstance );
  895. // Shutdown GL
  896. GL_Shutdown();
  897. // Return Failure
  898. return NULL;
  899. }
  900. //------------------------------------------------------------------------------
  901. // Attempt to Grab a handle to the DeviceContext of our window.
  902. //------------------------------------------------------------------------------
  903. HDC testDC = GetDC( testWindow );
  904. if ( !testDC )
  905. {
  906. // Destroy the Window
  907. DestroyWindow( testWindow );
  908. // Unregister the Window Class
  909. UnregisterClass( dT("OGLTest"), winState.appInstance );
  910. // Shutdown GL
  911. GL_Shutdown();
  912. // Return Failure.
  913. return NULL;
  914. }
  915. //------------------------------------------------------------------------------
  916. // Create Pixel Format ( Default 16bpp )
  917. //------------------------------------------------------------------------------
  918. PIXELFORMATDESCRIPTOR pfd;
  919. CreatePixelFormat( &pfd, 16, 16, 8, false );
  920. U32 chosenFormat = ChooseBestPixelFormat( testDC, &pfd );
  921. if ( chosenFormat != 0 )
  922. {
  923. dwglDescribePixelFormat( testDC, chosenFormat, sizeof( pfd ), &pfd );
  924. result = !( pfd.dwFlags & PFD_GENERIC_FORMAT );
  925. if ( result && winState.desktopBitsPixel < 16 && !smCanDo32Bit)
  926. {
  927. // If Windows 95 cannot switch bit depth, it should only attempt 16-bit cards
  928. // with a 16-bit desktop
  929. // See if we can get a 32-bit pixel format:
  930. PIXELFORMATDESCRIPTOR pfd;
  931. CreatePixelFormat( &pfd, 32, 24, 8, false );
  932. S32 chosenFormat = ChooseBestPixelFormat( testDC, &pfd );
  933. if ( chosenFormat != 0 )
  934. {
  935. dwglDescribePixelFormat( winState.appDC, chosenFormat, sizeof( pfd ), &pfd );
  936. if (pfd.cColorBits == 16)
  937. {
  938. Platform::AlertOK("Requires 16-Bit Desktop",
  939. "You must run in 16-bit color to run a Torque game.\nPlease quit the game, set your desktop color depth to 16-bit\nand then restart the application.");
  940. result = false;
  941. }
  942. }
  943. }
  944. }
  945. else if ( winState.desktopBitsPixel < 16 && smCanSwitchBitDepth )
  946. {
  947. // Try again after changing the display to 16-bit:
  948. ReleaseDC( testWindow, testDC );
  949. DestroyWindow( testWindow );
  950. DEVMODE devMode;
  951. dMemset( &devMode, 0, sizeof( devMode ) );
  952. devMode.dmSize = sizeof( devMode );
  953. devMode.dmBitsPerPel = 16;
  954. devMode.dmFields = DM_BITSPERPEL;
  955. U32 test = ChangeDisplaySettings( &devMode, 0 );
  956. if ( test == DISP_CHANGE_SUCCESSFUL )
  957. {
  958. //MIN_RESOLUTION defined in platformWin32/platformGL.h
  959. testWindow = CreateWindow( dT("OGLTest"), dT(""), WS_OVERLAPPED | WS_CAPTION, 0, 0, MIN_RESOLUTION_X, MIN_RESOLUTION_Y, NULL, NULL, winState.appInstance, NULL );
  960. if ( testWindow )
  961. {
  962. testDC = GetDC( testWindow );
  963. if ( testDC )
  964. {
  965. CreatePixelFormat( &pfd, 16, 16, 8, false );
  966. chosenFormat = ChooseBestPixelFormat( testDC, &pfd );
  967. if ( chosenFormat != 0 )
  968. {
  969. dwglDescribePixelFormat( testDC, chosenFormat, sizeof( pfd ), &pfd );
  970. result = !( pfd.dwFlags & PFD_GENERIC_FORMAT );
  971. if ( result )
  972. fullScreenOnly = true;
  973. }
  974. }
  975. }
  976. }
  977. ChangeDisplaySettings( NULL, 0 );
  978. }
  979. //------------------------------------------------------------------------------
  980. // Can't do even 16 bit, alert user they need to upgrade.
  981. //------------------------------------------------------------------------------
  982. else if ( winState.desktopBitsPixel < 16 && !smCanSwitchBitDepth )
  983. {
  984. Platform::AlertOK("Requires 16-Bit Desktop", "You must run in 16-bit color to run a Torque game.\nPlease quit the game, set your desktop color depth to 16-bit\nand then restart the application.");
  985. }
  986. ReleaseDC( testWindow, testDC );
  987. DestroyWindow( testWindow );
  988. UnregisterClass( dT("OGLTest"), winState.appInstance );
  989. GL_Shutdown();
  990. if ( result )
  991. {
  992. OpenGLDevice* newOGLDevice = new OpenGLDevice();
  993. if ( newOGLDevice )
  994. {
  995. newOGLDevice->mFullScreenOnly = fullScreenOnly;
  996. return (DisplayDevice*) newOGLDevice;
  997. }
  998. else
  999. return NULL;
  1000. }
  1001. else
  1002. return NULL;
  1003. }