Skip to content

Commit

Permalink
test ArtistFadeOverlay
Browse files Browse the repository at this point in the history
  • Loading branch information
kcsaff committed Sep 1, 2016
1 parent fdfb274 commit 204349b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
18 changes: 12 additions & 6 deletions sappho/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class pygame:


# Indicate to center artist's image
CENTER = object()
def CENTER(image):
return tuple(x // 2 for x in image.get_size())


class Particle(object):
Expand Down Expand Up @@ -557,8 +558,8 @@ def __init__(self, image, origin=CENTER, special_flags=0):
additive, subtractive, etc)
"""
self.image = image
if origin is CENTER:
self.origin = tuple(x // 2 for x in self.image.get_size())
if callable(origin):
self.origin = origin(self.image)
else:
self.origin = origin
self.special_flags = special_flags
Expand Down Expand Up @@ -602,9 +603,14 @@ def __init__(self, image, origin, tints,
additive, subtractive, etc) the tint will be applied to
the image. By default this is multiplicative.
"""
self.image = image.convert_alpha()
if origin is CENTER:
self.origin = tuple(x // 2 for x in self.image.get_size())
self.image = image
try:
self.image = self.image.convert_alpha()
except: # pragma: no cover
pass

if callable(origin):
self.origin = origin(self.image)
else:
self.origin = origin
self.tints = tints
Expand Down
61 changes: 60 additions & 1 deletion tests/test_particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ def test_artist_draw_center(self):
particle.Particle(0, 1, 1, 0, 3, 'pixel'),
particle.EmitterBurst.single(1),
artist=particle.ArtistSimple(
block, particle.ArtistSimple.CENTER),
block, particle.CENTER
),
)
world = pygame.surface.Surface((4, 4))
world.fill((0, 255, 0), pygame.Rect(0, 0, 4, 4))
Expand All @@ -368,5 +369,63 @@ def test_artist_draw_center(self):
assert(compare_surfaces(desired_world, world))


class TestArtistFadeOverlay(unittest.TestCase):
def test_artist_draw_origin(self):
block = pygame.surface.Surface((2, 2))
block.fill((255, 0, 0), pygame.Rect(0, 0, 2, 2))

ps = particle.ParticleSystem(
particle.Particle(0, 1, 1, 0, 3, 'pixel'),
particle.EmitterBurst.single(1),
artist=particle.ArtistFadeOverlay(
block, (0, 0),
[(255, 255, 255, 255), (255, 255, 255, 255)]
),
)
world = pygame.surface.Surface((4, 4))
world.fill((0, 255, 0), pygame.Rect(0, 0, 4, 4))

desired_world = pygame.surface.Surface((4, 4))
desired_world.fill((0, 255, 0), pygame.Rect(0, 0, 4, 4))
for x in (1, 2):
for y in (1, 2):
# Ugh this is only 254 for some reason
# get it together pygame
desired_world.set_at((x, y), (254, 0, 0))

ps.update_state(1)
ps.draw_on(world)
assert(compare_surfaces(desired_world, world))

def test_artist_draw_center(self):
block = pygame.surface.Surface((2, 2))
block.fill((255, 0, 0), pygame.Rect(0, 0, 2, 2))

ps = particle.ParticleSystem(
particle.Particle(0, 1, 1, 0, 3, 'pixel'),
particle.EmitterBurst.single(1),
artist=particle.ArtistFadeOverlay(
block, particle.CENTER,
[(255, 255, 255, 255), (255, 255, 255, 255)]
),
)
world = pygame.surface.Surface((4, 4))
world.fill((0, 255, 0), pygame.Rect(0, 0, 4, 4))

desired_world = pygame.surface.Surface((4, 4))
desired_world.fill((0, 255, 0), pygame.Rect(0, 0, 4, 4))
for x in (0, 1):
for y in (0, 1):
# Ugh this is only 254 for some reason
# get it together pygame
desired_world.set_at((x, y), (254, 0, 0))

ps.update_state(1)
ps.draw_on(world)

print(world.get_at((1, 1)))
assert(compare_surfaces(desired_world, world))


if __name__ == '__main__':
unittest.main()

0 comments on commit 204349b

Please sign in to comment.