]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/wgl/glcontext.cpp
98aabb310abb99f95356a6000f8997587d426ee6
[libs/gui.git] / source / graphics / wgl / glcontext.cpp
1 #include <vector>
2 #include <windows.h>
3 #include <GL/gl.h>
4 #include <GL/wglext.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 HGLRC ContextHandle;
14
15 struct GLContext::Private
16 {
17         ContextHandle context;
18 };
19
20
21 void GLContext::platform_init(const GLOptions &opts)
22 {
23         HDC dc = GetDC(window.get_private().window);
24
25         PIXELFORMATDESCRIPTOR pfd;
26         memset(&pfd, 0, sizeof(pfd));
27
28         pfd.nSize = sizeof(pfd);
29         pfd.nVersion = 1;
30         pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
31         if(opts.doublebuffer)
32                 pfd.dwFlags |= PFD_DOUBLEBUFFER;
33         pfd.iPixelType = PFD_TYPE_RGBA;
34         if(opts.alpha)
35                 pfd.cAlphaBits = 1;
36         pfd.cDepthBits = 1;
37         if(opts.stencil)
38                 pfd.cStencilBits = 1;
39
40         int pf_index = ChoosePixelFormat(dc, &pfd);
41         if(!pf_index)
42                 throw unsupported_gl_mode(opts);
43         SetPixelFormat(dc, pf_index, &pfd);
44
45         priv = new Private;
46         if(opts.forward_compatible || opts.gl_version_major)
47         {
48                 PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = reinterpret_cast<PFNWGLCREATECONTEXTATTRIBSARBPROC>(wglGetProcAddress("wglCreateContextAttribs"));
49                 if(!wglCreateContextAttribs)
50                         throw unsupported_gl_mode(opts);
51
52                 vector<int> ctx_attribs;
53
54                 if(opts.forward_compatible)
55                 {
56                         ctx_attribs.push_back(WGL_CONTEXT_FLAGS_ARB);
57                         ctx_attribs.push_back(WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
58                 }
59
60                 if(opts.gl_version_major)
61                 {
62                         ctx_attribs.push_back(WGL_CONTEXT_MAJOR_VERSION_ARB);
63                         ctx_attribs.push_back(opts.gl_version_major);
64                         ctx_attribs.push_back(WGL_CONTEXT_MINOR_VERSION_ARB);
65                         ctx_attribs.push_back(opts.gl_version_minor);
66                 }
67
68                 ctx_attribs.push_back(0);
69
70                 priv->context = wglCreateContextAttribs(dc, 0, &ctx_attribs[0]);
71         }
72         else
73                 priv->context = wglCreateContext(dc);
74         wglMakeCurrent(dc, priv->context);
75
76         ReleaseDC(window.get_private().window, dc);
77 }
78
79 GLContext::~GLContext()
80 {
81         wglMakeCurrent(0, 0);
82         wglDeleteContext(priv->context);
83
84         delete priv;
85 }
86
87 void GLContext::set_swap_interval(unsigned i)
88 {
89         PFNWGLSWAPINTERVALEXTPROC wglSwapInterval = reinterpret_cast<PFNWGLSWAPINTERVALEXTPROC>(wglGetProcAddress("wglSwapIntervalEXT"));
90         if(!wglSwapInterval)
91                 throw runtime_error("wglSwapIntervalEXT not found");
92         wglSwapInterval(i);
93 }
94
95 void GLContext::swap_buffers()
96 {
97         HDC dc = GetDC(window.get_private().window);
98         SwapBuffers(dc);
99         ReleaseDC(window.get_private().window, dc);
100 }
101
102 void GLContext::window_resized(unsigned, unsigned)
103 { }
104
105 } // namespace Graphics
106 } // namespace Msp