From 5f05077f4d8b8b195a1ccf92a4a198eadfff54fb Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Fri, 9 Sep 2016 20:39:10 +0200 Subject: [PATCH] Use rects to improve rendering times. --- tabakrolletjie/lights.py | 49 +++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/tabakrolletjie/lights.py b/tabakrolletjie/lights.py index 84318d1..9ecef86 100644 --- a/tabakrolletjie/lights.py +++ b/tabakrolletjie/lights.py @@ -6,9 +6,11 @@ import pymunk.pygame_util import pygame.display import pygame.draw import pygame.locals as pgl +import pygame.rect from .constants import LIGHT_CATEGORY, FITTINGS_CATEGORY from .rays import RayPolyManager +from .utils import DetailedTimer LIGHT_FILTER = pymunk.ShapeFilter( mask=pymunk.ShapeFilter.ALL_MASKS ^ ( @@ -137,18 +139,20 @@ class BaseLight(object): radius_mask = self._surface_cache.get('radius_mask') if radius_mask is None: radius_mask = self._surface_cache['radius_mask'] = ( - pygame.surface.Surface(surface.get_size(), pgl.SWSURFACE)) + pygame.surface.Surface( + surface.get_size(), pgl.SWSURFACE)).convert() 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)) + 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 | pgl.SRCALPHA)) + surface.get_size(), pgl.SWSURFACE)).convert_alpha() return radius_mask, ray_mask, overlay_surf @@ -156,36 +160,55 @@ class BaseLight(object): if not self.on: return - radius_mask, ray_mask, overlay_surf = self._cached_surfaces(surface) + dt = DetailedTimer("render_light") + dt.start() + dt.show("%s, %s" % (self.ray_manager._start, self.ray_manager._end)) + + max_radius = self.radius_limits[1] or 50.0 + min_radius = self.radius_limits[0] or 0 + + rw = max_radius * 2 + rx, ry = pymunk.pygame_util.to_pygame(self.position, surface) + 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) + radius_mask, ray_mask, overlay_surf = 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): pygame.draw.polygon(ray_mask, white, pygame_poly, 0) pygame.draw.aalines(ray_mask, white, True, pygame_poly, 1) + dt.lap("ray mask rendered") radius_mask.fill(black) centre = pymunk.pygame_util.to_pygame(self.position, surface) - max_radius = self.radius_limits[1] or 50.0 - min_radius = self.radius_limits[0] or 0 - width = max_radius - min_radius pygame.draw.circle( - radius_mask, white, centre, int(max_radius), int(width)) + radius_mask, white, centre, int(max_radius), 0) pygame.draw.circle( - radius_mask, white, (centre[0] + 1, centre[1]), - int(max_radius), int(width)) + radius_mask, black, centre, int(min_radius), 0) + dt.lap("radius mask rendered") - ray_mask.blit(radius_mask, (0, 0), None, pgl.BLEND_RGB_MULT) + ray_mask.blit(radius_mask, dest_rect, dest_rect, pgl.BLEND_RGB_MULT) ray_mask.set_colorkey(black) + 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, (0, 0), None) + 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") - surface.blit(overlay_surf, (0, 0), None) + surface.blit(overlay_surf, dest_rect, dest_rect) + dt.lap("blitted surface") + dt.end() def render_fitting(self, surface): pygame.draw.circle( -- 2.34.1