3 This file is part of libmspgbase
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/core/except.h>
11 #include "glcontext.h"
17 GLOptions::GLOptions():
25 GLContext::GLContext(Display &d, const GLOptions &opts):
28 std::vector<int> attribs;
30 attribs.push_back(GLX_RGBA);
31 attribs.push_back(GLX_DEPTH_SIZE);
36 attribs.push_back(GLX_ALPHA_SIZE);
42 attribs.push_back(GLX_STENCIL_SIZE);
47 attribs.push_back(GLX_DOUBLEBUFFER);
49 if(opts.multisample>0)
51 attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
52 attribs.push_back(opts.multisample);
57 ::Display *dpy=display.get_display();
58 XVisualInfo *vi=glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
60 throw Exception("Couldn't find a GLX visual");
61 context=glXCreateContext(dpy, vi, 0, true);
63 XSetWindowAttributes attr;
64 attr.colormap=XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
66 window=XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1024, 768, 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
70 glXMakeCurrent(dpy, window, context);
73 GLContext::~GLContext()
75 glXDestroyContext(display.get_display(), context);
76 XDestroyWindow(display.get_display(), window);
79 void GLContext::attach(Window &wnd)
81 XReparentWindow(display.get_display(), window, wnd.get_handle(), 0, 0);
82 XMapWindow(display.get_display(), window);
84 wnd.signal_resize.connect(sigc::mem_fun(this, &GLContext::window_resized));
85 window_resized(wnd.get_width(), wnd.get_height());
88 void GLContext::swap_buffers()
90 glXSwapBuffers(display.get_display(), window);
93 void GLContext::window_resized(unsigned w, unsigned h)
95 XMoveResizeWindow(display.get_display(), window, 0, 0, w, h);
96 glViewport(0, 0, w, h);
99 } // namespace Graphics