x86UNIXOGLVideo.client.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #if 0
  23. #include "console/console.h"
  24. #include "platform/event.h"
  25. #include "platform/gameInterface.h"
  26. #include "platformX86UNIX/platformX86UNIX.h"
  27. #include "platformX86UNIX/platformGL.h"
  28. #include "platformX86UNIX/x86UNIXOGLVideo.h"
  29. #include "platformX86UNIX/x86UNIXState.h"
  30. #include <SDL/SDL.h>
  31. #include <SDL/SDL_syswm.h>
  32. #include <SDL/SDL_version.h>
  33. //------------------------------------------------------------------------------
  34. bool InitOpenGL()
  35. {
  36. DisplayDevice::init();
  37. // Get the video settings from the prefs:
  38. const char* resString = Con::getVariable( "$pref::Video::resolution" );
  39. char* tempBuf = new char[dStrlen( resString ) + 1];
  40. dStrcpy( tempBuf, resString );
  41. char* temp = dStrtok( tempBuf, " x\0" );
  42. U32 width = ( temp ? dAtoi( temp ) : 800 );
  43. temp = dStrtok( NULL, " x\0" );
  44. U32 height = ( temp ? dAtoi( temp ) : 600 );
  45. temp = dStrtok( NULL, "\0" );
  46. U32 bpp = ( temp ? dAtoi( temp ) : 16 );
  47. delete [] tempBuf;
  48. bool fullScreen = Con::getBoolVariable( "$pref::Video::fullScreen" );
  49. // the only supported video device in unix is OpenGL
  50. if ( !Video::setDevice( "OpenGL", width, height, bpp, fullScreen ) )
  51. {
  52. Con::errorf("Unable to create default OpenGL mode: %d %d %d %d",
  53. width, height, bpp, fullScreen);
  54. // if we can't create the default, attempt to create a "safe" window
  55. if ( !Video::setDevice( "OpenGL", 640, 480, 16, true ) )
  56. {
  57. DisplayErrorAlert("Could not find a compatible OpenGL display " \
  58. "resolution. Please check your driver configuration.");
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. //------------------------------------------------------------------------------
  65. bool OpenGLDevice::smCanSwitchBitDepth = false;
  66. //------------------------------------------------------------------------------
  67. OpenGLDevice::OpenGLDevice()
  68. {
  69. initDevice();
  70. }
  71. //------------------------------------------------------------------------------
  72. OpenGLDevice::~OpenGLDevice()
  73. {
  74. }
  75. //------------------------------------------------------------------------------
  76. void OpenGLDevice::addResolution(S32 width, S32 height, bool check)
  77. {
  78. Point2I desktopSize = x86UNIXState->getDesktopSize();
  79. U32 desktopBpp = x86UNIXState->getDesktopBpp();
  80. // don't allow any resolution under this size
  81. if (width < 640 || height < 480)
  82. return;
  83. if (check)
  84. {
  85. // don't allow resolutions that exceed the current desktop size
  86. if (width > desktopSize.x || height > desktopSize.y)
  87. return;
  88. }
  89. if (smCanSwitchBitDepth)
  90. {
  91. // add both 16 and 32 bit resolutions
  92. mResolutionList.push_back(Resolution(width, height, 16));
  93. mResolutionList.push_back(Resolution(width, height, 32));
  94. }
  95. else
  96. {
  97. // add just the desktop resolution
  98. mResolutionList.push_back(Resolution(width, height, desktopBpp));
  99. }
  100. }
  101. //------------------------------------------------------------------------------
  102. void OpenGLDevice::initDevice()
  103. {
  104. mDeviceName = "OpenGL";
  105. mFullScreenOnly = false;
  106. }
  107. //------------------------------------------------------------------------------
  108. void OpenGLDevice::loadResolutions()
  109. {
  110. mResolutionList.clear();
  111. // X cannot switch bit depths on the fly. In case this feature is
  112. // implemented someday, calling this function will let you take
  113. // advantage of it
  114. if (Con::getBoolVariable("$pref::Unix::CanSwitchBitDepth"))
  115. smCanSwitchBitDepth = true;
  116. // add some default resolutions
  117. addResolution(640, 480);
  118. addResolution(800, 600);
  119. addResolution(1024, 768);
  120. addResolution(1152, 864);
  121. addResolution(1280, 1024);
  122. addResolution(1600, 1200);
  123. // specifying full screen should give us the resolutions that the
  124. // X server allows
  125. SDL_Rect** modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
  126. if (modes &&
  127. (modes != (SDL_Rect **)-1))
  128. {
  129. for (int i = 0; modes[i] != NULL; ++i)
  130. {
  131. // do we already have this mode?
  132. bool found = false;
  133. for (Vector<Resolution>::iterator iter = mResolutionList.begin();
  134. iter != mResolutionList.end();
  135. ++iter)
  136. {
  137. if (iter->w == modes[i]->w && iter->h == modes[i]->h)
  138. {
  139. found = true;
  140. break;
  141. }
  142. }
  143. if (!found)
  144. // don't check these resolutions because they should be OK
  145. // (and checking might drop resolutions that are higher than the
  146. // current desktop bpp)
  147. addResolution(modes[i]->w, modes[i]->h, false);
  148. }
  149. }
  150. }
  151. //------------------------------------------------------------------------------
  152. bool OpenGLDevice::activate( U32 width, U32 height, U32 bpp, bool fullScreen )
  153. {
  154. if (!setScreenMode(width, height, bpp, fullScreen))
  155. {
  156. Con::printf("Unable to set screen mode.");
  157. return false;
  158. }
  159. // Output some driver info to the console
  160. const char* vendorString = (const char*) glGetString( GL_VENDOR );
  161. const char* rendererString = (const char*) glGetString( GL_RENDERER );
  162. const char* versionString = (const char*) glGetString( GL_VERSION );
  163. Con::printf( "OpenGL driver information:" );
  164. if ( vendorString )
  165. Con::printf( " Vendor: %s", vendorString );
  166. if ( rendererString )
  167. Con::printf( " Renderer: %s", rendererString );
  168. if ( versionString )
  169. Con::printf( " Version: %s", versionString );
  170. GL_EXT_Init();
  171. Con::setVariable( "$pref::Video::displayDevice", mDeviceName );
  172. // Do this here because we now know about the extensions:
  173. if ( gGLState.suppSwapInterval )
  174. setVerticalSync(
  175. !Con::getBoolVariable( "$pref::Video::disableVerticalSync" ) );
  176. Con::setBoolVariable("$pref::OpenGL::allowTexGen", true);
  177. return true;
  178. }
  179. //------------------------------------------------------------------------------
  180. void OpenGLDevice::shutdown()
  181. {
  182. // Shutdown is deferred to Platform::shutdown()
  183. }
  184. //------------------------------------------------------------------------------
  185. static void PrintGLAttributes()
  186. {
  187. int doubleBuf;
  188. int bufferSize, depthSize, stencilSize;
  189. int red, green, blue, alpha;
  190. int aRed, aGreen, aBlue, aAlpha;
  191. SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doubleBuf);
  192. SDL_GL_GetAttribute(SDL_GL_BUFFER_SIZE, &bufferSize);
  193. SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depthSize);
  194. SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &stencilSize);
  195. SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red);
  196. SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green);
  197. SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &blue);
  198. SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &alpha);
  199. SDL_GL_GetAttribute(SDL_GL_ACCUM_RED_SIZE, &aRed);
  200. SDL_GL_GetAttribute(SDL_GL_ACCUM_GREEN_SIZE, &aGreen);
  201. SDL_GL_GetAttribute(SDL_GL_ACCUM_BLUE_SIZE, &aBlue);
  202. SDL_GL_GetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, &aAlpha);
  203. Con::printf("OpenGL Attributes:");
  204. Con::printf(" DoubleBuffer: %d", doubleBuf);
  205. Con::printf(" BufferSize: %d, DepthSize: %d, StencilSize: %d",
  206. bufferSize, depthSize, stencilSize);
  207. Con::printf(" Red: %d, Green: %d, Blue: %d, Alpha: %d",
  208. red, green, blue, alpha);
  209. Con::printf(" Accum Red: %d, Green: %d, Blue: %d, Alpha: %d",
  210. aRed, aGreen, aBlue, aAlpha);
  211. }
  212. //------------------------------------------------------------------------------
  213. bool OpenGLDevice::setScreenMode( U32 width, U32 height, U32 bpp,
  214. bool fullScreen, bool forceIt, bool repaint )
  215. {
  216. // load resolutions, this is done lazily so that we can check the setting
  217. // of smCanSwitchBitDepth, which may be overridden by console
  218. if (mResolutionList.size()==0)
  219. loadResolutions();
  220. if (mResolutionList.size()==0)
  221. {
  222. Con::printf("No resolutions available!");
  223. return false;
  224. }
  225. if (bpp == 0)
  226. {
  227. // bpp comes in as "0" when it is set to "Default"
  228. bpp = x86UNIXState->getDesktopBpp();
  229. }
  230. if (height == 0 || width == 0)
  231. {
  232. // paranoia check. set it to the default to prevent crashing
  233. width = 800;
  234. height = 600;
  235. }
  236. U32 desktopDepth = x86UNIXState->getDesktopBpp();
  237. // if we can't switch bit depths and the requested bpp is not equal to
  238. // the desktop bpp, set bpp to the desktop bpp
  239. if (!smCanSwitchBitDepth &&
  240. bpp != desktopDepth)
  241. {
  242. bpp = desktopDepth;
  243. }
  244. bool IsInList = false;
  245. Resolution NewResolution( width, height, bpp );
  246. // See if the desired resolution is in the list
  247. if ( mResolutionList.size() )
  248. {
  249. for ( int i = 0; i < mResolutionList.size(); i++ )
  250. {
  251. if ( width == mResolutionList[i].w
  252. && height == mResolutionList[i].h
  253. && bpp == mResolutionList[i].bpp )
  254. {
  255. IsInList = true;
  256. break;
  257. }
  258. }
  259. if ( !IsInList )
  260. {
  261. Con::printf( "Selected resolution not available: %d %d %d",
  262. width, height, bpp);
  263. return false;
  264. }
  265. }
  266. else
  267. {
  268. AssertFatal( false, "No resolution list found!!" );
  269. }
  270. // Here if we found a matching resolution in the list
  271. bool needResurrect = false;
  272. if (x86UNIXState->windowCreated())
  273. {
  274. Con::printf( "Killing the texture manager..." );
  275. Game->textureKill();
  276. needResurrect = true;
  277. }
  278. // Set the desired GL Attributes
  279. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  280. // JMQ: NVIDIA 2802+ doesn't like this setting for stencil size
  281. // SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  282. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  283. SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0);
  284. SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 0);
  285. SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 0);
  286. SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0);
  287. // SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  288. // SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  289. // SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
  290. // SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  291. U32 flags = SDL_OPENGL;
  292. if (fullScreen)
  293. flags |= SDL_FULLSCREEN;
  294. Con::printf( "Setting screen mode to %dx%dx%d (%s)...", width, height,
  295. bpp, ( fullScreen ? "fs" : "w" ) );
  296. // set the new video mode
  297. if (SDL_SetVideoMode(width, height, bpp, flags) == NULL)
  298. {
  299. Con::printf("Unable to set SDL Video Mode: %s", SDL_GetError());
  300. return false;
  301. }
  302. PrintGLAttributes();
  303. // clear screen here to prevent buffer garbage from being displayed when
  304. // video mode is switched
  305. glClearColor(0.0, 0.0, 0.0, 0.0);
  306. glClear(GL_COLOR_BUFFER_BIT);
  307. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  308. if ( needResurrect )
  309. {
  310. // Reload the textures:
  311. Con::printf( "Resurrecting the texture manager..." );
  312. Game->textureResurrect();
  313. }
  314. if ( gGLState.suppSwapInterval )
  315. setVerticalSync( !Con::getBoolVariable( "$pref::Video::disableVerticalSync" ) );
  316. // reset the window in platform state
  317. SDL_SysWMinfo sysinfo;
  318. SDL_VERSION(&sysinfo.version);
  319. if (SDL_GetWMInfo(&sysinfo) == 0)
  320. {
  321. Con::printf("Unable to set SDL Video Mode: %s", SDL_GetError());
  322. return false;
  323. }
  324. x86UNIXState->setWindow(sysinfo.info.x11.window);
  325. // set various other parameters
  326. x86UNIXState->setWindowCreated(true);
  327. smCurrentRes = NewResolution;
  328. Platform::setWindowSize ( width, height );
  329. smIsFullScreen = fullScreen;
  330. Con::setBoolVariable( "$pref::Video::fullScreen", smIsFullScreen );
  331. char tempBuf[15];
  332. dSprintf( tempBuf, sizeof( tempBuf ), "%d %d %d",
  333. smCurrentRes.w, smCurrentRes.h, smCurrentRes.bpp );
  334. Con::setVariable( "$pref::Video::resolution", tempBuf );
  335. // post a TORQUE_SETVIDEOMODE user event
  336. SDL_Event event;
  337. event.type = SDL_USEREVENT;
  338. event.user.code = TORQUE_SETVIDEOMODE;
  339. event.user.data1 = NULL;
  340. event.user.data2 = NULL;
  341. SDL_PushEvent(&event);
  342. // reset the caption
  343. SDL_WM_SetCaption(x86UNIXState->getWindowName(), NULL);
  344. // repaint
  345. if ( repaint )
  346. Con::evaluate( "resetCanvas();" );
  347. return true;
  348. }
  349. //------------------------------------------------------------------------------
  350. void OpenGLDevice::swapBuffers()
  351. {
  352. SDL_GL_SwapBuffers();
  353. }
  354. //------------------------------------------------------------------------------
  355. const char* OpenGLDevice::getDriverInfo()
  356. {
  357. const char* vendorString = (const char*) glGetString( GL_VENDOR );
  358. const char* rendererString = (const char*) glGetString( GL_RENDERER );
  359. const char* versionString = (const char*) glGetString( GL_VERSION );
  360. const char* extensionsString = (const char*) glGetString( GL_EXTENSIONS );
  361. U32 bufferLen = ( vendorString ? dStrlen( vendorString ) : 0 )
  362. + ( rendererString ? dStrlen( rendererString ) : 0 )
  363. + ( versionString ? dStrlen( versionString ) : 0 )
  364. + ( extensionsString ? dStrlen( extensionsString ) : 0 )
  365. + 4;
  366. char* returnString = Con::getReturnBuffer( bufferLen );
  367. dSprintf( returnString, bufferLen, "%s\t%s\t%s\t%s",
  368. ( vendorString ? vendorString : "" ),
  369. ( rendererString ? rendererString : "" ),
  370. ( versionString ? versionString : "" ),
  371. ( extensionsString ? extensionsString : "" ) );
  372. return( returnString );
  373. }
  374. //------------------------------------------------------------------------------
  375. bool OpenGLDevice::getGammaCorrection(F32 &g)
  376. {
  377. U16 redtable[256];
  378. U16 greentable[256];
  379. U16 bluetable[256];
  380. if (SDL_GetGammaRamp(redtable, greentable, bluetable) == -1)
  381. {
  382. Con::warnf("getGammaCorrection error: %s", SDL_GetError());
  383. return false;
  384. }
  385. F32 csum = 0.0;
  386. U32 ccount = 0;
  387. for (U16 i = 0; i < 256; ++i)
  388. {
  389. if (i != 0 && redtable[i] != 0 && redtable[i] != 65535)
  390. {
  391. F64 b = (F64) i/256.0;
  392. F64 a = (F64) redtable[i]/65535.0;
  393. F32 c = (F32) (mLog(a)/mLog(b));
  394. csum += c;
  395. ++ccount;
  396. }
  397. }
  398. g = csum/ccount;
  399. return true;
  400. }
  401. //------------------------------------------------------------------------------
  402. bool OpenGLDevice::setGammaCorrection(F32 g)
  403. {
  404. U16 redtable[256];
  405. U16 greentable[256];
  406. U16 bluetable[256];
  407. for (U16 i = 0; i < 256; ++i)
  408. redtable[i] = static_cast<U16>(mPow((F32) i/256.0f, g) * 65535.0f);
  409. dMemcpy(greentable,redtable,256*sizeof(U16));
  410. dMemcpy(bluetable,redtable,256*sizeof(U16));
  411. S32 ok = SDL_SetGammaRamp(redtable, greentable, bluetable);
  412. if (ok == -1)
  413. Con::warnf("Error setting gamma correction: %s", SDL_GetError());
  414. return ok != -1;
  415. }
  416. //------------------------------------------------------------------------------
  417. bool OpenGLDevice::setVerticalSync( bool on )
  418. {
  419. Con::printf("WARNING: OpenGLDevice::setVerticalSync is unimplemented %s %d\n", __FILE__, __LINE__);
  420. return false;
  421. #if 0
  422. if ( !gGLState.suppSwapInterval )
  423. return( false );
  424. return( qwglSwapIntervalEXT( on ? 1 : 0 ) );
  425. #endif
  426. }
  427. //------------------------------------------------------------------------------
  428. DisplayDevice* OpenGLDevice::create()
  429. {
  430. return new OpenGLDevice();
  431. }
  432. #endif // 0