]> git.tdb.fi Git - libs/gui.git/blob - source/glwindow.cpp
Initial revision
[libs/gui.git] / source / glwindow.cpp
1 /* $Id$
2
3 This file is part of libmspgbase
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <vector>
9 #include <msp/core/error.h>
10 #include "glwindow.h"
11
12 using namespace std;
13
14 #include <iostream>
15
16 namespace Msp {
17
18 GLDisplayOptions::GLDisplayOptions():
19         alpha(false),
20         doublebuffer(true),
21         multisample(0)
22 { }
23
24
25 GLWindow::GLWindow(unsigned w, unsigned h)
26 {
27         options.width=w;
28         options.height=h;
29         init();
30 }
31
32 GLWindow::GLWindow(const DisplayOptions &dopt, const GLDisplayOptions &gl_dopt)
33 {
34         options=dopt;
35         gl_options=gl_dopt;
36         init();
37 }
38
39 GLWindow::~GLWindow()
40 {
41         glXMakeCurrent(display, window, 0);
42         glXDestroyContext(display, context);
43 }
44
45 void GLWindow::swap_buffers()
46 {
47         glXSwapBuffers(display, window);
48 }
49
50 void GLWindow::init()
51 {
52         prepare();
53
54         vector<int> attribs;
55         attribs.push_back(GLX_RGBA);
56         attribs.push_back(GLX_BUFFER_SIZE);
57         attribs.push_back(24);
58         if(gl_options.alpha)
59         {
60                 attribs.push_back(GLX_ALPHA_SIZE);
61                 attribs.push_back(1);
62         }
63         if(gl_options.doublebuffer)
64                 attribs.push_back(GLX_DOUBLEBUFFER);
65         if(gl_options.multisample>0)
66         {
67                 attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
68                 attribs.push_back(gl_options.multisample);
69         }
70         attribs.push_back(0);
71
72         XVisualInfo *visual=glXChooseVisual(display, DefaultScreen(display), &attribs.front());
73         if(!visual)
74                 throw Exception("Couldn't get a matching GLX visual");
75
76         context=glXCreateContext(display, visual, 0, true);
77         if(!context)
78                 throw Exception("Couldn't create a GLX context");
79
80         window=XCreateWindow(display, DefaultRootWindow(display), 0, 0, options.width, options.height, 0, CopyFromParent, InputOutput, CopyFromParent, 0, 0);
81         if(!window)
82                 throw Exception("Couldn't create a window");
83
84         XSelectInput(display, window, ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask);
85
86         glXMakeCurrent(display, window, context);
87 }
88
89 } // namespace Msp