|
| 1 | +.. _pg_spritelists_advanced: |
| 2 | + |
| 3 | +Advanced SpriteList Techniques |
| 4 | +------------------------------ |
| 5 | + |
| 6 | +This page provides overviews of advanced techniques. Runnable examples are |
| 7 | +not guaranteed, as the reader is expected to be able to put the work into |
| 8 | +implementing them. |
| 9 | + |
| 10 | +Beginners should be careful of the following sections. Some of these techniques |
| 11 | +can slow down or crash your game if misused. |
| 12 | + |
| 13 | + |
| 14 | +.. _pg_spritelists_advanced_draw_order_and_sorting: |
| 15 | + |
| 16 | +Draw Order & Sorting |
| 17 | +^^^^^^^^^^^^^^^^^^^^ |
| 18 | + |
| 19 | +In some cases, you can combine two features of SpriteList: |
| 20 | + |
| 21 | +* By default, SpriteLists draw starting from their lowest index. |
| 22 | +* :py:class:`~arcade.SpriteList` has a :py:meth:`~arcade.SpriteList.sort` |
| 23 | + method nearly identical to :py:meth:`list.sort`. |
| 24 | + |
| 25 | + |
| 26 | +First, Consider Alternatives |
| 27 | +"""""""""""""""""""""""""""" |
| 28 | + |
| 29 | +Sorting in Python is a slow, CPU-bound function. Consider the following |
| 30 | +techniques to eliminate or minimize this cost: |
| 31 | + |
| 32 | +* Use multiple sprite lists or :py:class:`arcade.Scene` to |
| 33 | + achieve layering |
| 34 | +* Chunk your game world into smaller regions with sprite lists |
| 35 | + for each, and only sort when something inside moves or changes |
| 36 | +* Use the :py:attr:`Sprite.depth <arcade.BasicSprite.depth>` attribute |
| 37 | + with :ref:`shaders <tutorials_shaders>` to sort on the GPU |
| 38 | + |
| 39 | +For a conceptual overview of chunks as used in a commercial 2D game, please |
| 40 | +see the following: |
| 41 | + |
| 42 | +* `Chunks in Factorio <https://wiki.factorio.com/Map_structure#Chunk>`_ |
| 43 | + |
| 44 | + |
| 45 | +Sorting SpriteLists |
| 46 | +""""""""""""""""""" |
| 47 | + |
| 48 | +Although the alternative listed above are often better, sorting sprite lists to |
| 49 | +control draw order can still be useful. |
| 50 | + |
| 51 | +Like Python's built-in :py:meth:`list.sort`, you can pass a |
| 52 | +`callable object <https://docs.python.org/3/library/functions.html#callable>`_ |
| 53 | +via the key argument to specify how to sort, along with an optional ``reverse`` |
| 54 | +keyword to reverse the direction of sorting. |
| 55 | + |
| 56 | +Here's an example of how you could use sorting to quickly create an |
| 57 | +inefficient prototype: |
| 58 | + |
| 59 | +.. code:: python |
| 60 | +
|
| 61 | +
|
| 62 | + import random |
| 63 | + import arcade |
| 64 | +
|
| 65 | +
|
| 66 | + # Warning: the bottom property is extra slow compared to other attributes! |
| 67 | + def bottom_edge_as_sort_key(sprite): |
| 68 | + return sprite.bottom |
| 69 | +
|
| 70 | +
|
| 71 | + class InefficientTopDownGame(arcade.Window): |
| 72 | + """ |
| 73 | + Uses sorting to allow the player to move in front of & behind shrubs |
| 74 | +
|
| 75 | + For non-prototyping purposes, other approaches will be better. |
| 76 | + """ |
| 77 | +
|
| 78 | + def __init__(self, num_shrubs=50): |
| 79 | + super().__init__(800, 600, "Inefficient Top-Down Game") |
| 80 | +
|
| 81 | + self.background_color = arcade.color.SAND |
| 82 | + self.shrubs = arcade.SpriteList() |
| 83 | + self.drawable = arcade.SpriteList() |
| 84 | +
|
| 85 | + # Randomly place pale green shrubs around the screen |
| 86 | + for i in range(num_shrubs): |
| 87 | + shrub = arcade.SpriteSolidColor(20, 40, color=arcade.color.BUD_GREEN) |
| 88 | + shrub.position = random.randrange(self.width), random.randrange(self.height) |
| 89 | + self.shrubs.append(shrub) |
| 90 | + self.drawable.append(shrub) |
| 91 | +
|
| 92 | + self.player = arcade.SpriteSolidColor(16, 30, color=arcade.color.RED) |
| 93 | + self.drawable.append(self.player) |
| 94 | +
|
| 95 | + def on_mouse_motion(self, x, y, dx, dy): |
| 96 | + # Update the player position |
| 97 | + self.player.position = x, y |
| 98 | + # Sort the sprites so the highest on the screen draw first |
| 99 | + self.drawable.sort(key=bottom_edge_as_sort_key, reverse=True) |
| 100 | +
|
| 101 | + def on_draw(self): |
| 102 | + self.clear() |
| 103 | + self.drawable.draw() |
| 104 | +
|
| 105 | +
|
| 106 | + game = InefficientTopDownGame() |
| 107 | + game.run() |
| 108 | +
|
| 109 | +
|
| 110 | +.. _pg_spritelist_advanced_texture_atlases: |
| 111 | + |
| 112 | +Custom Texture Atlases |
| 113 | +^^^^^^^^^^^^^^^^^^^^^^ |
| 114 | + |
| 115 | +A :py:class:`~arcade.TextureAtlas` represents :py:class:`~arcade.Texture` |
| 116 | +data packed side-by-side in video memory. As textures are added, the atlas |
| 117 | +grows to fit them all into the same portion of your GPU's memory. |
| 118 | + |
| 119 | +By default, each :py:class:`~arcade.SpriteList` uses the same default |
| 120 | +atlas. Use the ``atlas`` keyword argument to specify a custom atlas |
| 121 | +for an instance. |
| 122 | + |
| 123 | +This is especially useful to prevent problems when using large or oddly |
| 124 | +shaped textures. |
| 125 | + |
| 126 | +Please see the following for more information: |
| 127 | + |
| 128 | +* :ref:`pg_textureatlas_custom_atlas` |
| 129 | +* The :py:class:`~arcade.TextureAtlas` API documentation |
| 130 | + |
| 131 | + |
| 132 | +.. _pg_spritelist_advanced_lazy_spritelists: |
| 133 | + |
| 134 | +Lazy SpriteLists |
| 135 | +^^^^^^^^^^^^^^^^ |
| 136 | + |
| 137 | +You can delay creating the OpenGL resources for a |
| 138 | +:py:class:`~arcade.SpriteList` by passing ``lazy=True`` on creation: |
| 139 | + |
| 140 | +.. code:: python |
| 141 | +
|
| 142 | + sprite_list = SpriteList(lazy=True) |
| 143 | +
|
| 144 | +The SpriteList won't create the OpenGL resources until forced to |
| 145 | +by one of the following: |
| 146 | + |
| 147 | +1. The first :py:meth:`SpriteList.draw() <arcade.SpriteList.draw>` call on it |
| 148 | +2. :py:meth:`SpriteList.initialize() <arcade.SpriteList.initialize>` |
| 149 | +3. GPU-backed collisions, if enabled |
| 150 | + |
| 151 | +This behavior is most useful in the following cases: |
| 152 | + |
| 153 | +.. list-table:: |
| 154 | + :header-rows: 1 |
| 155 | + |
| 156 | + * - Case |
| 157 | + - Primary Purpose |
| 158 | + |
| 159 | + * - Creating SpriteLists before a Window |
| 160 | + - CPU-only `unit tests <https://docs.python.org/3/library/unittest.html>`_ which |
| 161 | + never draw |
| 162 | + |
| 163 | + * - Parallelized SpriteList creation |
| 164 | + - Faster loading & world generation via :py:mod:`threading` |
| 165 | + or :py:mod:`subprocess` & :py:mod:`pickle` |
| 166 | + |
| 167 | + |
| 168 | +.. _pg_spritelist_advanced_parallel_loading: |
| 169 | + |
| 170 | +Parallelized Loading |
| 171 | +"""""""""""""""""""" |
| 172 | + |
| 173 | +To increase loading speed & reduce stutters during gameplay, you can |
| 174 | +run pre-gameplay tasks in parallel, such as pre-generating maps |
| 175 | +or pre-loading assets from disk into RAM. |
| 176 | + |
| 177 | + |
| 178 | +.. warning:: Only the main thread is allowed to access OpenGL! |
| 179 | + |
| 180 | + Attempting to access OpenGL from non-main threads will |
| 181 | + raise an OpenGL Error! |
| 182 | + |
| 183 | +To safely implement parallel loading, you will want to use the following |
| 184 | +general approach before allowing gameplay to begin: |
| 185 | + |
| 186 | +1. Pass ``lazy=True`` when creating :py:class:`~arcade.SpriteList` instances |
| 187 | + in your loading code as described above |
| 188 | +2. Sync the SpriteList data back to the main thread or process once loading |
| 189 | + is finished |
| 190 | +3. Inside the main thread, call |
| 191 | + :py:meth:`Spritelist.initialize() <arcade.SpriteList.initialize>` on each |
| 192 | + sprite list once it's ready to allocate GPU resources |
| 193 | + |
| 194 | + |
| 195 | +Very advanced users can use :py:mod:`subprocess` to create SpriteLists |
| 196 | +inside another process and the :py:mod:`pickle` module to help pass data |
| 197 | +back to the main process. |
| 198 | + |
| 199 | +Please see the following for additional information: |
| 200 | + |
| 201 | +* :ref:`Arcade's OpenGL notes <open_gl_notes>` for arcade-specific |
| 202 | + threading considerations |
| 203 | +* Python's :py:mod:`threading` documentation |
| 204 | +* Python's :py:mod:`subprocess` and :py:mod:`pickle` documentation |
0 commit comments