Mercurial > pyntnclick
changeset 571:20e296d4a3a5 pyntnclick
Add load_image to Resources.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 11 Feb 2012 15:48:04 +0200 |
parents | 9c3528c2cbe5 |
children | e393954e3749 |
files | pyntnclick/resources.py pyntnclick/tests/test_resources.py |
diffstat | 2 files changed, 23 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/pyntnclick/resources.py Sat Feb 11 15:48:06 2012 +0200 +++ b/pyntnclick/resources.py Sat Feb 11 15:48:04 2012 +0200 @@ -3,6 +3,8 @@ import os from pkg_resources import resource_filename +import pygame + class ResourceNotFound(Exception): pass @@ -10,12 +12,15 @@ class Resources(object): DEFAULT_RESOURCE_MODULE = "pyntnclick.data" + CONVERT_ALPHA = True def __init__(self, resource_module, language=None): self.resource_module = resource_module self.language = language + self._image_cache = {} - def get_resource_path(self, resource_name): + def get_resource_path(self, *resource_path_fragments): + resource_name = os.path.join(*resource_path_fragments) for path in self.get_paths(resource_name): if os.path.exists(path): return path @@ -29,3 +34,12 @@ paths.append(resource_filename(module, fn)) paths.append(resource_filename(module, resource_path)) return paths + + def load_image(self, image_name): + image_path = self.get_resource_path('images', image_name) + 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 + return self._image_cache[image_path]
--- a/pyntnclick/tests/test_resources.py Sat Feb 11 15:48:06 2012 +0200 +++ b/pyntnclick/tests/test_resources.py Sat Feb 11 15:48:04 2012 +0200 @@ -1,6 +1,8 @@ import os.path from unittest import TestCase +from pygame.surface import Surface + from pyntnclick.resources import Resources, ResourceNotFound @@ -40,3 +42,9 @@ res = Resources('pyntnclick.tests') self.assertEqual(data_path('images/pyntnclick/hand.png'), res.get_resource_path('images/pyntnclick/hand.png')) + + def test_load_image(self): + res = Resources('pyntnclick.tests') + res.CONVERT_ALPHA = False + image = res.load_image('pyntnclick/hand.png') + self.assertTrue(isinstance(image, Surface))