Mercurial > touhou
comparison pytouhou/ui/sdl/gamerenderer.py @ 544:b895ed2de71f
Implement text rendering for the SDL backend.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 15 May 2014 02:56:08 +0200 |
parents | de778a80820a |
children | 56bca8ce4b68 |
comparison
equal
deleted
inserted
replaced
543:fb837b32c3dd | 544:b895ed2de71f |
---|---|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 ## GNU General Public License for more details. | 12 ## GNU General Public License for more details. |
13 ## | 13 ## |
14 | 14 |
15 from itertools import chain | 15 from itertools import chain |
16 from os.path import join | |
16 | 17 |
17 from pytouhou.lib.sdl import Rect | 18 from pytouhou.lib.sdl import Rect, SDLError |
18 | 19 |
19 from pytouhou.utils.helpers import get_logger | 20 from pytouhou.utils.helpers import get_logger |
20 logger = get_logger(__name__) | 21 logger = get_logger(__name__) |
21 | 22 |
22 | 23 |
23 class GameRenderer(object): | 24 class GameRenderer(object): |
24 def __init__(self, resource_loader, window): | 25 def __init__(self, resource_loader, window): |
25 self.window = window | 26 self.window = window |
26 self.texture_manager = TextureManager(resource_loader, self.window.win) | 27 self.texture_manager = TextureManager(resource_loader, self.window.win) |
28 font_name = join(resource_loader.game_dir, 'font.ttf') | |
29 try: | |
30 self.font_manager = FontManager(font_name, 16, self.window.win) | |
31 except SDLError: | |
32 self.font_manager = None | |
33 logger.error('Font file ā%sā not found, disabling text rendering altogether.', font_name) | |
27 | 34 |
28 | 35 |
29 def load_textures(self, anms): | 36 def load_textures(self, anms): |
30 self.texture_manager.load(anms) | 37 self.texture_manager.load(anms) |
31 | 38 |
129 | 136 |
130 nb_vertices += 4 | 137 nb_vertices += 4 |
131 | 138 |
132 | 139 |
133 def render_text(self, texts): | 140 def render_text(self, texts): |
134 pass | 141 if self.font_manager is None: |
142 return | |
143 | |
144 self.font_manager.load(texts) | |
145 | |
146 for label in texts.itervalues(): | |
147 texture = label.texture | |
148 | |
149 source = Rect(0, 0, label.width, label.height) | |
150 rect = Rect(label.x, label.y, label.width, label.height) | |
151 | |
152 texture.set_alpha_mod(label.alpha) | |
153 | |
154 if label.shadow: | |
155 shadow_rect = Rect(label.x + 1, label.y + 1, label.width, label.height) | |
156 texture.set_color_mod(0, 0, 0) | |
157 self.window.win.render_copy(label.texture, source, shadow_rect) | |
158 texture.set_color_mod(192, 192, 255) | |
159 self.window.win.render_copy(label.texture, source, rect) | |
160 else: | |
161 texture.set_color_mod(192, 192, 255) | |
162 self.window.win.render_copy(label.texture, source, rect) |