Skip to content

Commit bcee128

Browse files
committed
Improve effects
1 parent 190769f commit bcee128

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

sc8pr/effect/__init__.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ class Effect:
1616
_fill = rgba("#ffffff00")
1717

1818
@staticmethod
19-
def shift(img, dt):
19+
def shift(img, dt, reverse=False):
2020
"Shift effect timing"
2121
for e in img.effects:
2222
e._t0 += dt
2323
e._t1 += dt
24+
if reverse:
25+
e._t0, e._t1 = e._t1, e._t0
2426
return img
2527

2628
@property
@@ -252,37 +254,30 @@ def apply(self, img, n):
252254

253255
class Checkerboard(Effect):
254256
grid = 8, 8
255-
mode = "+Y+Y"
257+
vel = 0, 1, 0, 1
256258

257259
def apply(self, img, n):
258260
srf = surface(img, True)
259261
if n <= 0 or n >= 1: return self.nofx(srf, n)
260262
w, h = srf.get_size()
261-
mode = self.mode.upper()
262263
srect = pygame.Rect(0, 0, w, h)
263264
gx, gy = self.grid
264265
sw = ceil(w / gx)
265266
sh = ceil(h / gy)
266-
late = n > 0.5
267-
sd = ceil((1 - 2 * (1 - n if late else n)) * (sh if late else sw) + 1)
268-
for c in range(gx):
269-
for r in range(gy):
270-
odd = (r + c) % 2
271-
y = r * sh
272-
x = c * sw
273-
if odd:
274-
if n >= 0.5: rect = None
275-
else:
276-
if mode[0] == "+":
277-
if mode[1] == "X": x += sw - sd
278-
else: y += sh - sd
279-
rect = (x, y, sw, sd) if mode[1] == "Y" else (x, y, sd, sh)
280-
else:
281-
if n <= 0.5: rect = (x, y, sw, sh)
282-
else:
283-
if mode[2] == "+":
284-
if mode[3] == "X": x += sd
285-
else: y += sd
286-
rect = (x, y, sw, sh - sd) if mode[3] == "Y" else (x, y, sw - sd, sh)
287-
if rect: srf.subsurface(srect.clip(rect)).fill(self._fill)
267+
rects = [[pygame.Rect(c*sw, r*sh, sw, sh).clip(srect) for c in range(gx)] for r in range(gy)]
268+
m = self.vel
269+
for r in range(gy):
270+
for c in range(gx):
271+
rect = rects[c][r]
272+
if (r + c) % 2: # Odd squares
273+
if n > 0.5:
274+
dx = 2 * (n - 0.5) * sw * m[2]
275+
dy = 2 * (n - 0.5) * sh * m[3]
276+
rect = rect.clip(rect.move(dx, dy))
277+
srf.subsurface(rect).fill(self._fill)
278+
elif n < 0.5: # Even squares
279+
dx = 2 * n * sw * m[0]
280+
dy = 2 * n * sh * m[1]
281+
rect = rect.clip(rect.move(dx, dy))
282+
srf.subsurface(rect).fill(self._fill)
288283
return srf

sc8pr/effect/stamp.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,25 @@
77

88
class Stamp(Effect):
99
_fill = rgba("white")
10+
special_flags = pygame.BLEND_RGBA_MIN
1011

1112
@staticmethod
1213
def new_surface(size):
1314
srf = pygame.Surface(size).convert_alpha()
1415
srf.fill(4*(0,))
1516
return srf
1617

17-
def apply_stamp(self, img, n):
18-
srf = surface(self.get_stamp(n, img.get_size()))
19-
srf.blit(img, (0, 0), special_flags=pygame.BLEND_RGBA_MIN)
20-
return srf
21-
2218
def apply(self, img, n=0):
2319
img = surface(img, True)
2420
if n <= 0 or n >= 1: return self.nofx(img, n)
25-
return self.apply_stamp(img, n)
21+
srf = surface(self.get_stamp(n, img.get_size()))
22+
srf.blit(img, (0, 0), special_flags=self.special_flags)
23+
return srf
2624

2725

2826
class Pupil(Stamp):
2927
sides = None
30-
31-
def __init__(self, center=(0.5, 0.5), **kwargs):
32-
self.center = center
33-
self.config(**kwargs)
34-
35-
def angle(self, t): return 90 - 180 / self.sides
28+
center = 0.5, 0.5
3629

3730
def get_stamp(self, t, size):
3831
w, h = size
@@ -43,9 +36,10 @@ def get_stamp(self, t, size):
4336
srf = self.new_surface(size)
4437
if self.sides is None: circle(srf, self._fill, (x, y), r)
4538
else:
39+
angle = 90 - 180 / self.sides
4640
a = 2 * pi / self.sides
4741
r /= cos(a/2)
4842
pts = [(r*cos(i*a), r*sin(i*a)) for i in range(self.sides)]
49-
poly = Polygon(pts, (0,0)).config(angle=self.angle(t)).config(pos=(x, y))
43+
poly = Polygon(pts, (0,0)).config(angle=angle).config(pos=(x, y))
5044
polygon(srf, self._fill, poly.vertices)
5145
return srf

0 commit comments

Comments
 (0)