From 52214e03b3546381f5d963c4f34abfe5b77b66b4 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Fri, 9 Sep 2016 21:40:32 +0200 Subject: [PATCH] 40 FPS and one fewer temporary surfaces. --- tabakrolletjie/lights.py | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/tabakrolletjie/lights.py b/tabakrolletjie/lights.py index 9ecef86..de75310 100644 --- a/tabakrolletjie/lights.py +++ b/tabakrolletjie/lights.py @@ -140,27 +140,21 @@ class BaseLight(object): if radius_mask is None: radius_mask = self._surface_cache['radius_mask'] = ( pygame.surface.Surface( - surface.get_size(), pgl.SWSURFACE)).convert() + surface.get_size(), pgl.SWSURFACE)).convert_alpha() ray_mask = self._surface_cache.get('ray_mask') if ray_mask is None: ray_mask = self._surface_cache['ray_mask'] = ( - pygame.surface.Surface( - surface.get_size(), pgl.SWSURFACE)).convert() - - overlay_surf = self._surface_cache.get('overlay_surf') - if overlay_surf is None: - overlay_surf = self._surface_cache['overlay_surf'] = ( pygame.surface.Surface( surface.get_size(), pgl.SWSURFACE)).convert_alpha() - return radius_mask, ray_mask, overlay_surf + return radius_mask, ray_mask def render_light(self, surface): if not self.on: return - dt = DetailedTimer("render_light") + dt = DetailedTimer("render_light", debug=True) dt.start() dt.show("%s, %s" % (self.ray_manager._start, self.ray_manager._end)) @@ -172,12 +166,11 @@ class BaseLight(object): dest_rect = pygame.rect.Rect(rx, ry, rw, rw) dest_rect.move_ip(- max_radius, - max_radius) - white, black = (255, 255, 255), (0, 0, 0) + white, black = (255, 255, 255, 255), (0, 0, 0, 0) - radius_mask, ray_mask, overlay_surf = self._cached_surfaces(surface) + radius_mask, ray_mask = self._cached_surfaces(surface) radius_mask.set_clip(dest_rect) ray_mask.set_clip(dest_rect) - overlay_surf.set_clip(dest_rect) ray_mask.fill(black) for pygame_poly in self.ray_manager.pygame_polys(surface): @@ -193,20 +186,17 @@ class BaseLight(object): radius_mask, black, centre, int(min_radius), 0) dt.lap("radius mask rendered") - ray_mask.blit(radius_mask, dest_rect, dest_rect, pgl.BLEND_RGB_MULT) - ray_mask.set_colorkey(black) + ray_mask.blit(radius_mask, dest_rect, dest_rect, pgl.BLEND_RGBA_MULT) dt.lap("blitted radius mask to ray mask") light_colour = self.COLOURS[self.colour] intensity = int(255 * self.intensity) light_colour = light_colour + (intensity,) - overlay_surf.blit(ray_mask, dest_rect, dest_rect) - dt.lap("blitted ray mask to overlay") - overlay_surf.fill(light_colour, None, pgl.BLEND_RGBA_MULT) - dt.lap("coloured overlay") + ray_mask.fill(light_colour, dest_rect, pgl.BLEND_RGBA_MULT) + dt.lap("coloured ray mask") - surface.blit(overlay_surf, dest_rect, dest_rect) + surface.blit(ray_mask, dest_rect, dest_rect) dt.lap("blitted surface") dt.end() -- 2.34.1