6 #include "window_private.h"
13 typedef HGLRC ContextHandle;
15 struct GLContext::Private
17 ContextHandle context;
21 void GLContext::platform_init(const GLOptions &opts)
23 HDC dc = GetDC(window.get_private().window);
25 PIXELFORMATDESCRIPTOR pfd;
26 memset(&pfd, 0, sizeof(pfd));
28 pfd.nSize = sizeof(pfd);
30 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
32 pfd.dwFlags |= PFD_DOUBLEBUFFER;
33 pfd.iPixelType = PFD_TYPE_RGBA;
40 int pf_index = ChoosePixelFormat(dc, &pfd);
42 throw unsupported_gl_mode(opts);
43 SetPixelFormat(dc, pf_index, &pfd);
46 if(opts.forward_compatible || opts.gl_version_major)
48 ContextHandle fake_context = wglCreateContext(dc);
49 wglMakeCurrent(dc, fake_context);
51 PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = reinterpret_cast<PFNWGLCREATECONTEXTATTRIBSARBPROC>(wglGetProcAddress("wglCreateContextAttribsARB"));
52 if(!wglCreateContextAttribs)
53 throw unsupported_gl_mode(opts);
55 vector<int> ctx_attribs;
57 if(opts.forward_compatible)
59 ctx_attribs.push_back(WGL_CONTEXT_FLAGS_ARB);
60 ctx_attribs.push_back(WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
65 ctx_attribs.push_back(WGL_CONTEXT_PROFILE_MASK_ARB);
66 ctx_attribs.push_back(WGL_CONTEXT_CORE_PROFILE_BIT_ARB);
69 if(opts.gl_version_major)
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);
77 ctx_attribs.push_back(0);
79 priv->context = wglCreateContextAttribs(dc, 0, &ctx_attribs[0]);
81 throw unsupported_gl_mode(opts);
84 wglDeleteContext(fake_context);
87 priv->context = wglCreateContext(dc);
88 wglMakeCurrent(dc, priv->context);
90 ReleaseDC(window.get_private().window, dc);
93 GLContext::~GLContext()
96 wglDeleteContext(priv->context);
101 void GLContext::set_swap_interval(unsigned i)
103 PFNWGLSWAPINTERVALEXTPROC wglSwapInterval = reinterpret_cast<PFNWGLSWAPINTERVALEXTPROC>(wglGetProcAddress("wglSwapIntervalEXT"));
105 throw runtime_error("wglSwapIntervalEXT not found");
109 void GLContext::swap_buffers()
111 HDC dc = GetDC(window.get_private().window);
113 ReleaseDC(window.get_private().window, dc);
116 void GLContext::window_resized(unsigned, unsigned)
119 } // namespace Graphics