]> git.tdb.fi Git - libs/gui.git/blobdiff - source/gbase/glcontext.cpp
Add SimpleWindow and SimpleGLWindow
[libs/gui.git] / source / gbase / glcontext.cpp
index c002a39dd0c3c41d43ddec5670969cc599f47f7e..4783e0cbc995c0eee85908e4e63f7ab76a0333fc 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspgbase
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2008  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -9,11 +9,17 @@ Distributed under the LGPL
 #ifdef WIN32
 #include <windows.h>
 #endif
+#ifdef WITH_OPENGL
 #include <GL/gl.h>
+#ifndef WIN32
+#include <GL/glx.h>
+#endif
+#endif
 #include <msp/core/application.h>
 #include <msp/core/except.h>
 #include "display.h"
 #include "glcontext.h"
+#include "types.h"
 #include "window.h"
 
 namespace Msp {
@@ -27,10 +33,31 @@ GLOptions::GLOptions():
 { }
 
 
+#ifdef WITH_OPENGL
+#ifdef WIN32
+typedef HGLRC Context;
+#else
+typedef GLXContext Context;
+#endif
+
+struct GLContext::Private
+{
+       Context context;
+#ifndef WIN32
+       // In X11, we need to create a window with the chosen visual
+       WindowHandle subwnd;
+#endif
+};
+#endif
+
+
 GLContext::GLContext(Window &wnd, const GLOptions &opts):
        display(wnd.get_display()),
        window(wnd)
 {
+#ifdef WITH_OPENGL
+       priv=new Private;
+
 #ifdef WIN32
        HDC dc=GetDC(window.get_handle());
 
@@ -54,8 +81,8 @@ GLContext::GLContext(Window &wnd, const GLOptions &opts):
                throw Exception("Couldn't find a suitable pixel format");
        SetPixelFormat(dc, pf_index, &pfd);
 
-       context=wglCreateContext(dc);
-       wglMakeCurrent(dc, context);
+       priv->context=wglCreateContext(dc);
+       wglMakeCurrent(dc, priv->context);
 
        ReleaseDC(window.get_handle(), dc);
 #else
@@ -93,17 +120,22 @@ GLContext::GLContext(Window &wnd, const GLOptions &opts):
        XVisualInfo *vi=glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
        if(!vi)
                throw Exception("Couldn't find a suitable GLX visual");
-       context=glXCreateContext(dpy, vi, 0, true);
+       priv->context=glXCreateContext(dpy, vi, 0, true);
 
        XSetWindowAttributes attr;
        attr.colormap=XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
 
-       subwnd=XCreateWindow(dpy, window.get_handle(), 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
-       XMapWindow(display.get_display(), subwnd);
+       priv->subwnd=XCreateWindow(dpy, window.get_handle(), 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
+       XMapWindow(display.get_display(), priv->subwnd);
 
        XFree(vi);
 
-       glXMakeCurrent(dpy, subwnd, context);
+       glXMakeCurrent(dpy, priv->subwnd, priv->context);
+#endif
+#else
+       (void)wnd;
+       (void)opts;
+       throw Exception("OpenGL support not compiled in");
 #endif
 
        window.signal_resize.connect(sigc::mem_fun(this, &GLContext::window_resized));
@@ -111,35 +143,45 @@ GLContext::GLContext(Window &wnd, const GLOptions &opts):
 
 GLContext::~GLContext()
 {
+#ifdef WITH_OPENGL
 #ifdef WIN32
        wglMakeCurrent(0, 0);
-       wglDeleteContext(context);
+       wglDeleteContext(priv->context);
 #else
        ::Display *dpy=display.get_display();
 
        glXMakeCurrent(dpy, 0, 0);
-       glXDestroyContext(dpy, context);
-       XDestroyWindow(dpy, subwnd);
+       glXDestroyContext(dpy, priv->context);
+       XDestroyWindow(dpy, priv->subwnd);
+#endif
+       delete priv;
 #endif
 }
 
 void GLContext::swap_buffers()
 {
+#ifdef WITH_OPENGL
 #ifdef WIN32
        HDC dc=GetDC(window.get_handle());
        SwapBuffers(dc);
        ReleaseDC(window.get_handle(), dc);
 #else
-       glXSwapBuffers(display.get_display(), subwnd);
+       glXSwapBuffers(display.get_display(), priv->subwnd);
+#endif
 #endif
 }
 
 void GLContext::window_resized(unsigned w, unsigned h)
 {
+#ifdef WITH_OPENGL
 #ifndef WIN32
-       XMoveResizeWindow(display.get_display(), subwnd, 0, 0, w, h);
+       XMoveResizeWindow(display.get_display(), priv->subwnd, 0, 0, w, h);
 #endif
        glViewport(0, 0, w, h);
+#else
+       (void)w;
+       (void)h;
+#endif
 }
 
 } // namespace Graphics