From: Neil Date: Tue, 6 Sep 2016 20:04:36 +0000 (+0200) Subject: Image loading X-Git-Tag: tabakrolletjie-v1.0.0~221 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=ab39c18968fd4435bcf1bfcaf89ede5ad08e3f88;p=tabakrolletjie.git Image loading --- diff --git a/tabakrolletjie/loader.py b/tabakrolletjie/loader.py index 7dad79a..d37120f 100644 --- a/tabakrolletjie/loader.py +++ b/tabakrolletjie/loader.py @@ -3,12 +3,16 @@ 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) @@ -24,6 +28,18 @@ class Loader(object): 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"))