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