]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/glx/glcontext.cpp
Use default member initializers and defaulted default constructors
[libs/gui.git] / source / graphics / glx / glcontext.cpp
index 3ee8865d7559e0890df797cb79ef893097f50847..699f856f35d06ce239f7576e7b499500f3b6fc6a 100644 (file)
@@ -1,9 +1,10 @@
+#include "glcontext.h"
 #include <set>
 #include <vector>
 #include <GL/glx.h>
 #include <GL/glxext.h>
+#include <msp/strings/lexicalcast.h>
 #include <msp/strings/utils.h>
-#include "glcontext.h"
 #include "display_private.h"
 #include "window_private.h"
 
@@ -16,10 +17,10 @@ typedef GLXContext ContextHandle;
 
 struct GLContext::Private
 {
-       ContextHandle context;
+       ContextHandle context = 0;
        // We need to create a window with the chosen visual
-       WindowHandle subwnd;
-       GLXWindow glxwnd;
+       WindowHandle subwnd = None;
+       GLXWindow glxwnd = None;
 };
 
 
@@ -98,11 +99,32 @@ void GLContext::platform_init(const GLOptions &opts)
 
                priv->glxwnd = glXCreateWindow(dpy, fb_configs[0], priv->subwnd, 0);
 
-               if(opts.forward_compatible || opts.gl_version_major)
+               if(opts.forward_compatible || opts.gl_version_major!=GLOptions::DEFAULT_VERSION)
                {
                        if(!extensions.count("GLX_ARB_create_context") || !extensions.count("GLX_ARB_get_proc_address"))
                                throw unsupported_gl_mode(opts);
 
+                       unsigned gl_version_major = opts.gl_version_major;
+                       unsigned gl_version_minor = opts.gl_version_minor;
+                       if(opts.gl_version_major==GLOptions::LATEST_VERSION)
+                       {
+                               ContextHandle probe_context = glXCreateNewContext(dpy, fb_configs[0], GLX_RGBA_TYPE, 0, true);
+                               glXMakeContextCurrent(dpy, priv->glxwnd, priv->glxwnd, probe_context);
+
+                               const char *gl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_VERSION));
+                               if(!gl_ver_ptr)
+                                       throw unsupported_gl_mode(opts);
+
+                               string gl_ver = gl_ver_ptr;
+                               vector<string> parts = split(gl_ver.substr(0, gl_ver.find(' ')), '.');
+
+                               gl_version_major = lexical_cast<unsigned>(parts[0]);
+                               gl_version_minor = lexical_cast<unsigned>(parts[1]);
+
+                               glXMakeContextCurrent(dpy, 0, 0, 0);
+                               glXDestroyContext(dpy, probe_context);
+                       }
+
                        vector<int> ctx_attribs;
 
                        ctx_attribs.push_back(GLX_RENDER_TYPE);
@@ -123,9 +145,9 @@ void GLContext::platform_init(const GLOptions &opts)
                        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(gl_version_major);
                                ctx_attribs.push_back(GLX_CONTEXT_MINOR_VERSION_ARB);
-                               ctx_attribs.push_back(opts.gl_version_minor);
+                               ctx_attribs.push_back(gl_version_minor);
                        }
 
                        ctx_attribs.push_back(0);