Skip to content

Commit

Permalink
Don't draw spritelists with alpha 0 + tests (#1640)
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Mar 17, 2023
1 parent 1152955 commit 2482fb8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion arcade/sprite_list/sprite_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ def draw(self, *, filter=None, pixelated=None, blend_function=None):
:param blend_function: Optional parameter to set the OpenGL blend function used for drawing the
sprite list, such as 'arcade.Window.ctx.BLEND_ADDITIVE' or 'arcade.Window.ctx.BLEND_DEFAULT'
"""
if len(self.sprite_list) == 0 or not self._visible:
if len(self.sprite_list) == 0 or not self._visible or self.alpha_normalized == 0.0:
return

self._init_deferred()
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/spritelist/test_spritelist_draw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
import arcade
from arcade.gl import Geometry


def test_visible(window, monkeypatch):
"""Ensure invisible spritelists are not drawn"""
sp = arcade.SpriteList()
# Monkeypatch Geometry.render to raise an error if called
def mock_draw(*args, **kwargs):
raise AssertionError("Should not be called")
monkeypatch.setattr(Geometry, "render", mock_draw)

# Empty spritelist should not be rendered
sp.draw()

# It will draw if it has sprites
sp.append(arcade.SpriteSolidColor(10, 10, color=arcade.color.RED))
# Geometry.render should be called
with pytest.raises(AssertionError):
sp.draw()

# Invisible should not be rendered
sp.visible = False
sp.draw()
sp.visible = True

# Alpha 0 should not be rendered
sp.alpha = 0
sp.draw()

0 comments on commit 2482fb8

Please sign in to comment.