annotate gamelib/imagecache.py @ 41:6055a8a8678d

Add image cache (with support for modified versions of an image).
author Simon Cross <hodgestar@gmail.com>
date Mon, 31 Aug 2009 16:47:54 +0000
parents
children f20dd3dcb118
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
1 """Central image cache to avoid loading images multiple times."""
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
2
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
3 import pygame
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
4 import data
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
5
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
6 class ImageCache(object):
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
7 """Create an image cache with a list of modifiers."""
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
8
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
9 def __init__(self):
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
10 # (image name, ordered modifiers tuple) -> pygame surface
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
11 self._cache = {}
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
12 self._modifiers = {}
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
13
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
14 def register_modifier(self, name, function):
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
15 """Register a post-load modifying function."""
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
16 if name in self._modifiers:
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
17 raise ValueError("Attempt to re-register and existing modifier function.")
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
18 self._modifiers[name] = function
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
19
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
20 def load_image(self, name, modifiers=None):
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
21 """Load an image from disk or return a cached image.
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
22
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
23 name: image name relative to data path.
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
24 modifers: ordered list of modifiers to apply.
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
25 """
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
26 # convert lists to tuples
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
27 if modifiers is not None:
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
28 modifiers = tuple(modifiers)
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
29
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
30 # convert empty tuples to None
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
31 if not modifiers:
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
32 modifiers = None
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
33
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
34 # look for modified image
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
35 key = (name, modifiers)
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
36 if key in self._cache:
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
37 return self._cache[key]
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
38
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
39 # look for unmodified image
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
40 base_key = (name, None)
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
41 if base_key in self._cache:
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
42 image = self._cache[base_key]
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
43 else:
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
44 image = pygame.image.load(data.filepath(name))
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
45 self._cache[base_key] = image
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
46
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
47 # handle unmodified case
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
48 if modifiers is None:
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
49 return image
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
50
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
51 for mod in modifiers:
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
52 image = self._modifiers[mod](image)
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
53
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
54 self._cache[key] = image
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
55 return image
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
56
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
57 # modifiers
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
58
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
59 from pygame.locals import BLEND_RGBA_MULT
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
60 NIGHT_COLOUR = (100.0, 100.0, 200.0, 255.0)
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
61
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
62 def convert_to_night(image):
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
63 """Convert a day tile to a night tile."""
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
64 night_image = image.copy()
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
65 night_image.fill(NIGHT_COLOUR, None, BLEND_RGBA_MULT)
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
66 return night_image
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
67
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
68 # globals
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
69
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
70 cache = ImageCache()
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
71 cache.register_modifier("night", convert_to_night)
6055a8a8678d Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
72 load_image = cache.load_image