]> git.tdb.fi Git - libs/gui.git/blob - source/glwindow.cpp
React to resize and close events
[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 namespace Msp {
15
16 GLDisplayOptions::GLDisplayOptions():
17         alpha(false),
18         doublebuffer(true),
19         multisample(0)
20 { }
21
22
23 GLWindow::GLWindow(unsigned w, unsigned h)
24 {
25         options.width=w;
26         options.height=h;
27         init();
28 }
29
30 GLWindow::GLWindow(const DisplayOptions &dopt, const GLDisplayOptions &gl_dopt)
31 {
32         options=dopt;
33         gl_options=gl_dopt;
34         init();
35 }
36
37 GLWindow::~GLWindow()
38 {
39         glXMakeCurrent(display, 0, 0);
40         glXDestroyContext(display, context);
41 }
42
43 void GLWindow::swap_buffers()
44 {
45         glXSwapBuffers(display, window);
46 }
47
48 void GLWindow::init()
49 {
50         prepare();
51
52         vector<int> attribs;
53         attribs.push_back(GLX_RGBA);
54         attribs.push_back(GLX_BUFFER_SIZE);
55         attribs.push_back(24);
56         if(gl_options.alpha)
57         {
58                 attribs.push_back(GLX_ALPHA_SIZE);
59                 attribs.push_back(1);
60         }
61         if(gl_options.doublebuffer)
62                 attribs.push_back(GLX_DOUBLEBUFFER);
63         if(gl_options.multisample>0)
64         {
65                 attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
66                 attribs.push_back(gl_options.multisample);
67         }
68         attribs.push_back(0);
69
70         XVisualInfo *visual=glXChooseVisual(display, DefaultScreen(display), &attribs.front());
71         if(!visual)
72                 throw Exception("Couldn't get a matching GLX visual");
73
74         context=glXCreateContext(display, visual, 0, true);
75         if(!context)
76                 throw Exception("Couldn't create a GLX context");
77
78         create();
79
80         glXMakeCurrent(display, window, context);
81 }
82
83 void GLWindow::on_resize()
84 {
85         glViewport(0, 0, options.width, options.height);
86 }
87
88 } // namespace Msp