X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=tabakrolletjie%2Futils.py;h=6be4469ecf8b404fc8032cb3f2bddb8dcacf0250;hb=923e068edb27b9b6d35359e496668038a1db6fa4;hp=bc5aef2572cf42e3c7ba20146fe2bd80f45974ad;hpb=58ac0aea3fcfbac1a3b2f8ec310c4f5d818bceb4;p=tabakrolletjie.git diff --git a/tabakrolletjie/utils.py b/tabakrolletjie/utils.py index bc5aef2..6be4469 100644 --- a/tabakrolletjie/utils.py +++ b/tabakrolletjie/utils.py @@ -2,11 +2,14 @@ import functools import time +import pygame.surface +import pygame.locals as pgl from .constants import DEBUG +from .loader import loader -def debug_timer(label): +def debug_timer(label, debug=False): """ A decorator for printing how long a function took if debug is true. """ def debug_inner(f): @@ -17,7 +20,52 @@ def debug_timer(label): return f(*args, **kw) finally: duration = time.time() - start_time - if DEBUG: + if DEBUG or debug: print "%s [%g seconds]" % (label, duration) return wrapper return debug_inner + + +class DetailedTimer(object): + """ A detailed timer for overly complex functions. + """ + def __init__(self, title, debug=False): + if DEBUG or debug: + self.title = title + self.times = [] + else: + # fast funtions with the correct signature + self.start = int + self.show = str + self.lap = str + self.end = int + + def start(self): + print "---- %s ----" % self.title + self.times.append(time.time()) + + def lap(self, label): + now = time.time() + print " %s: %s" % (label, now - self.times[-1]) + self.times.append(now) + + def show(self, text): + print " %s" % text + + def end(self): + print " %s: %s" % ("total", time.time() - self.times[0]) + print "---- ----" + + +def shadowed_text(text, font_name, size, offset=4): + font_black = loader.load_font(font_name, size=size) + font_white = loader.load_font(font_name, size=size) + background = font_black.render(text, True, (0, 0, 0)) + foreground = font_white.render(text, True, (255, 255, 255)) + new_size = (background.get_width() + offset, + background.get_height() + offset) + base = pygame.surface.Surface(new_size, pgl.SWSURFACE).convert_alpha() + base.fill((0, 0, 0, 0)) + base.blit(background, (offset, offset), None) + base.blit(foreground, (0, 0), None) + return base