Skip to content

Commit 91769e2

Browse files
authored
Bugfix: Expose capacity in Spritelist.clear (#2479)
1 parent 729e1b2 commit 91769e2

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

arcade/sprite_list/sprite_list.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ def index(self, sprite: SpriteType) -> int:
567567
"""
568568
return self.sprite_list.index(sprite)
569569

570-
def clear(self, deep: bool = True) -> None:
570+
def clear(self, *, capacity: int | None = None, deep: bool = True) -> None:
571571
"""
572572
Remove all the sprites resetting the spritelist
573573
to it's initial state.
@@ -582,7 +582,9 @@ def clear(self, deep: bool = True) -> None:
582582
Sprite and SpriteList have a circular reference for performance reasons.
583583
584584
Args:
585-
deep: Wether to do a deep clear or not. Default is ``True``.
585+
deep: Whether to do a deep clear or not. Default is ``True``.
586+
capacity: The size of the internal buffers used to store the sprites.
587+
Defaults to preserving the current capacity.
586588
"""
587589
from .spatial_hash import SpatialHash
588590

@@ -599,8 +601,10 @@ def clear(self, deep: bool = True) -> None:
599601
self.spatial_hash = SpatialHash(cell_size=self._spatial_hash_cell_size)
600602

601603
# Clear the slot_idx and slot info and other states
602-
self._buf_capacity = _DEFAULT_CAPACITY
603-
self._idx_capacity = _DEFAULT_CAPACITY
604+
capacity = abs(capacity or self._buf_capacity)
605+
606+
self._buf_capacity = capacity
607+
self._idx_capacity = capacity
604608
self._sprite_buffer_slots = 0
605609
self._sprite_index_slots = 0
606610
self._sprite_buffer_free_slots = deque()

tests/unit/spritelist/test_spritelist.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def make_named_sprites(amount):
1010

1111
sprites = []
1212
for i in range(amount):
13-
c = i + 1
13+
c = min(255, i + 1)
1414
sprite = arcade.SpriteSolidColor(16, 16, color=(c, c, c, 1))
1515
sprite.name = i
1616
sprites.append(sprite)
@@ -219,19 +219,25 @@ def test_sort(ctx):
219219
assert spritelist._sprite_index_data[0:3] == array("f", [0, 1, 2])
220220

221221

222-
def test_clear(ctx):
223-
sp = arcade.SpriteList()
224-
sp.clear()
225-
sp.extend(make_named_sprites(100))
226-
sp.clear()
222+
@pytest.mark.parametrize('capacity', (128, 512, 1024))
223+
def test_clear(ctx, capacity):
224+
sp = arcade.SpriteList(capacity=capacity)
225+
sp.clear(capacity=None)
226+
assert len(sp._sprite_index_data) == capacity
227+
assert len(sp._sprite_pos_data) == capacity * 3
228+
assert sp._sprite_index_buf.size == capacity * 4
229+
assert sp._sprite_pos_buf.size == capacity * 4 * 3
230+
231+
sp.extend(make_named_sprites(capacity))
232+
sp.clear(capacity=capacity)
227233
assert len(sp) == 0
228234
assert sp._sprite_index_slots == 0
229235
assert sp._sprite_buffer_slots == 0
230236
assert sp.atlas is not None
231-
assert len(sp._sprite_index_data) == 100
232-
assert len(sp._sprite_pos_data) == 100 * 3
233-
assert sp._sprite_index_buf.size == 100 * 4
234-
assert sp._sprite_pos_buf.size == 100 * 4 * 3
237+
assert len(sp._sprite_index_data) == capacity
238+
assert len(sp._sprite_pos_data) == capacity * 3
239+
assert sp._sprite_index_buf.size == capacity * 4
240+
assert sp._sprite_pos_buf.size == capacity * 4 * 3
235241

236242

237243
def test_color():

0 commit comments

Comments
 (0)