Skip to content

Commit

Permalink
Merge pull request #54301 from groud/optimize_tilemap_layer_update
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Oct 27, 2021
2 parents 8c162f4 + a6584e4 commit 435d50b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
71 changes: 37 additions & 34 deletions scene/2d/tile_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ int TileMap::get_selected_layer() const {
void TileMap::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
pending_update = true;
_clear_internals();
_recreate_internals();
} break;
Expand Down Expand Up @@ -623,8 +622,8 @@ String TileMap::get_layer_name(int p_layer) const {
void TileMap::set_layer_enabled(int p_layer, bool p_enabled) {
ERR_FAIL_INDEX(p_layer, (int)layers.size());
layers[p_layer].enabled = p_enabled;
_clear_internals();
_recreate_internals();
_clear_layer_internals(p_layer);
_recreate_layer_internals(p_layer);
emit_signal(SNAME("changed"));

update_configuration_warnings();
Expand All @@ -638,8 +637,8 @@ bool TileMap::is_layer_enabled(int p_layer) const {
void TileMap::set_layer_modulate(int p_layer, Color p_modulate) {
ERR_FAIL_INDEX(p_layer, (int)layers.size());
layers[p_layer].modulate = p_modulate;
_clear_internals();
_recreate_internals();
_clear_layer_internals(p_layer);
_recreate_layer_internals(p_layer);
emit_signal(SNAME("changed"));
}

Expand All @@ -651,8 +650,8 @@ Color TileMap::get_layer_modulate(int p_layer) const {
void TileMap::set_layer_y_sort_enabled(int p_layer, bool p_y_sort_enabled) {
ERR_FAIL_INDEX(p_layer, (int)layers.size());
layers[p_layer].y_sort_enabled = p_y_sort_enabled;
_clear_internals();
_recreate_internals();
_clear_layer_internals(p_layer);
_recreate_layer_internals(p_layer);
emit_signal(SNAME("changed"));

update_configuration_warnings();
Expand All @@ -666,8 +665,8 @@ bool TileMap::is_layer_y_sort_enabled(int p_layer) const {
void TileMap::set_layer_y_sort_origin(int p_layer, int p_y_sort_origin) {
ERR_FAIL_INDEX(p_layer, (int)layers.size());
layers[p_layer].y_sort_origin = p_y_sort_origin;
_clear_internals();
_recreate_internals();
_clear_layer_internals(p_layer);
_recreate_layer_internals(p_layer);
emit_signal(SNAME("changed"));
}

Expand All @@ -679,8 +678,8 @@ int TileMap::get_layer_y_sort_origin(int p_layer) const {
void TileMap::set_layer_z_index(int p_layer, int p_z_index) {
ERR_FAIL_INDEX(p_layer, (int)layers.size());
layers[p_layer].z_index = p_z_index;
_clear_internals();
_recreate_internals();
_clear_layer_internals(p_layer);
_recreate_layer_internals(p_layer);
emit_signal(SNAME("changed"));

update_configuration_warnings();
Expand Down Expand Up @@ -847,37 +846,41 @@ void TileMap::_update_dirty_quadrants() {
_recompute_rect_cache();
}

void TileMap::_recreate_internals() {
for (unsigned int layer = 0; layer < layers.size(); layer++) {
// Make sure that _clear_internals() was called prior.
ERR_FAIL_COND_MSG(layers[layer].quadrant_map.size() > 0, "TileMap layer " + itos(layer) + " had a non-empty quadrant map.");
void TileMap::_recreate_layer_internals(int p_layer) {
// Make sure that _clear_internals() was called prior.
ERR_FAIL_COND_MSG(layers[p_layer].quadrant_map.size() > 0, "TileMap layer " + itos(p_layer) + " had a non-empty quadrant map.");

if (!layers[layer].enabled) {
continue;
}
if (!layers[p_layer].enabled) {
return;
}

// Upadate the layer internals.
_rendering_update_layer(layer);
// Upadate the layer internals.
_rendering_update_layer(p_layer);

// Recreate the quadrants.
const Map<Vector2i, TileMapCell> &tile_map = layers[layer].tile_map;
for (const KeyValue<Vector2i, TileMapCell> &E : tile_map) {
Vector2i qk = _coords_to_quadrant_coords(layer, Vector2i(E.key.x, E.key.y));
// Recreate the quadrants.
const Map<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
for (const KeyValue<Vector2i, TileMapCell> &E : tile_map) {
Vector2i qk = _coords_to_quadrant_coords(p_layer, Vector2i(E.key.x, E.key.y));

Map<Vector2i, TileMapQuadrant>::Element *Q = layers[layer].quadrant_map.find(qk);
if (!Q) {
Q = _create_quadrant(layer, qk);
layers[layer].dirty_quadrant_list.add(&Q->get().dirty_list_element);
}
Map<Vector2i, TileMapQuadrant>::Element *Q = layers[p_layer].quadrant_map.find(qk);
if (!Q) {
Q = _create_quadrant(p_layer, qk);
layers[p_layer].dirty_quadrant_list.add(&Q->get().dirty_list_element);
}

Vector2i pk = E.key;
Q->get().cells.insert(pk);
Vector2i pk = E.key;
Q->get().cells.insert(pk);

_make_quadrant_dirty(Q);
}
_make_quadrant_dirty(Q);
}

_update_dirty_quadrants();
_queue_update_dirty_quadrants();
}

void TileMap::_recreate_internals() {
for (unsigned int layer = 0; layer < layers.size(); layer++) {
_recreate_layer_internals(layer);
}
}

void TileMap::_erase_quadrant(Map<Vector2i, TileMapQuadrant>::Element *Q) {
Expand Down
1 change: 1 addition & 0 deletions scene/2d/tile_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class TileMap : public Node2D {

void _update_dirty_quadrants();

void _recreate_layer_internals(int p_layer);
void _recreate_internals();

void _erase_quadrant(Map<Vector2i, TileMapQuadrant>::Element *Q);
Expand Down

0 comments on commit 435d50b

Please sign in to comment.