From 01dc6fdbe8a3107dc7ae1e0901839d329d2bcfe7 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Sun, 4 Sep 2016 22:56:08 +0200 Subject: [PATCH] Add performance debugging for lights. --- tabakrolletjie/constants.py | 3 +++ tabakrolletjie/lights.py | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/tabakrolletjie/constants.py b/tabakrolletjie/constants.py index d5aebb6..8e6bac7 100644 --- a/tabakrolletjie/constants.py +++ b/tabakrolletjie/constants.py @@ -3,6 +3,9 @@ TITLE = "Space Turnips" +# Debug +DEBUG = False + # 704 is 768 minus space for window decorations :) SCREEN_SIZE = (1024, 704) diff --git a/tabakrolletjie/lights.py b/tabakrolletjie/lights.py index a22ea14..ece9c4e 100644 --- a/tabakrolletjie/lights.py +++ b/tabakrolletjie/lights.py @@ -1,11 +1,13 @@ """ May it be a light for you in dark places, when all other lights go out. """ +import time + import pymunk import pymunk.pygame_util import pygame.draw -from .constants import SCREEN_SIZE, LIGHT_CATEGORY +from .constants import SCREEN_SIZE, LIGHT_CATEGORY, DEBUG LIGHT_FILTER = pymunk.ShapeFilter( mask=pymunk.ShapeFilter.ALL_MASKS ^ LIGHT_CATEGORY, @@ -19,17 +21,19 @@ def screen_rays(pos): """ w, h = SCREEN_SIZE left, right, bottom, top = 0, w, 0, h - for y in range(h): + step = 1 + for y in range(0, h, step): yield pymunk.Vec2d(left, y) - for x in range(w): + for x in range(0, w, step): yield pymunk.Vec2d(x, top) - for y in range(top, -1, -1): + for y in range(top, -1, -step): yield pymunk.Vec2d(right, y) - for x in range(right, -1, -1): + for x in range(right, -1, -step): yield pymunk.Vec2d(x, bottom) def calculate_ray_polys(space, body, position): + start_time = time.time() position = pymunk.Vec2d(position) vertices = [position] ray_polys = [] @@ -48,7 +52,11 @@ def calculate_ray_polys(space, body, position): ray_polys.append(new_poly) if len(vertices) > 2: ray_polys.append(pymunk.Poly(body, vertices)) - print "NUM POLYS: ", len(ray_polys) + end_time = time.time() + if DEBUG: + print( + "calculate_ray_polys: %d polys, %g seconds" % + (len(ray_polys), end_time - start_time)) return ray_polys -- 2.34.1