]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/cgl/glcontext.cpp
Add OpenGL context support on OS X
[libs/gui.git] / source / graphics / cgl / glcontext.cpp
diff --git a/source/graphics/cgl/glcontext.cpp b/source/graphics/cgl/glcontext.cpp
new file mode 100644 (file)
index 0000000..94b9b2f
--- /dev/null
@@ -0,0 +1,83 @@
+#include <vector>
+#include <OpenGL/gl.h>
+#include "cocoaglcontext.h"
+#include "cocoapixelformat.h"
+#include "glcontext.h"
+#include "window_private.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Graphics {
+
+typedef CocoaGLContext *ContextHandle;
+
+struct GLContext::Private
+{
+       ContextHandle context;
+};
+
+void GLContext::platform_init(const GLOptions &opts)
+{
+       priv = new Private;
+
+       vector<unsigned> attribs;
+       
+       attribs.push_back(CPF_DEPTH_SIZE);
+       attribs.push_back(1);
+       
+       if(opts.alpha)
+       {
+               attribs.push_back(CPF_ALPHA_SIZE);
+               attribs.push_back(1);
+       }
+       
+       if(opts.stencil)
+       {
+               attribs.push_back(CPF_STENCIL_SIZE);
+               attribs.push_back(1);
+       }
+       
+       if(opts.doublebuffer)
+               attribs.push_back(CPF_DOUBLEBUFFER);
+       
+       if(opts.multisample>0)
+       {
+               attribs.push_back(CPF_SAMPLE_BUFFERS);
+               attribs.push_back(1);
+               attribs.push_back(CPF_SAMPLES);
+               attribs.push_back(opts.multisample);
+       }
+
+       attribs.push_back(0);
+
+       CocoaPixelFormat *pixfmt = choose_pixel_format(&attribs.front());
+       if(!pixfmt)
+               throw unsupported_gl_mode(opts);
+
+       priv->context = create_gl_context(pixfmt);
+       destroy_pixel_format(pixfmt);
+
+       attach_gl_context_to_window(priv->context, window.get_private().window);
+       make_gl_context_current(priv->context);
+}
+
+GLContext::~GLContext()
+{
+       destroy_gl_context(priv->context);
+       delete priv;
+}
+
+void GLContext::swap_buffers()
+{
+       flush_gl_buffer(priv->context);
+}
+
+void GLContext::window_resized(unsigned w, unsigned h)
+{
+       // XXX Call [context update] here?
+       glViewport(0, 0, w, h);
+}
+
+} // namespace Graphics
+} // namespace Msp