GLContext.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "GLContext.h"
  23. #include "Assert.h"
  24. #include "Types.h"
  25. namespace crown
  26. {
  27. static Display* s_x11_display = NULL;
  28. static Window s_x11_window = None;
  29. //-----------------------------------------------------------------------------
  30. void set_x11_display_and_window(Display* dpy, Window win)
  31. {
  32. s_x11_display = dpy;
  33. s_x11_window = win;
  34. }
  35. //-----------------------------------------------------------------------------
  36. GLContext::GLContext() :
  37. m_glx_context(NULL)
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. void GLContext::create_context()
  42. {
  43. // Color index buffer not supported - deprecated
  44. static int fb_attribs[] =
  45. {
  46. GLX_RENDER_TYPE, static_cast<int>(GLX_RGBA_BIT),
  47. GLX_DRAWABLE_TYPE, static_cast<int>(GLX_WINDOW_BIT),
  48. GLX_DOUBLEBUFFER, static_cast<int>(True),
  49. GLX_RED_SIZE, 8,
  50. GLX_GREEN_SIZE, 8,
  51. GLX_BLUE_SIZE, 8,
  52. GLX_DEPTH_SIZE, 24,
  53. static_cast<int>(None)
  54. };
  55. int fb_count;
  56. GLXFBConfig* fb_config = glXChooseFBConfig(s_x11_display, DefaultScreen(s_x11_display), fb_attribs, &fb_count);
  57. CE_ASSERT(fb_config != NULL, "Unable to find a matching frame buffer configuration");
  58. XVisualInfo* vi = glXGetVisualFromFBConfig(s_x11_display, fb_config[0]);
  59. CE_ASSERT(vi != NULL, "Unable to find a matching visual for frame buffer configuration.");
  60. m_glx_context = glXCreateContext(s_x11_display, vi, 0, True);
  61. CE_ASSERT(m_glx_context != NULL, "Unable to create GLX context");
  62. glXMakeCurrent(s_x11_display, s_x11_window, m_glx_context);
  63. XFlush(s_x11_display);
  64. XFree(vi);
  65. XFree(fb_config);
  66. }
  67. //-----------------------------------------------------------------------------
  68. void GLContext::destroy_context()
  69. {
  70. if (s_x11_display != NULL)
  71. {
  72. if (m_glx_context != None)
  73. {
  74. glXMakeCurrent(s_x11_display, None, None);
  75. glXDestroyContext(s_x11_display, m_glx_context);
  76. }
  77. }
  78. }
  79. //-----------------------------------------------------------------------------
  80. void GLContext::swap_buffers()
  81. {
  82. glXSwapBuffers(s_x11_display, s_x11_window);
  83. }
  84. } // namespace crown