diff pytouhou/lib/sdl.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 ec327e58b477
line wrap: on
line diff
--- a/pytouhou/lib/sdl.pyx
+++ b/pytouhou/lib/sdl.pyx
@@ -116,6 +116,27 @@ cdef class Chunk:
         Mix_VolumeChunk(self.chunk, int(volume * 128))
 
 
+cdef class Font:
+    def __init__(self, const char *filename, int ptsize):
+        self.font = TTF_OpenFont(filename, ptsize)
+        if self.font == NULL:
+            raise SDLError(SDL_GetError())
+
+    def __dealloc__(self):
+        if self.font != NULL:
+            TTF_CloseFont(self.font)
+
+    cdef Surface render(self, unicode text):
+        cdef SDL_Color white
+        white = SDL_Color(255, 255, 255, 255)
+        surface = Surface()
+        string = text.encode('utf-8')
+        surface.surface = TTF_RenderUTF8_Blended(self.font, string, white)
+        if surface.surface == NULL:
+            raise SDLError(SDL_GetError())
+        return surface
+
+
 cdef void init(Uint32 flags) except *:
     if SDL_Init(flags) < 0:
         raise SDLError(SDL_GetError())
@@ -131,6 +152,11 @@ cdef void mix_init(int flags) except *:
         raise SDLError(SDL_GetError())
 
 
+cdef void ttf_init() except *:
+    if TTF_Init() < 0:
+        raise SDLError(SDL_GetError())
+
+
 IF UNAME_SYSNAME == "Windows":
     cdef void set_main_ready():
         SDL_SetMainReady()
@@ -148,6 +174,10 @@ cdef void mix_quit() nogil:
     Mix_Quit()
 
 
+cdef void ttf_quit() nogil:
+    TTF_Quit()
+
+
 cdef void gl_set_attribute(SDL_GLattr attr, int value) except *:
     if SDL_GL_SetAttribute(attr, value) < 0:
         raise SDLError(SDL_GetError())