]> Pileus Git - grits/blobdiff - src/plugins/map.c
Move low level tile loading to GritsTile
[grits] / src / plugins / map.c
index 13f986c10e5dd2dbdc22245ff03d0a09483c1408..a573dcbf4b085518951e33268cc4759ff7685f0b 100644 (file)
 #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, 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
+       {{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 {
-       GritsPluginMap *map;
-       GritsTile      *tile;
-       guint8         *pixels;
-       gboolean        alpha;
-       gint            width;
-       gint            height;
-};
-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);
-       glBindTexture(GL_TEXTURE_2D, *tex);
-
-       glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-       glPixelStorei(GL_PACK_ALIGNMENT, 1);
-       glTexImage2D(GL_TEXTURE_2D, 0, 4, data->width, data->height, 0,
-                       (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_TO_EDGE);
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-       glFlush();
-
-       data->tile->data = tex;
-       if (data->map->viewer)
-               gtk_widget_queue_draw(GTK_WIDGET(data->map->viewer));
-       g_free(data->pixels);
-       g_free(data);
-       return FALSE;
-}
-
 static void _load_tile(GritsTile *tile, gpointer _map)
 {
        GritsPluginMap *map = _map;
@@ -108,52 +70,31 @@ static void _load_tile(GritsTile *tile, gpointer _map)
        }
        g_free(path);
 
-       /* Copy pixbuf data for callback */
-       struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
-       data->map    = map;
-       data->tile   = tile;
-       data->pixels = gdk_pixbuf_get_pixels(pixbuf);
-       data->alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
-       data->width  = gdk_pixbuf_get_width(pixbuf);
-       data->height = gdk_pixbuf_get_height(pixbuf);
-       data->pixels = g_memdup(data->pixels,
-                       data->width * data->height * (data->alpha ? 4 : 3));
-       g_object_unref(pixbuf);
-
+#ifdef MAP_MAP_COLORS
        /* Map texture colors, if needed */
-       for (int i = 0; i < data->width * data->height; i++) {
+       gint    width  = gdk_pixbuf_get_width(pixbuf);
+       gint    height = gdk_pixbuf_get_height(pixbuf);
+       guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
+       for (int i = 0; i < width * height; i++) {
                for (int j = 0; j < G_N_ELEMENTS(colormap); j++) {
-                       if (data->pixels[i*4+0] == colormap[j][0][0] &&
-                           data->pixels[i*4+1] == colormap[j][0][1] &&
-                           data->pixels[i*4+2] == colormap[j][0][2]) {
-                               data->pixels[i*4+0] = colormap[j][1][0];
-                               data->pixels[i*4+1] = colormap[j][1][1];
-                               data->pixels[i*4+2] = colormap[j][1][2];
-                               data->pixels[i*4+3] = colormap[j][1][3];
+                       if (pixels[i*4+0] == colormap[j][0][0] &&
+                           pixels[i*4+1] == colormap[j][0][1] &&
+                           pixels[i*4+2] == colormap[j][0][2]) {
+                               pixels[i*4+0] = colormap[j][1][0];
+                               pixels[i*4+1] = colormap[j][1][1];
+                               pixels[i*4+2] = colormap[j][1][2];
+                               pixels[i*4+3] = colormap[j][1][3];
                                break;
                        }
                }
        }
+#endif
 
        /* Load the GL texture from the main thread */
-       g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
+       grits_tile_load_pixbuf(tile, pixbuf);
        g_debug("GritsPluginMap: _load_tile end %p", g_thread_self());
 }
 
-static gboolean _free_tile_cb(gpointer data)
-{
-       glDeleteTextures(1, data);
-       g_free(data);
-       return FALSE;
-}
-static void _free_tile(GritsTile *tile, gpointer _map)
-{
-       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)
 {
        g_debug("GritsPluginMap: _update_tiles");
@@ -163,8 +104,7 @@ static void _update_tiles(gpointer _, gpointer _map)
        grits_tile_update(map->tiles, &eye,
                        MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
                        _load_tile, map);
-       grits_tile_gc(map->tiles, time(NULL)-10,
-                       _free_tile, map);
+       grits_tile_gc(map->tiles, time(NULL)-10, NULL, map);
 }
 
 /*************
@@ -252,8 +192,6 @@ static void grits_plugin_map_dispose(GObject *gobject)
                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(viewer);
        }
        G_OBJECT_CLASS(grits_plugin_map_parent_class)->dispose(gobject);
@@ -265,7 +203,7 @@ static void grits_plugin_map_finalize(GObject *gobject)
        /* Free data */
        grits_tms_free(map->tms);
        //grits_wms_free(map->wms);
-       grits_tile_free(map->tiles, _free_tile, map);
+       grits_tile_free(map->tiles, NULL, map);
        G_OBJECT_CLASS(grits_plugin_map_parent_class)->finalize(gobject);
 
 }