changeset 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 577c3a88fb67
children 75ae628522c9
files pytouhou/ui/opengl/gamerenderer.pyx pytouhou/ui/opengl/texture.pxd pytouhou/ui/opengl/texture.pyx
diffstat 3 files changed, 19 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/ui/opengl/gamerenderer.pyx
+++ b/pytouhou/ui/opengl/gamerenderer.pyx
@@ -195,24 +195,21 @@ cdef class GameRenderer(Renderer):
         if self.font_manager is None:
             return
 
-        labels = [label for label in texts.itervalues() if label is not None]
-        self.font_manager.load(labels)
+        self.font_manager.load(texts)
 
         black = Color(0, 0, 0, 255)
 
-        for label in labels:
-            if label is None:
-                continue
-
+        for label in texts.itervalues():
+            texture = (<Texture>label.texture).texture
             rect = Rect(label.x, label.y, label.width, label.height)
             gradient = [Color(*color, a=label.alpha) for color in label.gradient]
 
             if label.shadow:
                 shadow_rect = Rect(label.x + 1, label.y + 1, label.width, label.height)
                 shadow = [black._replace(a=label.alpha)] * 4
-                self.render_quads([shadow_rect, rect], [shadow, gradient], (<Texture>label.texture).texture)
+                self.render_quads([shadow_rect, rect], [shadow, gradient], texture)
             else:
-                self.render_quads([rect], [gradient], (<Texture>label.texture).texture)
+                self.render_quads([rect], [gradient], texture)
 
 
     cdef void render_interface(self, interface, game_boss):
--- a/pytouhou/ui/opengl/texture.pxd
+++ b/pytouhou/ui/opengl/texture.pxd
@@ -9,4 +9,4 @@ cdef class FontManager:
     cdef Font font
     cdef object renderer, texture_class
 
-    cdef void load(self, list labels) except *
+    cdef void load(self, dict labels) except *
--- 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':