]> Pileus Git - grits/blobdiff - src/gtkgl.c
Add support for Mercator projections in tiles
[grits] / src / gtkgl.c
index e99f5f82755ae6e2c58ff9556e10735d4ddaf54e..bb8749403e63ca72bee0dfb5a6356be9a4fc6f75 100644 (file)
@@ -1,9 +1,26 @@
+/*
+ * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <gtk/gtk.h>
 
 /***************************
  * GtkGlExt implementation *
  ***************************/
-#if defined(USE_GTKGLEXT)
+#if defined(SYS_GTKGLEXT)
 #include <gtk/gtkgl.h>
 void gtk_gl_enable(GtkWidget *widget)
 {
@@ -36,7 +53,7 @@ void gtk_gl_disable(GtkWidget *widget)
 /**********************
  * X11 implementation *
  **********************/
-#elif defined(USE_GLX)
+#elif defined(SYS_X11)
 #include <GL/glx.h>
 #include <gdk/gdkx.h>
 void gtk_gl_enable(GtkWidget *widget)
@@ -99,7 +116,7 @@ void gtk_gl_disable(GtkWidget *widget)
 /************************
  * Win32 implementation *
  ************************/
-#elif defined(USE_WGL)
+#elif defined(SYS_WIN)
 #include <windows.h>
 #include <gdk/gdkwin32.h>
 static void on_realize(GtkWidget *widget, gpointer _)
@@ -173,42 +190,58 @@ void gtk_gl_disable(GtkWidget *widget)
 /**************************
  * Mac OSX implementation *
  **************************/
-#elif defined(USE_CGL)
+#elif defined(SYS_MAC)
+#include <gdk/gdkquartz.h>
 void gtk_gl_enable(GtkWidget *widget)
 {
-       CGDisplayCapture( kCGDirectMainDisplay );
-       CGLPixelFormatAttribute attribs[] =
-       {
-               kCGLPFANoRecovery,
-               kCGLPFADoubleBuffer,
-               kCGLPFAFullScreen,
-               kCGLPFAStencilSize, ( CGLPixelFormatAttribute ) 8,
-               kCGLPFADisplayMask, ( CGLPixelFormatAttribute ) CGDisplayIDToOpenGLDisplayMask( kCGDirectMainDisplay ),
-               ( CGLPixelFormatAttribute ) NULL
-       };
-
-       CGLPixelFormatObj pixelFormatObj;
-       GLint numPixelFormats;
-       CGLChoosePixelFormat( attribs, &pixelFormatObj, &numPixelFormats );
+       g_debug("GtkGl: enable");
 
-       CGLCreateContext( pixelFormatObj, NULL, &contextObj );
+       /* Create context */
+       NSOpenGLPixelFormatAttribute attribs[] = {
+               NSOpenGLPFAColorSize, 24,
+               NSOpenGLPFAAlphaSize, 8,
+               NSOpenGLPFADepthSize, 1,
+               NSOpenGLPFADoubleBuffer,
+               0
+       };
+       NSOpenGLPixelFormat *pix = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
+       NSOpenGLContext     *ctx = [[NSOpenGLContext     alloc] initWithFormat:pix shareContext:nil];
 
-       CGLDestroyPixelFormat( pixelFormatObj );
+       /* Attach to widget */
+       gtk_widget_set_double_buffered(widget, FALSE);
 
-       CGLSetCurrentContext( contextObj );
-       CGLSetFullScreen( contextObj );
+       /* Save context */
+       g_object_set_data(G_OBJECT(widget), "glcontext", ctx);
 }
 
 void gtk_gl_begin(GtkWidget *widget)
 {
+       g_debug("GtkGl: begin");
+       GtkAllocation alloc;
+       gdk_window_ensure_native(gtk_widget_get_window(widget));
+       gtk_widget_get_allocation(widget, &alloc);
+
+       NSOpenGLContext *ctx  = g_object_get_data(G_OBJECT(widget), "glcontext");
+       GdkWindow       *win  = gtk_widget_get_window(widget);
+       NSView          *view = gdk_quartz_window_get_nsview(win);
+       NSRect           rect = NSMakeRect(alloc.x, alloc.y, alloc.width, alloc.height);
+
+       [ctx  setView:view];
+       [ctx  makeCurrentContext];
+       [ctx  update];
+       [view setFrame:rect];
 }
 
 void gtk_gl_end(GtkWidget *widget)
 {
+       g_debug("GtkGl: end");
+       NSOpenGLContext *ctx = g_object_get_data(G_OBJECT(widget), "glcontext");
+       [ctx flushBuffer];
 }
 
 void gtk_gl_disable(GtkWidget *widget)
 {
+       g_debug("GtkGl: disable");
 }