Mercurial > rinkhals
annotate gamelib/imagecache.py @ 203:653da96db572
Fixed dangling cursor sprite and did some TODO list maintenance.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Fri, 04 Sep 2009 20:25:32 +0000 |
parents | f06010d34cd3 |
children | f8e9a8851d7d |
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 |
186
f06010d34cd3
Add sprite cursors for building placement.
Simon Cross <hodgestar@gmail.com>
parents:
181
diff
changeset
|
59 from pygame.locals import BLEND_MULT, BLEND_ADD, BLEND_RGBA_MULT |
41
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) |
159
7bb8d9d6858a
Darken center of game over splash screen to make text legible.
Simon Cross <hodgestar@gmail.com>
parents:
53
diff
changeset
|
61 DARKEN_COLOUR = (100.0, 100.0, 100.0, 255.0) |
181
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
62 LIGHTEN_COLOUR = (200.0, 200.0, 200.0, 225.0) |
41
6055a8a8678d
Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
63 |
6055a8a8678d
Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
64 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
|
65 """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
|
66 night_image = image.copy() |
181
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
67 night_image.fill(NIGHT_COLOUR, None, BLEND_MULT) |
41
6055a8a8678d
Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
68 return night_image |
6055a8a8678d
Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
69 |
53
f20dd3dcb118
foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
41
diff
changeset
|
70 def convert_to_right_facing(image): |
f20dd3dcb118
foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
41
diff
changeset
|
71 right_facing_image = image.copy() |
f20dd3dcb118
foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
41
diff
changeset
|
72 right_facing_image = pygame.transform.flip(right_facing_image, 1, 0) |
f20dd3dcb118
foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
41
diff
changeset
|
73 return right_facing_image |
f20dd3dcb118
foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
41
diff
changeset
|
74 |
159
7bb8d9d6858a
Darken center of game over splash screen to make text legible.
Simon Cross <hodgestar@gmail.com>
parents:
53
diff
changeset
|
75 def darken_center(image): |
7bb8d9d6858a
Darken center of game over splash screen to make text legible.
Simon Cross <hodgestar@gmail.com>
parents:
53
diff
changeset
|
76 darkened = image.copy() |
7bb8d9d6858a
Darken center of game over splash screen to make text legible.
Simon Cross <hodgestar@gmail.com>
parents:
53
diff
changeset
|
77 w, h = darkened.get_size() |
181
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
78 over_w, over_h = int(w*0.5), int(h*0.5) |
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
79 over_x, over_y = int(w*0.25), int(h*0.25) |
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
80 overlay = pygame.Surface((over_w, over_h)) |
159
7bb8d9d6858a
Darken center of game over splash screen to make text legible.
Simon Cross <hodgestar@gmail.com>
parents:
53
diff
changeset
|
81 overlay.fill(DARKEN_COLOUR) |
181
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
82 darkened.blit(overlay, (over_x, over_y), None, BLEND_MULT) |
159
7bb8d9d6858a
Darken center of game over splash screen to make text legible.
Simon Cross <hodgestar@gmail.com>
parents:
53
diff
changeset
|
83 return darkened |
7bb8d9d6858a
Darken center of game over splash screen to make text legible.
Simon Cross <hodgestar@gmail.com>
parents:
53
diff
changeset
|
84 |
181
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
85 def lighten_most(image): |
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
86 lighten = image.copy() |
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
87 w, h = lighten.get_size() |
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
88 over_w, over_h = int(w*0.9), int(h*0.9) |
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
89 over_x, over_y = int(w*0.05), int(h*0.05) |
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
90 overlay = pygame.Surface((over_w, over_h)) |
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
91 overlay.fill(LIGHTEN_COLOUR) |
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
92 lighten.blit(overlay, (over_x, over_y), None, BLEND_ADD) |
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
93 return lighten |
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
94 |
186
f06010d34cd3
Add sprite cursors for building placement.
Simon Cross <hodgestar@gmail.com>
parents:
181
diff
changeset
|
95 def sprite_cursor(image): |
f06010d34cd3
Add sprite cursors for building placement.
Simon Cross <hodgestar@gmail.com>
parents:
181
diff
changeset
|
96 cursor = image.copy() |
f06010d34cd3
Add sprite cursors for building placement.
Simon Cross <hodgestar@gmail.com>
parents:
181
diff
changeset
|
97 cursor.fill((255, 255, 255, 100), None, BLEND_RGBA_MULT) |
f06010d34cd3
Add sprite cursors for building placement.
Simon Cross <hodgestar@gmail.com>
parents:
181
diff
changeset
|
98 return cursor |
f06010d34cd3
Add sprite cursors for building placement.
Simon Cross <hodgestar@gmail.com>
parents:
181
diff
changeset
|
99 |
41
6055a8a8678d
Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
100 # globals |
6055a8a8678d
Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
101 |
6055a8a8678d
Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
102 cache = ImageCache() |
6055a8a8678d
Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
103 cache.register_modifier("night", convert_to_night) |
53
f20dd3dcb118
foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
41
diff
changeset
|
104 cache.register_modifier("right_facing", convert_to_right_facing) |
159
7bb8d9d6858a
Darken center of game over splash screen to make text legible.
Simon Cross <hodgestar@gmail.com>
parents:
53
diff
changeset
|
105 cache.register_modifier("darken_center", darken_center) |
181
7e556ef40100
Lighten help screen. Fix some oddities in darken modifier.
Simon Cross <hodgestar@gmail.com>
parents:
159
diff
changeset
|
106 cache.register_modifier("lighten_most", lighten_most) |
186
f06010d34cd3
Add sprite cursors for building placement.
Simon Cross <hodgestar@gmail.com>
parents:
181
diff
changeset
|
107 cache.register_modifier("sprite_cursor", sprite_cursor) |
41
6055a8a8678d
Add image cache (with support for modified versions of an image).
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
108 load_image = cache.load_image |