]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/wgl/glcontext.cpp
Add a core profile flag to OpenGL context options
[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                 ContextHandle fake_context = wglCreateContext(dc);
49                 wglMakeCurrent(dc, fake_context);
50
51                 PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = reinterpret_cast<PFNWGLCREATECONTEXTATTRIBSARBPROC>(wglGetProcAddress("wglCreateContextAttribsARB"));
52                 if(!wglCreateContextAttribs)
53                         throw unsupported_gl_mode(opts);
54
55                 vector<int> ctx_attribs;
56
57                 if(opts.forward_compatible)
58                 {
59                         ctx_attribs.push_back(WGL_CONTEXT_FLAGS_ARB);
60                         ctx_attribs.push_back(WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
61                 }
62
63                 if(opts.core_profile)
64                 {
65                         ctx_attribs.push_back(WGL_CONTEXT_PROFILE_MASK_ARB);
66                         ctx_attribs.push_back(WGL_CONTEXT_CORE_PROFILE_BIT_ARB);
67                 }
68
69                 if(opts.gl_version_major)
70                 {
71                         ctx_attribs.push_back(WGL_CONTEXT_MAJOR_VERSION_ARB);
72                         ctx_attribs.push_back(opts.gl_version_major);
73                         ctx_attribs.push_back(WGL_CONTEXT_MINOR_VERSION_ARB);
74                         ctx_attribs.push_back(opts.gl_version_minor);
75                 }
76
77                 ctx_attribs.push_back(0);
78
79                 priv->context = wglCreateContextAttribs(dc, 0, &ctx_attribs[0]);
80                 if(!priv->context)
81                         throw unsupported_gl_mode(opts);
82
83                 wglMakeCurrent(0, 0);
84                 wglDeleteContext(fake_context);
85         }
86         else
87                 priv->context = wglCreateContext(dc);
88         wglMakeCurrent(dc, priv->context);
89
90         ReleaseDC(window.get_private().window, dc);
91 }
92
93 GLContext::~GLContext()
94 {
95         wglMakeCurrent(0, 0);
96         wglDeleteContext(priv->context);
97
98         delete priv;
99 }
100
101 void GLContext::set_swap_interval(unsigned i)
102 {
103         PFNWGLSWAPINTERVALEXTPROC wglSwapInterval = reinterpret_cast<PFNWGLSWAPINTERVALEXTPROC>(wglGetProcAddress("wglSwapIntervalEXT"));
104         if(!wglSwapInterval)
105                 throw runtime_error("wglSwapIntervalEXT not found");
106         wglSwapInterval(i);
107 }
108
109 void GLContext::swap_buffers()
110 {
111         HDC dc = GetDC(window.get_private().window);
112         SwapBuffers(dc);
113         ReleaseDC(window.get_private().window, dc);
114 }
115
116 void GLContext::window_resized(unsigned, unsigned)
117 { }
118
119 } // namespace Graphics
120 } // namespace Msp