comparison pytouhou/ui/texture.pyx @ 456:cae1ae9de430

Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 16 Jul 2013 21:11:40 +0200
parents 6864a38b2413
children 1c891c71cf22
comparison
equal deleted inserted replaced
455:6864a38b2413 456:cae1ae9de430
17 GL_LINEAR, GL_BGRA, GL_RGBA, GL_RGB, GL_LUMINANCE, GL_UNSIGNED_BYTE, 17 GL_LINEAR, GL_BGRA, GL_RGBA, GL_RGB, GL_LUMINANCE, GL_UNSIGNED_BYTE,
18 GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4_REV, 18 GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4_REV,
19 glGenTextures, glBindTexture, glTexImage2D, GL_TEXTURE_2D, GLuint, 19 glGenTextures, glBindTexture, glTexImage2D, GL_TEXTURE_2D, GLuint,
20 glDeleteTextures) 20 glDeleteTextures)
21 21
22 from pytouhou.lib.sdl cimport load_png, create_rgb_surface 22 from pytouhou.lib.sdl cimport load_png, create_rgb_surface, Font
23 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere? 23 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere?
24 24
25 import os 25 import os
26 26
27 27
46 entry.texture = load_texture(texture) 46 entry.texture = load_texture(texture)
47 elif not isinstance(entry.texture, TextureId): 47 elif not isinstance(entry.texture, TextureId):
48 entry.texture = load_texture(entry.texture) 48 entry.texture = load_texture(entry.texture)
49 self.renderer.add_texture(entry.texture) 49 self.renderer.add_texture(entry.texture)
50 entry.texture.renderer = self.renderer 50 entry.texture.renderer = self.renderer
51
52
53 cdef class FontManager:
54 cdef Font font
55 cdef object renderer
56
57 def __init__(self, fontname, fontsize=16, renderer=None):
58 self.font = Font(fontname, fontsize)
59 self.renderer = renderer
60
61
62 def load(self, label_list):
63 for label in label_list:
64 if not hasattr(label, 'texture'):
65 surface = self.font.render(label.text)
66 label.width, label.height = surface.surface.w, surface.surface.h
67
68 if label.align == 'center':
69 label.x -= label.width // 2
70 elif label.align == 'right':
71 label.x -= label.width
72 else:
73 assert label.align == 'left'
74
75 texture = Texture(label.width, label.height, -4, surface.pixels)
76 label.texture = load_texture(texture)
77 label.texture.renderer = self.renderer
51 78
52 79
53 cdef decode_png(loader, first_name, secondary_name): 80 cdef decode_png(loader, first_name, secondary_name):
54 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)))
55 width, height = image_file.surface.w, image_file.surface.h 82 width, height = image_file.surface.w, image_file.surface.h