From: Pierre-Luc Beaudoin Date: Fri, 15 Aug 2008 12:35:14 +0000 (-0400) Subject: Tile visibility X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3fd51149ec4776f9fc618494641ea4c2af66db05;p=libchamplain Tile visibility --- diff --git a/src/champlain_map_tile.h b/src/champlain_map_tile.h index aab6bd9..c31e903 100644 --- a/src/champlain_map_tile.h +++ b/src/champlain_map_tile.h @@ -28,6 +28,8 @@ typedef struct ClutterActor* actor; int x; int y; + int size; + gboolean visible; // Wether the tile is visible in the viewport } ChamplainMapTile; diff --git a/src/champlain_map_zoom_level.c b/src/champlain_map_zoom_level.c index 5f68109..87b8651 100644 --- a/src/champlain_map_zoom_level.c +++ b/src/champlain_map_zoom_level.c @@ -53,11 +53,9 @@ champlain_map_zoom_level_create(ChamplainMapZoomLevel* level, gint zoom_level) int x = i % level->row_count; int y = i / level->row_count; - ChamplainMapTile* tile = champlain_map_tile_new(x, y); - clutter_actor_set_position (tile->actor, x * level->tile_size, y * level->tile_size); - clutter_actor_set_size (tile->actor, level->tile_size, level->tile_size); + ChamplainMapTile* tile = champlain_map_tile_new(x, y, level->tile_size); - clutter_container_add (CLUTTER_CONTAINER (level->group), tile->actor, NULL); + //clutter_container_add (CLUTTER_CONTAINER (level->group), tile->actor, NULL); g_ptr_array_add (level->tiles, tile); } } diff --git a/src/champlain_private.h b/src/champlain_private.h index c986b4e..1a0568f 100644 --- a/src/champlain_private.h +++ b/src/champlain_private.h @@ -25,6 +25,6 @@ void champlain_map_create_tiles(gint zoom_level); ChamplainMapZoomLevel* champlain_map_zoom_level_new(gint zoom_level, gint row, gint column, gint tile_size); -ChamplainMapTile* champlain_map_tile_new(gint x, gint y); +ChamplainMapTile* champlain_map_tile_new(gint x, gint y, gint tile_size); #endif diff --git a/src/champlain_widget.c b/src/champlain_widget.c index bb8bfa9..a58e37a 100644 --- a/src/champlain_widget.c +++ b/src/champlain_widget.c @@ -54,7 +54,7 @@ typedef struct /* Units to store the origin of a click when scrolling */ ClutterUnit x; ClutterUnit y; -} ScrollMotion; +} ChamplainPoint; struct _ChamplainWidgetPrivate { @@ -64,8 +64,8 @@ struct _ChamplainWidgetPrivate ClutterActor *viewport; // Scrolling - ScrollMotion position; - ScrollMotion hitPoint; + ChamplainPoint position; + ChamplainPoint hitPoint; ChamplainMap* map; }; @@ -193,38 +193,61 @@ champlain_widget_init (ChamplainWidget * champlainWidget) } +static gboolean +tile_is_visible(ClutterUnit viewport_w, ClutterUnit viewport_h, ChamplainPoint position, ChamplainMapTile* tile) +{ + ClutterUnit size = CLUTTER_UNITS_FROM_INT(tile->size); + + + if( ((tile->x + 1)* size + position.x < 0 || tile->x* size + position.x > viewport_w) || + ((tile->y + 1)* size + position.y < 0 || tile->y* size + position.y > viewport_h)) + { + g_print ("Tile I: %d, %d\t p: %d, %d \n", + tile->x, tile->y, + CLUTTER_UNITS_TO_INT (position.x), + CLUTTER_UNITS_TO_INT (position.y)); + return FALSE; + } + g_print ("Tile V: %d, %d\t p: %d, %d \n", + tile->x, tile->y, + CLUTTER_UNITS_TO_INT (position.x), + CLUTTER_UNITS_TO_INT (position.y)); + return TRUE; + +} + static void champlain_widget_verify_tiles (ChamplainWidget * champlainWidget) { ChamplainWidgetPrivate *priv = CHAMPLAIN_WIDGET_GET_PRIVATE (champlainWidget); ClutterActor *stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->clutterEmbed)); - + ClutterUnit stage_w, stage_h; + clutter_actor_get_sizeu(stage, &stage_w, &stage_h); + g_print("Stage: %d, %d\n", CLUTTER_UNITS_TO_INT(stage_w), CLUTTER_UNITS_TO_INT(stage_h)); // Check for tiles that left the viewport - ClutterColor red; - clutter_color_parse ("red", &red); - ClutterColor white; - clutter_color_parse ("white", &white); - ClutterColor blue; - clutter_color_parse ("blue", &blue); + int i; - /*for (i = 0; i < priv->tiles->len; i++) + int tile_count = priv->map->current_level->tiles->len; + for (i = 0; i < tile_count; i++) { - int x = i % ROW_SIZE; - int y = i / ROW_SIZE; - ClutterActor * tile = CLUTTER_ACTOR(g_ptr_array_index (priv->tiles, i)); - ClutterUnit actor_x, actor_y, stage_w, stage_h; - clutter_actor_get_sizeu(stage, &stage_w, &stage_h); - clutter_actor_get_positionu(tile, &actor_x, &actor_y); - if( (actor_x + CLUTTER_UNITS_FROM_INT(TILE_SIZE) + priv->position.x < 0 || actor_x + priv->position.x > stage_w) || - (actor_y + CLUTTER_UNITS_FROM_INT(TILE_SIZE) + priv->position.y < 0 || actor_y + priv->position.y > stage_h)) + ChamplainMapTile* tile = g_ptr_array_index (priv->map->current_level->tiles, i); + if (tile_is_visible(stage_w, stage_h, priv->position, tile)) { - //clutter_rectangle_set_color(CLUTTER_RECTANGLE(tile), &red); - } - else + if(!tile->visible) + { + tile->visible = TRUE; + clutter_container_add (CLUTTER_CONTAINER (priv->map->current_level->group), tile->actor, NULL); + } + } + else { - clutter_rectangle_set_color(CLUTTER_RECTANGLE(tile), ( (y + x) % 2 % 2 ? &blue : &white)); + if (tile->visible) + { + clutter_container_remove_actor (CLUTTER_CONTAINER (priv->map->current_level->group), tile->actor); + tile->visible = FALSE; + } } - }*/ + } // Check for missing tiles in the viewport @@ -248,10 +271,13 @@ viewport_motion_event_cb (ClutterActor * actor, ClutterMotionEvent * event, Cham ChamplainWidgetPrivate *priv = CHAMPLAIN_WIDGET_GET_PRIVATE (champlainWidget); ClutterActor *stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->clutterEmbed)); + //FIXME: doesn't work when the viewport's size is larger than the stage (?) ClutterUnit x, y; if (clutter_actor_transform_stage_point (stage, - CLUTTER_UNITS_FROM_DEVICE (event-> - x), CLUTTER_UNITS_FROM_DEVICE (event->y), &x, &y)) + CLUTTER_UNITS_FROM_DEVICE (event->x), + CLUTTER_UNITS_FROM_DEVICE (event->y), + &x, + &y)) { ClutterUnit dx, dy; @@ -361,7 +387,7 @@ champlain_widget_load_map (ChamplainWidget * champlainWidget) champlain_map_load(priv->map, 1); clutter_container_add_actor (CLUTTER_CONTAINER (priv->viewport), priv->map->current_level->group); - + champlain_widget_verify_tiles (champlainWidget); } GtkWidget *