CmGTKGLSupport.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "CmGTKGLSupport.h"
  25. #include "CmGTKWindow.h"
  26. #include "CmException.h"
  27. #include "CmStringConverter.h"
  28. using namespace CamelotEngine;
  29. template<> GTKGLSupport* Singleton<GTKGLSupport>::ms_Singleton = 0;
  30. GTKGLSupport* GTKGLSupport::getSingletonPtr(void)
  31. {
  32. return ms_Singleton;
  33. }
  34. GTKGLSupport& GTKGLSupport::getSingleton(void)
  35. {
  36. assert( ms_Singleton ); return ( *ms_Singleton );
  37. }
  38. GTKGLSupport::GTKGLSupport() :
  39. _kit(0, NULL),
  40. _context_ref(0)
  41. {
  42. Gtk::GL::init(0, NULL);
  43. _main_context = 0;
  44. _main_window = 0;
  45. //_ogre_widget = 0;
  46. }
  47. void GTKGLSupport::addConfig()
  48. {
  49. ConfigOption optFullScreen;
  50. ConfigOption optVideoMode;
  51. // FS setting possiblities
  52. optFullScreen.name = "Full Screen";
  53. optFullScreen.possibleValues.push_back("Yes");
  54. optFullScreen.possibleValues.push_back("No");
  55. optFullScreen.currentValue = "No";
  56. optFullScreen.immutable = false;
  57. // Video mode possiblities
  58. // XXX Actually do this
  59. optVideoMode.name = "Video Mode";
  60. optVideoMode.immutable = false;
  61. optVideoMode.possibleValues.push_back("640 x 480");
  62. optVideoMode.possibleValues.push_back("800 x 600");
  63. optVideoMode.possibleValues.push_back("1024 x 768");
  64. optVideoMode.possibleValues.push_back("1280 x 1024");
  65. optVideoMode.currentValue = "800 x 600";
  66. mOptions[optFullScreen.name] = optFullScreen;
  67. mOptions[optVideoMode.name] = optVideoMode;
  68. }
  69. String GTKGLSupport::validateConfig()
  70. {
  71. return String("");
  72. }
  73. RenderWindow* GTKGLSupport::createWindow(bool autoCreateWindow,
  74. GLRenderSystem* renderSystem,
  75. const String& windowTitle)
  76. {
  77. if (autoCreateWindow)
  78. {
  79. ConfigOptionMap::iterator opt = mOptions.find("Full Screen");
  80. if (opt == mOptions.end())
  81. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't find full screen options!", "GTKGLSupport::createWindow");
  82. bool fullscreen = (opt->second.currentValue == "Yes");
  83. opt = mOptions.find("Video Mode");
  84. if (opt == mOptions.end())
  85. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't find video mode options!", "GTKGLSupport::createWindow");
  86. String val = opt->second.currentValue;
  87. String::size_type pos = val.find('x');
  88. if (pos == String::npos)
  89. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Invalid Video Mode provided", "GTKGLSupport::createWindow");
  90. unsigned int w = StringConverter::parseUnsignedInt(val.substr(0, pos));
  91. unsigned int h = StringConverter::parseUnsignedInt(val.substr(pos + 1));
  92. return renderSystem->createRenderWindow(windowTitle, w, h, 32,
  93. fullscreen);
  94. }
  95. else
  96. {
  97. // XXX What is the else?
  98. return NULL;
  99. }
  100. }
  101. RenderWindow* GTKGLSupport::newWindow(const String& name, unsigned int width,
  102. unsigned int height, unsigned int colourDepth, bool fullScreen, int left, int top,
  103. bool depthBuffer, RenderWindow* parentWindowHandle, bool vsync)
  104. {
  105. GTKWindow* window = new GTKWindow();
  106. window->create(name, width, height, colourDepth, fullScreen, left, top,
  107. depthBuffer, parentWindowHandle);
  108. //if(!_ogre_widget)
  109. // _ogre_widget = window->get_ogre_widget();
  110. // Copy some important information for future reference, for example
  111. // for when the context is needed
  112. if(!_main_context)
  113. _main_context = window->get_ogre_widget()->get_gl_context();
  114. if(!_main_window)
  115. _main_window = window->get_ogre_widget()->get_gl_window();
  116. return window;
  117. }
  118. void GTKGLSupport::start()
  119. {
  120. LogManager::getSingleton().logMessage(
  121. "******************************\n"
  122. "*** Starting GTK Subsystem ***\n"
  123. "******************************");
  124. }
  125. void GTKGLSupport::stop()
  126. {
  127. LogManager::getSingleton().logMessage(
  128. "******************************\n"
  129. "*** Stopping GTK Subsystem ***\n"
  130. "******************************");
  131. }
  132. void GTKGLSupport::begin_context(RenderTarget *_target)
  133. {
  134. // Support nested contexts, in which case.. nothing happens
  135. ++_context_ref;
  136. if (_context_ref == 1) {
  137. if(_target) {
  138. // Begin a specific context
  139. OGREWidget *_ogre_widget = static_cast<GTKWindow*>(_target)->get_ogre_widget();
  140. _ogre_widget->get_gl_window()->gl_begin(_ogre_widget->get_gl_context());
  141. } else {
  142. // Begin a generic main context
  143. _main_window->gl_begin(_main_context);
  144. }
  145. }
  146. }
  147. void GTKGLSupport::end_context()
  148. {
  149. --_context_ref;
  150. if(_context_ref < 0)
  151. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Too many contexts destroyed!", "GTKGLSupport::end_context");
  152. if (_context_ref == 0)
  153. {
  154. // XX is this enough? (_main_window might not be the current window,
  155. // but we can never be sure the previous rendering window
  156. // even still exists)
  157. _main_window->gl_end();
  158. }
  159. }
  160. void GTKGLSupport::initialiseExtensions(void)
  161. {
  162. // XXX anythign to actually do here?
  163. }
  164. bool GTKGLSupport::checkMinGLVersion(const String& v) const
  165. {
  166. int major, minor;
  167. Gdk::GL::query_version(major, minor);
  168. std::string::size_type pos = v.find(".");
  169. int cmaj = atoi(v.substr(0, pos).c_str());
  170. int cmin = atoi(v.substr(pos + 1).c_str());
  171. return ( (major >= cmaj) && (minor >= cmin) );
  172. }
  173. bool GTKGLSupport::checkExtension(const String& ext) const
  174. {
  175. // query_gl_extension needs an active context, doesn't matter which one
  176. if (_context_ref == 0)
  177. _main_window->gl_begin(_main_context);
  178. bool result = Gdk::GL::query_gl_extension(ext.c_str());
  179. if (_context_ref == 0)
  180. _main_window->gl_end();
  181. }
  182. void* GTKGLSupport::getProcAddress(const String& procname)
  183. {
  184. return (void*)Gdk::GL::get_proc_address(procname.c_str());
  185. }
  186. Glib::RefPtr<const Gdk::GL::Context> GTKGLSupport::getMainContext() const {
  187. return _main_context;
  188. }