1 #include "glxbackend.h"
9 /* attributes for a double buffered visual in RGBA format with at least
11 static int attrList[] =
13 GLX_RGBA, GLX_DOUBLEBUFFER,
20 VisualID GLXBackend::initialize(Display *display)
22 if (this->display == NULL) { // this implies that the context is also unitialized
23 this->display = display;
24 // debugging: show version information
25 int glxMajor, glxMinor;
26 glXQueryVersion(display, &glxMajor, &glxMinor);
27 printf("Using GLX version %d.%d\n", glxMajor, glxMinor);
28 // check for swap control functions
29 const char *extensions = glXQueryExtensionsString(display, DefaultScreen(display));
30 if (std::string(extensions).find("GLX_MESA_swap_control") != std::string::npos) {
31 funSwapIntervalMesa = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalMESA");
32 if (funSwapIntervalMesa) printf("glXSwapIntervalMESA is present\n");
35 funSwapIntervalMesa = 0;
37 if (std::string(extensions).find("GLX_EXT_swap_control") != std::string::npos) {
38 funSwapIntervalExt = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
39 if (funSwapIntervalExt) printf("glXSwapIntervalEXT is present\n");
42 funSwapIntervalExt = 0;
45 vi = glXChooseVisual(display, DefaultScreen(display), attrList);
50 void GLXBackend::createContext(Window window)
52 assert(display != NULL && context == None);
53 this->window = window;
54 // create context with that window
55 context = glXCreateContext(display, vi, 0, GL_TRUE);
56 glXMakeCurrent(display, window, context);
57 assert(glXIsDirect(display, context));
60 GLXBackend::~GLXBackend()
62 if (display != NULL) {
63 if (context != None) {
64 glXMakeCurrent(display, None, NULL);
65 glXDestroyContext(display, context);
71 void GLXBackend::swapBuffers() const
73 assert(context != None); // this implies the display is also initialized
74 glXSwapBuffers(display, window);
77 void GLXBackend::setSwapInterval(int i) const
79 assert(context != None);
80 if (funSwapIntervalExt)
81 funSwapIntervalExt(display, window, i);
82 else if (funSwapIntervalMesa)
83 funSwapIntervalMesa(i);
85 fprintf(stderr, "At least one of glXSwapIntervalMESA, glXSwapIntervalEXT must be provided by the system\n");