X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgbase%2Fglcontext.cpp;h=6e02018b897ca013ee71079fc0f915a9869e938b;hb=746e5da7730baee990fb1e307d416e0593b3f083;hp=a749c6ce8f3966dbfc7514180a25a76603e29724;hpb=999ca92aa9ee10585c0b2094d84364159253982f;p=libs%2Fgui.git diff --git a/source/gbase/glcontext.cpp b/source/gbase/glcontext.cpp index a749c6c..6e02018 100644 --- a/source/gbase/glcontext.cpp +++ b/source/gbase/glcontext.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgbase -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -9,15 +9,18 @@ Distributed under the LGPL #ifdef WIN32 #include #endif +#ifdef WITH_OPENGL #include +#ifndef WIN32 +#include +#endif +#endif #include #include #include "display.h" #include "glcontext.h" #include "window.h" - -#include -using namespace std; +#include "display_priv.h" namespace Msp { namespace Graphics { @@ -30,12 +33,33 @@ GLOptions::GLOptions(): { } +#ifdef WITH_OPENGL +#ifdef WIN32 +typedef HGLRC Context; +#else +typedef GLXContext Context; +#endif + +struct GLContext::Private +{ + Context context; +#ifndef WIN32 + // In X11, we need to create a window with the chosen visual + WindowHandle subwnd; +#endif +}; +#endif + + GLContext::GLContext(Window &wnd, const GLOptions &opts): display(wnd.get_display()), window(wnd) { +#ifdef WITH_OPENGL + priv=new Private; + #ifdef WIN32 - HDC dc=GetDC(window.get_handle()); + HDC dc=GetDC(window.get_private().window); PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(pfd)); @@ -57,10 +81,10 @@ GLContext::GLContext(Window &wnd, const GLOptions &opts): throw Exception("Couldn't find a suitable pixel format"); SetPixelFormat(dc, pf_index, &pfd); - context=wglCreateContext(dc); - wglMakeCurrent(dc, context); + priv->context=wglCreateContext(dc); + wglMakeCurrent(dc, priv->context); - ReleaseDC(window.get_handle(), dc); + ReleaseDC(window.get_private().window, dc); #else std::vector attribs; @@ -91,58 +115,73 @@ GLContext::GLContext(Window &wnd, const GLOptions &opts): attribs.push_back(0); - ::Display *dpy=display.get_display(); + ::Display *dpy=display.get_private().display; XVisualInfo *vi=glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front()); if(!vi) throw Exception("Couldn't find a suitable GLX visual"); - context=glXCreateContext(dpy, vi, 0, true); + priv->context=glXCreateContext(dpy, vi, 0, true); XSetWindowAttributes attr; attr.colormap=XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone); - 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); + priv->subwnd=XCreateWindow(dpy, window.get_private().window, 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr); + XMapWindow(dpy, priv->subwnd); XFree(vi); - glXMakeCurrent(dpy, subwnd, context); + glXMakeCurrent(dpy, priv->subwnd, priv->context); +#endif +#else + (void)wnd; + (void)opts; + throw Exception("OpenGL support not compiled in"); +#endif window.signal_resize.connect(sigc::mem_fun(this, &GLContext::window_resized)); -#endif } GLContext::~GLContext() { +#ifdef WITH_OPENGL #ifdef WIN32 wglMakeCurrent(0, 0); - wglDeleteContext(context); + wglDeleteContext(priv->context); #else - ::Display *dpy=display.get_display(); + ::Display *dpy=display.get_private().display; glXMakeCurrent(dpy, 0, 0); - glXDestroyContext(dpy, context); - XDestroyWindow(dpy, subwnd); + glXDestroyContext(dpy, priv->context); + XDestroyWindow(dpy, priv->subwnd); +#endif + delete priv; #endif } void GLContext::swap_buffers() { +#ifdef WITH_OPENGL #ifdef WIN32 - HDC dc=GetDC(window.get_handle()); + HDC dc=GetDC(window.get_private().window); SwapBuffers(dc); - ReleaseDC(window.get_handle(), dc); + ReleaseDC(window.get_private().window, dc); #else - glXSwapBuffers(display.get_display(), subwnd); + glXSwapBuffers(display.get_private().display, priv->subwnd); +#endif #endif } void GLContext::window_resized(unsigned w, unsigned h) { +#ifdef WITH_OPENGL #ifndef WIN32 - XMoveResizeWindow(display.get_display(), subwnd, 0, 0, w, h); + XMoveResizeWindow(display.get_private().display, priv->subwnd, 0, 0, w, h); #endif glViewport(0, 0, w, h); +#else + (void)w; + (void)h; +#endif } } // namespace Graphics