Add support for transforming images when loading them.
authorSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 19:09:01 +0000 (21:09 +0200)
committerSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 19:09:01 +0000 (21:09 +0200)
tabakrolletjie/loader.py

index 7f012a0f0375ef525f16a265cbd17e7291ddaec5..e4a5aef3de424ed8253f3f96d641d257bb4987d5 100644 (file)
@@ -9,11 +9,14 @@ import pygame.display
 import pygame.mixer
 
 from .constants import DEBUG
+from .transforms import NullTransform
 
 
 class Loader(object):
     """ Load data files from beneath a prefix. """
 
+    NULL_TRANSFORM = NullTransform()
+
     def __init__(self, prefix):
         self._prefix = prefix
         self._cache = {}
@@ -33,16 +36,18 @@ class Loader(object):
         with self.open_file("stations", *parts) as f:
             return json.load(f)
 
-    def load_image(self, *parts):
+    def load_image(self, *parts, **kwargs):
         """Return a pygame surface of the requested image."""
         fn = self.full_path("images", *parts)
-        img = self._cache.get(fn, None)
+        transform = kwargs.pop("transform", self.NULL_TRANSFORM)
+        img = self._cache.get((fn, transform.hash), 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
+            img = transform.apply(img)
+            self._cache[(fn, transform.hash)] = img
         return img
 
     def load_font(self, *parts, **kwargs):