]> Pileus Git - grits/blobdiff - src/plugins/sat.c
Fix compiler warnings
[grits] / src / plugins / sat.c
index 488a4e505f76986e4b4cbeed41183f71fca88ff9..1e61f1de2c630fb6d10e1489be3d82c71929ea39 100644 (file)
@@ -24,6 +24,7 @@
  */
 
 #include <time.h>
+#include <string.h>
 #include <glib/gstdio.h>
 #include <GL/gl.h>
 
 struct _LoadTileData {
        GisPluginSat *sat;
        GisTile      *tile;
-       GdkPixbuf    *pixbuf;
+       gchar        *path;
 };
 static gboolean _load_tile_cb(gpointer _data)
 {
        struct _LoadTileData *data = _data;
-       GisPluginSat *sat    = data->sat;
-       GisTile      *tile   = data->tile;
-       GdkPixbuf    *pixbuf = data->pixbuf;
+       GisPluginSat *sat  = data->sat;
+       GisTile      *tile = data->tile;
+       gchar        *path = data->path;
        g_free(data);
 
-       /* Create Texture */
+       /* Load pixbuf */
        g_debug("GisPluginSat: _load_tile_cb start");
+       GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
+       if (!pixbuf) {
+               g_warning("GisPluginSat: _load_tile - Error loading pixbuf %s", path);
+               g_remove(path);
+               g_free(path);
+               return FALSE;
+       }
+       g_free(path);
+
+       /* Create Texture */
        guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
        gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
        gint      width  = gdk_pixbuf_get_width(pixbuf);
        gint      height = gdk_pixbuf_get_height(pixbuf);
 
+       /* Draw a border */
+       //gint border = 10;
+       //gint stride = gdk_pixbuf_get_rowstride(pixbuf);
+       //for (int i = 0; i < border; i++) {
+       //      memset(&pixels[(       i)*stride], 0xff, stride);
+       //      memset(&pixels[(height-i)*stride], 0xff, stride);
+       //}
+       //for (int i = 0; i < height; i++) {
+       //      memset(&pixels[(i*stride)], 0xff, border*4);
+       //      memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
+       //}
+
        guint *tex = g_new0(guint, 1);
        glGenTextures(1, tex);
        glBindTexture(GL_TEXTURE_2D, *tex);
@@ -63,10 +86,10 @@ static gboolean _load_tile_cb(gpointer _data)
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
        glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
                        (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
-       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
+       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);
        glFlush();
 
        tile->data = tex;
@@ -79,19 +102,11 @@ static void _load_tile(GisTile *tile, gpointer _sat)
 {
        GisPluginSat *sat = _sat;
        g_debug("GisPluginSat: _load_tile start %p", g_thread_self());
-       char *path = gis_wms_fetch(sat->wms, tile, GIS_ONCE, NULL, NULL);
        struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
-       data->sat   = sat;
-       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("GisPluginSat: _load_tile - Error loading pixbuf %s", path);
-               g_free(data);
-               g_remove(path);
-       }
-       g_free(path);
+       data->sat  = sat;
+       data->tile = tile;
+       data->path = gis_wms_fetch(sat->wms, tile, GIS_ONCE, NULL, NULL);
+       g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
        g_debug("GisPluginSat: _load_tile end %p", g_thread_self());
 }
 
@@ -103,21 +118,21 @@ static gboolean _free_tile_cb(gpointer data)
 }
 static void _free_tile(GisTile *tile, gpointer _sat)
 {
-       GisPluginSat *sat = _sat;
-       g_debug("GisPluginSat: _free_tile: %p=%d", tile->data, *(guint*)tile->data);
-       g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
+       g_debug("GisPluginSat: _free_tile: %p", tile->data);
+       if (tile->data)
+               g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
 }
 
 static gpointer _update_tiles(gpointer _sat)
 {
        g_debug("GisPluginSat: _update_tiles");
        GisPluginSat *sat = _sat;
-       g_mutex_lock(sat->mutex);
-       gdouble lat, lon, elev;
-       gis_viewer_get_location(sat->viewer, &lat, &lon, &elev);
-       gis_tile_update(sat->tiles,
+       if (!g_mutex_trylock(sat->mutex))
+               return NULL;
+       GisPoint eye;
+       gis_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
+       gis_tile_update(sat->tiles, &eye,
                        MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
-                       lat, lon, elev,
                        _load_tile, sat);
        gis_tile_gc(sat->tiles, time(NULL)-10,
                        _free_tile, sat);
@@ -134,6 +149,13 @@ static void _on_location_changed(GisViewer *viewer,
        g_thread_create(_update_tiles, sat, FALSE, NULL);
 }
 
+static gpointer _threaded_init(GisPluginSat *sat)
+{
+       _load_tile(sat->tiles, sat);
+       _update_tiles(sat);
+       return NULL;
+}
+
 /***********
  * Methods *
  ***********/
@@ -152,15 +174,14 @@ GisPluginSat *gis_plugin_sat_new(GisViewer *viewer)
        sat->viewer = g_object_ref(viewer);
 
        /* Load initial tiles */
-       _load_tile(sat->tiles, sat);
-       g_thread_create(_update_tiles, sat, FALSE, NULL);
+       g_thread_create((GThreadFunc)_threaded_init, sat, FALSE, NULL);
 
        /* Connect signals */
        sat->sigid = g_signal_connect(sat->viewer, "location-changed",
                        G_CALLBACK(_on_location_changed), sat);
 
        /* Add renderers */
-       gis_viewer_add(viewer, GIS_OBJECT(sat->tiles), GIS_LEVEL_WORLD, 0);
+       gis_viewer_add(viewer, GIS_OBJECT(sat->tiles), GIS_LEVEL_WORLD, FALSE);
 
        return sat;
 }