platformVideo.cc 16 KB

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