1 #include "glxbackend.h"
8 /* attributes for a double buffered visual in RGBA format with at least
10 static int attrList[] =
12 GLX_RGBA, GLX_DOUBLEBUFFER,
19 VisualID GLXBackend::initialize(Display *display)
21 if (this->display == NULL) { // this implies that the context is also unitialized
22 this->display = display;
23 // debugging: show version information
24 int glxMajor, glxMinor;
25 glXQueryVersion(display, &glxMajor, &glxMinor);
26 printf("Using GLX version %d.%d\n", glxMajor, glxMinor);
27 // check for swap control functions
28 const char *extensions = glXQueryExtensionsString(display, DefaultScreen(display));
29 if (std::string(extensions).find("GLX_MESA_swap_control") != std::string::npos) {
30 funSwapIntervalMesa = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalMESA");
31 if (funSwapIntervalMesa) printf("glXSwapIntervalMESA is present\n");
34 funSwapIntervalMesa = 0;
36 if (std::string(extensions).find("GLX_EXT_swap_control") != std::string::npos) {
37 funSwapIntervalExt = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
38 if (funSwapIntervalExt) printf("glXSwapIntervalEXT is present\n");
41 funSwapIntervalExt = 0;
44 vi = glXChooseVisual(display, DefaultScreen(display), attrList);
49 void GLXBackend::createContext(Window window)
51 assert(display != NULL && context == None);
52 this->window = window;
53 // create context with that window
54 context = glXCreateContext(display, vi, 0, GL_TRUE);
55 glXMakeCurrent(display, window, context);
56 assert(glXIsDirect(display, context));
59 GLXBackend::~GLXBackend()
61 if (display != NULL) {
62 if (context != None) {
63 glXMakeCurrent(display, None, NULL);
64 glXDestroyContext(display, context);
70 void GLXBackend::swapBuffers() const
72 assert(context != None); // this implies the display is also initialized
73 glXSwapBuffers(display, window);
76 void GLXBackend::setSwapInterval(int i) const
78 assert(context != None);
79 if (funSwapIntervalExt)
80 funSwapIntervalExt(display, window, i);
81 else if (funSwapIntervalMesa)
82 funSwapIntervalMesa(i);
84 assert(false && "At least one of glXSwapIntervalMESA, glXSwapIntervalEXT must be provided by the system");