platformVideo.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. if (sgOriginalGamma != -1.0 || Video::getGammaCorrection(sgOriginalGamma))
  168. Video::setGammaCorrection(sgOriginalGamma + sgGammaCorrection);
  169. Con::evaluate("resetCanvas();");
  170. }
  171. // Show Maximum Texture Size reported by the graphics hardware.
  172. GLint maxTextureSize;
  173. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
  174. if ( maxTextureSize > 0 )
  175. Con::printf("Max Texture Size reported as: %d", maxTextureSize);
  176. else
  177. Con::warnf("Max Texture Size reported as: %d !", maxTextureSize);
  178. // The video mode activate may have failed above, return that status
  179. return( result );
  180. }
  181. extern bool retinaEnabled;
  182. //------------------------------------------------------------------------------
  183. bool Video::setScreenMode( U32 width, U32 height, U32 bpp, bool fullScreen )
  184. {
  185. if ( smCurrentDevice )
  186. {
  187. //MIN_RESOLUTION defined in platformWin32/platformGL.h
  188. #ifdef TORQUE_OS_IOS
  189. if(width == 0)
  190. width = IOS_DEFAULT_RESOLUTION_X;
  191. if(height == 0)
  192. height = IOS_DEFAULT_RESOLUTION_Y;
  193. if(bpp == 0)
  194. bpp = IOS_DEFAULT_RESOLUTION_BIT_DEPTH;
  195. //if(retinaEnabled)
  196. //{
  197. // width *=2;
  198. // height *=2;
  199. //}
  200. #elif TORQUE_OS_ANDROID
  201. if(width == 0)
  202. width = _AndroidGetScreenWidth();
  203. if(height == 0)
  204. height = _AndroidGetScreenHeight();
  205. if(bpp == 0)
  206. bpp = ANDROID_DEFAULT_RESOLUTION_BIT_DEPTH;
  207. #else
  208. if(width == 0)
  209. width = MIN_RESOLUTION_X;
  210. if(height == 0)
  211. height = MIN_RESOLUTION_Y;
  212. if(bpp == 0)
  213. bpp = MIN_RESOLUTION_BIT_DEPTH;
  214. #endif// TORQUE_OS_IOS
  215. smCritical = true;
  216. bool result = smCurrentDevice->setScreenMode( width, height, bpp, fullScreen );
  217. smCritical = false;
  218. return( result );
  219. }
  220. return( false );
  221. }
  222. //------------------------------------------------------------------------------
  223. void Video::deactivate( bool force )
  224. {
  225. if ( smCritical ) return;
  226. bool doDeactivate = force ? true : DisplayDevice::isFullScreen();
  227. Game->gameDeactivate( doDeactivate );
  228. if ( smCurrentDevice && doDeactivate )
  229. {
  230. smCritical = true;
  231. Game->textureKill();
  232. smCurrentDevice->shutdown();
  233. Platform::minimizeWindow();
  234. smCritical = false;
  235. }
  236. }
  237. //------------------------------------------------------------------------------
  238. void Video::reactivate( bool force )
  239. {
  240. if ( smCritical ) return;
  241. bool doReactivate = force ? true : DisplayDevice::isFullScreen();
  242. if ( smCurrentDevice && doReactivate )
  243. {
  244. Resolution res = DisplayDevice::getResolution();
  245. smCritical = true;
  246. smCurrentDevice->activate(res.w,res.h,res.bpp,DisplayDevice::isFullScreen());
  247. Game->textureResurrect();
  248. smCritical = false;
  249. if (sgOriginalGamma != -1.0)
  250. Video::setGammaCorrection(sgOriginalGamma + sgGammaCorrection);
  251. }
  252. Game->gameReactivate();
  253. }
  254. //------------------------------------------------------------------------------
  255. bool Video::setResolution( U32 width, U32 height, U32 bpp )
  256. {
  257. if ( smCurrentDevice )
  258. {
  259. if ( bpp == 0 )
  260. bpp = DisplayDevice::getResolution().bpp;
  261. smCritical = true;
  262. bool result = smCurrentDevice->setResolution( width, height, bpp );
  263. smCritical = false;
  264. return( result );
  265. }
  266. return( false );
  267. }
  268. //------------------------------------------------------------------------------
  269. bool Video::toggleFullScreen()
  270. {
  271. if ( smCurrentDevice )
  272. {
  273. smCritical = true;
  274. bool result = smCurrentDevice->toggleFullScreen();
  275. smCritical = false;
  276. return( result );
  277. }
  278. return( false );
  279. }
  280. //------------------------------------------------------------------------------
  281. DisplayDevice* Video::getDevice( const char* renderName )
  282. {
  283. for ( S32 i = 0; i < smDeviceList.size(); i++ )
  284. {
  285. if ( dStrcmp( smDeviceList[i]->mDeviceName, renderName ) == 0 )
  286. return( smDeviceList[i] );
  287. }
  288. return( NULL );
  289. }
  290. //------------------------------------------------------------------------------
  291. bool Video::prevRes()
  292. {
  293. if ( smCurrentDevice )
  294. {
  295. smCritical = true;
  296. bool result = smCurrentDevice->prevRes();
  297. smCritical = false;
  298. return( result );
  299. }
  300. return( false );
  301. }
  302. //------------------------------------------------------------------------------
  303. bool Video::nextRes()
  304. {
  305. if ( smCurrentDevice )
  306. {
  307. smCritical = true;
  308. bool result = smCurrentDevice->nextRes();
  309. smCritical = false;
  310. return( result );
  311. }
  312. return( false );
  313. }
  314. //------------------------------------------------------------------------------
  315. Resolution Video::getResolution()
  316. {
  317. return DisplayDevice::getResolution();
  318. }
  319. //------------------------------------------------------------------------------
  320. const char* Video::getDeviceList()
  321. {
  322. U32 deviceCount = smDeviceList.size();
  323. if ( deviceCount > 0 ) // It better be...
  324. {
  325. U32 strLen = 0, i;
  326. for ( i = 0; i < deviceCount; i++ )
  327. strLen += ( dStrlen( smDeviceList[i]->mDeviceName ) + 1 );
  328. char* returnString = Con::getReturnBuffer( strLen );
  329. dStrcpy( returnString, smDeviceList[0]->mDeviceName );
  330. for ( i = 1; i < deviceCount; i++ )
  331. {
  332. dStrcat( returnString, "\t" );
  333. dStrcat( returnString, smDeviceList[i]->mDeviceName );
  334. }
  335. return( returnString );
  336. }
  337. return( NULL );
  338. }
  339. //------------------------------------------------------------------------------
  340. const char* Video::getResolutionList()
  341. {
  342. if ( smCurrentDevice )
  343. return smCurrentDevice->getResolutionList();
  344. else
  345. return NULL;
  346. }
  347. //------------------------------------------------------------------------------
  348. const char* Video::getDriverInfo()
  349. {
  350. if ( smCurrentDevice )
  351. return smCurrentDevice->getDriverInfo();
  352. else
  353. return NULL;
  354. }
  355. //------------------------------------------------------------------------------
  356. bool Video::isFullScreen()
  357. {
  358. return DisplayDevice::isFullScreen();
  359. }
  360. //------------------------------------------------------------------------------
  361. void Video::swapBuffers()
  362. {
  363. if ( smCurrentDevice )
  364. smCurrentDevice->swapBuffers();
  365. }
  366. //------------------------------------------------------------------------------
  367. bool Video::getGammaCorrection(F32 &g)
  368. {
  369. if (smCurrentDevice)
  370. return smCurrentDevice->getGammaCorrection(g);
  371. return false;
  372. }
  373. //------------------------------------------------------------------------------
  374. bool Video::setGammaCorrection(F32 g)
  375. {
  376. if (smCurrentDevice)
  377. return smCurrentDevice->setGammaCorrection(g);
  378. return false;
  379. }
  380. //------------------------------------------------------------------------------
  381. bool Video::setVerticalSync( bool on )
  382. {
  383. if ( smCurrentDevice )
  384. return( smCurrentDevice->setVerticalSync( on ) );
  385. return( false );
  386. }
  387. //------------------------------------------------------------------------------
  388. DisplayDevice::DisplayDevice()
  389. {
  390. mDeviceName = NULL;
  391. }
  392. //------------------------------------------------------------------------------
  393. void DisplayDevice::init()
  394. {
  395. smCurrentRes = Resolution( 0, 0, 0 );
  396. smIsFullScreen = false;
  397. }
  398. //------------------------------------------------------------------------------
  399. bool DisplayDevice::prevRes()
  400. {
  401. U32 resIndex;
  402. for ( resIndex = mResolutionList.size() - 1; resIndex > 0; resIndex-- )
  403. {
  404. if ( mResolutionList[resIndex].bpp == smCurrentRes.bpp
  405. && mResolutionList[resIndex].w <= smCurrentRes.w
  406. && mResolutionList[resIndex].h != smCurrentRes.h )
  407. break;
  408. }
  409. if ( mResolutionList[resIndex].bpp == smCurrentRes.bpp )
  410. return( Video::setResolution( mResolutionList[resIndex].w, mResolutionList[resIndex].h, mResolutionList[resIndex].bpp ) );
  411. return( false );
  412. }
  413. //------------------------------------------------------------------------------
  414. bool DisplayDevice::nextRes()
  415. {
  416. U32 resIndex;
  417. for ( resIndex = 0; resIndex < (U32)mResolutionList.size() - 1; resIndex++ )
  418. {
  419. if ( mResolutionList[resIndex].bpp == smCurrentRes.bpp
  420. && mResolutionList[resIndex].w >= smCurrentRes.w
  421. && mResolutionList[resIndex].h != smCurrentRes.h )
  422. break;
  423. }
  424. if ( mResolutionList[resIndex].bpp == smCurrentRes.bpp )
  425. return( Video::setResolution( mResolutionList[resIndex].w, mResolutionList[resIndex].h, mResolutionList[resIndex].bpp ) );
  426. return( false );
  427. }
  428. //------------------------------------------------------------------------------
  429. // This function returns a string containing all of the available resolutions for this device
  430. // in the format "<bit depth> <width> <height>", separated by tabs.
  431. //
  432. const char* DisplayDevice::getResolutionList()
  433. {
  434. if (Con::getBoolVariable("$pref::Video::clipHigh", false))
  435. for (S32 i = mResolutionList.size()-1; i >= 0; --i)
  436. if (mResolutionList[i].w > 1152 || mResolutionList[i].h > 864)
  437. mResolutionList.erase(i);
  438. if (Con::getBoolVariable("$pref::Video::only16", false))
  439. for (S32 i = mResolutionList.size()-1; i >= 0; --i)
  440. if (mResolutionList[i].bpp == 32)
  441. mResolutionList.erase(i);
  442. U32 resCount = mResolutionList.size();
  443. if ( resCount > 0 )
  444. {
  445. char* tempBuffer = new char[resCount * 15];
  446. tempBuffer[0] = 0;
  447. for ( U32 i = 0; i < resCount; i++ )
  448. {
  449. char newString[15];
  450. dSprintf( newString, sizeof( newString ), "%d %d %d\t", mResolutionList[i].w, mResolutionList[i].h, mResolutionList[i].bpp );
  451. dStrcat( tempBuffer, newString );
  452. }
  453. tempBuffer[dStrlen( tempBuffer ) - 1] = 0;
  454. char* returnString = Con::getReturnBuffer( dStrlen( tempBuffer ) + 1 );
  455. dStrcpy( returnString, tempBuffer );
  456. delete [] tempBuffer;
  457. return returnString;
  458. }
  459. return NULL;
  460. }