From: Pierre-Luc Beaudoin Date: Tue, 19 Aug 2008 03:09:37 +0000 (-0400) Subject: Downloading tiles! X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=706d86cde6a8fe3f2de14396056edb30f39c592b;p=libchamplain Downloading tiles! --- diff --git a/configure.ac b/configure.ac index bd15c90..65c23fd 100644 --- a/configure.ac +++ b/configure.ac @@ -21,11 +21,12 @@ AC_SUBST(DEPS_CFLAGS) AC_SUBST(DEPS_LIBS) PKG_CHECK_MODULES(DEPS, - [ glib-2.0 >= 2.2, + [ glib-2.0 >= 2.16, gobject-2.0 >= 2.10, gtk+-2.0 >= 2.2, clutter-0.8 >= 0.8, - clutter-gtk-0.8 >= 0.8 + clutter-gtk-0.8 >= 0.8, + libsoup-2.4 >= 2.4.1 ] ) diff --git a/src/launcher.c b/src/launcher.c index 447288c..0e1bb21 100644 --- a/src/launcher.c +++ b/src/launcher.c @@ -50,7 +50,8 @@ main (int argc, char *argv[]) GtkWidget *window; GtkWidget *widget, *vbox, *bbox, *button, *viewport; GtkWidget *scrolled; - + + g_thread_init (NULL); gtk_clutter_init (&argc, &argv); /* create the main, top level, window */ diff --git a/src/map.c b/src/map.c index ea8dc58..6489cc6 100644 --- a/src/map.c +++ b/src/map.c @@ -44,7 +44,7 @@ map_load(Map* map, gint zoom_level) { guint row_count = map->get_row_count(map, zoom_level); guint column_count = map->get_column_count(map, zoom_level); - + map->current_level = zoom_level_new(zoom_level, row_count, column_count, map->tile_size); } @@ -81,7 +81,6 @@ map_load_visible_tiles (Map* map, ChamplainRect viewport) Tile* tile = map->get_tile(map, map->current_level->level, i, j); g_ptr_array_add (map->current_level->tiles, tile); - clutter_container_add (CLUTTER_CONTAINER (map->current_level->group), tile->actor, NULL); } } } diff --git a/src/sources/debugmap.c b/src/sources/debugmap.c index b4a5043..33f2453 100644 --- a/src/sources/debugmap.c +++ b/src/sources/debugmap.c @@ -80,7 +80,6 @@ Tile* debugmap_get_tile (Map* map, guint zoom_level, guint x, guint y) tile->x = x; tile->y = y; - tile->visible = FALSE; tile->actor = clutter_group_new(); ClutterActor* actor = clutter_rectangle_new_with_color (color); diff --git a/src/sources/openstreetmap.c b/src/sources/openstreetmap.c index 811ab77..159149b 100644 --- a/src/sources/openstreetmap.c +++ b/src/sources/openstreetmap.c @@ -21,6 +21,14 @@ #include #include #include +#include + + +typedef struct { + Map* map; + Tile* tile; +} TwoPtr ; + //http://wiki.openstreetmap.org/index.php/Slippy_map_tilenames#C.2FC.2B.2B @@ -60,26 +68,92 @@ osm_column_count(Map* map, guint zoom_level) { return pow (2, zoom_level); } +#define MAX_READ 100000 + +static void +file_loaded_cb (SoupSession *session, + SoupMessage *msg, + TwoPtr* ptr) +{ + GError *error = NULL; + GdkPixbufLoader* pixloader; + Tile* tile = ptr->tile; + Map* map = ptr->map; + + if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) + { + g_warning ("Unable to download tile %d, %d", tile->x, tile->y); + return; + } + + pixloader = gdk_pixbuf_loader_new(); + if (!gdk_pixbuf_loader_write (pixloader, + (const guchar *) msg->response_body->data, + msg->response_body->length, + NULL)) + { + if (error) + { + g_warning ("Unable to load the pixbuf: %s", error->message); + g_error_free (error); + } + + g_object_unref (pixloader); + } + + gdk_pixbuf_loader_close (pixloader, NULL); + if (error) + { + g_warning ("Unable to close the pixbuf loader: %s", error->message); + g_error_free (error); + g_object_unref (pixloader); + } + else + { + GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(pixloader); + + tile->actor = clutter_texture_new(); + clutter_texture_set_from_rgb_data(tile->actor, + gdk_pixbuf_get_pixels (pixbuf), + gdk_pixbuf_get_has_alpha (pixbuf), + gdk_pixbuf_get_width(pixbuf), + gdk_pixbuf_get_height(pixbuf), + gdk_pixbuf_get_rowstride (pixbuf), + 3, 0, NULL); + + + clutter_actor_set_position (tile->actor, tile->x * map->tile_size, tile->y * map->tile_size); + clutter_actor_set_size (tile->actor, map->tile_size, map->tile_size); + clutter_actor_show (tile->actor); + //g_object_ref(tile->actor); // to prevent actors to be destroyed when they are removed from groups + + clutter_container_add (CLUTTER_CONTAINER (map->current_level->group), tile->actor, NULL); + } +} Tile* osm_get_tile (Map* map, guint zoom_level, guint x, guint y) { + static SoupSession * session; + Tile* tile = g_new0(Tile, 1); tile->x = x; tile->y = y; - tile->visible = FALSE; - // For no apparent reason, the group is necessary even if - // it contains only one actor... if missing, the viewport will break - tile->actor = clutter_group_new(); - - ClutterActor* actor = clutter_texture_new_from_file(g_strdup_printf("/home/plbeaudoin/champlain/tiles/%d/%d/%d.png", zoom_level, x, y), NULL); - clutter_actor_set_position (actor, x * map->tile_size, y * map->tile_size); - clutter_actor_set_size (actor, map->tile_size, map->tile_size); - clutter_actor_show (actor); - clutter_container_add_actor (CLUTTER_CONTAINER (tile->actor), actor); - g_object_ref(tile->actor); // to prevent actors to be destroyed when they are removed from groups + TwoPtr* ptr = g_new0(TwoPtr, 1); + ptr->map = map; + ptr->tile = tile; + + SoupMessage *msg; + if (!session) + session = soup_session_async_new (); + + msg = soup_message_new (SOUP_METHOD_GET, g_strdup_printf("http://tile.openstreetmap.org/%d/%d/%d.png", zoom_level, x, y)); + + soup_session_queue_message (session, msg, + file_loaded_cb, + ptr); return tile; diff --git a/src/tile.h b/src/tile.h index 7065c3b..76bcd0d 100644 --- a/src/tile.h +++ b/src/tile.h @@ -29,7 +29,6 @@ typedef struct int x; int y; int size; - gboolean visible; // Wether the tile is visible in the viewport } Tile;