From ab39c18968fd4435bcf1bfcaf89ede5ad08e3f88 Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 6 Sep 2016 22:04:36 +0200 Subject: [PATCH] Image loading --- tabakrolletjie/loader.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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")) -- 2.34.1