--- /dev/null
+/**************************************************************************
+ *
+ * Copyright 2011 Jose Fonseca
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#include "glws.hpp"
+
+
+namespace glws {
+
+
+bool debug = true;
+
+
+bool
+checkExtension(const char *extName, const char *extString)
+{
+ const char *p = extString;
+ const char *q = extName;
+ char c;
+ do {
+ c = *p++;
+ if (c == '\0' || c == ' ') {
+ if (q && *q == '\0') {
+ return true;
+ } else {
+ q = extName;
+ }
+ } else {
+ if (q && *q == c) {
+ ++q;
+ } else {
+ q = 0;
+ }
+ }
+ } while (c);
+ return false;
+}
+
+
+} /* namespace glws */
*
**************************************************************************/
+#include <assert.h>
#include <stdlib.h>
+
#include <iostream>
#include "glws.hpp"
-
-#include <X11/Xlib.h>
-#include <GL/gl.h>
-#include <GL/glx.h>
+#include "glproc.hpp"
namespace glws {
static Display *display = NULL;
static int screen = 0;
+static unsigned glxVersion = 0;
+static const char *extensions = 0;
+static bool has_GLX_ARB_create_context = false;
+
class GlxVisual : public Visual
{
public:
+ GLXFBConfig fbconfig;
XVisualInfo *visinfo;
- GlxVisual(XVisualInfo *vi) :
- visinfo(vi)
+ GlxVisual() :
+ fbconfig(0),
+ visinfo(0)
{}
~GlxVisual() {
XFree(visinfo);
+ XFree(fbconfig);
}
};
void
init(void) {
+ display = XOpenDisplay(NULL);
if (!display) {
- display = XOpenDisplay(NULL);
- if (!display) {
- std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
- exit(1);
- }
- screen = DefaultScreen(display);
+ std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
+ exit(1);
}
+
+ screen = DefaultScreen(display);
+
+ int major = 0, minor = 0;
+ glXQueryVersion(display, &major, &minor);
+ glxVersion = (major << 8) | minor;
+
+ extensions = glXQueryExtensionsString(display, screen);
+ has_GLX_ARB_create_context = checkExtension("GLX_ARB_create_context", extensions);
}
void
Visual *
createVisual(bool doubleBuffer) {
- int single_attribs[] = {
- GLX_RGBA,
- GLX_RED_SIZE, 1,
- GLX_GREEN_SIZE, 1,
- GLX_BLUE_SIZE, 1,
- GLX_ALPHA_SIZE, 1,
- GLX_DEPTH_SIZE, 1,
- GLX_STENCIL_SIZE, 1,
- None
- };
-
- int double_attribs[] = {
- GLX_RGBA,
- GLX_RED_SIZE, 1,
- GLX_GREEN_SIZE, 1,
- GLX_BLUE_SIZE, 1,
- GLX_ALPHA_SIZE, 1,
- GLX_DOUBLEBUFFER,
- GLX_DEPTH_SIZE, 1,
- GLX_STENCIL_SIZE, 1,
- None
- };
-
- XVisualInfo *visinfo;
+ GlxVisual *visual = new GlxVisual;
+
+ if (glxVersion >= 0x0103) {
+ Attributes<int> attribs;
+ attribs.add(GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT);
+ attribs.add(GLX_RENDER_TYPE, GLX_RGBA_BIT);
+ attribs.add(GLX_RED_SIZE, 1);
+ attribs.add(GLX_GREEN_SIZE, 1);
+ attribs.add(GLX_BLUE_SIZE, 1);
+ attribs.add(GLX_ALPHA_SIZE, 1);
+ attribs.add(GLX_DOUBLEBUFFER, doubleBuffer ? GL_TRUE : GL_FALSE);
+ attribs.add(GLX_DEPTH_SIZE, 1);
+ attribs.add(GLX_STENCIL_SIZE, 1);
+ attribs.end();
+
+ int num_configs = 0;
+ GLXFBConfig * fbconfigs;
+ fbconfigs = glXChooseFBConfig(display, screen, attribs, &num_configs);
+ assert(num_configs && fbconfigs);
+ visual->fbconfig = fbconfigs[0];
+ assert(visual->fbconfig);
+ visual->visinfo = glXGetVisualFromFBConfig(display, visual->fbconfig);
+ assert(visual->visinfo);
+ } else {
+ Attributes<int> attribs;
+ attribs.add(GLX_RGBA);
+ attribs.add(GLX_RED_SIZE, 1);
+ attribs.add(GLX_GREEN_SIZE, 1);
+ attribs.add(GLX_BLUE_SIZE, 1);
+ attribs.add(GLX_ALPHA_SIZE, 1);
+ if (doubleBuffer) {
+ attribs.add(GLX_DOUBLEBUFFER);
+ }
+ attribs.add(GLX_DEPTH_SIZE, 1);
+ attribs.add(GLX_STENCIL_SIZE, 1);
+ attribs.end();
- visinfo = glXChooseVisual(display, screen, doubleBuffer ? double_attribs : single_attribs);
+ visual->visinfo = glXChooseVisual(display, screen, attribs);
+ }
- return new GlxVisual(visinfo);
+ return visual;
}
Drawable *
}
Context *
-createContext(const Visual *visual, Context *shareContext)
+createContext(const Visual *_visual, Context *shareContext)
{
- XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
+ const GlxVisual *visual = dynamic_cast<const GlxVisual *>(_visual);
GLXContext share_context = NULL;
GLXContext context;
share_context = dynamic_cast<GlxContext*>(shareContext)->context;
}
- context = glXCreateContext(display, visinfo,
- share_context, True);
+
+ if (glxVersion >= 0x0104 && has_GLX_ARB_create_context) {
+ Attributes<int> attribs;
+ attribs.add(GLX_RENDER_TYPE, GLX_RGBA_TYPE);
+ if (debug) {
+ attribs.add(GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB);
+ }
+ attribs.end();
+
+ context = glXCreateContextAttribsARB(display, visual->fbconfig, share_context, True, attribs);
+ } else if (glxVersion >= 0x103) {
+ context = glXCreateNewContext(display, visual->fbconfig, GLX_RGBA_TYPE, share_context, True);
+ } else {
+ context = glXCreateContext(display, visual->visinfo, share_context, True);
+ }
+
return new GlxContext(visual, context);
}