def getOverlay(self, rect):
"""
Draw the ShaderOverlay and return the result
parameters: pygame.Rect bounds of the Overlay
return values: pygame.Surface the result
"""
try:
surface = pygame.Surface(rect.size, pygame.SRCALPHA, 32)
except:
return pygame.Surface((0, 0), pygame.SRCALPHA, 32)
surface.fill(self._background)
shapes = pygame.Surface(rect.size, pygame.SRCALPHA, 32)
lightsources = pygame.Surface(rect.size, pygame.SRCALPHA, 32)
removeAfter = []
for ls in self._lightsources:
quality = self._lightsources[ls][3]
intensity = self._lightsources[ls][2]
color = self._lightsources[ls][1]
radius = self._lightsources[ls][0]
r = ls.rect
polygon = []
try:
for x in xrange(1, 2 * radius + r.w, quality):
polygon.append(self.raycast(r.center, (r.left - radius + x, r.top - radius), rect, radius))
for y in xrange(1, 2 * radius + r.h, quality):
polygon.append(self.raycast(r.center, (r.right + radius, r.top - radius + y), rect, radius))
for x in xrange(1, 2 * radius + r.w, quality):
polygon.append(self.raycast(r.center, (r.right + radius - x, r.bottom + radius), rect, radius))
for y in xrange(1, 2 * radius + r.h, quality):
polygon.append(self.raycast(r.center, (r.left - radius, r.bottom + radius - y), rect, radius))
except:
removeAfter.append(ls)
if polygon:
pygame.draw.polygon(shapes, color, polygon)
gradient = pygame.Surface(rect.size, pygame.SRCALPHA, 32)
try:
for n in range(2 * radius, 0, -1):
alpha = intensity - int(intensity * (float(n) / (2 * radius)))
pygame.draw.ellipse(gradient, color + (alpha,), r.inflate(n, n))
except:
removeAfter.append(ls)
shapes.blit(gradient, (0, 0), special_flags = pygame.BLEND_RGBA_MULT)
lightsources.blit(shapes, (0, 0), special_flags = pygame.BLEND_RGBA_MAX)
surface.blit(lightsources, (0, 0))
for ls in removeAfter:
self.removeLightsource(ls)
return surface
评论列表
文章目录