platformVideo.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 "platform/platformVideo.h"
  23. #include "gui/guiCanvas.h"
  24. #include "console/console.h"
  25. #include "game/gameInterface.h"
  26. #include "platformVideo_ScriptBinding.h"
  27. extern void GameDeactivate( bool noRender );
  28. extern void GameReactivate();
  29. // Static class data:
  30. Vector<DisplayDevice*> Video::smDeviceList;
  31. DisplayDevice* Video::smCurrentDevice;
  32. bool Video::smCritical = false;
  33. bool Video::smNeedResurrect = false;
  34. Resolution DisplayDevice::smCurrentRes;
  35. bool DisplayDevice::smIsFullScreen;
  36. //------------------------------------------------------------------------------
  37. void Video::init()
  38. {
  39. Con::printSeparator();
  40. Con::printf("Video initialization:");
  41. destroy();
  42. }
  43. //------------------------------------------------------------------------------
  44. void Video::destroy()
  45. {
  46. if ( smCurrentDevice )
  47. {
  48. smCritical = true;
  49. smCurrentDevice->shutdown();
  50. smCritical = false;
  51. }
  52. smCurrentDevice = NULL;
  53. for ( U32 i = 0; i < (U32)smDeviceList.size(); i++ )
  54. delete smDeviceList[i];
  55. smDeviceList.clear();
  56. }
  57. //------------------------------------------------------------------------------
  58. bool Video::installDevice( DisplayDevice *dev )
  59. {
  60. if ( dev )
  61. {
  62. smDeviceList.push_back( dev );
  63. return true;
  64. }
  65. return false;
  66. }
  67. //------------------------------------------------------------------------------
  68. bool Video::setDevice( const char *renderName, U32 width, U32 height, U32 bpp, bool fullScreen )
  69. {
  70. S32 deviceIndex = NO_DEVICE;
  71. S32 iOpenGL = -1;
  72. S32 iD3D = -1;
  73. bool bOpenglRender = true; //(bool)(dStricmp(renderName,"OpenGL") == 0);
  74. bool bD3DRender = false; //(bool)(dStricmp(renderName,"D3D") == 0);
  75. bool bAllowD3D = false; //Con::getBoolVariable("$pref::Video::allowD3D");
  76. bool bAllowOpengl = true; //Con::getBoolVariable("$pref::Video::allowOpenGL");
  77. for ( S32 i = 0; i < smDeviceList.size(); i++ )
  78. {
  79. if ( dStrcmp( smDeviceList[i]->mDeviceName, renderName ) == 0 )
  80. deviceIndex = i;
  81. if ( dStrcmp( smDeviceList[i]->mDeviceName, "OpenGL" ) == 0 )
  82. iOpenGL = i;
  83. if ( dStrcmp( smDeviceList[i]->mDeviceName, "D3D" ) == 0 )
  84. iD3D = i;
  85. }
  86. if ( deviceIndex == NO_DEVICE )
  87. {
  88. Con::warnf( ConsoleLogEntry::General, "\"%s\" display device not found!", renderName );
  89. return false;
  90. }
  91. // Change the display device:
  92. if ( smDeviceList[deviceIndex] == NULL )
  93. return false;
  94. if (smCurrentDevice && smCurrentDevice != smDeviceList[deviceIndex])
  95. {
  96. Con::printf( "Deactivating the previous display device..." );
  97. Game->textureKill();
  98. smNeedResurrect = true;
  99. smCurrentDevice->shutdown();
  100. }
  101. if (iOpenGL != -1 && !bAllowOpengl )
  102. {
  103. // change to D3D, delete OpenGL in the recursive call
  104. if ( bOpenglRender )
  105. {
  106. U32 w, h, d;
  107. if( fullScreen )
  108. dSscanf(Con::getVariable("$pref::Video::resolution"), "%d %d %d", &w, &h, &d);
  109. else
  110. dSscanf(Con::getVariable("$pref::Video::windowedRes"), "%d %d %d", &w, &h, &d);
  111. return setDevice("D3D",w,h,d,fullScreen);
  112. }
  113. else
  114. {
  115. delete smDeviceList[iOpenGL];
  116. smDeviceList.erase(iOpenGL);
  117. }
  118. }
  119. else if (iD3D != -1 && !bAllowD3D )
  120. {
  121. // change to OpenGL, delete D3D in the recursive call
  122. if ( bD3DRender )
  123. {
  124. U32 w, h, d;
  125. if( fullScreen )
  126. dSscanf(Con::getVariable("$pref::Video::resolution"), "%d %d %d", &w, &h, &d);
  127. else
  128. dSscanf(Con::getVariable("$pref::Video::windowedRes"), "%d %d %d", &w, &h, &d);
  129. return setDevice("OpenGL",w,h,d,fullScreen);
  130. }
  131. else
  132. {
  133. delete smDeviceList[iD3D];
  134. smDeviceList.erase(iD3D);
  135. }
  136. }
  137. else if (iD3D != -1 && bOpenglRender &&
  138. !Con::getBoolVariable("$pref::Video::preferOpenGL") &&
  139. !Con::getBoolVariable("$pref::Video::appliedPref"))
  140. {
  141. U32 w, h, d;
  142. if( fullScreen )
  143. dSscanf(Con::getVariable("$pref::Video::resolution"), "%d %d %d", &w, &h, &d);
  144. else
  145. dSscanf(Con::getVariable("$pref::Video::windowedRes"), "%d %d %d", &w, &h, &d);
  146. Con::setBoolVariable("$pref::Video::appliedPref", true);
  147. return setDevice("D3D",w,h,d,fullScreen);
  148. }
  149. else
  150. Con::setBoolVariable("$pref::Video::appliedPref", true);
  151. Con::printf( "Activating the %s display device...", renderName );
  152. smCurrentDevice = smDeviceList[deviceIndex];
  153. smCritical = true;
  154. bool result = smCurrentDevice->activate( width, height, bpp, fullScreen );
  155. smCritical = false;
  156. if ( result )
  157. {
  158. if (smNeedResurrect)
  159. {
  160. Game->textureResurrect();
  161. smNeedResurrect = false;
  162. }
  163. if (sgOriginalGamma != -1.0 || Video::getGammaCorrection(sgOriginalGamma))
  164. Video::setGammaCorrection(sgOriginalGamma + sgGammaCorrection);
  165. Con::evaluate("resetCanvas();");
  166. }
  167. // Show Maximum Texture Size reported by the graphics hardware.
  168. GLint maxTextureSize;
  169. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
  170. if ( maxTextureSize > 0 )
  171. Con::printf("Max Texture Size reported as: %d", maxTextureSize);
  172. else
  173. Con::warnf("Max Texture Size reported as: %d !", maxTextureSize);
  174. // The video mode activate may have failed above, return that status
  175. return( result );
  176. }
  177. extern bool retinaEnabled;
  178. //------------------------------------------------------------------------------
  179. bool Video::setScreenMode( U32 width, U32 height, U32 bpp, bool fullScreen )
  180. {
  181. if ( smCurrentDevice )
  182. {
  183. //MIN_RESOLUTION defined in platformWin32/platformGL.h
  184. #ifdef TORQUE_OS_IOS
  185. if(width == 0)
  186. width = IOS_DEFAULT_RESOLUTION_X;
  187. if(height == 0)
  188. height = IOS_DEFAULT_RESOLUTION_Y;
  189. if(bpp == 0)
  190. bpp = IOS_DEFAULT_RESOLUTION_BIT_DEPTH;
  191. //if(retinaEnabled)
  192. //{
  193. // width *=2;
  194. // height *=2;
  195. //}
  196. #else
  197. if(width == 0)
  198. width = MIN_RESOLUTION_X;
  199. if(height == 0)
  200. height = MIN_RESOLUTION_Y;
  201. if(bpp == 0)
  202. bpp = MIN_RESOLUTION_BIT_DEPTH;
  203. #endif// TORQUE_OS_IOS
  204. smCritical = true;
  205. bool result = smCurrentDevice->setScreenMode( width, height, bpp, fullScreen );
  206. smCritical = false;
  207. return( result );
  208. }
  209. return( false );
  210. }
  211. //------------------------------------------------------------------------------
  212. void Video::deactivate( bool force )
  213. {
  214. if ( smCritical ) return;
  215. bool doDeactivate = force ? true : DisplayDevice::isFullScreen();
  216. Game->gameDeactivate( doDeactivate );
  217. if ( smCurrentDevice && doDeactivate )
  218. {
  219. smCritical = true;
  220. Game->textureKill();
  221. smCurrentDevice->shutdown();
  222. Platform::minimizeWindow();
  223. smCritical = false;
  224. }
  225. }
  226. //------------------------------------------------------------------------------
  227. void Video::reactivate( bool force )
  228. {
  229. if ( smCritical ) return;
  230. bool doReactivate = force ? true : DisplayDevice::isFullScreen();
  231. if ( smCurrentDevice && doReactivate )
  232. {
  233. Resolution res = DisplayDevice::getResolution();
  234. smCritical = true;
  235. smCurrentDevice->activate(res.w,res.h,res.bpp,DisplayDevice::isFullScreen());
  236. Game->textureResurrect();
  237. smCritical = false;
  238. if (sgOriginalGamma != -1.0)
  239. Video::setGammaCorrection(sgOriginalGamma + sgGammaCorrection);
  240. }
  241. Game->gameReactivate();
  242. }
  243. //------------------------------------------------------------------------------
  244. bool Video::setResolution( U32 width, U32 height, U32 bpp )
  245. {
  246. if ( smCurrentDevice )
  247. {
  248. if ( bpp == 0 )
  249. bpp = DisplayDevice::getResolution().bpp;
  250. smCritical = true;
  251. bool result = smCurrentDevice->setResolution( width, height, bpp );
  252. smCritical = false;
  253. return( result );
  254. }
  255. return( false );
  256. }
  257. //------------------------------------------------------------------------------
  258. bool Video::toggleFullScreen()
  259. {
  260. if ( smCurrentDevice )
  261. {
  262. smCritical = true;
  263. bool result = smCurrentDevice->toggleFullScreen();
  264. smCritical = false;
  265. return( result );
  266. }
  267. return( false );
  268. }
  269. //------------------------------------------------------------------------------
  270. DisplayDevice* Video::getDevice( const char* renderName )
  271. {
  272. for ( S32 i = 0; i < smDeviceList.size(); i++ )
  273. {
  274. if ( dStrcmp( smDeviceList[i]->mDeviceName, renderName ) == 0 )
  275. return( smDeviceList[i] );
  276. }
  277. return( NULL );
  278. }
  279. //------------------------------------------------------------------------------
  280. bool Video::prevRes()
  281. {
  282. if ( smCurrentDevice )
  283. {
  284. smCritical = true;
  285. bool result = smCurrentDevice->prevRes();
  286. smCritical = false;
  287. return( result );
  288. }
  289. return( false );
  290. }
  291. //------------------------------------------------------------------------------
  292. bool Video::nextRes()
  293. {
  294. if ( smCurrentDevice )
  295. {
  296. smCritical = true;
  297. bool result = smCurrentDevice->nextRes();
  298. smCritical = false;
  299. return( result );
  300. }
  301. return( false );
  302. }
  303. //------------------------------------------------------------------------------
  304. Resolution Video::getResolution()
  305. {
  306. return DisplayDevice::getResolution();
  307. }
  308. //------------------------------------------------------------------------------
  309. const char* Video::getDeviceList()
  310. {
  311. U32 deviceCount = smDeviceList.size();
  312. if ( deviceCount > 0 ) // It better be...
  313. {
  314. U32 strLen = 0, i;
  315. for ( i = 0; i < deviceCount; i++ )
  316. strLen += ( dStrlen( smDeviceList[i]->mDeviceName ) + 1 );
  317. char* returnString = Con::getReturnBuffer( strLen );
  318. dStrcpy( returnString, smDeviceList[0]->mDeviceName );
  319. for ( i = 1; i < deviceCount; i++ )
  320. {
  321. dStrcat( returnString, "\t" );
  322. dStrcat( returnString, smDeviceList[i]->mDeviceName );
  323. }
  324. return( returnString );
  325. }
  326. return( NULL );
  327. }
  328. //------------------------------------------------------------------------------
  329. const char* Video::getResolutionList()
  330. {
  331. if ( smCurrentDevice )
  332. return smCurrentDevice->getResolutionList();
  333. else
  334. return NULL;
  335. }
  336. //------------------------------------------------------------------------------
  337. const char* Video::getDriverInfo()
  338. {
  339. if ( smCurrentDevice )
  340. return smCurrentDevice->getDriverInfo();
  341. else
  342. return NULL;
  343. }
  344. //------------------------------------------------------------------------------
  345. bool Video::isFullScreen()
  346. {
  347. return DisplayDevice::isFullScreen();
  348. }
  349. //------------------------------------------------------------------------------
  350. void Video::swapBuffers()
  351. {
  352. if ( smCurrentDevice )
  353. smCurrentDevice->swapBuffers();
  354. }
  355. //------------------------------------------------------------------------------
  356. bool Video::getGammaCorrection(F32 &g)
  357. {
  358. if (smCurrentDevice)
  359. return smCurrentDevice->getGammaCorrection(g);
  360. return false;
  361. }
  362. //------------------------------------------------------------------------------
  363. bool Video::setGammaCorrection(F32 g)
  364. {
  365. if (smCurrentDevice)
  366. return smCurrentDevice->setGammaCorrection(g);
  367. return false;
  368. }
  369. //------------------------------------------------------------------------------
  370. bool Video::setVerticalSync( bool on )
  371. {
  372. if ( smCurrentDevice )
  373. return( smCurrentDevice->setVerticalSync( on ) );
  374. return( false );
  375. }
  376. //------------------------------------------------------------------------------
  377. DisplayDevice::DisplayDevice()
  378. {
  379. mDeviceName = NULL;
  380. }
  381. //------------------------------------------------------------------------------
  382. void DisplayDevice::init()
  383. {
  384. smCurrentRes = Resolution( 0, 0, 0 );
  385. smIsFullScreen = false;
  386. }
  387. //------------------------------------------------------------------------------
  388. bool DisplayDevice::prevRes()
  389. {
  390. U32 resIndex;
  391. for ( resIndex = mResolutionList.size() - 1; resIndex > 0; resIndex-- )
  392. {
  393. if ( mResolutionList[resIndex].bpp == smCurrentRes.bpp
  394. && mResolutionList[resIndex].w <= smCurrentRes.w
  395. && mResolutionList[resIndex].h != smCurrentRes.h )
  396. break;
  397. }
  398. if ( mResolutionList[resIndex].bpp == smCurrentRes.bpp )
  399. return( Video::setResolution( mResolutionList[resIndex].w, mResolutionList[resIndex].h, mResolutionList[resIndex].bpp ) );
  400. return( false );
  401. }
  402. //------------------------------------------------------------------------------
  403. bool DisplayDevice::nextRes()
  404. {
  405. U32 resIndex;
  406. for ( resIndex = 0; resIndex < (U32)mResolutionList.size() - 1; resIndex++ )
  407. {
  408. if ( mResolutionList[resIndex].bpp == smCurrentRes.bpp
  409. && mResolutionList[resIndex].w >= smCurrentRes.w
  410. && mResolutionList[resIndex].h != smCurrentRes.h )
  411. break;
  412. }
  413. if ( mResolutionList[resIndex].bpp == smCurrentRes.bpp )
  414. return( Video::setResolution( mResolutionList[resIndex].w, mResolutionList[resIndex].h, mResolutionList[resIndex].bpp ) );
  415. return( false );
  416. }
  417. //------------------------------------------------------------------------------
  418. // This function returns a string containing all of the available resolutions for this device
  419. // in the format "<bit depth> <width> <height>", separated by tabs.
  420. //
  421. const char* DisplayDevice::getResolutionList()
  422. {
  423. if (Con::getBoolVariable("$pref::Video::clipHigh", false))
  424. for (S32 i = mResolutionList.size()-1; i >= 0; --i)
  425. if (mResolutionList[i].w > 1152 || mResolutionList[i].h > 864)
  426. mResolutionList.erase(i);
  427. if (Con::getBoolVariable("$pref::Video::only16", false))
  428. for (S32 i = mResolutionList.size()-1; i >= 0; --i)
  429. if (mResolutionList[i].bpp == 32)
  430. mResolutionList.erase(i);
  431. U32 resCount = mResolutionList.size();
  432. if ( resCount > 0 )
  433. {
  434. char* tempBuffer = new char[resCount * 15];
  435. tempBuffer[0] = 0;
  436. for ( U32 i = 0; i < resCount; i++ )
  437. {
  438. char newString[15];
  439. dSprintf( newString, sizeof( newString ), "%d %d %d\t", mResolutionList[i].w, mResolutionList[i].h, mResolutionList[i].bpp );
  440. dStrcat( tempBuffer, newString );
  441. }
  442. tempBuffer[dStrlen( tempBuffer ) - 1] = 0;
  443. char* returnString = Con::getReturnBuffer( dStrlen( tempBuffer ) + 1 );
  444. dStrcpy( returnString, tempBuffer );
  445. delete [] tempBuffer;
  446. return returnString;
  447. }
  448. return NULL;
  449. }