comparison pytouhou/ui/sdl/texture.pyx @ 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 43ecf0f98f4d
children 3c2f96f1d715
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 pytouhou.lib.sdl cimport load_png, create_rgb_surface 15 from pytouhou.lib.sdl cimport load_png, create_rgb_surface
16 from pytouhou.lib.sdl import SDLError
17 from pytouhou.game.text cimport NativeText
16 18
17 import os 19 import os
20
21 from pytouhou.utils.helpers import get_logger
22 logger = get_logger(__name__)
18 23
19 24
20 cdef class TextureManager: 25 cdef class TextureManager:
21 def __init__(self, loader, window): 26 def __init__(self, loader, window):
22 self.loader = loader 27 self.loader = loader
40 45
41 def is_ascii(anm): 46 def is_ascii(anm):
42 return anm[0].first_name.endswith('ascii.png') 47 return anm[0].first_name.endswith('ascii.png')
43 48
44 49
50 cdef class FontManager:
51 def __init__(self, fontname, fontsize=16, window=None):
52 self.font = Font(fontname, fontsize)
53 self.window = window
54
55
56 cdef void load(self, dict labels):
57 cdef NativeText label
58
59 for i, label in labels.items():
60 if label.texture is None:
61 try:
62 surface = self.font.render(label.text)
63 except SDLError as e:
64 logger.error(u'Rendering of label “%s” failed: %s', label.text, e)
65 del labels[i] # Prevents it from retrying to render.
66 continue
67
68 label.width, label.height = surface.surface.w, surface.surface.h
69
70 if label.align == 'center':
71 label.x -= label.width // 2
72 elif label.align == 'right':
73 label.x -= label.width
74 else:
75 assert label.align == 'left'
76
77 label.texture = self.window.create_texture_from_surface(surface)
78
79
45 cdef Surface decode_png(loader, first_name, secondary_name): 80 cdef Surface decode_png(loader, first_name, secondary_name):
46 image_file = load_png(loader.get_file(os.path.basename(first_name))) 81 image_file = load_png(loader.get_file(os.path.basename(first_name)))
47 width, height = image_file.surface.w, image_file.surface.h 82 width, height = image_file.surface.w, image_file.surface.h
48 83
49 # Support only 32 bits RGBA. Paletted surfaces are awful to work with. 84 # Support only 32 bits RGBA. Paletted surfaces are awful to work with.