CmGTKWindow.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "CmGTKWindow.h"
  25. #include "CmGTKGLSupport.h"
  26. using namespace CamelotEngine;
  27. OGREWidget::OGREWidget(bool useDepthBuffer) :
  28. Gtk::GL::DrawingArea()
  29. {
  30. Glib::RefPtr<Gdk::GL::Config> glconfig;
  31. Gdk::GL::ConfigMode mode = Gdk::GL::MODE_RGBA | Gdk::GL::MODE_DOUBLE;
  32. if (useDepthBuffer)
  33. mode |= Gdk::GL::MODE_DEPTH;
  34. glconfig = Gdk::GL::Config::create(mode);
  35. if (glconfig.is_null())
  36. {
  37. // TODO PORT - Log this somewhere
  38. //LogManager::getSingleton().logMessage("[gtk] GLCONFIG BLOWUP");
  39. }
  40. // Inherit GL context from Ogre main context
  41. set_gl_capability(glconfig, GTKGLSupport::getSingleton().getMainContext());
  42. add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
  43. }
  44. // OGREWidget TODO:
  45. // - resize events et al
  46. // - Change aspect ratio
  47. GTKWindow::GTKWindow():
  48. mGtkWindow(0)
  49. {
  50. //kit = Gtk::Main::instance();
  51. // Should this move to GTKGLSupport?
  52. // Gtk::GL::init(0, NULL);
  53. // Already done in GTKGLSupport
  54. mWidth = 0;
  55. mHeight = 0;
  56. }
  57. GTKWindow::~GTKWindow()
  58. {
  59. }
  60. bool GTKWindow::pump_events()
  61. {
  62. Gtk::Main *kit = Gtk::Main::instance();
  63. if (kit->events_pending())
  64. {
  65. kit->iteration(false);
  66. return true;
  67. }
  68. return false;
  69. }
  70. OGREWidget* GTKWindow::get_ogre_widget()
  71. {
  72. return ogre;
  73. }
  74. void GTKWindow::create(const String& name, unsigned int width, unsigned int height, unsigned int colourDepth,
  75. bool fullScreen, int left, int top, bool depthBuffer,
  76. void* miscParam, ...)
  77. {
  78. mName = name;
  79. mWidth = width;
  80. mHeight = height;
  81. if(!miscParam) {
  82. mGtkWindow = new Gtk::Window();
  83. mGtkWindow->set_title(mName);
  84. if (fullScreen)
  85. {
  86. mIsFullScreen = true;
  87. mGtkWindow->set_decorated(false);
  88. mGtkWindow->fullscreen();
  89. }
  90. else
  91. {
  92. mIsFullScreen = false;
  93. mGtkWindow->set_default_size(mWidth, mHeight);
  94. mGtkWindow->move(left, top);
  95. }
  96. } else {
  97. // If miscParam is not 0, a parent widget has been passed in,
  98. // we will handle this later on after the widget has been created.
  99. }
  100. ogre = Gtk::manage(new OGREWidget(depthBuffer));
  101. ogre->set_size_request(width, height);
  102. ogre->signal_delete_event().connect(SigC::slot(*this, &GTKWindow::on_delete_event));
  103. ogre->signal_expose_event().connect(SigC::slot(*this, &GTKWindow::on_expose_event));
  104. if(mGtkWindow) {
  105. mGtkWindow->add(*ogre);
  106. mGtkWindow->show_all();
  107. }
  108. if(miscParam) {
  109. // Attach it!
  110. // Note that the parent widget *must* be visible already at this point,
  111. // or the widget won't get realized in time for the GLinit that follows
  112. // this call. This is usually the case for Glade generated windows, anyway.
  113. reinterpret_cast<Gtk::Container*>(miscParam)->add(*ogre);
  114. ogre->show();
  115. }
  116. //ogre->realize();
  117. }
  118. void GTKWindow::destroy()
  119. {
  120. Root::getSingleton().getRenderSystem()->detachRenderTarget( this->getName() );
  121. // We could detach the widget from its parent and destroy it here too,
  122. // but then again, it is managed so we rely on GTK to destroy it.
  123. delete mGtkWindow;
  124. mGtkWindow = 0;
  125. }
  126. void GTKWindow::setFullscreen(bool fullScreen, unsigned int width, unsigned int height)
  127. {
  128. if((fullScreen) && (!mIsFullScreen))
  129. {
  130. mIsFullScreen = true;
  131. mGtkWindow->fullscreen();
  132. ogre->set_size_request(width, height);
  133. }
  134. else if((!fullScreen) && (mIsFullScreen))
  135. {
  136. mIsFullScreen = false;
  137. mGtkWindow->unfullscreen();
  138. ogre->set_size_request(width, height);
  139. }
  140. }
  141. bool GTKWindow::isActive() const
  142. {
  143. return ogre->is_realized();
  144. }
  145. bool GTKWindow::isClosed() const
  146. {
  147. return ogre->is_visible();
  148. }
  149. void GTKWindow::reposition(int left, int top)
  150. {
  151. if(mGtkWindow)
  152. mGtkWindow->move(left, top);
  153. }
  154. void GTKWindow::resize(unsigned int width, unsigned int height)
  155. {
  156. if(mGtkWindow)
  157. mGtkWindow->resize(width, height);
  158. }
  159. void GTKWindow::swapBuffers(bool waitForVSync)
  160. {
  161. Glib::RefPtr<Gdk::GL::Window> glwindow = ogre->get_gl_window();
  162. glwindow->swap_buffers();
  163. }
  164. void GTKWindow::copyContentsToMemory(const PixelBox &dst, FrameBuffer buffer)
  165. {
  166. if ((dst.left < 0) || (dst.right > mWidth) ||
  167. (dst.top < 0) || (dst.bottom > mHeight) ||
  168. (dst.front != 0) || (dst.back != 1))
  169. {
  170. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  171. "Invalid box.",
  172. "GTKWindow::copyContentsToMemory" );
  173. }
  174. if (buffer == FB_AUTO)
  175. {
  176. buffer = mIsFullScreen? FB_FRONT : FB_BACK;
  177. }
  178. GLenum format = Ogre::GLPixelUtil::getGLOriginFormat(dst.format);
  179. GLenum type = Ogre::GLPixelUtil::getGLOriginDataType(dst.format);
  180. if ((format == GL_NONE) || (type == 0))
  181. {
  182. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  183. "Unsupported format.",
  184. "GTKWindow::copyContentsToMemory" );
  185. }
  186. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  187. glReadPixels((GLint)dst.left, (GLint)dst.top,
  188. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  189. format, type, dst.data);
  190. //vertical flip
  191. {
  192. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.format);
  193. size_t height = dst.getHeight();
  194. uchar *tmpData = new uchar[rowSpan * height];
  195. uchar *srcRow = (uchar *)dst.data, *tmpRow = tmpData + (height - 1) * rowSpan;
  196. while (tmpRow >= tmpData)
  197. {
  198. memcpy(tmpRow, srcRow, rowSpan);
  199. srcRow += rowSpan;
  200. tmpRow -= rowSpan;
  201. }
  202. memcpy(dst.data, tmpData, rowSpan * height);
  203. delete [] tmpData;
  204. }
  205. }
  206. void GTKWindow::getCustomAttribute( const String& name, void* pData )
  207. {
  208. if( name == "GTKMMWINDOW" )
  209. {
  210. Gtk::Window **win = static_cast<Gtk::Window **>(pData);
  211. // Oh, the burdens of multiple inheritance
  212. *win = mGtkWindow;
  213. return;
  214. }
  215. else if( name == "GTKGLMMWIDGET" )
  216. {
  217. Gtk::GL::DrawingArea **widget = static_cast<Gtk::GL::DrawingArea **>(pData);
  218. *widget = ogre;
  219. return;
  220. }
  221. else if( name == "isTexture" )
  222. {
  223. bool *b = reinterpret_cast< bool * >( pData );
  224. *b = false;
  225. return;
  226. }
  227. RenderWindow::getCustomAttribute(name, pData);
  228. }
  229. bool GTKWindow::on_delete_event(GdkEventAny* event)
  230. {
  231. Root::getSingleton().getRenderSystem()->detachRenderTarget( this->getName() );
  232. return false;
  233. }
  234. bool GTKWindow::on_expose_event(GdkEventExpose* event)
  235. {
  236. // Window exposed, update interior
  237. //std::cout << "Window exposed, update interior" << std::endl;
  238. // TODO: time between events, as expose events can be sent crazily fast
  239. update();
  240. return false;
  241. }