5 #include "display_private.h"
6 #include "window_private.h"
13 typedef GLXContext ContextHandle;
15 struct GLContext::Private
17 ContextHandle context;
18 // We need to create a window with the chosen visual
23 void GLContext::platform_init(const GLOptions &opts)
27 attribs.push_back(GLX_RGBA);
28 attribs.push_back(GLX_DEPTH_SIZE);
33 attribs.push_back(GLX_ALPHA_SIZE);
39 attribs.push_back(GLX_STENCIL_SIZE);
44 attribs.push_back(GLX_DOUBLEBUFFER);
46 if(opts.multisample>0)
48 attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
50 attribs.push_back(GLX_SAMPLES_ARB);
51 attribs.push_back(opts.multisample);
56 DisplayHandle dpy = display.get_private().display;
58 XVisualInfo *vi = glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
60 throw unsupported_gl_mode(opts);
63 priv->context = glXCreateContext(dpy, vi, 0, true);
65 XSetWindowAttributes attr;
66 attr.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
68 priv->subwnd = XCreateWindow(dpy, window.get_private().window, 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
69 XMapWindow(dpy, priv->subwnd);
73 glXMakeCurrent(dpy, priv->subwnd, priv->context);
76 GLContext::~GLContext()
78 DisplayHandle dpy = display.get_private().display;
80 glXMakeCurrent(dpy, 0, 0);
81 glXDestroyContext(dpy, priv->context);
82 XDestroyWindow(dpy, priv->subwnd);
87 void GLContext::set_swap_interval(unsigned i)
89 const GLubyte *name = reinterpret_cast<const GLubyte *>("glXSwapIntervalEXT");
90 PFNGLXSWAPINTERVALEXTPROC func = reinterpret_cast<PFNGLXSWAPINTERVALEXTPROC>(glXGetProcAddress(name));
92 throw runtime_error("glXSwapIntervalEXT not found");
93 func(display.get_private().display, priv->subwnd, i);
96 void GLContext::swap_buffers()
98 glXSwapBuffers(display.get_private().display, priv->subwnd);
101 void GLContext::window_resized(unsigned w, unsigned h)
103 XMoveResizeWindow(display.get_private().display, priv->subwnd, 0, 0, w, h);
106 } // namespace Graphics