comparison pytouhou/ui/gamerunner.pyx @ 503:c622eaf64428

Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 04 Oct 2013 14:27:11 +0200
parents 6be9c99a3a24
children 69c73023f7a0
comparison
equal deleted inserted replaced
502:3d3285918ba1 503:c622eaf64428
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 cimport cython
16
15 from pytouhou.lib cimport sdl 17 from pytouhou.lib cimport sdl
16 18
17 from .window cimport Window, Runner 19 from .window cimport Window, Runner
18 from .gamerenderer cimport GameRenderer 20 from .gamerenderer cimport GameRenderer
19 from .music import MusicPlayer, SFXPlayer, NullPlayer 21 from .music import MusicPlayer, SFXPlayer, NullPlayer
22 from pytouhou.game.game cimport Game
20 23
21 24
22 cdef class GameRunner(Runner): 25 cdef class GameRunner(Runner):
23 cdef object game, background, con 26 cdef object background, con, resource_loader, keys, replay_level, common
27 cdef Game game
24 cdef GameRenderer renderer 28 cdef GameRenderer renderer
25 cdef Window window 29 cdef Window window
26 cdef object replay_level, save_keystates 30 cdef list save_keystates
27 cdef bint skip 31 cdef bint skip
28 32
29 def __init__(self, Window window, resource_loader, bint skip=False, 33 def __init__(self, Window window, common, resource_loader, bint skip=False,
30 con=None): 34 con=None):
31 self.renderer = GameRenderer(resource_loader, window.use_fixed_pipeline) 35 self.renderer = GameRenderer(resource_loader, window.use_fixed_pipeline)
36 self.common = common
37 self.resource_loader = resource_loader
32 38
33 self.window = window 39 self.window = window
34 self.replay_level = None 40 self.replay_level = None
35 self.skip = skip 41 self.skip = skip
36 self.con = con 42 self.con = con
37 43
38 self.width = window.width #XXX 44 self.width = common.interface.width
39 self.height = window.height #XXX 45 self.height = common.interface.height
40 46
41 47
42 def load_game(self, game=None, background=None, bgms=None, replay=None, save_keystates=None): 48 def load_game(self, Game game, background=None, bgms=None, replay=None, save_keystates=None):
43 self.game = game 49 self.game = game
44 self.background = background 50 self.background = background
45 51
46 self.renderer.texture_manager.load(game.resource_loader.instanced_anms.values()) 52 self.renderer.texture_manager.load(self.resource_loader.instanced_anms.values())
47 self.renderer.load_background(background) 53 self.renderer.load_background(background)
48 54
49 self.set_input(replay) 55 self.set_input(replay)
50 if replay and replay.levels[game.stage - 1]: 56 if replay and replay.levels[game.stage - 1]:
51 game.players[0].lives = self.replay_level.lives 57 game.players[0].lives = self.replay_level.lives
55 61
56 self.save_keystates = save_keystates 62 self.save_keystates = save_keystates
57 63
58 null_player = NullPlayer() 64 null_player = NullPlayer()
59 if bgms: 65 if bgms:
60 game.music = MusicPlayer(game.resource_loader, bgms) 66 game.music = MusicPlayer(self.resource_loader, bgms)
61 game.music.play(0) 67 game.music.play(0)
62 else: 68 else:
63 game.music = null_player 69 game.music = null_player
64 70
65 game.sfx_player = SFXPlayer(game.resource_loader) if not self.skip else null_player 71 game.sfx_player = SFXPlayer(self.resource_loader) if not self.skip else null_player
66 72
67 73
68 def set_input(self, replay=None): 74 cdef void set_input(self, replay=None) except *:
69 if not replay or not replay.levels[self.game.stage-1]: 75 if not replay or not replay.levels[self.game.stage-1]:
70 self.replay_level = None 76 self.replay_level = None
71 else: 77 else:
72 self.replay_level = replay.levels[self.game.stage-1] 78 self.replay_level = replay.levels[self.game.stage-1]
73 self.keys = self.replay_level.iter_keystates() 79 self.keys = self.replay_level.iter_keystates()
74 80
75 81
82 @cython.cdivision(True)
83 cdef void set_renderer_size(self, long width, long height) nogil:
84 runner_width = float(self.width)
85 runner_height = float(self.height)
86
87 scale = min(width / runner_width,
88 height / runner_height)
89
90 self.renderer.width = int(runner_width * scale)
91 self.renderer.height = int(runner_height * scale)
92
93 self.renderer.x = (width - self.renderer.width) // 2
94 self.renderer.y = (height - self.renderer.height) // 2
95
96
76 cdef void start(self) except *: 97 cdef void start(self) except *:
77 cdef long width, height 98 self.set_renderer_size(self.width, self.height)
78 width = self.game.interface.width if self.game is not None else 640 99 self.renderer.start(self.common)
79 height = self.game.interface.height if self.game is not None else 480
80 if width != self.width or height != self.height:
81 self.window.set_size(width, height)
82
83 self.renderer.start(self.game)
84 100
85 101
86 cdef bint update(self) except *: 102 cdef bint update(self) except *:
87 cdef long keystate 103 cdef long keystate
88 104
97 elif type_ == sdl.QUIT: 113 elif type_ == sdl.QUIT:
98 return False 114 return False
99 elif type_ == sdl.WINDOWEVENT: 115 elif type_ == sdl.WINDOWEVENT:
100 event_ = event[1] 116 event_ = event[1]
101 if event_ == sdl.WINDOWEVENT_RESIZED: 117 if event_ == sdl.WINDOWEVENT_RESIZED:
118 self.set_renderer_size(event[2], event[3])
102 self.window.set_size(event[2], event[3]) 119 self.window.set_size(event[2], event[3])
103 if self.game: 120 if self.game:
104 if self.replay_level is None: 121 if self.replay_level is None:
105 #TODO: allow user settings 122 #TODO: allow user settings
106 keys = sdl.get_keyboard_state() 123 keys = sdl.get_keyboard_state()
127 except StopIteration: 144 except StopIteration:
128 keystate = 0 145 keystate = 0
129 if self.skip: 146 if self.skip:
130 self.set_input() 147 self.set_input()
131 self.skip = False 148 self.skip = False
132 self.game.sfx_player = SFXPlayer(self.game.resource_loader) 149 self.game.sfx_player = SFXPlayer(self.resource_loader)
133 150
134 if self.save_keystates is not None: 151 if self.save_keystates is not None:
135 self.save_keystates.append(keystate) 152 self.save_keystates.append(keystate)
136 153
137 if self.con is not None: 154 if self.con is not None:
139 else: 156 else:
140 self.game.run_iter([keystate]) 157 self.game.run_iter([keystate])
141 158
142 self.game.interface.labels['framerate'].set_text('%.2ffps' % self.window.get_fps()) 159 self.game.interface.labels['framerate'].set_text('%.2ffps' % self.window.get_fps())
143 if not self.skip: 160 if not self.skip:
144 self.renderer.render(self.game, self.window) 161 self.renderer.render(self.game)
145 return True 162 return True