import json
import os
+import pygame.image
+import pygame.display
+
class Loader(object):
""" Load data files from beneath a prefix. """
def __init__(self, prefix):
self._prefix = prefix
+ self._cache = {}
def full_path(self, *parts):
path = "/".join(parts)
with self.open_file("stations", *parts) as f:
return json.load(f)
+ def load_image(self, *parts):
+ """Return a pygame surface of the requested image."""
+ fn = self.full_path("images", *parts)
+ img = self._cache.get(fn, None)
+ if img is None:
+ img = pygame.image.load(fn)
+ # We assume pygame.display has been initialised
+ # Fix this if that changes
+ img.convert_alpha(pygame.display.get_surface())
+ self._cache[fn] = img
+ return img
+
_DATA_PREFIX = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "data"))