-/* $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 "glwindow.h"
-
-using namespace std;
-
-namespace Msp {
-
-GLDisplayOptions::GLDisplayOptions():
- alpha(false),
- stencil(false),
- doublebuffer(true),
- multisample(0)
-{ }
-
-
-GLWindow::GLWindow(unsigned w, unsigned h)
-{
- options.width=w;
- options.height=h;
- init();
-}
-
-GLWindow::GLWindow(const DisplayOptions &dopt, const GLDisplayOptions &gl_dopt)
-{
- options=dopt;
- gl_options=gl_dopt;
- init();
-}
-
-GLWindow::~GLWindow()
-{
- glXMakeContextCurrent(display, 0, 0, 0);
- glXDestroyWindow(display, glx_wnd);
- glXDestroyContext(display, context);
-}
-
-void GLWindow::swap_buffers()
-{
- glXSwapBuffers(display, glx_wnd);
-}
-
-void GLWindow::init()
-{
- prepare();
-
- vector<int> attribs;
-
- attribs.push_back(GLX_RENDER_TYPE);
- attribs.push_back(GLX_RGBA_BIT);
-
- attribs.push_back(GLX_DRAWABLE_TYPE);
- attribs.push_back(GLX_WINDOW_BIT);
-
- attribs.push_back(GLX_DEPTH_SIZE);
- attribs.push_back(1);
-
- if(gl_options.alpha)
- {
- attribs.push_back(GLX_ALPHA_SIZE);
- attribs.push_back(1);
- }
-
- if(gl_options.stencil)
- {
- attribs.push_back(GLX_STENCIL_SIZE);
- attribs.push_back(1);
- }
-
- if(gl_options.doublebuffer)
- {
- attribs.push_back(GLX_DOUBLEBUFFER);
- attribs.push_back(true);
- }
-
- if(gl_options.multisample>0)
- {
- attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
- attribs.push_back(gl_options.multisample);
- }
-
- attribs.push_back(None);
-
- int count;
- GLXFBConfig *config=glXChooseFBConfig(display, DefaultScreen(display), &attribs.front(), &count);
- if(!config)
- throw Exception("Couldn't get a GLX framebuffer configuration");
-
- context=glXCreateNewContext(display, config[0], GLX_RGBA_TYPE, 0, true);
- if(!context)
- throw Exception("Couldn't create a GLX context");
-
- XVisualInfo *vi=glXGetVisualFromFBConfig(display, config[0]);
- Handle root=RootWindow(display, vi->screen);
-
- Colormap cmap=XCreateColormap(display, root, vi->visual, AllocNone);
- XSetWindowAttributes attr;
- attr.colormap=cmap;
- attr.override_redirect=options.fullscreen;
-
- Handle wnd=XCreateWindow(display, root, 0, 0, options.width, options.height, 0, vi->depth, InputOutput, vi->visual, CWColormap|CWOverrideRedirect, &attr);
- set_window(wnd);
-
- glx_wnd=glXCreateWindow(display, config[0], wnd, 0);
-
- glXMakeContextCurrent(display, glx_wnd, glx_wnd, context);
-
- XFree(config);
-}
-
-void GLWindow::on_event(const XEvent &event)
-{
- if(event.type==ConfigureNotify)
- glViewport(0, 0, options.width, options.height);
-}
-
-} // namespace Msp