Skip to content

Commit 76fe92b

Browse files
pvcravenPaul Craven
andauthored
Fix SpriteList.insert() not rendering inserted sprite until next list sync (#2864)
* Fix SpriteList.insert() not marking index buffer as changed SpriteList.insert() updated the CPU-side index data but never set self._sprite_index_changed = True, unlike append() and every other mutating method. As a result, a sprite added via insert() was present in the list but never uploaded to the GPU index buffer, so it was not rendered until an unrelated flag-setting operation forced a sync. Set the flag at the end of insert() to mirror append(). Fixes #2863 * Validate texture in SpriteList.insert() to match append() append() raises ValueError when a textureless sprite is added to an initialized SpriteList. insert() silently accepted it. Mirror the same guard in insert() for consistency between the two entry points. Note: this is a validation-only change; the atlas registration itself is already handled by _update_all() for both append() and insert(). --------- Co-authored-by: Paul Craven <paul.craven@optimizely.com>
1 parent 6936399 commit 76fe92b

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

arcade/sprite_list/sprite_list.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,10 +751,15 @@ def insert(self, index: int, sprite: SpriteType) -> None:
751751
self._grow_index_buffer()
752752
self._sprite_index_data.insert(index, slot)
753753
self._sprite_index_data.pop()
754+
self._sprite_index_changed = True
754755

755756
if self.spatial_hash is not None:
756757
self.spatial_hash.add(sprite)
757758

759+
if self._initialized:
760+
if sprite.texture is None:
761+
raise ValueError("Sprite must have a texture when added to a SpriteList")
762+
758763
def reverse(self) -> None:
759764
"""Reverses the current list in-place"""
760765
# Reverse the sprites and index buffer

tests/unit/spritelist/test_spritelist.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,23 @@ def test_it_can_insert_in_a_spritelist():
116116
assert [spritelist.sprite_slot[s] for s in spritelist] == [0, 2, 1]
117117
# Index buffer should refer to the slots in the same order
118118
assert list(spritelist._sprite_index_data[:3]) == [0, 2, 1]
119+
# insert() must flag the index buffer as changed so the sprite is
120+
# actually uploaded to the GPU and rendered on the next draw (#2863)
121+
assert spritelist._sprite_index_changed is True
122+
123+
124+
def test_insert_requires_texture_when_initialized(ctx):
125+
"""insert() into an initialized list should validate the texture, like append()"""
126+
spritelist = make_named_sprites(1)
127+
# Force initialization (as a draw would do)
128+
spritelist.draw()
129+
130+
sprite = arcade.SpriteSolidColor(16, 16, color=arcade.color.RED)
131+
# Bypass the texture setter to simulate a textureless sprite
132+
sprite._texture = None
133+
134+
with pytest.raises(ValueError):
135+
spritelist.insert(0, sprite)
119136

120137

121138
def test_it_can_reverse_a_spritelist():

0 commit comments

Comments
 (0)