]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/wgl/glcontext.cpp
964dea2b7fba92a66815b7d84e89254c0c840880
[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         priv = new Private;
20
21         HDC dc = GetDC(window.get_private().window);
22
23         PIXELFORMATDESCRIPTOR pfd;
24         memset(&pfd, 0, sizeof(pfd));
25
26         pfd.nSize = sizeof(pfd);
27         pfd.nVersion = 1;
28         pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
29         if(opts.doublebuffer)
30                 pfd.dwFlags |= PFD_DOUBLEBUFFER;
31         pfd.iPixelType = PFD_TYPE_RGBA;
32         if(opts.alpha)
33                 pfd.cAlphaBits = 1;
34         pfd.cDepthBits = 1;
35         if(opts.stencil)
36                 pfd.cStencilBits = 1;
37
38         int pf_index = ChoosePixelFormat(dc, &pfd);
39         if(!pf_index)
40                 throw unsupported_gl_mode(opts);
41         SetPixelFormat(dc, pf_index, &pfd);
42
43         priv->context = wglCreateContext(dc);
44         wglMakeCurrent(dc, priv->context);
45
46         ReleaseDC(window.get_private().window, dc);
47 }
48
49 GLContext::~GLContext()
50 {
51         wglMakeCurrent(0, 0);
52         wglDeleteContext(priv->context);
53
54         delete priv;
55 }
56
57 void GLContext::swap_buffers()
58 {
59         HDC dc = GetDC(window.get_private().window);
60         SwapBuffers(dc);
61         ReleaseDC(window.get_private().window, dc);
62 }
63
64 void GLContext::window_resized(unsigned w, unsigned h)
65 {
66         glViewport(0, 0, w, h);
67 }
68
69 } // namespace Graphics
70 } // namespace Msp