diff pytouhou/ui/opengl/texture.pyx @ 517:dec43940f092

Don’t crash if SDL2_ttf couldn’t render a specific string.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 07 Dec 2013 21:37:55 +0100
parents b3193b43a86c
children 43ecf0f98f4d
line wrap: on
line diff
--- a/pytouhou/ui/opengl/texture.pyx
+++ b/pytouhou/ui/opengl/texture.pyx
@@ -19,11 +19,15 @@ from pytouhou.lib.opengl cimport \
           glGenTextures, glBindTexture, glTexImage2D, GL_TEXTURE_2D, GLuint)
 
 from pytouhou.lib.sdl cimport load_png, create_rgb_surface
+from pytouhou.lib.sdl import SDLError
 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere?
 from pytouhou.game.text cimport NativeText
 
 import os
 
+from pytouhou.utils.helpers import get_logger
+logger = get_logger(__name__)
+
 
 cdef class TextureManager:
     def __init__(self, loader=None, renderer=None, texture_class=None):
@@ -54,12 +58,18 @@ cdef class FontManager:
         self.texture_class = texture_class
 
 
-    cdef void load(self, list labels):
+    cdef void load(self, dict labels):
         cdef NativeText label
 
-        for label in labels:
+        for i, label in labels.items():
             if label.texture is None:
-                surface = self.font.render(label.text)
+                try:
+                    surface = self.font.render(label.text)
+                except SDLError as e:
+                    logger.error(u'Rendering of label “%s” failed: %s', label.text, e)
+                    del labels[i]  # Prevents it from retrying to render.
+                    continue
+
                 label.width, label.height = surface.surface.w, surface.surface.h
 
                 if label.align == 'center':