Skip to content

Commit bd98e82

Browse files
authored
Tilemaps: Support lazy spritelist creation (#1400)
1 parent 2793ffd commit bd98e82

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

arcade/tilemap/tilemap.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class TileMap:
114114
can be overridden with the layer_options dict.
115115
:param Optional[arcade.TextureAtlas] texture_atlas: A default texture atlas to use for the
116116
SpriteLists created by this map. If not supplied the global default atlas will be used.
117+
:param bool lazy: SpriteLists will be created lazily.
117118
118119
119120
The `layer_options` parameter can be used to specify per layer arguments.
@@ -178,6 +179,7 @@ def __init__(
178179
tiled_map: Optional[pytiled_parser.TiledMap] = None,
179180
offset: Vec2 = Vec2(0, 0),
180181
texture_atlas: Optional["TextureAtlas"] = None,
182+
lazy: bool = False,
181183
) -> None:
182184
"""
183185
Given a .json file, this will read in a Tiled map file, and
@@ -206,6 +208,8 @@ def __init__(
206208
if not texture_atlas:
207209
texture_atlas = get_window().ctx.default_atlas
208210

211+
self._lazy = lazy
212+
209213
# Set Map Attributes
210214
self.width = self.tiled_map.map_size.width
211215
self.height = self.tiled_map.map_size.height
@@ -600,7 +604,11 @@ def _process_image_layer(
600604
custom_class_args: Dict[str, Any] = {},
601605
) -> SpriteList:
602606

603-
sprite_list: SpriteList = SpriteList(use_spatial_hash=use_spatial_hash, atlas=texture_atlas)
607+
sprite_list: SpriteList = SpriteList(
608+
use_spatial_hash=use_spatial_hash,
609+
atlas=texture_atlas,
610+
lazy=self._lazy,
611+
)
604612

605613
map_source = self.tiled_map.map_file
606614
map_directory = os.path.dirname(map_source)
@@ -691,7 +699,11 @@ def _process_tile_layer(
691699
custom_class_args: Dict[str, Any] = {},
692700
) -> SpriteList:
693701

694-
sprite_list: SpriteList = SpriteList(use_spatial_hash=use_spatial_hash, atlas=texture_atlas)
702+
sprite_list: SpriteList = SpriteList(
703+
use_spatial_hash=use_spatial_hash,
704+
atlas=texture_atlas,
705+
lazy=self._lazy,
706+
)
695707
map_array = layer.data
696708

697709
# Loop through the layer and add in the list
@@ -777,7 +789,11 @@ def _process_object_layer(
777789
# shape: Optional[Union[Point, PointList, Rect]] = None
778790
if isinstance(cur_object, pytiled_parser.tiled_object.Tile):
779791
if not sprite_list:
780-
sprite_list = SpriteList(use_spatial_hash=use_spatial_hash, atlas=texture_atlas)
792+
sprite_list = SpriteList(
793+
use_spatial_hash=use_spatial_hash,
794+
atlas=texture_atlas,
795+
lazy=self._lazy,
796+
)
781797

782798
tile = self._get_tile_by_gid(cur_object.gid)
783799
my_sprite = self._create_sprite_from_tile(
@@ -953,6 +969,7 @@ def load_tilemap(
953969
hit_box_detail: float = 4.5,
954970
offset: Vec2 = Vec2(0, 0),
955971
texture_atlas: Optional["TextureAtlas"] = None,
972+
lazy: bool = False,
956973
) -> TileMap:
957974
"""
958975
Given a .json map file, loads in and returns a `TileMap` object.
@@ -975,6 +992,7 @@ def load_tilemap(
975992
:param pyglet.math.Vec2 offset: Can be used to offset the position of all sprites and objects
976993
within the map. This will be applied in addition to any offsets from Tiled. This value
977994
can be overridden with the layer_options dict.
995+
:param bool lazy: SpriteLists will be created lazily.
978996
"""
979997
return TileMap(
980998
map_file=map_file,
@@ -985,6 +1003,7 @@ def load_tilemap(
9851003
hit_box_detail=hit_box_detail,
9861004
offset=offset,
9871005
texture_atlas=texture_atlas,
1006+
lazy=lazy,
9881007
)
9891008

9901009

tests/unit2/test_sprite_list.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ def test_spritelist_lazy():
139139
)
140140
assert len(spritelist) == 100
141141
assert spritelist.spatial_hash
142-
142+
assert spritelist._initialized is False
143+
spritelist.initialize()
144+
assert spritelist._initialized
145+
assert spritelist._sprite_pos_buf
146+
assert spritelist._geometry
143147

144148
def test_sort(ctx):
145149
s1 = arcade.SpriteSolidColor(10, 10, arcade.color.WHITE)

0 commit comments

Comments
 (0)