]> Pileus Git - grits/blobdiff - src/plugins/bmng.c
Cleanup memory on exit
[grits] / src / plugins / bmng.c
index 8b027493e7036da1dc982898353d4a6515c6f698..e8ec5025995af61a186ba29ee3260198b703fcc5 100644 (file)
@@ -16,6 +16,7 @@
  */
 
 #include <time.h>
+#include <glib/gstdio.h>
 #include <GL/gl.h>
 
 #include <gis.h>
@@ -47,7 +48,7 @@ static gboolean _load_tile_cb(gpointer _data)
        gint      height = gdk_pixbuf_get_height(pixbuf);
 
        guint *tex = g_new0(guint, 1);
-       gis_opengl_begin(self->opengl);
+       gis_viewer_begin(self->viewer);
        glGenTextures(1, tex);
        glBindTexture(GL_TEXTURE_2D, *tex);
 
@@ -60,10 +61,10 @@ static gboolean _load_tile_cb(gpointer _data)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
        glFlush();
-       gis_opengl_end(self->opengl);
+       gis_viewer_end(self->viewer);
 
        tile->data = tex;
-       gis_opengl_redraw(self->opengl);
+       gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
        g_object_unref(pixbuf);
        return FALSE;
 }
@@ -77,8 +78,13 @@ static void _load_tile(GisTile *tile, gpointer _self)
        data->self   = self;
        data->tile   = tile;
        data->pixbuf = gdk_pixbuf_new_from_file(path, NULL);
+       if (data->pixbuf) {
+               g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
+       } else {
+               g_warning("GisPluginBmng: _load_tile - Error loading pixbuf %s", path);
+               g_remove(path);
+       }
        g_free(path);
-       g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
        g_debug("GisPluginBmng: _load_tile end %p", g_thread_self());
 }
 
@@ -101,7 +107,7 @@ static gpointer _update_tiles(gpointer _self)
        GisPluginBmng *self = _self;
        g_mutex_lock(self->mutex);
        gdouble lat, lon, elev;
-       gis_view_get_location(self->view, &lat, &lon, &elev);
+       gis_viewer_get_location(self->viewer, &lat, &lon, &elev);
        gis_tile_update(self->tiles,
                        MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
                        lat, lon, elev,
@@ -115,37 +121,43 @@ static gpointer _update_tiles(gpointer _self)
 /*************
  * Callbacks *
  *************/
-static void _on_location_changed(GisView *view, gdouble lat, gdouble lon, gdouble elev,
-               GisPluginBmng *self)
+static void _on_location_changed(GisViewer *viewer,
+               gdouble lat, gdouble lon, gdouble elev, GisPluginBmng *self)
 {
        g_thread_create(_update_tiles, self, FALSE, NULL);
 }
 
+static gpointer _expose(GisCallback *callback, gpointer _self)
+{
+       GisPluginBmng *self = GIS_PLUGIN_BMNG(_self);
+       g_debug("GisPluginBmng: expose viewer=%p tiles=%p,%p",
+                       self->viewer, self->tiles, self->tiles->data);
+       gis_viewer_render_tiles(self->viewer, self->tiles);
+       return NULL;
+}
+
 /***********
  * Methods *
  ***********/
-GisPluginBmng *gis_plugin_bmng_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
+GisPluginBmng *gis_plugin_bmng_new(GisViewer *viewer)
 {
        g_debug("GisPluginBmng: new");
        GisPluginBmng *self = g_object_new(GIS_TYPE_PLUGIN_BMNG, NULL);
-       self->view   = view;
-       self->opengl = opengl;
+       self->viewer = g_object_ref(viewer);
 
        /* Load initial tiles */
        _load_tile(self->tiles, self);
        g_thread_create(_update_tiles, self, FALSE, NULL);
 
        /* Connect signals */
-       g_signal_connect(self->view, "location-changed", G_CALLBACK(_on_location_changed), self);
+       self->sigid = g_signal_connect(self->viewer, "location-changed",
+                       G_CALLBACK(_on_location_changed), self);
 
-       return self;
-}
+       /* Add renderers */
+       GisCallback *callback = gis_callback_new(_expose, self);
+       gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_WORLD, 0);
 
-static void gis_plugin_bmng_expose(GisPlugin *_self)
-{
-       GisPluginBmng *self = GIS_PLUGIN_BMNG(_self);
-       g_debug("GisPluginBmng: expose");
-       gis_opengl_render_tiles(self->opengl, self->tiles);
+       return self;
 }
 
 
@@ -161,7 +173,6 @@ static void gis_plugin_bmng_plugin_init(GisPluginInterface *iface)
 {
        g_debug("GisPluginBmng: plugin_init");
        /* Add methods to the interface */
-       iface->expose = gis_plugin_bmng_expose;
 }
 /* Class/Object init */
 static void gis_plugin_bmng_init(GisPluginBmng *self)
@@ -172,13 +183,18 @@ static void gis_plugin_bmng_init(GisPluginBmng *self)
        self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
        self->wms   = gis_wms_new(
                "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
-               "bmng", ".jpg", TILE_WIDTH, TILE_HEIGHT);
+               "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
 }
 static void gis_plugin_bmng_dispose(GObject *gobject)
 {
        g_debug("GisPluginBmng: dispose");
        GisPluginBmng *self = GIS_PLUGIN_BMNG(gobject);
        /* Drop references */
+       if (self->viewer) {
+               g_signal_handler_disconnect(self->viewer, self->sigid);
+               g_object_unref(self->viewer);
+               self->viewer = NULL;
+       }
        G_OBJECT_CLASS(gis_plugin_bmng_parent_class)->dispose(gobject);
 }
 static void gis_plugin_bmng_finalize(GObject *gobject)