comparison pytouhou/ui/gamerunner.pyx @ 508:1bc014f9d572

Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 19 Nov 2013 14:19:14 +0100
parents 69c73023f7a0
children 2e8ceaa85d5c
comparison
equal deleted inserted replaced
507:507d446dc6cf 508:1bc014f9d572
28 cdef GameRenderer renderer 28 cdef GameRenderer renderer
29 cdef Window window 29 cdef Window window
30 cdef list save_keystates 30 cdef list save_keystates
31 cdef bint skip 31 cdef bint skip
32 32
33 def __init__(self, Window window, common, resource_loader, bint skip=False, 33 def __init__(self, Window window, GameRenderer renderer, common,
34 con=None): 34 resource_loader, bint skip=False, con=None):
35 self.renderer = GameRenderer(resource_loader, window.use_fixed_pipeline) 35 self.renderer = renderer
36 self.common = common 36 self.common = common
37 self.resource_loader = resource_loader 37 self.resource_loader = resource_loader
38 38
39 self.window = window 39 self.window = window
40 self.replay_level = None 40 self.replay_level = None
47 47
48 def load_game(self, Game game, 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):
49 self.game = game 49 self.game = game
50 self.background = background 50 self.background = background
51 51
52 self.renderer.texture_manager.load(self.resource_loader.instanced_anms) 52 if self.renderer is not None:
53 self.renderer.load_background(background) 53 self.renderer.texture_manager.load(self.resource_loader.instanced_anms)
54 self.renderer.load_background(background)
54 55
55 self.set_input(replay) 56 self.set_input(replay)
56 if replay and replay.levels[game.stage - 1]: 57 if replay and replay.levels[game.stage - 1]:
57 game.players[0].lives = self.replay_level.lives 58 game.players[0].lives = self.replay_level.lives
58 game.players[0].power = self.replay_level.power 59 game.players[0].power = self.replay_level.power
79 self.keys = self.replay_level.iter_keystates() 80 self.keys = self.replay_level.iter_keystates()
80 81
81 82
82 @cython.cdivision(True) 83 @cython.cdivision(True)
83 cdef void set_renderer_size(self, long width, long height) nogil: 84 cdef void set_renderer_size(self, long width, long height) nogil:
84 runner_width = float(self.width) 85 if self.renderer is not None:
85 runner_height = float(self.height) 86 runner_width = float(self.width)
87 runner_height = float(self.height)
86 88
87 scale = min(width / runner_width, 89 scale = min(width / runner_width,
88 height / runner_height) 90 height / runner_height)
89 91
90 self.renderer.width = int(runner_width * scale) 92 self.renderer.width = int(runner_width * scale)
91 self.renderer.height = int(runner_height * scale) 93 self.renderer.height = int(runner_height * scale)
92 94
93 self.renderer.x = (width - self.renderer.width) // 2 95 self.renderer.x = (width - self.renderer.width) // 2
94 self.renderer.y = (height - self.renderer.height) // 2 96 self.renderer.y = (height - self.renderer.height) // 2
95 97
96 98
97 cdef void start(self) except *: 99 cdef void start(self) except *:
98 self.set_renderer_size(self.width, self.height) 100 if self.renderer is not None:
99 self.renderer.start(self.common) 101 self.set_renderer_size(self.width, self.height)
102 self.renderer.start(self.common)
100 103
101 104
102 cdef bint update(self) except *: 105 cdef bint update(self) except *:
103 cdef long keystate 106 cdef long keystate
104 107
105 if self.background: 108 if self.background is not None:
106 self.background.update(self.game.frame) 109 self.background.update(self.game.frame)
107 for event in sdl.poll_events(): 110 for event in sdl.poll_events():
108 type_ = event[0] 111 type_ = event[0]
109 if type_ == sdl.KEYDOWN: 112 if type_ == sdl.KEYDOWN:
110 scancode = event[1] 113 scancode = event[1]
114 return False 117 return False
115 elif type_ == sdl.WINDOWEVENT: 118 elif type_ == sdl.WINDOWEVENT:
116 event_ = event[1] 119 event_ = event[1]
117 if event_ == sdl.WINDOWEVENT_RESIZED: 120 if event_ == sdl.WINDOWEVENT_RESIZED:
118 self.set_renderer_size(event[2], event[3]) 121 self.set_renderer_size(event[2], event[3])
119 self.window.set_size(event[2], event[3]) 122 if self.window is not None:
120 if self.game: 123 self.window.set_size(event[2], event[3])
121 if self.replay_level is None: 124 if self.replay_level is None:
122 #TODO: allow user settings 125 #TODO: allow user settings
123 keys = sdl.get_keyboard_state() 126 keys = sdl.get_keyboard_state()
127 keystate = 0
128 if keys[sdl.SCANCODE_Z]:
129 keystate |= 1
130 if keys[sdl.SCANCODE_X]:
131 keystate |= 2
132 if keys[sdl.SCANCODE_LSHIFT]:
133 keystate |= 4
134 if keys[sdl.SCANCODE_UP]:
135 keystate |= 16
136 if keys[sdl.SCANCODE_DOWN]:
137 keystate |= 32
138 if keys[sdl.SCANCODE_LEFT]:
139 keystate |= 64
140 if keys[sdl.SCANCODE_RIGHT]:
141 keystate |= 128
142 if keys[sdl.SCANCODE_LCTRL]:
143 keystate |= 256
144 else:
145 try:
146 keystate = self.keys.next()
147 except StopIteration:
124 keystate = 0 148 keystate = 0
125 if keys[sdl.SCANCODE_Z]: 149 if self.skip:
126 keystate |= 1 150 self.set_input()
127 if keys[sdl.SCANCODE_X]: 151 self.skip = False
128 keystate |= 2 152 self.game.sfx_player = SFXPlayer(self.resource_loader)
129 if keys[sdl.SCANCODE_LSHIFT]:
130 keystate |= 4
131 if keys[sdl.SCANCODE_UP]:
132 keystate |= 16
133 if keys[sdl.SCANCODE_DOWN]:
134 keystate |= 32
135 if keys[sdl.SCANCODE_LEFT]:
136 keystate |= 64
137 if keys[sdl.SCANCODE_RIGHT]:
138 keystate |= 128
139 if keys[sdl.SCANCODE_LCTRL]:
140 keystate |= 256
141 else:
142 try:
143 keystate = self.keys.next()
144 except StopIteration:
145 keystate = 0
146 if self.skip:
147 self.set_input()
148 self.skip = False
149 self.game.sfx_player = SFXPlayer(self.resource_loader)
150 153
151 if self.save_keystates is not None: 154 if self.save_keystates is not None:
152 self.save_keystates.append(keystate) 155 self.save_keystates.append(keystate)
153 156
154 if self.con is not None: 157 if self.con is not None:
155 self.con.run_iter(self.game, keystate) 158 self.con.run_iter(self.game, keystate)
156 else: 159 else:
157 self.game.run_iter([keystate]) 160 self.game.run_iter([keystate])
158 161
162 if self.window is not None:
159 self.game.interface.labels['framerate'].set_text('%.2ffps' % self.window.get_fps()) 163 self.game.interface.labels['framerate'].set_text('%.2ffps' % self.window.get_fps())
160 if not self.skip: 164 if not self.skip and self.renderer is not None:
161 self.renderer.render(self.game) 165 self.renderer.render(self.game)
162 return True 166 return True