Skip to content

Commit 679d484

Browse files
authored
Fix setTileGID corrupting existing tiles. (#3153)
* Fix setTileGID corrupting existing tiles. * Applied review fix
1 parent b5c181a commit 679d484

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

axmol/2d/FastTMXLayer.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,15 @@ void FastTMXLayer::updateIndexBuffers()
387387
if (batch.indices.empty())
388388
continue;
389389
const auto size = sizeof(decltype(batch.indices)::value_type) * batch.indices.size();
390-
// Recreate the buffer if the tile count grew beyond its allocated capacity.
391-
if (batch.indexBuffer && size > batch.indexBuffer->getCapacity())
390+
if (!batch.indexBuffer)
392391
{
393-
AX_SAFE_RELEASE(batch.indexBuffer);
394-
batch.indexBuffer = nullptr;
392+
// Pre-allocate at maximum possible size so the buffer never needs to be
393+
// recreated when setTileGID adds tiles. Recreating would invalidate existing
394+
// CustomCommand references (same reasoning as the vertex buffer).
395+
const auto maxSize = sizeof(decltype(batch.indices)::value_type) * 6 /* 6 indices per quad */ *
396+
static_cast<size_t>(_layerSize.width) * static_cast<size_t>(_layerSize.height);
397+
batch.indexBuffer = axdrv->createBuffer(maxSize, rhi::BufferType::INDEX, rhi::BufferUsage::DYNAMIC);
395398
}
396-
if (!batch.indexBuffer)
397-
batch.indexBuffer = axdrv->createBuffer(size, rhi::BufferType::INDEX, rhi::BufferUsage::DYNAMIC);
398399
batch.indexBuffer->updateData(batch.indices.data(), size);
399400
}
400401
}

0 commit comments

Comments
 (0)