diff 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
line wrap: on
line diff
--- a/pytouhou/ui/texture.pyx
+++ b/pytouhou/ui/texture.pyx
@@ -19,7 +19,7 @@ from pytouhou.lib.opengl cimport \
           glGenTextures, glBindTexture, glTexImage2D, GL_TEXTURE_2D, GLuint,
           glDeleteTextures)
 
-from pytouhou.lib.sdl cimport load_png, create_rgb_surface
+from pytouhou.lib.sdl cimport load_png, create_rgb_surface, Font
 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere?
 
 import os
@@ -50,6 +50,33 @@ class TextureManager(object):
                 entry.texture.renderer = self.renderer
 
 
+cdef class FontManager:
+    cdef Font font
+    cdef object renderer
+
+    def __init__(self, fontname, fontsize=16, renderer=None):
+        self.font = Font(fontname, fontsize)
+        self.renderer = renderer
+
+
+    def load(self, label_list):
+        for label in label_list:
+            if not hasattr(label, 'texture'):
+                surface = self.font.render(label.text)
+                label.width, label.height = surface.surface.w, surface.surface.h
+
+                if label.align == 'center':
+                    label.x -= label.width // 2
+                elif label.align == 'right':
+                    label.x -= label.width
+                else:
+                    assert label.align == 'left'
+
+                texture = Texture(label.width, label.height, -4, surface.pixels)
+                label.texture = load_texture(texture)
+                label.texture.renderer = self.renderer
+
+
 cdef decode_png(loader, first_name, secondary_name):
     image_file = load_png(loader.get_file(os.path.basename(first_name)))
     width, height = image_file.surface.w, image_file.surface.h