1 """ Tabakutilities. """
6 import pygame.locals as pgl
11 from .constants import DEBUG
12 from .loader import loader
15 def debug_timer(label, debug=False):
16 """ A decorator for printing how long a function took if debug is true.
20 def wrapper(*args, **kw):
21 start_time = time.time()
25 duration = time.time() - start_time
27 print "%s [%g seconds]" % (label, duration)
32 class DetailedTimer(object):
33 """ A detailed timer for overly complex functions.
35 def __init__(self, title, debug=False):
40 # fast funtions with the correct signature
47 print "---- %s ----" % self.title
48 self.times.append(time.time())
52 print " %s: %s" % (label, now - self.times[-1])
53 self.times.append(now)
59 print " %s: %s" % ("total", time.time() - self.times[0])
63 def shadowed_text(text, font_name, size, offset=4):
64 font_black = loader.load_font(font_name, size=size)
65 font_white = loader.load_font(font_name, size=size)
66 background = font_black.render(text, True, (0, 0, 0))
67 foreground = font_white.render(text, True, (255, 255, 255))
68 new_size = (background.get_width() + offset,
69 background.get_height() + offset)
70 base = pygame.surface.Surface(new_size, pgl.SWSURFACE).convert_alpha()
71 base.fill((0, 0, 0, 0))
72 base.blit(background, (offset, offset), None)
73 base.blit(foreground, (0, 0), None)
78 """Return the directory for the save location."""
79 app = "tabakrolletjie"
80 if sys.platform.startswith('win'):
81 if 'APPDATA' in os.environ:
82 return os.path.join(os.environ['APPDATA'], app)
83 return os.path.join(os.path.expanduser('~'), '.' + app)
84 elif 'XDG_DATA_HOME' in os.environ:
85 return os.path.join(os.environ['XDG_DATA_HOME'], app)
86 return os.path.join(os.path.expanduser('~'), '.local', 'share', app)
89 def get_save_file_name():
90 return os.path.join(save_location(), 'savegame.json')
93 def save_file_exists():
94 savefile = get_save_file_name()
95 return os.path.isfile(savefile)
98 def write_save_file(json_data):
99 save_dir = save_location()
100 if not os.path.exists(save_dir):
101 os.makedirs(save_dir)
102 savefile = get_save_file_name()
103 with open(savefile, 'wb') as f:
104 json.dump(json_data, f, indent=3)