changeset 511:2e8ceaa85d5c

Don’t type the renderer in GameRunner, so that we can switch it without type issues.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 28 Nov 2013 18:55:43 +0100
parents 64a72df88de5
children b39ad30c6620
files pytouhou/ui/gamerenderer.pxd pytouhou/ui/gamerenderer.pyx pytouhou/ui/gamerunner.pyx pytouhou/ui/texture.pxd pytouhou/ui/texture.pyx
diffstat 5 files changed, 34 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/ui/gamerenderer.pxd
+++ b/pytouhou/ui/gamerenderer.pxd
@@ -10,9 +10,6 @@ cdef class GameRenderer(Renderer):
     cdef Framebuffer framebuffer
     cdef BackgroundRenderer background_renderer
 
-    cdef void load_background(self, background) except *
-    cdef void start(self, common) except *
-    cdef void render(self, Game game) except *
     cdef void render_game(self, Game game) except *
     cdef void render_text(self, texts) except *
     cdef void render_interface(self, interface, game_boss) except *
--- a/pytouhou/ui/gamerenderer.pyx
+++ b/pytouhou/ui/gamerenderer.pyx
@@ -46,7 +46,17 @@ cdef class GameRenderer(Renderer):
             self.framebuffer = Framebuffer(0, 0, 640, 480)
 
 
-    cdef void load_background(self, background):
+    property size:
+        # We never need to get back the computed size, so size is write-only.
+        def __set__(self, tuple size):
+            self.x, self.y, self.width, self.height = size
+
+
+    def load_textures(self, dict anms):
+        self.texture_manager.load(anms)
+
+
+    def load_background(self, background):
         if background is not None:
             self.background_renderer = BackgroundRenderer(self.use_fixed_pipeline)
             self.background_renderer.load(background)
@@ -54,7 +64,7 @@ cdef class GameRenderer(Renderer):
             self.background_renderer = None
 
 
-    cdef void start(self, common):
+    def start(self, common):
         self.proj = perspective(30, float(common.width) / float(common.height),
                                 101010101./2010101., 101010101./10101.)
         game_view = setup_camera(0, 0, 1)
@@ -63,7 +73,7 @@ cdef class GameRenderer(Renderer):
                                       float(common.interface.height), 0.)
 
 
-    cdef void render(self, Game game):
+    def render(self, Game game):
         if not self.use_fixed_pipeline:
             self.framebuffer.bind()
 
--- a/pytouhou/ui/gamerunner.pyx
+++ b/pytouhou/ui/gamerunner.pyx
@@ -17,7 +17,6 @@ cimport cython
 from pytouhou.lib cimport sdl
 
 from .window cimport Window, Runner
-from .gamerenderer cimport GameRenderer
 from .music import MusicPlayer, SFXPlayer, NullPlayer
 from pytouhou.game.game cimport Game
 
@@ -25,13 +24,16 @@ from pytouhou.game.game cimport Game
 cdef class GameRunner(Runner):
     cdef object background, con, resource_loader, keys, replay_level, common
     cdef Game game
-    cdef GameRenderer renderer
     cdef Window window
     cdef list save_keystates
     cdef bint skip
 
-    def __init__(self, Window window, GameRenderer renderer, common,
-                 resource_loader, bint skip=False, con=None):
+    # Since we want to support multiple renderers, don’t specify its type.
+    #TODO: find a way to still specify its interface.
+    cdef object renderer
+
+    def __init__(self, Window window, renderer, common, resource_loader,
+                 bint skip=False, con=None):
         self.renderer = renderer
         self.common = common
         self.resource_loader = resource_loader
@@ -45,12 +47,13 @@ cdef class GameRunner(Runner):
         self.height = common.interface.height
 
 
-    def load_game(self, Game game, background=None, bgms=None, replay=None, save_keystates=None):
+    def load_game(self, Game game, background=None, bgms=None, replay=None,
+                  save_keystates=None):
         self.game = game
         self.background = background
 
         if self.renderer is not None:
-            self.renderer.texture_manager.load(self.resource_loader.instanced_anms)
+            self.renderer.load_textures(self.resource_loader.instanced_anms)
             self.renderer.load_background(background)
 
         self.set_input(replay)
@@ -63,7 +66,7 @@ cdef class GameRunner(Runner):
         self.save_keystates = save_keystates
 
         null_player = NullPlayer()
-        if bgms:
+        if bgms is not None:
             game.music = MusicPlayer(self.resource_loader, bgms)
             game.music.play(0)
         else:
@@ -81,7 +84,7 @@ cdef class GameRunner(Runner):
 
 
     @cython.cdivision(True)
-    cdef void set_renderer_size(self, long width, long height) nogil:
+    cdef void set_renderer_size(self, long width, long height) except *:
         if self.renderer is not None:
             runner_width = float(self.width)
             runner_height = float(self.height)
@@ -89,11 +92,13 @@ cdef class GameRunner(Runner):
             scale = min(width / runner_width,
                         height / runner_height)
 
-            self.renderer.width = int(runner_width * scale)
-            self.renderer.height = int(runner_height * scale)
+            new_width = <long>(runner_width * scale)
+            new_height = <long>(runner_height * scale)
 
-            self.renderer.x = (width - self.renderer.width) // 2
-            self.renderer.y = (height - self.renderer.height) // 2
+            x = (width - new_width) // 2
+            y = (height - new_height) // 2
+
+            self.renderer.size = x, y, new_width, new_height
 
 
     cdef void start(self) except *:
--- a/pytouhou/ui/texture.pxd
+++ b/pytouhou/ui/texture.pxd
@@ -3,10 +3,10 @@ from pytouhou.lib.sdl cimport Font
 cdef class TextureManager:
     cdef object loader, renderer, texture_class
 
-    cdef load(self, dict anms)
+    cdef void load(self, dict anms) except *
 
 cdef class FontManager:
     cdef Font font
     cdef object renderer, texture_class
 
-    cdef load(self, list labels)
+    cdef void load(self, list labels) except *
--- a/pytouhou/ui/texture.pyx
+++ b/pytouhou/ui/texture.pyx
@@ -32,7 +32,7 @@ cdef class TextureManager:
         self.texture_class = texture_class
 
 
-    cdef load(self, dict anms):
+    cdef void load(self, dict anms):
         for anm in sorted(anms.values(), key=is_ascii):
             for entry in anm:
                 if not hasattr(entry, 'texture'):
@@ -54,7 +54,7 @@ cdef class FontManager:
         self.texture_class = texture_class
 
 
-    cdef load(self, list labels):
+    cdef void load(self, list labels):
         cdef NativeText label
 
         for label in labels: