changeset 587:f20d211d2c91 pyntnclick

load_image() -> get_image() and more docs.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 11 Feb 2012 17:02:26 +0200
parents cf65e91b30b1
children 0571deb177e9
files pyntnclick/endscreen.py pyntnclick/main.py pyntnclick/menu.py pyntnclick/resources.py pyntnclick/state.py pyntnclick/tests/test_resources.py
diffstat 6 files changed, 40 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/pyntnclick/endscreen.py	Sat Feb 11 16:56:01 2012 +0200
+++ b/pyntnclick/endscreen.py	Sat Feb 11 17:02:26 2012 +0200
@@ -15,7 +15,7 @@
 class EndScreen(Screen):
     def __init__(self, shell, game_description):
         Screen.__init__(self, shell)
-        self.background = game_description.resource.load_image(
+        self.background = game_description.resource.get_image(
                 ('won', 'won.png'))
         self._menu_button = EndImageButton('menu.png', 26, 500,
                 action=self.main_menu)
--- a/pyntnclick/main.py	Sat Feb 11 16:56:01 2012 +0200
+++ b/pyntnclick/main.py	Sat Feb 11 17:02:26 2012 +0200
@@ -104,7 +104,7 @@
             self._debug_rects = opts.rects
         display = pygame.display.set_mode(self.constants.screen,
                                           SWSURFACE)
-        pygame.display.set_icon(self.resource.load_image(
+        pygame.display.set_icon(self.resource.get_image(
                 'suspended_sentence24x24.png', basedir='icons'))
         pygame.display.set_caption("Suspended Sentence")
         shell = MainShell(display, self)
--- a/pyntnclick/menu.py	Sat Feb 11 16:56:01 2012 +0200
+++ b/pyntnclick/menu.py	Sat Feb 11 17:02:26 2012 +0200
@@ -15,7 +15,7 @@
 class MenuScreen(Screen):
     def __init__(self, shell, game_description):
         Screen.__init__(self, shell)
-        self._background = game_description.resource.load_image(
+        self._background = game_description.resource.get_image(
                 ('splash', 'splash.png'))
         self._start_button = SplashButton('play.png', 16, 523, self.start)
         self._resume_button = SplashButton('resume.png', 256, 523, self.resume,
--- a/pyntnclick/resources.py	Sat Feb 11 16:56:01 2012 +0200
+++ b/pyntnclick/resources.py	Sat Feb 11 17:02:26 2012 +0200
@@ -48,6 +48,8 @@
         raise ResourceNotFound(resource_name)
 
     def get_paths(self, resource_path):
+        """Get list of resource paths to search.
+        """
         paths = []
         for module in [self.resource_module, self.DEFAULT_RESOURCE_MODULE]:
             if self.language:
@@ -56,23 +58,41 @@
             paths.append(resource_filename(module, resource_path))
         return paths
 
-    def load_image(self, image_name_fragments, mutators=(), basedir='images'):
+    def get_image(self, image_name_fragments, mutators=(), basedir='images'):
+        """Load an image and optionally apply mutators.
+
+        The `image_name_fragments` parameter may be either a string or a list
+        of strings. The latter is a convenience for things that compose an
+        image name out of several fragments.
+
+        The `mutators` param may contain mutators, which modify an image
+        in-place to apply various effects. TODO: Implement mutators somewhere,
+        so this becomes useful.
+
+        The `basedir` param defaults to 'images', but may be overriden to load
+        images from other places. ('icons', for example.)
+        """
+
+        # Juggle our various params and find an appropriate image path.
         if isinstance(image_name_fragments, basestring):
             image_name_fragments = [image_name_fragments]
         image_path = self.get_resource_path(basedir, *image_name_fragments)
 
+        key = (image_path, mutators)
+        if key in self._mutated_image_cache:
+            # We already have this cached, so shortcut the whole process.
+            return self._mutated_image_cache[key]
+
         if image_path not in self._image_cache:
             image = pygame.image.load(image_path)
             if self.CONVERT_ALPHA:
                 image = image.convert_alpha(pygame.display.get_surface())
             self._image_cache[image_path] = image
-
         image = self._image_cache[image_path]
-        key = (image_path, mutators)
 
-        if key not in self._mutated_image_cache:
-            for mutator in mutators:
-                image = mutator(image)
-            self._mutated_image_cache[key] = image
+        # Apply any mutators we're given.
+        for mutator in mutators:
+            image = mutator(image)
+        self._mutated_image_cache[key] = image
 
-        return self._mutated_image_cache[key]
+        return image
--- a/pyntnclick/state.py	Sat Feb 11 16:56:01 2012 +0200
+++ b/pyntnclick/state.py	Sat Feb 11 17:02:26 2012 +0200
@@ -280,7 +280,7 @@
 
     def _cache_background(self):
         if self.BACKGROUND and not self._background:
-            self._background = self.state.gd.resource.load_image(
+            self._background = self.state.gd.resource.get_image(
                     (self.FOLDER, self.BACKGROUND))
 
     def draw_background(self, surface):
@@ -504,7 +504,7 @@
 
     def _cache_inventory_image(self):
         if not self.inventory_image:
-            self.inventory_image = self.state.resource.load_image(
+            self.inventory_image = self.state.resource.get_image(
                     'items', self.INVENTORY_IMAGE)
 
     def set_state(self, state):
--- a/pyntnclick/tests/test_resources.py	Sat Feb 11 16:56:01 2012 +0200
+++ b/pyntnclick/tests/test_resources.py	Sat Feb 11 17:02:26 2012 +0200
@@ -48,21 +48,21 @@
             data_path('images/pyntnclick/hand.png'),
             self.res.get_resource_path('images/pyntnclick/hand.png'))
 
-    def test_load_image(self):
-        image = self.res.load_image('pyntnclick/hand.png')
+    def test_get_image(self):
+        image = self.res.get_image('pyntnclick/hand.png')
         self.assertTrue(isinstance(image, Surface))
 
-    def test_load_image_fragments(self):
-        image = self.res.load_image(['pyntnclick', 'hand.png'])
+    def test_get_image_fragments(self):
+        image = self.res.get_image(['pyntnclick', 'hand.png'])
         self.assertTrue(isinstance(image, Surface))
 
-    def test_load_image_different_basedir(self):
-        image = self.res.load_image('hand.png', basedir='images/pyntnclick')
+    def test_get_image_different_basedir(self):
+        image = self.res.get_image('hand.png', basedir='images/pyntnclick')
         self.assertTrue(isinstance(image, Surface))
 
     def test_load_missing(self):
         try:
-            self.res.load_image('should_not_exist')
+            self.res.get_image('should_not_exist')
             self.fail('Expected ResourceNotFound error.')
         except ResourceNotFound, e:
             self.assertEqual('images/should_not_exist', e.args[0])