]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/wgl/glcontext.cpp
Split platform-specific parts into separate directories
[libs/gui.git] / source / graphics / wgl / glcontext.cpp
diff --git a/source/graphics/wgl/glcontext.cpp b/source/graphics/wgl/glcontext.cpp
new file mode 100644 (file)
index 0000000..1a0ad7a
--- /dev/null
@@ -0,0 +1,70 @@
+#include <windows.h>
+#include <GL/gl.h>
+#include "glcontext.h"
+#include "window_private.h"
+
+namespace Msp {
+namespace Graphics {
+
+typedef HGLRC ContextHandle;
+
+struct GLContext::Private
+{
+       ContextHandle context;
+};
+
+
+void GLContext::platform_init()
+{
+       priv = new Private;
+
+       HDC dc = GetDC(window.get_private().window);
+
+       PIXELFORMATDESCRIPTOR pfd;
+       memset(&pfd, 0, sizeof(pfd));
+
+       pfd.nSize = sizeof(pfd);
+       pfd.nVersion = 1;
+       pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
+       if(opts.doublebuffer)
+               pfd.dwFlags |= PFD_DOUBLEBUFFER;
+       pfd.iPixelType = PFD_TYPE_RGBA;
+       if(opts.alpha)
+               pfd.cAlphaBits = 1;
+       pfd.cDepthBits = 1;
+       if(opts.stencil)
+               pfd.cStencilBits = 1;
+
+       int pf_index = ChoosePixelFormat(dc, &pfd);
+       if(!pf_index)
+               throw unsupported_gl_mode(opts);
+       SetPixelFormat(dc, pf_index, &pfd);
+
+       priv->context = wglCreateContext(dc);
+       wglMakeCurrent(dc, priv->context);
+
+       ReleaseDC(window.get_private().window, dc);
+}
+
+GLContext::~GLContext()
+{
+       wglMakeCurrent(0, 0);
+       wglDeleteContext(priv->context);
+
+       delete priv;
+}
+
+void GLContext::swap_buffers()
+{
+       HDC dc = GetDC(window.get_private().window);
+       SwapBuffers(dc);
+       ReleaseDC(window.get_private().window, dc);
+}
+
+void GLContext::window_resized(unsigned w, unsigned h)
+{
+       glViewport(0, 0, w, h);
+}
+
+} // namespace Graphics
+} // namespace Msp