Skip to content

Don't draw spritelists with alpha 0 + tests #1640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()