Skip to content

Fix SpriteList.insert() not rendering inserted sprite until next list sync#2864

Open
pvcraven wants to merge 2 commits into
developmentfrom
fix/2863-insert-index-changed
Open

Fix SpriteList.insert() not rendering inserted sprite until next list sync#2864
pvcraven wants to merge 2 commits into
developmentfrom
fix/2863-insert-index-changed

Conversation

@pvcraven

@pvcraven pvcraven commented Jul 15, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #2863.

Primary fix: inserted sprites not rendered

SpriteList.insert() updated the CPU-side index data (_sprite_index_data) but never set self._sprite_index_changed = True. Every other mutating method (append, remove, pop, swap, reverse, sort, clear) sets it.

Because _write_sprite_buffers_to_gpu() only uploads the index buffer when that flag is set, a sprite added via insert() was fully present in the list (updated, collides, removable) but never rendered — it silently appeared only when some later unrelated operation set the flag and forced a full sync. This produced hard-to-diagnose intermittent "invisible sprite" bugs.

Fix: set self._sprite_index_changed = True at the end of insert(), mirroring append().

Secondary: texture validation consistency

append() raises ValueError when a textureless sprite is added to an initialized list, but insert() did not. Added the same guard to insert() so the two entry points behave identically.

Note: this is validation-only. The atlas registration itself is already handled by _update_all() for both append() and insert(), so there was no atlas/rendering bug there.

Verification

  • Reproduced the original bug with the offscreen render/pixel-readback script from the issue; the inserted sprite is now drawn on the very next draw().
  • Added a regression assertion to test_it_can_insert_in_a_spritelist confirming the index-changed flag is set.
  • Added test_insert_requires_texture_when_initialized covering the validation guard.
  • pytest tests/unit/spritelist/test_spritelist.py passes (19 passed); ruff clean.

Paul Craven added 2 commits July 15, 2026 16:03
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
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().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SpriteList.insert() does not set _sprite_index_changed: inserted sprite is not drawn until another list operation triggers a sync

1 participant