]> git.tdb.fi Git - libs/gui.git/blobdiff - source/glcontext.cpp
Redesign and refactor
[libs/gui.git] / source / glcontext.cpp
diff --git a/source/glcontext.cpp b/source/glcontext.cpp
new file mode 100644 (file)
index 0000000..5995384
--- /dev/null
@@ -0,0 +1,92 @@
+/* $Id$
+
+This file is part of libmspgbase
+Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <vector>
+#include <msp/core/except.h>
+#include "display.h"
+#include "glcontext.h"
+#include "window.h"
+
+namespace Msp {
+namespace Graphics {
+
+GLOptions::GLOptions():
+       alpha(false),
+       stencil(false),
+       doublebuffer(true),
+       multisample(0)
+{ }
+
+
+GLContext::GLContext(Display &d, const GLOptions &opts):
+       display(d)
+{
+       std::vector<int> attribs;
+       
+       attribs.push_back(GLX_RGBA);
+       attribs.push_back(GLX_DEPTH_SIZE);
+       attribs.push_back(1);
+       
+       if(opts.alpha)
+       {
+               attribs.push_back(GLX_ALPHA_SIZE);
+               attribs.push_back(1);
+       }
+       
+       if(opts.stencil)
+       {
+               attribs.push_back(GLX_STENCIL_SIZE);
+               attribs.push_back(1);
+       }
+       
+       if(opts.doublebuffer)
+               attribs.push_back(GLX_DOUBLEBUFFER);
+       
+       if(opts.multisample>0)
+       {
+               attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
+               attribs.push_back(opts.multisample);
+       }
+       
+       attribs.push_back(0);
+
+       ::Display *dpy=display.get_display();
+       XVisualInfo *vi=glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
+       if(!vi)
+               throw Exception("Couldn't find a GLX visual");
+       context=glXCreateContext(dpy, vi, 0, true);
+
+       XSetWindowAttributes attr;
+       attr.colormap=XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
+
+       window=XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1024, 768, 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
+
+       glXMakeCurrent(dpy, window, context);
+}
+
+void GLContext::attach(Window &wnd)
+{
+       XReparentWindow(display.get_display(), window, wnd.get_handle(), 0, 0);
+       XMapWindow(display.get_display(), window);
+
+       wnd.signal_resize.connect(sigc::mem_fun(this, &GLContext::window_resized));
+       window_resized(wnd.get_width(), wnd.get_height());
+}
+
+void GLContext::swap_buffers()
+{
+       glXSwapBuffers(display.get_display(), window);
+}
+
+void GLContext::window_resized(unsigned w, unsigned h)
+{
+       XMoveResizeWindow(display.get_display(), window, 0, 0, w, h);
+       glViewport(0, 0, w, h);
+}
+
+} // namespace Graphics
+} // namespace Msp