/* $Id$
This file is part of libmspgbase
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
#ifdef WIN32
#include <windows.h>
#endif
+#ifdef WITH_OPENGL
#include <GL/gl.h>
+#ifndef WIN32
+#include <GL/glx.h>
+#endif
+#endif
#include <msp/core/application.h>
#include <msp/core/except.h>
#include "display.h"
#include "glcontext.h"
+#include "types.h"
#include "window.h"
namespace Msp {
{ }
+#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());
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);
#else
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_handle(), 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
+ XMapWindow(display.get_display(), 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));
GLContext::~GLContext()
{
+#ifdef WITH_OPENGL
#ifdef WIN32
wglMakeCurrent(0, 0);
- wglDeleteContext(context);
+ wglDeleteContext(priv->context);
#else
::Display *dpy=display.get_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());
SwapBuffers(dc);
ReleaseDC(window.get_handle(), dc);
#else
- glXSwapBuffers(display.get_display(), subwnd);
+ glXSwapBuffers(display.get_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_display(), priv->subwnd, 0, 0, w, h);
#endif
glViewport(0, 0, w, h);
+#else
+ (void)w;
+ (void)h;
+#endif
}
} // namespace Graphics
--- /dev/null
+/* $Id$
+
+This file is part of libmspgbase
+Copyright © 2008 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef MSP_GBASE_SIMPLEWINDOW_H_
+#define MSP_GBASE_SIMPLEWINDOW_H_
+
+#include "display.h"
+#include "glcontext.h"
+#include "window.h"
+
+namespace Msp {
+namespace Graphics {
+
+/**
+Helper class for SimpleWindow.
+*/
+class SimpleWindowBase
+{
+protected:
+ Display dpy;
+
+ SimpleWindowBase() { }
+};
+
+
+/**
+A simplified Window that encapsulates a Display.
+*/
+class SimpleWindow: public SimpleWindowBase, public Window
+{
+public:
+ SimpleWindow(unsigned, unsigned);
+};
+
+
+/**
+A SimpleWindow bundled with a GLContext.
+*/
+class SimpleGLWindow: public SimpleWindow
+{
+private:
+ GLContext gl_ctx;
+
+public:
+ SimpleGLWindow(unsigned, unsigned);
+ GLContext &get_gl_context() { return gl_ctx; }
+ void swap_buffers();
+};
+
+} // namespace Graphics
+} // namespace Msp
+
+#endif