diff eosd @ 460:ec327e58b477

Add a context manager to initialize and shut down SDL outside of Window.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 06 Sep 2013 23:29:19 +0200
parents a502887557ac
children 5f5955635d2c
line wrap: on
line diff
--- a/eosd
+++ b/eosd
@@ -53,6 +53,7 @@ args = parser.parse_args()
 import sys
 import logging
 
+from pytouhou.lib.sdl import SDL
 from pytouhou.ui.window import Window
 from pytouhou.resource.loader import Loader
 from pytouhou.ui.gamerunner import GameRunner
@@ -108,115 +109,116 @@ def main(path, data, stage_num, rank, ch
         sys.stderr.write('Some data files were not found, did you forget the -p option?\n')
         exit(1)
 
-    window = Window(double_buffer=(not single_buffer), fps_limit=fps_limit, fixed_pipeline=fixed_pipeline)
+    with SDL():
+        window = Window(double_buffer=(not single_buffer), fps_limit=fps_limit, fixed_pipeline=fixed_pipeline)
 
-    if stage_num is None:
-        story = True
-        stage_num = 1
-        continues = 3
-    else:
-        story = False
-        continues = 0
+        if stage_num is None:
+            story = True
+            stage_num = 1
+            continues = 3
+        else:
+            story = False
+            continues = 0
 
-    if debug:
-        if not verbosity:
-            verbosity = 'DEBUG'
-        continues = float('inf')
+        if debug:
+            if not verbosity:
+                verbosity = 'DEBUG'
+            continues = float('inf')
 
-    if verbosity:
-        logging.basicConfig(level=logging.__getattribute__(verbosity))
+        if verbosity:
+            logging.basicConfig(level=logging.__getattribute__(verbosity))
 
-    if replay:
-        with open(replay, 'rb') as file:
-            replay = T6RP.read(file)
-        rank = replay.rank
-        character = replay.character
+        if replay:
+            with open(replay, 'rb') as file:
+                replay = T6RP.read(file)
+            rank = replay.rank
+            character = replay.character
 
-    save_keystates = None
-    if save_filename:
-        save_replay = T6RP()
-        save_replay.rank = rank
-        save_replay.character = character
+        save_keystates = None
+        if save_filename:
+            save_replay = T6RP()
+            save_replay.rank = rank
+            save_replay.character = character
 
-    if hints:
-        with open(hints, 'rb') as file:
-            hints = Hint.read(file)
+        if hints:
+            with open(hints, 'rb') as file:
+                hints = Hint.read(file)
 
-    difficulty = 16
-    default_power = [0, 64, 128, 128, 128, 128, 0][stage_num - 1]
-    states = [PlayerState(character=character, power=default_power)]
+        difficulty = 16
+        default_power = [0, 64, 128, 128, 128, 128, 0][stage_num - 1]
+        states = [PlayerState(character=character, power=default_power)]
 
-    game_class = GameBossRush if boss_rush else Game
+        game_class = GameBossRush if boss_rush else Game
 
-    common = Common(resource_loader)
-    runner = GameRunner(window, resource_loader, skip=skip_replay)
-    while True:
-        if replay:
-            level = replay.levels[stage_num - 1]
-            if not level:
-                raise Exception
+        common = Common(resource_loader)
+        runner = GameRunner(window, resource_loader, skip=skip_replay)
+        while True:
+            if replay:
+                level = replay.levels[stage_num - 1]
+                if not level:
+                    raise Exception
 
-            prng = Random(level.random_seed)
+                prng = Random(level.random_seed)
 
-            #TODO: apply the replay to the other players.
-            #TODO: see if the stored score is used or if it’s the one from the previous stage.
-            if stage_num != 1 and stage_num - 2 in replay.levels:
-                previous_level = replay.levels[stage_num - 1]
-                states[0].score = previous_level.score
-                states[0].effective_score = previous_level.score
-            states[0].points = level.point_items
-            states[0].power = level.power
-            states[0].lives = level.lives
-            states[0].bombs = level.bombs
-            difficulty = level.difficulty
-        else:
-            prng = Random()
+                #TODO: apply the replay to the other players.
+                #TODO: see if the stored score is used or if it’s the one from the previous stage.
+                if stage_num != 1 and stage_num - 2 in replay.levels:
+                    previous_level = replay.levels[stage_num - 1]
+                    states[0].score = previous_level.score
+                    states[0].effective_score = previous_level.score
+                states[0].points = level.point_items
+                states[0].power = level.power
+                states[0].lives = level.lives
+                states[0].bombs = level.bombs
+                difficulty = level.difficulty
+            else:
+                prng = Random()
 
-        if save_filename:
-            if not replay:
-                save_replay.levels[stage_num - 1] = level = Level()
-                level.score = states[0].score
-                level.random_seed = prng.seed
-                level.point_items = states[0].points
-                level.power = states[0].power
-                level.lives = states[0].lives
-                level.bombs = states[0].bombs
-                level.difficulty = difficulty
-            save_keystates = []
+            if save_filename:
+                if not replay:
+                    save_replay.levels[stage_num - 1] = level = Level()
+                    level.score = states[0].score
+                    level.random_seed = prng.seed
+                    level.point_items = states[0].points
+                    level.power = states[0].power
+                    level.lives = states[0].lives
+                    level.bombs = states[0].bombs
+                    level.difficulty = difficulty
+                save_keystates = []
 
-        hints_stage = hints.stages[stage_num - 1] if hints else None
+            hints_stage = hints.stages[stage_num - 1] if hints else None
 
-        game = game_class(resource_loader, states, stage_num, rank, difficulty, common, prng=prng, continues=continues, hints=hints_stage)
+            game = game_class(resource_loader, states, stage_num, rank, difficulty, common, prng=prng, continues=continues, hints=hints_stage)
 
-        if not enable_particles:
-            def new_particle(pos, anim, amp, number=1, reverse=False, duration=24):
-                pass
-            game.new_particle = new_particle
+            if not enable_particles:
+                def new_particle(pos, anim, amp, number=1, reverse=False, duration=24):
+                    pass
+                game.new_particle = new_particle
 
-        background = game.background if enable_background else None
-        bgms = game.std.bgms if enable_music else None
+            background = game.background if enable_background else None
+            bgms = game.std.bgms if enable_music else None
 
-        # Main loop
-        runner.load_game(game, background, bgms, replay, save_keystates)
-        window.set_runner(runner)
-        try:
-            window.run()
-            break
-        except NextStage:
-            if not story or stage_num == (7 if boss_rush else 6 if rank > 0 else 5):
+            # Main loop
+            runner.load_game(game, background, bgms, replay, save_keystates)
+            window.set_runner(runner)
+            try:
+                window.run()
                 break
-            stage_num += 1
-            states = [player.state.copy() for player in game.players]  # if player.state.lives >= 0]
-        except GameOver:
-            print('Game over')
-            break
-        finally:
-            if save_filename:
-                last_key = -1
-                for time, key in enumerate(save_keystates):
-                    if key != last_key:
-                        level.keys.append((time, key, 0))
-                    last_key = key
+            except NextStage:
+                if not story or stage_num == (7 if boss_rush else 6 if rank > 0 else 5):
+                    break
+                stage_num += 1
+                states = [player.state.copy() for player in game.players]  # if player.state.lives >= 0]
+            except GameOver:
+                print('Game over')
+                break
+            finally:
+                if save_filename:
+                    last_key = -1
+                    for time, key in enumerate(save_keystates):
+                        if key != last_key:
+                            level.keys.append((time, key, 0))
+                        last_key = key
 
     if save_filename:
         with open(save_filename, 'wb+') as file: