comparison eosd @ 469:58b47e788c59

Move SDL context-manager and Window creation outside of main.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 14 Sep 2013 01:05:42 +0200
parents 5f5955635d2c
children 2f53be1b2f60
comparison
equal deleted inserted replaced
468:feecdb4a8928 469:58b47e788c59
94 or effect._laser in self.lasers] 94 or effect._laser in self.lasers]
95 self.bullets = [bullet for bullet in self.bullets if bullet.frame > 1] 95 self.bullets = [bullet for bullet in self.bullets if bullet.frame > 1]
96 Game.cleanup(self) 96 Game.cleanup(self)
97 97
98 98
99 def main(path, data, stage_num, rank, character, replay, save_filename, 99 def main(window, path, data, stage_num, rank, character, replay, save_filename,
100 skip_replay, boss_rush, fps_limit, single_buffer, debug, 100 skip_replay, boss_rush, debug, enable_background, enable_particles,
101 fixed_pipeline, enable_background, enable_particles, enable_music, 101 enable_music, hints, verbosity):
102 hints, verbosity):
103 102
104 resource_loader = Loader(path) 103 resource_loader = Loader(path)
105 104
106 try: 105 try:
107 resource_loader.scan_archives(data) 106 resource_loader.scan_archives(data)
108 except IOError: 107 except IOError:
109 sys.stderr.write('Some data files were not found, did you forget the -p option?\n') 108 sys.stderr.write('Some data files were not found, did you forget the -p option?\n')
110 exit(1) 109 exit(1)
111 110
112 with SDL(): 111 if stage_num is None:
113 window = Window(double_buffer=(not single_buffer), fps_limit=fps_limit, fixed_pipeline=fixed_pipeline) 112 story = True
114 113 stage_num = 1
115 if stage_num is None: 114 continues = 3
116 story = True 115 else:
117 stage_num = 1 116 story = False
118 continues = 3 117 continues = 0
118
119 if debug:
120 if not verbosity:
121 verbosity = 'DEBUG'
122 continues = -1 # Infinite lives
123
124 if verbosity:
125 logging.basicConfig(level=logging.__getattribute__(verbosity))
126
127 if replay:
128 with open(replay, 'rb') as file:
129 replay = T6RP.read(file)
130 rank = replay.rank
131 character = replay.character
132
133 save_keystates = None
134 if save_filename:
135 save_replay = T6RP()
136 save_replay.rank = rank
137 save_replay.character = character
138
139 if hints:
140 with open(hints, 'rb') as file:
141 hints = Hint.read(file)
142
143 difficulty = 16
144 default_power = [0, 64, 128, 128, 128, 128, 0][stage_num - 1]
145 states = [PlayerState(character=character, power=default_power, continues=continues)]
146
147 game_class = GameBossRush if boss_rush else Game
148
149 common = Common(resource_loader)
150 runner = GameRunner(window, resource_loader, skip=skip_replay)
151 while True:
152 if replay:
153 level = replay.levels[stage_num - 1]
154 if not level:
155 raise Exception
156
157 prng = Random(level.random_seed)
158
159 #TODO: apply the replay to the other players.
160 #TODO: see if the stored score is used or if it’s the one from the previous stage.
161 if stage_num != 1 and stage_num - 2 in replay.levels:
162 previous_level = replay.levels[stage_num - 1]
163 states[0].score = previous_level.score
164 states[0].effective_score = previous_level.score
165 states[0].points = level.point_items
166 states[0].power = level.power
167 states[0].lives = level.lives
168 states[0].bombs = level.bombs
169 difficulty = level.difficulty
119 else: 170 else:
120 story = False 171 prng = Random()
121 continues = 0 172
122
123 if debug:
124 if not verbosity:
125 verbosity = 'DEBUG'
126 continues = -1 # Infinite lives
127
128 if verbosity:
129 logging.basicConfig(level=logging.__getattribute__(verbosity))
130
131 if replay:
132 with open(replay, 'rb') as file:
133 replay = T6RP.read(file)
134 rank = replay.rank
135 character = replay.character
136
137 save_keystates = None
138 if save_filename: 173 if save_filename:
139 save_replay = T6RP() 174 if not replay:
140 save_replay.rank = rank 175 save_replay.levels[stage_num - 1] = level = Level()
141 save_replay.character = character 176 level.score = states[0].score
142 177 level.random_seed = prng.seed
143 if hints: 178 level.point_items = states[0].points
144 with open(hints, 'rb') as file: 179 level.power = states[0].power
145 hints = Hint.read(file) 180 level.lives = states[0].lives
146 181 level.bombs = states[0].bombs
147 difficulty = 16 182 level.difficulty = difficulty
148 default_power = [0, 64, 128, 128, 128, 128, 0][stage_num - 1] 183 save_keystates = []
149 states = [PlayerState(character=character, power=default_power, continues=continues)] 184
150 185 hints_stage = hints.stages[stage_num - 1] if hints else None
151 game_class = GameBossRush if boss_rush else Game 186
152 187 game = game_class(resource_loader, states, stage_num, rank, difficulty, common, prng=prng, hints=hints_stage)
153 common = Common(resource_loader) 188
154 runner = GameRunner(window, resource_loader, skip=skip_replay) 189 if not enable_particles:
155 while True: 190 def new_particle(pos, anim, amp, number=1, reverse=False, duration=24):
156 if replay: 191 pass
157 level = replay.levels[stage_num - 1] 192 game.new_particle = new_particle
158 if not level: 193
159 raise Exception 194 background = game.background if enable_background else None
160 195 bgms = game.std.bgms if enable_music else None
161 prng = Random(level.random_seed) 196
162 197 # Main loop
163 #TODO: apply the replay to the other players. 198 runner.load_game(game, background, bgms, replay, save_keystates)
164 #TODO: see if the stored score is used or if it’s the one from the previous stage. 199 window.set_runner(runner)
165 if stage_num != 1 and stage_num - 2 in replay.levels: 200 try:
166 previous_level = replay.levels[stage_num - 1] 201 window.run()
167 states[0].score = previous_level.score 202 break
168 states[0].effective_score = previous_level.score 203 except NextStage:
169 states[0].points = level.point_items 204 if not story or stage_num == (7 if boss_rush else 6 if rank > 0 else 5):
170 states[0].power = level.power 205 break
171 states[0].lives = level.lives 206 stage_num += 1
172 states[0].bombs = level.bombs 207 states = [player.state for player in game.players]
173 difficulty = level.difficulty 208 except GameOver:
174 else: 209 print('Game over')
175 prng = Random() 210 break
176 211 finally:
177 if save_filename: 212 if save_filename:
178 if not replay: 213 last_key = -1
179 save_replay.levels[stage_num - 1] = level = Level() 214 for time, key in enumerate(save_keystates):
180 level.score = states[0].score 215 if key != last_key:
181 level.random_seed = prng.seed 216 level.keys.append((time, key, 0))
182 level.point_items = states[0].points 217 last_key = key
183 level.power = states[0].power 218
184 level.lives = states[0].lives 219 window.set_runner(None)
185 level.bombs = states[0].bombs
186 level.difficulty = difficulty
187 save_keystates = []
188
189 hints_stage = hints.stages[stage_num - 1] if hints else None
190
191 game = game_class(resource_loader, states, stage_num, rank, difficulty, common, prng=prng, hints=hints_stage)
192
193 if not enable_particles:
194 def new_particle(pos, anim, amp, number=1, reverse=False, duration=24):
195 pass
196 game.new_particle = new_particle
197
198 background = game.background if enable_background else None
199 bgms = game.std.bgms if enable_music else None
200
201 # Main loop
202 runner.load_game(game, background, bgms, replay, save_keystates)
203 window.set_runner(runner)
204 try:
205 window.run()
206 break
207 except NextStage:
208 if not story or stage_num == (7 if boss_rush else 6 if rank > 0 else 5):
209 break
210 stage_num += 1
211 states = [player.state for player in game.players]
212 except GameOver:
213 print('Game over')
214 break
215 finally:
216 if save_filename:
217 last_key = -1
218 for time, key in enumerate(save_keystates):
219 if key != last_key:
220 level.keys.append((time, key, 0))
221 last_key = key
222 220
223 if save_filename: 221 if save_filename:
224 with open(save_filename, 'wb+') as file: 222 with open(save_filename, 'wb+') as file:
225 save_replay.write(file) 223 save_replay.write(file)
226 224
227 225
228 main(args.path, tuple(args.data), args.stage, args.rank, args.character, 226 with SDL():
229 args.replay, args.save_replay, args.skip_replay, args.boss_rush, 227 window = Window(double_buffer=(not args.single_buffer),
230 args.fps_limit, args.single_buffer, args.debug, args.fixed_pipeline, 228 fps_limit=args.fps_limit,
231 args.no_background, args.no_particles, args.no_music, args.hints, 229 fixed_pipeline=args.fixed_pipeline)
232 args.verbosity) 230
231 main(window, args.path, tuple(args.data), args.stage, args.rank,
232 args.character, args.replay, args.save_replay, args.skip_replay,
233 args.boss_rush, args.debug, args.no_background, args.no_particles,
234 args.no_music, args.hints, args.verbosity)
235
236 import gc
237 gc.collect()