]> err.no Git - libchamplain/commitdiff
Downloading tiles!
authorPierre-Luc Beaudoin <pierre-luc@squidy.info>
Tue, 19 Aug 2008 03:09:37 +0000 (23:09 -0400)
committerPierre-Luc Beaudoin <pierre-luc@squidy.info>
Tue, 19 Aug 2008 03:09:37 +0000 (23:09 -0400)
configure.ac
src/launcher.c
src/map.c
src/sources/debugmap.c
src/sources/openstreetmap.c
src/tile.h

index bd15c90a96299239606c857d647fff5f1466b252..65c23fdb303be908ef37bf7483b7a64db1d7b7ab 100644 (file)
@@ -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
   ]
 )
 
index 447288c7b22668ffd2775e005afe34e287d44a2b..0e1bb2198f7ee4a6c3b54575741828685c4352e7 100644 (file)
@@ -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 */
index ea8dc580896608c6ced21746e17603270c63a352..6489cc640683ba96f6d307123749a54bbca8fde3 100644 (file)
--- 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);
             }
         }
     }
index b4a504356253a3c50578985bb3ed37ce4f025e37..33f2453d6cd4fb6d8f0a5c85d97d94f1166fabfd 100644 (file)
@@ -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);
index 811ab770e6494a75336c7e73f165c10bccd7d2a5..159149bbfdeb5b9a4ffa10ea7b91f76da1f16d78 100644 (file)
 #include <map.h>
 #include <math.h>
 #include <clutter/clutter.h>
+#include <libsoup/soup.h>
+
+
+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;
   
index 7065c3be2cc1cf838e2c047353431da7d00cc67e..76bcd0d7a82cac4bc6e55ae08a71064d21091290 100644 (file)
@@ -29,7 +29,6 @@ typedef struct
   int x;
   int y;
   int size;
-  gboolean visible;            // Wether the tile is visible in the viewport
   
 } Tile;