File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments