]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/cgl/glcontext.cpp
Add OpenGL context support on OS X
[libs/gui.git] / source / graphics / cgl / glcontext.cpp
1 #include <vector>
2 #include <OpenGL/gl.h>
3 #include "cocoaglcontext.h"
4 #include "cocoapixelformat.h"
5 #include "glcontext.h"
6 #include "window_private.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace Graphics {
12
13 typedef CocoaGLContext *ContextHandle;
14
15 struct GLContext::Private
16 {
17         ContextHandle context;
18 };
19
20 void GLContext::platform_init(const GLOptions &opts)
21 {
22         priv = new Private;
23
24         vector<unsigned> attribs;
25         
26         attribs.push_back(CPF_DEPTH_SIZE);
27         attribs.push_back(1);
28         
29         if(opts.alpha)
30         {
31                 attribs.push_back(CPF_ALPHA_SIZE);
32                 attribs.push_back(1);
33         }
34         
35         if(opts.stencil)
36         {
37                 attribs.push_back(CPF_STENCIL_SIZE);
38                 attribs.push_back(1);
39         }
40         
41         if(opts.doublebuffer)
42                 attribs.push_back(CPF_DOUBLEBUFFER);
43         
44         if(opts.multisample>0)
45         {
46                 attribs.push_back(CPF_SAMPLE_BUFFERS);
47                 attribs.push_back(1);
48                 attribs.push_back(CPF_SAMPLES);
49                 attribs.push_back(opts.multisample);
50         }
51
52         attribs.push_back(0);
53
54         CocoaPixelFormat *pixfmt = choose_pixel_format(&attribs.front());
55         if(!pixfmt)
56                 throw unsupported_gl_mode(opts);
57
58         priv->context = create_gl_context(pixfmt);
59         destroy_pixel_format(pixfmt);
60
61         attach_gl_context_to_window(priv->context, window.get_private().window);
62         make_gl_context_current(priv->context);
63 }
64
65 GLContext::~GLContext()
66 {
67         destroy_gl_context(priv->context);
68         delete priv;
69 }
70
71 void GLContext::swap_buffers()
72 {
73         flush_gl_buffer(priv->context);
74 }
75
76 void GLContext::window_resized(unsigned w, unsigned h)
77 {
78         // XXX Call [context update] here?
79         glViewport(0, 0, w, h);
80 }
81
82 } // namespace Graphics
83 } // namespace Msp