X-Git-Url: http://pileus.org/git/?p=grits;a=blobdiff_plain;f=src%2Fplugins%2Fmap.c;h=d93e33633f73fe5862752ae00ccfdf0abd43f330;hp=e30975f0df673348a89dba977324fcad7a0d18fb;hb=4d9d0eb76317c97b94f44a745ffecde29de0454d;hpb=8f21dbf234141f9bd8dda156f0d6f6a129820f63 diff --git a/src/plugins/map.c b/src/plugins/map.c index e30975f..d93e336 100644 --- a/src/plugins/map.c +++ b/src/plugins/map.c @@ -15,7 +15,16 @@ * along with this program. If not, see . */ +/** + * SECTION:map + * @short_description: Map plugin + * + * #GisPluginMap provides map overlays. Much of this data is obtained from the + * OpenStreetMap project. + */ + #include +#include #include #include @@ -27,29 +36,39 @@ #define TILE_WIDTH 1024 #define TILE_HEIGHT 512 -const guchar colormap[][2][4] = { +static const guchar colormap[][2][4] = { {{0x73, 0x91, 0xad}, {0x73, 0x91, 0xad, 0x20}}, // 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 { - GisPluginMap *self; + GisPluginMap *map; GisTile *tile; - GdkPixbuf *pixbuf; + gchar *path; }; -#include static gboolean _load_tile_cb(gpointer _data) { struct _LoadTileData *data = _data; - GisPluginMap *self = data->self; - GisTile *tile = data->tile; - GdkPixbuf *pixbuf = data->pixbuf; + GisPluginMap *map = data->map; + GisTile *tile = data->tile; + gchar *path = data->path; g_free(data); - /* Create Texture */ + /* Load pixbuf */ g_debug("GisPluginMap: _load_tile_cb start"); + GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL); + if (!pixbuf) { + g_warning("GisPluginMap: _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); @@ -70,7 +89,6 @@ static gboolean _load_tile_cb(gpointer _data) } guint *tex = g_new0(guint, 1); - gis_viewer_begin(self->viewer); glGenTextures(1, tex); glBindTexture(GL_TEXTURE_2D, *tex); @@ -78,35 +96,27 @@ 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(); - gis_viewer_end(self->viewer); tile->data = tex; - gtk_widget_queue_draw(GTK_WIDGET(self->viewer)); + gtk_widget_queue_draw(GTK_WIDGET(map->viewer)); g_object_unref(pixbuf); return FALSE; } -static void _load_tile(GisTile *tile, gpointer _self) +static void _load_tile(GisTile *tile, gpointer _map) { - GisPluginMap *self = _self; + GisPluginMap *map = _map; g_debug("GisPluginMap: _load_tile start %p", g_thread_self()); - char *path = gis_wms_make_local(self->wms, tile); struct _LoadTileData *data = g_new0(struct _LoadTileData, 1); - 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("GisPluginMap: _load_tile - Error loading pixbuf %s", path); - g_remove(path); - } - g_free(path); + data->map = map; + data->tile = tile; + data->path = gis_wms_fetch(map->wms, tile, GIS_ONCE, NULL, NULL); + g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL); g_debug("GisPluginMap: _load_tile end %p", g_thread_self()); } @@ -116,27 +126,27 @@ static gboolean _free_tile_cb(gpointer data) g_free(data); return FALSE; } -static void _free_tile(GisTile *tile, gpointer _self) +static void _free_tile(GisTile *tile, gpointer _map) { - GisPluginMap *self = _self; - g_debug("GisPluginMap: _free_tile: %p=%d", tile->data, *(guint*)tile->data); - g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL); + g_debug("GisPluginMap: _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 _self) +static gpointer _update_tiles(gpointer _map) { g_debug("GisPluginMap: _update_tiles"); - GisPluginMap *self = _self; - g_mutex_lock(self->mutex); - gdouble lat, lon, elev; - gis_viewer_get_location(self->viewer, &lat, &lon, &elev); - gis_tile_update(self->tiles, + GisPluginMap *map = _map; + if (!g_mutex_trylock(map->mutex)) + return NULL; + GisPoint eye; + gis_viewer_get_location(map->viewer, &eye.lat, &eye.lon, &eye.elev); + gis_tile_update(map->tiles, &eye, MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH, - lat, lon, elev, - _load_tile, self); - gis_tile_gc(self->tiles, time(NULL)-10, - _free_tile, self); - g_mutex_unlock(self->mutex); + _load_tile, map); + gis_tile_gc(map->tiles, time(NULL)-10, + _free_tile, map); + g_mutex_unlock(map->mutex); return NULL; } @@ -144,42 +154,46 @@ static gpointer _update_tiles(gpointer _self) * Callbacks * *************/ static void _on_location_changed(GisViewer *viewer, - gdouble lat, gdouble lon, gdouble elev, GisPluginMap *self) + gdouble lat, gdouble lon, gdouble elev, GisPluginMap *map) { - g_thread_create(_update_tiles, self, FALSE, NULL); + g_thread_create(_update_tiles, map, FALSE, NULL); } -static gpointer _expose(GisCallback *callback, gpointer _self) +static gpointer _threaded_init(GisPluginMap *map) { - GisPluginMap *self = GIS_PLUGIN_MAP(_self); - g_debug("GisPluginMap: expose viewer=%p tiles=%p,%p", - self->viewer, self->tiles, self->tiles->data); - gis_viewer_render_tiles(self->viewer, self->tiles); + _load_tile(map->tiles, map); + _update_tiles(map); return NULL; } /*********** * Methods * ***********/ +/** + * gis_plugin_map_new: + * @viewer: the #GisViewer to use for drawing + * + * Create a new instance of the map plugin. + * + * Returns: the new #GisPluginMap + */ GisPluginMap *gis_plugin_map_new(GisViewer *viewer) { g_debug("GisPluginMap: new"); - GisPluginMap *self = g_object_new(GIS_TYPE_PLUGIN_MAP, NULL); - self->viewer = g_object_ref(viewer); + GisPluginMap *map = g_object_new(GIS_TYPE_PLUGIN_MAP, NULL); + map->viewer = g_object_ref(viewer); /* Load initial tiles */ - _load_tile(self->tiles, self); - g_thread_create(_update_tiles, self, FALSE, NULL); + g_thread_create((GThreadFunc)_threaded_init, map, FALSE, NULL); /* Connect signals */ - self->sigid = g_signal_connect(self->viewer, "location-changed", - G_CALLBACK(_on_location_changed), self); + map->sigid = g_signal_connect(map->viewer, "location-changed", + G_CALLBACK(_on_location_changed), map); /* Add renderers */ - GisCallback *callback = gis_callback_new(_expose, self); - gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_OVERLAY, 0); + gis_viewer_add(viewer, GIS_OBJECT(map->tiles), GIS_LEVEL_OVERLAY-1, 0); - return self; + return map; } @@ -197,36 +211,36 @@ static void gis_plugin_map_plugin_init(GisPluginInterface *iface) /* Add methods to the interface */ } /* Class/Object init */ -static void gis_plugin_map_init(GisPluginMap *self) +static void gis_plugin_map_init(GisPluginMap *map) { g_debug("GisPluginMap: init"); /* Set defaults */ - self->mutex = g_mutex_new(); - self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST); - self->wms = gis_wms_new( + map->mutex = g_mutex_new(); + map->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST); + map->wms = gis_wms_new( "http://labs.metacarta.com/wms/vmap0", "basic", "image/png", "osm/", "png", TILE_WIDTH, TILE_HEIGHT); } static void gis_plugin_map_dispose(GObject *gobject) { g_debug("GisPluginMap: dispose"); - GisPluginMap *self = GIS_PLUGIN_MAP(gobject); + GisPluginMap *map = GIS_PLUGIN_MAP(gobject); /* Drop references */ - if (self->viewer) { - g_signal_handler_disconnect(self->viewer, self->sigid); - g_object_unref(self->viewer); - self->viewer = NULL; + if (map->viewer) { + g_signal_handler_disconnect(map->viewer, map->sigid); + g_object_unref(map->viewer); + map->viewer = NULL; } G_OBJECT_CLASS(gis_plugin_map_parent_class)->dispose(gobject); } static void gis_plugin_map_finalize(GObject *gobject) { g_debug("GisPluginMap: finalize"); - GisPluginMap *self = GIS_PLUGIN_MAP(gobject); + GisPluginMap *map = GIS_PLUGIN_MAP(gobject); /* Free data */ - gis_tile_free(self->tiles, _free_tile, self); - gis_wms_free(self->wms); - g_mutex_free(self->mutex); + gis_tile_free(map->tiles, _free_tile, map); + gis_wms_free(map->wms); + g_mutex_free(map->mutex); G_OBJECT_CLASS(gis_plugin_map_parent_class)->finalize(gobject); }