]> git.tdb.fi Git - libs/gui.git/blobdiff - source/glcontext.cpp
Win32 compatibility (for most things)
[libs/gui.git] / source / glcontext.cpp
index 9377e888a8b977133ce82f915afd5bcf4ec599e1..a749c6ce8f3966dbfc7514180a25a76603e29724 100644 (file)
@@ -6,11 +6,19 @@ Distributed under the LGPL
 */
 
 #include <vector>
+#ifdef WIN32
+#include <windows.h>
+#endif
+#include <GL/gl.h>
+#include <msp/core/application.h>
 #include <msp/core/except.h>
 #include "display.h"
 #include "glcontext.h"
 #include "window.h"
 
+#include <iostream>
+using namespace std;
+
 namespace Msp {
 namespace Graphics {
 
@@ -22,9 +30,38 @@ GLOptions::GLOptions():
 { }
 
 
-GLContext::GLContext(Display &d, const GLOptions &opts):
-       display(d)
+GLContext::GLContext(Window &wnd, const GLOptions &opts):
+       display(wnd.get_display()),
+       window(wnd)
 {
+#ifdef WIN32
+       HDC dc=GetDC(window.get_handle());
+
+       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 Exception("Couldn't find a suitable pixel format");
+       SetPixelFormat(dc, pf_index, &pfd);
+
+       context=wglCreateContext(dc);
+       wglMakeCurrent(dc, context);
+
+       ReleaseDC(window.get_handle(), dc);
+#else
        std::vector<int> attribs;
        
        attribs.push_back(GLX_RGBA);
@@ -55,44 +92,56 @@ GLContext::GLContext(Display &d, const GLOptions &opts):
        attribs.push_back(0);
 
        ::Display *dpy=display.get_display();
+
        XVisualInfo *vi=glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
        if(!vi)
-               throw Exception("Couldn't find a GLX visual");
+               throw Exception("Couldn't find a suitable GLX visual");
        context=glXCreateContext(dpy, vi, 0, true);
 
        XSetWindowAttributes attr;
        attr.colormap=XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
 
-       window=XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1024, 768, 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
+       subwnd=XCreateWindow(dpy, window.get_handle(), 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
+       XMapWindow(display.get_display(), subwnd);
 
        XFree(vi);
 
-       glXMakeCurrent(dpy, window, context);
-}
+       glXMakeCurrent(dpy, subwnd, context);
 
-GLContext::~GLContext()
-{
-       glXDestroyContext(display.get_display(), context);
-       XDestroyWindow(display.get_display(), window);
+       window.signal_resize.connect(sigc::mem_fun(this, &GLContext::window_resized));
+#endif
 }
 
-void GLContext::attach(Window &wnd)
+GLContext::~GLContext()
 {
-       XReparentWindow(display.get_display(), window, wnd.get_handle(), 0, 0);
-       XMapWindow(display.get_display(), window);
+#ifdef WIN32
+       wglMakeCurrent(0, 0);
+       wglDeleteContext(context);
+#else
+       ::Display *dpy=display.get_display();
 
-       wnd.signal_resize.connect(sigc::mem_fun(this, &GLContext::window_resized));
-       window_resized(wnd.get_width(), wnd.get_height());
+       glXMakeCurrent(dpy, 0, 0);
+       glXDestroyContext(dpy, context);
+       XDestroyWindow(dpy, subwnd);
+#endif
 }
 
 void GLContext::swap_buffers()
 {
-       glXSwapBuffers(display.get_display(), window);
+#ifdef WIN32
+       HDC dc=GetDC(window.get_handle());
+       SwapBuffers(dc);
+       ReleaseDC(window.get_handle(), dc);
+#else
+       glXSwapBuffers(display.get_display(), subwnd);
+#endif
 }
 
 void GLContext::window_resized(unsigned w, unsigned h)
 {
-       XMoveResizeWindow(display.get_display(), window, 0, 0, w, h);
+#ifndef WIN32
+       XMoveResizeWindow(display.get_display(), subwnd, 0, 0, w, h);
+#endif
        glViewport(0, 0, w, h);
 }