+ vector<int> fb_attribs;
+
+ fb_attribs.push_back(GLX_DRAWABLE_TYPE);
+ fb_attribs.push_back(GLX_WINDOW_BIT);
+
+ fb_attribs.push_back(GLX_RENDER_TYPE);
+ fb_attribs.push_back(GLX_RGBA_BIT);
+
+ fb_attribs.push_back(GLX_DEPTH_SIZE);
+ fb_attribs.push_back(1);
+
+ if(opts.alpha)
+ {
+ fb_attribs.push_back(GLX_ALPHA_SIZE);
+ fb_attribs.push_back(1);
+ }
+
+ if(opts.stencil)
+ {
+ fb_attribs.push_back(GLX_STENCIL_SIZE);
+ fb_attribs.push_back(1);
+ }
+
+ fb_attribs.push_back(GLX_DOUBLEBUFFER);
+ fb_attribs.push_back(opts.doublebuffer);
+
+ if(opts.multisample>0)
+ {
+ fb_attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
+ fb_attribs.push_back(1);
+ fb_attribs.push_back(GLX_SAMPLES_ARB);
+ fb_attribs.push_back(opts.multisample);
+ }
+
+ fb_attribs.push_back(0);
+
+ int n_configs = 0;
+ GLXFBConfig *fb_configs = glXChooseFBConfig(dpy, DefaultScreen(dpy), &fb_attribs[0], &n_configs);
+ if(!fb_configs)
+ throw unsupported_gl_mode(opts);
+
+ XVisualInfo *vi = glXGetVisualFromFBConfig(dpy, fb_configs[0]);
+
+ XSetWindowAttributes attr;
+ attr.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
+
+ priv = new Private;
+ priv->subwnd = XCreateWindow(dpy, window.get_private().window, 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
+ XMapWindow(dpy, priv->subwnd);
+
+ priv->glxwnd = glXCreateWindow(dpy, fb_configs[0], priv->subwnd, 0);
+
+ if(opts.forward_compatible || opts.gl_version_major)
+ {
+ if(!extensions.count("GLX_ARB_create_context") || !extensions.count("GLX_ARB_get_proc_address"))
+ throw unsupported_gl_mode(opts);
+
+ vector<int> ctx_attribs;
+
+ ctx_attribs.push_back(GLX_RENDER_TYPE);
+ ctx_attribs.push_back(GLX_RGBA_TYPE);
+
+ if(opts.forward_compatible)
+ {
+ ctx_attribs.push_back(GLX_CONTEXT_FLAGS_ARB);
+ ctx_attribs.push_back(GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
+ }
+
+ if(opts.core_profile)
+ {
+ ctx_attribs.push_back(GLX_CONTEXT_PROFILE_MASK_ARB);
+ ctx_attribs.push_back(GLX_CONTEXT_CORE_PROFILE_BIT_ARB);
+ }
+
+ if(opts.gl_version_major)
+ {
+ ctx_attribs.push_back(GLX_CONTEXT_MAJOR_VERSION_ARB);
+ ctx_attribs.push_back(opts.gl_version_major);
+ ctx_attribs.push_back(GLX_CONTEXT_MINOR_VERSION_ARB);
+ ctx_attribs.push_back(opts.gl_version_minor);
+ }
+
+ ctx_attribs.push_back(0);
+
+ const GLubyte *name = reinterpret_cast<const GLubyte *>("glXCreateContextAttribsARB");
+ PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs = reinterpret_cast<PFNGLXCREATECONTEXTATTRIBSARBPROC>(glXGetProcAddressARB(name));
+ priv->context = glXCreateContextAttribs(dpy, fb_configs[0], 0, true, &ctx_attribs[0]);
+ }
+ else
+ priv->context = glXCreateNewContext(dpy, fb_configs[0], GLX_RGBA_TYPE, 0, true);
+
+ XFree(vi);
+ XFree(fb_configs);
+
+ glXMakeContextCurrent(dpy, priv->glxwnd, priv->glxwnd, priv->context);