]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/glx/glcontext.cpp
a73b9c554760af4f1c5d368b69605533c48be16f
[libs/gui.git] / source / graphics / glx / glcontext.cpp
1 #include <vector>
2 #include <GL/gl.h>
3 #include <GL/glx.h>
4 #include "glcontext.h"
5 #include "display_private.h"
6 #include "window_private.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace Graphics {
12
13 typedef GLXContext ContextHandle;
14
15 struct GLContext::Private
16 {
17         ContextHandle context;
18         // We need to create a window with the chosen visual
19         WindowHandle subwnd;
20 };
21
22
23 void GLContext::platform_init(const GLOptions &opts)
24 {
25         priv = new Private;
26
27         vector<int> attribs;
28         
29         attribs.push_back(GLX_RGBA);
30         attribs.push_back(GLX_DEPTH_SIZE);
31         attribs.push_back(1);
32         
33         if(opts.alpha)
34         {
35                 attribs.push_back(GLX_ALPHA_SIZE);
36                 attribs.push_back(1);
37         }
38         
39         if(opts.stencil)
40         {
41                 attribs.push_back(GLX_STENCIL_SIZE);
42                 attribs.push_back(1);
43         }
44         
45         if(opts.doublebuffer)
46                 attribs.push_back(GLX_DOUBLEBUFFER);
47         
48         if(opts.multisample>0)
49         {
50                 attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
51                 attribs.push_back(1);
52                 attribs.push_back(GLX_SAMPLES_ARB);
53                 attribs.push_back(opts.multisample);
54         }
55         
56         attribs.push_back(0);
57
58         DisplayHandle dpy = display.get_private().display;
59
60         XVisualInfo *vi = glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
61         if(!vi)
62                 throw unsupported_gl_mode(opts);
63         priv->context = glXCreateContext(dpy, vi, 0, true);
64
65         XSetWindowAttributes attr;
66         attr.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
67
68         priv->subwnd = XCreateWindow(dpy, window.get_private().window, 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
69         XMapWindow(dpy, priv->subwnd);
70
71         XFree(vi);
72
73         glXMakeCurrent(dpy, priv->subwnd, priv->context);
74 }
75
76 GLContext::~GLContext()
77 {
78         DisplayHandle dpy = display.get_private().display;
79
80         glXMakeCurrent(dpy, 0, 0);
81         glXDestroyContext(dpy, priv->context);
82         XDestroyWindow(dpy, priv->subwnd);
83
84         delete priv;
85 }
86
87 void GLContext::swap_buffers()
88 {
89         glXSwapBuffers(display.get_private().display, priv->subwnd);
90 }
91
92 void GLContext::window_resized(unsigned w, unsigned h)
93 {
94         XMoveResizeWindow(display.get_private().display, priv->subwnd, 0, 0, w, h);
95         glViewport(0, 0, w, h);
96 }
97
98 } // namespace Graphics
99 } // namespace Msp