CmSDLWindow.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmSDLWindow.h"
  25. #include "CmRenderSystem.h"
  26. #include "CmException.h"
  27. #include "CmStringConverter.h"
  28. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
  29. # include <windows.h>
  30. # include <wingdi.h>
  31. # include <GL/gl.h>
  32. # define GL_GLEXT_PROTOTYPES
  33. # include "glprocs.h"
  34. # include <GL/glu.h>
  35. #elif OGRE_PLATFORM == OGRE_PLATFORM_LINUX
  36. # include <GL/gl.h>
  37. # include <GL/glu.h>
  38. #elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE
  39. # include <OpenGL/gl.h>
  40. # define GL_EXT_texture_env_combine 1
  41. # include <OpenGL/glext.h>
  42. # include <OpenGL/glu.h>
  43. #endif
  44. namespace CamelotEngine {
  45. SDLWindow::SDLWindow() :
  46. mScreen(NULL), mActive(false), mClosed(false)
  47. {
  48. }
  49. SDLWindow::~SDLWindow()
  50. {
  51. // according to http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSetVideoMode
  52. // never free the surface returned from SDL_SetVideoMode
  53. /*if (mScreen != NULL)
  54. SDL_FreeSurface(mScreen);*/
  55. }
  56. void SDLWindow::create(const String& name, unsigned int width, unsigned int height,
  57. bool fullScreen, const NameValuePairList *miscParams)
  58. {
  59. int colourDepth = 32;
  60. String title = name;
  61. if(miscParams)
  62. {
  63. // Parse miscellenous parameters
  64. NameValuePairList::const_iterator opt;
  65. // Bit depth
  66. opt = miscParams->find("colourDepth");
  67. if(opt != miscParams->end()) //check for FSAA parameter, if not ignore it...
  68. colourDepth = StringConverter::parseUnsignedInt(opt->second);
  69. // Full screen antialiasing
  70. opt = miscParams->find("FSAA");
  71. if(opt != miscParams->end()) //check for FSAA parameter, if not ignore it...
  72. {
  73. size_t fsaa_x_samples = StringConverter::parseUnsignedInt(opt->second);
  74. if(fsaa_x_samples>1) {
  75. // If FSAA is enabled in the parameters, enable the MULTISAMPLEBUFFERS
  76. // and set the number of samples before the render window is created.
  77. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1);
  78. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,fsaa_x_samples);
  79. }
  80. }
  81. // Window title
  82. opt = miscParams->find("title");
  83. if(opt != miscParams->end()) //check for FSAA parameter, if not ignore it...
  84. title = opt->second;
  85. }
  86. LogManager::getSingleton().logMessage("SDLWindow::create", LML_TRIVIAL);
  87. SDL_Surface* screen;
  88. int flags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE;
  89. SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  90. // request good stencil size if 32-bit colour
  91. if (colourDepth == 32)
  92. {
  93. SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8);
  94. }
  95. if (fullScreen)
  96. flags |= SDL_FULLSCREEN;
  97. LogManager::getSingleton().logMessage("Create window", LML_TRIVIAL);
  98. screen = SDL_SetVideoMode(width, height, colourDepth, flags);
  99. if (!screen)
  100. {
  101. LogManager::getSingleton().logMessage(LML_CRITICAL,
  102. String("Could not make screen: ") + SDL_GetError());
  103. exit(1);
  104. }
  105. LogManager::getSingleton().logMessage("screen is valid", LML_TRIVIAL);
  106. mScreen = screen;
  107. mName = name;
  108. mWidth = width;
  109. mHeight = height;
  110. mActive = true;
  111. if (!fullScreen)
  112. SDL_WM_SetCaption(title.c_str(), 0);
  113. glXGetVideoSyncSGI = (int (*)(unsigned int *))SDL_GL_GetProcAddress("glXGetVideoSyncSGI");
  114. glXWaitVideoSyncSGI = (int (*)(int, int, unsigned int *))SDL_GL_GetProcAddress("glXWaitVideoSyncSGI");
  115. }
  116. void SDLWindow::destroy(void)
  117. {
  118. // according to http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSetVideoMode
  119. // never free the surface returned from SDL_SetVideoMode
  120. //SDL_FreeSurface(mScreen);
  121. mScreen = NULL;
  122. mActive = false;
  123. Root::getSingleton().getRenderSystem()->detachRenderTarget( this->getName() );
  124. }
  125. bool SDLWindow::isActive() const
  126. {
  127. return mActive;
  128. }
  129. bool SDLWindow::isClosed() const
  130. {
  131. return mClosed;
  132. }
  133. void SDLWindow::reposition(int left, int top)
  134. {
  135. // XXX FIXME
  136. }
  137. void SDLWindow::resize(unsigned int width, unsigned int height)
  138. {
  139. SDL_Surface* screen;
  140. int flags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE;
  141. LogManager::getSingleton().logMessage("Updating window", LML_TRIVIAL);
  142. screen = SDL_SetVideoMode(width, height, mScreen->format->BitsPerPixel, flags);
  143. if (!screen)
  144. {
  145. LogManager::getSingleton().logMessage(LML_CRITICAL,
  146. String("Could not make screen: ") + SDL_GetError());
  147. exit(1);
  148. }
  149. LogManager::getSingleton().logMessage("screen is valid", LML_TRIVIAL);
  150. mScreen = screen;
  151. mWidth = width;
  152. mHeight = height;
  153. for (ViewportList::iterator it = mViewportList.begin();
  154. it != mViewportList.end(); ++it)
  155. {
  156. (*it).second->_updateDimensions();
  157. }
  158. }
  159. void SDLWindow::swapBuffers(bool waitForVSync)
  160. {
  161. if ( waitForVSync && glXGetVideoSyncSGI && glXWaitVideoSyncSGI )
  162. {
  163. unsigned int retraceCount;
  164. glXGetVideoSyncSGI( &retraceCount );
  165. glXWaitVideoSyncSGI( 2, ( retraceCount + 1 ) & 1, &retraceCount);
  166. }
  167. SDL_GL_SwapBuffers();
  168. // XXX More?
  169. }
  170. void SDLWindow::copyContentsToMemory(const PixelBox &dst, FrameBuffer buffer)
  171. {
  172. if ((dst.left < 0) || (dst.right > mWidth) ||
  173. (dst.top < 0) || (dst.bottom > mHeight) ||
  174. (dst.front != 0) || (dst.back != 1))
  175. {
  176. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  177. "Invalid box.",
  178. "SDLWindow::copyContentsToMemory" );
  179. }
  180. if (buffer == FB_AUTO)
  181. {
  182. buffer = mIsFullScreen? FB_FRONT : FB_BACK;
  183. }
  184. GLenum format = Ogre::GLPixelUtil::getGLOriginFormat(dst.format);
  185. GLenum type = Ogre::GLPixelUtil::getGLOriginDataType(dst.format);
  186. if ((format == GL_NONE) || (type == 0))
  187. {
  188. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  189. "Unsupported format.",
  190. "SDLWindow::copyContentsToMemory" );
  191. }
  192. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  193. glReadPixels((GLint)dst.left, (GLint)dst.top,
  194. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  195. format, type, dst.data);
  196. //vertical flip
  197. {
  198. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.format);
  199. size_t height = dst.getHeight();
  200. uchar *tmpData = new uchar[rowSpan * height];
  201. uchar *srcRow = (uchar *)dst.data, *tmpRow = tmpData + (height - 1) * rowSpan;
  202. while (tmpRow >= tmpData)
  203. {
  204. memcpy(tmpRow, srcRow, rowSpan);
  205. srcRow += rowSpan;
  206. tmpRow -= rowSpan;
  207. }
  208. memcpy(dst.data, tmpData, rowSpan * height);
  209. delete [] tmpData;
  210. }
  211. }
  212. }