]> Pileus Git - grits/blobdiff - src/plugins/map.c
More reference counting updates
[grits] / src / plugins / map.c
index 9f89890a44771b988d313a005561398e20c1c5c9..13f986c10e5dd2dbdc22245ff03d0a09483c1408 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
+ * 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
 
 #include <time.h>
 #include <glib/gstdio.h>
-#include <GL/gl.h>
 
 #include <grits.h>
 
 #include "map.h"
 
-#define MAX_RESOLUTION 100
-#define TILE_WIDTH     1024
-#define TILE_HEIGHT    512
+#define MAX_RESOLUTION 1
+#define TILE_WIDTH     256
+#define TILE_HEIGHT    256
+//#define MAX_RESOLUTION 100
+//#define TILE_WIDTH     1024
+//#define TILE_HEIGHT    512
 
 static const guchar colormap[][2][4] = {
-       {{0x73, 0x91, 0xad}, {0x73, 0x91, 0xad, 0x20}}, // Oceans
-       {{0xf6, 0xee, 0xee}, {0xf6, 0xee, 0xee, 0x00}}, // Ground
-       {{0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff}}, // Borders
-       {{0x73, 0x93, 0xad}, {0x73, 0x93, 0xad, 0x40}}, // Lakes
-       {{0xff, 0xe1, 0x80}, {0xff, 0xe1, 0x80, 0x60}}, // Cities
+       //{{0x73, 0x91, 0xad}, {0x73, 0x91, 0xad, 0x00}}, // Oceans
+       //{{0xf6, 0xee, 0xee}, {0xf6, 0xee, 0xee, 0x00}}, // Ground
+       //{{0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff}}, // Borders
+       //{{0x73, 0x93, 0xad}, {0x73, 0x93, 0xad, 0x40}}, // Lakes
+       //{{0xff, 0xe1, 0x80}, {0xff, 0xe1, 0x80, 0x60}}, // Cities
 };
 
 struct _LoadTileData {
@@ -55,6 +57,10 @@ static gboolean _load_tile_cb(gpointer _data)
 {
        struct _LoadTileData *data = _data;
        g_debug("GritsPluginMap: _load_tile_cb start");
+       if (data->map->aborted) {
+               g_debug("GritsPluginMap: _load_tile - aborted");
+               return FALSE;
+       }
 
        guint *tex = g_new0(guint, 1);
        glGenTextures(1, tex);
@@ -66,12 +72,13 @@ static gboolean _load_tile_cb(gpointer _data)
                        (data->alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, data->pixels);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glFlush();
 
        data->tile->data = tex;
-       gtk_widget_queue_draw(GTK_WIDGET(data->map->viewer));
+       if (data->map->viewer)
+               gtk_widget_queue_draw(GTK_WIDGET(data->map->viewer));
        g_free(data->pixels);
        g_free(data);
        return FALSE;
@@ -87,7 +94,8 @@ static void _load_tile(GritsTile *tile, gpointer _map)
        }
 
        /* Download tile */
-       gchar *path = grits_wms_fetch(map->wms, tile, GRITS_ONCE, NULL, NULL);
+       gchar *path = grits_tms_fetch(map->tms, tile, GRITS_ONCE, NULL, NULL);
+       //gchar *path = grits_wms_fetch(map->wms, tile, GRITS_ONCE, NULL, NULL);
        if (!path) return; // Canceled/error
 
        /* Load pixbuf */
@@ -140,9 +148,10 @@ static gboolean _free_tile_cb(gpointer data)
 }
 static void _free_tile(GritsTile *tile, gpointer _map)
 {
-       g_debug("GritsPluginMap: _free_tile: %p", tile->data);
-       if (tile->data)
+       if (tile->data) {
+               g_debug("GritsPluginMap: _free_tile: %p", tile->data);
                g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
+       }
 }
 
 static void _update_tiles(gpointer _, gpointer _map)
@@ -185,7 +194,6 @@ GritsPluginMap *grits_plugin_map_new(GritsViewer *viewer)
        map->viewer = g_object_ref(viewer);
 
        /* Load initial tiles */
-       _load_tile(map->tiles, map);
        _update_tiles(NULL, map);
 
        /* Connect signals */
@@ -193,7 +201,7 @@ GritsPluginMap *grits_plugin_map_new(GritsViewer *viewer)
                        G_CALLBACK(_on_location_changed), map);
 
        /* Add renderers */
-       grits_viewer_add(viewer, GRITS_OBJECT(map->tiles), GRITS_LEVEL_OVERLAY-1, 0);
+       grits_viewer_add(viewer, GRITS_OBJECT(map->tiles), GRITS_LEVEL_WORLD, FALSE);
 
        return map;
 }
@@ -218,11 +226,15 @@ static void grits_plugin_map_init(GritsPluginMap *map)
        g_debug("GritsPluginMap: init");
        /* Set defaults */
        map->threads = g_thread_pool_new(_update_tiles, map, 1, FALSE, NULL);
-       map->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
-       map->wms   = grits_wms_new(
-               "http://vmap0.tiles.osgeo.org/wms/vmap0",
-               "basic,priroad,secroad,depthcontour,clabel,statelabel",
-                "image/png", "osm/", "png", TILE_WIDTH, TILE_HEIGHT);
+       map->tiles = grits_tile_new(NULL, 85.0511, -85.0511, EAST, WEST);
+       map->tms   = grits_tms_new("http://tile.openstreetmap.org",
+               "osmtile/", "png");
+       map->tiles->proj = GRITS_PROJ_MERCATOR;
+       //map->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
+       //map->wms   = grits_wms_new(
+       //      "http://vmap0.tiles.osgeo.org/wms/vmap0",
+       //      "basic,priroad,secroad,depthcontour,clabel,statelabel",
+       //       "image/png", "osm/", "png", TILE_WIDTH, TILE_HEIGHT);
        g_object_ref(map->tiles);
 }
 static void grits_plugin_map_dispose(GObject *gobject)
@@ -232,14 +244,17 @@ static void grits_plugin_map_dispose(GObject *gobject)
        map->aborted = TRUE;
        /* Drop references */
        if (map->viewer) {
-               g_signal_handler_disconnect(map->viewer, map->sigid);
-               grits_viewer_remove(map->viewer, GRITS_OBJECT(map->tiles));
-               soup_session_abort(map->wms->http->soup);
+               GritsViewer *viewer = map->viewer;
+               map->viewer = NULL;
+               g_signal_handler_disconnect(viewer, map->sigid);
+               grits_viewer_remove(viewer, GRITS_OBJECT(map->tiles));
+               g_object_unref(map->tiles);
+               soup_session_abort(map->tms->http->soup);
+               //soup_session_abort(map->wms->http->soup);
                g_thread_pool_free(map->threads, TRUE, TRUE);
                while (gtk_events_pending())
                        gtk_main_iteration();
-               g_object_unref(map->viewer);
-               map->viewer = NULL;
+               g_object_unref(viewer);
        }
        G_OBJECT_CLASS(grits_plugin_map_parent_class)->dispose(gobject);
 }
@@ -248,7 +263,8 @@ static void grits_plugin_map_finalize(GObject *gobject)
        g_debug("GritsPluginMap: finalize");
        GritsPluginMap *map = GRITS_PLUGIN_MAP(gobject);
        /* Free data */
-       grits_wms_free(map->wms);
+       grits_tms_free(map->tms);
+       //grits_wms_free(map->wms);
        grits_tile_free(map->tiles, _free_tile, map);
        G_OBJECT_CLASS(grits_plugin_map_parent_class)->finalize(gobject);