comparison pytouhou/ui/gamerunner.pyx @ 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 1bc014f9d572
children 7c3c90468996
comparison
equal deleted inserted replaced
510:64a72df88de5 511:2e8ceaa85d5c
15 cimport cython 15 cimport cython
16 16
17 from pytouhou.lib cimport sdl 17 from pytouhou.lib cimport sdl
18 18
19 from .window cimport Window, Runner 19 from .window cimport Window, Runner
20 from .gamerenderer cimport GameRenderer
21 from .music import MusicPlayer, SFXPlayer, NullPlayer 20 from .music import MusicPlayer, SFXPlayer, NullPlayer
22 from pytouhou.game.game cimport Game 21 from pytouhou.game.game cimport Game
23 22
24 23
25 cdef class GameRunner(Runner): 24 cdef class GameRunner(Runner):
26 cdef object background, con, resource_loader, keys, replay_level, common 25 cdef object background, con, resource_loader, keys, replay_level, common
27 cdef Game game 26 cdef Game game
28 cdef GameRenderer renderer
29 cdef Window window 27 cdef Window window
30 cdef list save_keystates 28 cdef list save_keystates
31 cdef bint skip 29 cdef bint skip
32 30
33 def __init__(self, Window window, GameRenderer renderer, common, 31 # Since we want to support multiple renderers, don’t specify its type.
34 resource_loader, bint skip=False, con=None): 32 #TODO: find a way to still specify its interface.
33 cdef object renderer
34
35 def __init__(self, Window window, renderer, common, resource_loader,
36 bint skip=False, con=None):
35 self.renderer = renderer 37 self.renderer = renderer
36 self.common = common 38 self.common = common
37 self.resource_loader = resource_loader 39 self.resource_loader = resource_loader
38 40
39 self.window = window 41 self.window = window
43 45
44 self.width = common.interface.width 46 self.width = common.interface.width
45 self.height = common.interface.height 47 self.height = common.interface.height
46 48
47 49
48 def load_game(self, Game game, background=None, bgms=None, replay=None, save_keystates=None): 50 def load_game(self, Game game, background=None, bgms=None, replay=None,
51 save_keystates=None):
49 self.game = game 52 self.game = game
50 self.background = background 53 self.background = background
51 54
52 if self.renderer is not None: 55 if self.renderer is not None:
53 self.renderer.texture_manager.load(self.resource_loader.instanced_anms) 56 self.renderer.load_textures(self.resource_loader.instanced_anms)
54 self.renderer.load_background(background) 57 self.renderer.load_background(background)
55 58
56 self.set_input(replay) 59 self.set_input(replay)
57 if replay and replay.levels[game.stage - 1]: 60 if replay and replay.levels[game.stage - 1]:
58 game.players[0].lives = self.replay_level.lives 61 game.players[0].lives = self.replay_level.lives
61 game.difficulty = self.replay_level.difficulty 64 game.difficulty = self.replay_level.difficulty
62 65
63 self.save_keystates = save_keystates 66 self.save_keystates = save_keystates
64 67
65 null_player = NullPlayer() 68 null_player = NullPlayer()
66 if bgms: 69 if bgms is not None:
67 game.music = MusicPlayer(self.resource_loader, bgms) 70 game.music = MusicPlayer(self.resource_loader, bgms)
68 game.music.play(0) 71 game.music.play(0)
69 else: 72 else:
70 game.music = null_player 73 game.music = null_player
71 74
79 self.replay_level = replay.levels[self.game.stage-1] 82 self.replay_level = replay.levels[self.game.stage-1]
80 self.keys = self.replay_level.iter_keystates() 83 self.keys = self.replay_level.iter_keystates()
81 84
82 85
83 @cython.cdivision(True) 86 @cython.cdivision(True)
84 cdef void set_renderer_size(self, long width, long height) nogil: 87 cdef void set_renderer_size(self, long width, long height) except *:
85 if self.renderer is not None: 88 if self.renderer is not None:
86 runner_width = float(self.width) 89 runner_width = float(self.width)
87 runner_height = float(self.height) 90 runner_height = float(self.height)
88 91
89 scale = min(width / runner_width, 92 scale = min(width / runner_width,
90 height / runner_height) 93 height / runner_height)
91 94
92 self.renderer.width = int(runner_width * scale) 95 new_width = <long>(runner_width * scale)
93 self.renderer.height = int(runner_height * scale) 96 new_height = <long>(runner_height * scale)
94 97
95 self.renderer.x = (width - self.renderer.width) // 2 98 x = (width - new_width) // 2
96 self.renderer.y = (height - self.renderer.height) // 2 99 y = (height - new_height) // 2
100
101 self.renderer.size = x, y, new_width, new_height
97 102
98 103
99 cdef void start(self) except *: 104 cdef void start(self) except *:
100 if self.renderer is not None: 105 if self.renderer is not None:
101 self.set_renderer_size(self.width, self.height) 106 self.set_renderer_size(self.width, self.height)