comparison pyntnclick/resources.py @ 575:970cdc219e15 pyntnclick

Add image mutation to resource loader.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 11 Feb 2012 16:01:33 +0200
parents 20e296d4a3a5
children 27809609eeca
comparison
equal deleted inserted replaced
574:4e14721e74a8 575:970cdc219e15
16 16
17 def __init__(self, resource_module, language=None): 17 def __init__(self, resource_module, language=None):
18 self.resource_module = resource_module 18 self.resource_module = resource_module
19 self.language = language 19 self.language = language
20 self._image_cache = {} 20 self._image_cache = {}
21 self._mutated_image_cache = {}
21 22
22 def get_resource_path(self, *resource_path_fragments): 23 def get_resource_path(self, *resource_path_fragments):
23 resource_name = os.path.join(*resource_path_fragments) 24 resource_name = os.path.join(*resource_path_fragments)
24 for path in self.get_paths(resource_name): 25 for path in self.get_paths(resource_name):
25 if os.path.exists(path): 26 if os.path.exists(path):
33 fn = os.path.join(self.language, resource_path) 34 fn = os.path.join(self.language, resource_path)
34 paths.append(resource_filename(module, fn)) 35 paths.append(resource_filename(module, fn))
35 paths.append(resource_filename(module, resource_path)) 36 paths.append(resource_filename(module, resource_path))
36 return paths 37 return paths
37 38
38 def load_image(self, image_name): 39 def load_image(self, image_name, mutators=()):
39 image_path = self.get_resource_path('images', image_name) 40 image_path = self.get_resource_path('images', image_name)
41
40 if image_path not in self._image_cache: 42 if image_path not in self._image_cache:
41 image = pygame.image.load(image_path) 43 image = pygame.image.load(image_path)
42 if self.CONVERT_ALPHA: 44 if self.CONVERT_ALPHA:
43 image = image.convert_alpha(pygame.display.get_surface()) 45 image = image.convert_alpha(pygame.display.get_surface())
44 self._image_cache[image_path] = image 46 self._image_cache[image_path] = image
45 return self._image_cache[image_path] 47
48 image = self._image_cache[image_path]
49 key = (image_path, mutators)
50
51 if key not in self._mutated_image_cache:
52 for mutator in mutators:
53 image = mutator(image)
54 self._mutated_image_cache[key] = image
55
56 return self._mutated_image_cache[key]