X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglcontext.cpp;fp=source%2Fglcontext.cpp;h=599538429e2e6ba27f65c109038727bd0fc725c2;hb=2d1312772711709fc44cb1a39283329864c25100;hp=0000000000000000000000000000000000000000;hpb=efdfd047fac06547a5a9173f25dea42aeef0a93f;p=libs%2Fgui.git diff --git a/source/glcontext.cpp b/source/glcontext.cpp new file mode 100644 index 0000000..5995384 --- /dev/null +++ b/source/glcontext.cpp @@ -0,0 +1,92 @@ +/* $Id$ + +This file is part of libmspgbase +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include +#include +#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 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