comparison 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
comparison
equal deleted inserted replaced
516:577c3a88fb67 517:dec43940f092
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 20
21 from pytouhou.lib.sdl cimport load_png, create_rgb_surface 21 from pytouhou.lib.sdl cimport load_png, create_rgb_surface
22 from pytouhou.lib.sdl import SDLError
22 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere? 23 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere?
23 from pytouhou.game.text cimport NativeText 24 from pytouhou.game.text cimport NativeText
24 25
25 import os 26 import os
27
28 from pytouhou.utils.helpers import get_logger
29 logger = get_logger(__name__)
26 30
27 31
28 cdef class TextureManager: 32 cdef class TextureManager:
29 def __init__(self, loader=None, renderer=None, texture_class=None): 33 def __init__(self, loader=None, renderer=None, texture_class=None):
30 self.loader = loader 34 self.loader = loader
52 self.font = Font(fontname, fontsize) 56 self.font = Font(fontname, fontsize)
53 self.renderer = renderer 57 self.renderer = renderer
54 self.texture_class = texture_class 58 self.texture_class = texture_class
55 59
56 60
57 cdef void load(self, list labels): 61 cdef void load(self, dict labels):
58 cdef NativeText label 62 cdef NativeText label
59 63
60 for label in labels: 64 for i, label in labels.items():
61 if label.texture is None: 65 if label.texture is None:
62 surface = self.font.render(label.text) 66 try:
67 surface = self.font.render(label.text)
68 except SDLError as e:
69 logger.error(u'Rendering of label “%s” failed: %s', label.text, e)
70 del labels[i] # Prevents it from retrying to render.
71 continue
72
63 label.width, label.height = surface.surface.w, surface.surface.h 73 label.width, label.height = surface.surface.w, surface.surface.h
64 74
65 if label.align == 'center': 75 if label.align == 'center':
66 label.x -= label.width // 2 76 label.x -= label.width // 2
67 elif label.align == 'right': 77 elif label.align == 'right':