6 #include "window_private.h"
13 T get_proc(const char *name)
15 return reinterpret_cast<T>(reinterpret_cast<void *>(wglGetProcAddress(name)));
23 typedef HGLRC ContextHandle;
25 struct GLContext::Private
27 ContextHandle context;
31 void GLContext::platform_init(const GLOptions &opts)
33 HDC dc = GetDC(window.get_private().window);
35 PIXELFORMATDESCRIPTOR pfd;
36 memset(&pfd, 0, sizeof(pfd));
38 pfd.nSize = sizeof(pfd);
40 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
42 pfd.dwFlags |= PFD_DOUBLEBUFFER;
43 pfd.iPixelType = PFD_TYPE_RGBA;
50 int pf_index = ChoosePixelFormat(dc, &pfd);
52 throw unsupported_gl_mode(opts);
53 SetPixelFormat(dc, pf_index, &pfd);
56 if(opts.forward_compatible || opts.gl_version_major)
58 ContextHandle fake_context = wglCreateContext(dc);
59 wglMakeCurrent(dc, fake_context);
61 PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = get_proc<PFNWGLCREATECONTEXTATTRIBSARBPROC>("wglCreateContextAttribsARB");
62 if(!wglCreateContextAttribs)
63 throw unsupported_gl_mode(opts);
65 vector<int> ctx_attribs;
67 if(opts.forward_compatible)
69 ctx_attribs.push_back(WGL_CONTEXT_FLAGS_ARB);
70 ctx_attribs.push_back(WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
75 ctx_attribs.push_back(WGL_CONTEXT_PROFILE_MASK_ARB);
76 ctx_attribs.push_back(WGL_CONTEXT_CORE_PROFILE_BIT_ARB);
79 if(opts.gl_version_major)
81 ctx_attribs.push_back(WGL_CONTEXT_MAJOR_VERSION_ARB);
82 ctx_attribs.push_back(opts.gl_version_major);
83 ctx_attribs.push_back(WGL_CONTEXT_MINOR_VERSION_ARB);
84 ctx_attribs.push_back(opts.gl_version_minor);
87 ctx_attribs.push_back(0);
89 priv->context = wglCreateContextAttribs(dc, 0, &ctx_attribs[0]);
91 throw unsupported_gl_mode(opts);
94 wglDeleteContext(fake_context);
97 priv->context = wglCreateContext(dc);
98 wglMakeCurrent(dc, priv->context);
100 ReleaseDC(window.get_private().window, dc);
103 GLContext::~GLContext()
105 wglMakeCurrent(0, 0);
106 wglDeleteContext(priv->context);
111 void GLContext::set_swap_interval(unsigned i)
113 PFNWGLSWAPINTERVALEXTPROC wglSwapInterval = get_proc<PFNWGLSWAPINTERVALEXTPROC>("wglSwapIntervalEXT");
115 throw runtime_error("wglSwapIntervalEXT not found");
119 void GLContext::swap_buffers()
121 HDC dc = GetDC(window.get_private().window);
123 ReleaseDC(window.get_private().window, dc);
126 void GLContext::window_resized(unsigned, unsigned)
129 } // namespace Graphics