]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/wgl/glcontext.cpp
Create GLContext::priv later to avoid memory leaks
[libs/gui.git] / source / graphics / wgl / glcontext.cpp
1 #include <windows.h>
2 #include <GL/gl.h>
3 #include "glcontext.h"
4 #include "window_private.h"
5
6 namespace Msp {
7 namespace Graphics {
8
9 typedef HGLRC ContextHandle;
10
11 struct GLContext::Private
12 {
13         ContextHandle context;
14 };
15
16
17 void GLContext::platform_init(const GLOptions &opts)
18 {
19         HDC dc = GetDC(window.get_private().window);
20
21         PIXELFORMATDESCRIPTOR pfd;
22         memset(&pfd, 0, sizeof(pfd));
23
24         pfd.nSize = sizeof(pfd);
25         pfd.nVersion = 1;
26         pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
27         if(opts.doublebuffer)
28                 pfd.dwFlags |= PFD_DOUBLEBUFFER;
29         pfd.iPixelType = PFD_TYPE_RGBA;
30         if(opts.alpha)
31                 pfd.cAlphaBits = 1;
32         pfd.cDepthBits = 1;
33         if(opts.stencil)
34                 pfd.cStencilBits = 1;
35
36         int pf_index = ChoosePixelFormat(dc, &pfd);
37         if(!pf_index)
38                 throw unsupported_gl_mode(opts);
39         SetPixelFormat(dc, pf_index, &pfd);
40
41         priv = new Private;
42         priv->context = wglCreateContext(dc);
43         wglMakeCurrent(dc, priv->context);
44
45         ReleaseDC(window.get_private().window, dc);
46 }
47
48 GLContext::~GLContext()
49 {
50         wglMakeCurrent(0, 0);
51         wglDeleteContext(priv->context);
52
53         delete priv;
54 }
55
56 void GLContext::swap_buffers()
57 {
58         HDC dc = GetDC(window.get_private().window);
59         SwapBuffers(dc);
60         ReleaseDC(window.get_private().window, dc);
61 }
62
63 void GLContext::window_resized(unsigned w, unsigned h)
64 {
65         glViewport(0, 0, w, h);
66 }
67
68 } // namespace Graphics
69 } // namespace Msp