changeset 341:61caded6b4f5

Clean music playback API a little.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 04 Jul 2012 23:05:21 +0200
parents 39bc59953dfa
children 83c9354ff3ef
files eosd pytouhou/game/game.py pytouhou/games/eosd.py pytouhou/ui/gamerenderer.pyx pytouhou/ui/gamerunner.py pytouhou/ui/music.py pytouhou/vm/msgrunner.py
diffstat 7 files changed, 47 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/eosd
+++ b/eosd
@@ -112,13 +112,13 @@ def main(path, data, stage_num, rank, ch
         # Load stage data
         stage = resource_loader.get_stage('stage%d.std' % stage_num)
 
-        game = game_class(resource_loader, states, stage_num, rank, difficulty, prng=prng, bgms=stage.bgms, continues=continues)
+        game = game_class(resource_loader, states, stage_num, rank, difficulty, prng=prng, continues=continues)
 
         background_anm_wrapper = resource_loader.get_anm_wrapper(('stg%dbg.anm' % stage_num,))
         background = Background(stage, background_anm_wrapper)
 
         # Main loop
-        runner.load_game(game, background, replay)
+        runner.load_game(game, background, stage.bgms, replay)
         try:
             runner.start()
             break
--- a/pytouhou/game/game.py
+++ b/pytouhou/game/game.py
@@ -116,14 +116,6 @@ class Game(object):
             self.difficulty = self.difficulty_max
 
 
-    def change_music(self, track):
-        #TODO: don’t crash if the track has already be played.
-        bgm = self.bgms[track]
-        if bgm:
-            self.music.queue(bgm)
-            self.music.next()
-
-
     def enable_spellcard_effect(self):
         self.spellcard_effect = Effect((-32., -16.), 0,
                                        self.spellcard_effect_anm_wrapper) #TODO: find why this offset is necessary.
--- a/pytouhou/games/eosd.py
+++ b/pytouhou/games/eosd.py
@@ -23,9 +23,6 @@ from pytouhou.game.orb import Orb
 from pytouhou.game.effect import Effect
 from pytouhou.game.text import Text, Counter
 
-from os.path import join
-from pytouhou.ui.music import InfiniteWaveSource
-
 
 SQ2 = 2. ** 0.5 / 2.
 
@@ -33,7 +30,7 @@ SQ2 = 2. ** 0.5 / 2.
 class EoSDGame(Game):
     def __init__(self, resource_loader, player_states, stage, rank, difficulty,
                  bullet_types=None, laser_types=None, item_types=None,
-                 nb_bullets_max=640, width=384, height=448, prng=None, bgms=None, continues=0):
+                 nb_bullets_max=640, width=384, height=448, prng=None, continues=0):
 
         if not bullet_types:
             etama3 = resource_loader.get_anm_wrapper(('etama3.anm',))
@@ -96,20 +93,6 @@ class EoSDGame(Game):
 
         interface = EoSDInterface(player_states, resource_loader)
 
-        self.bgms = []
-        for bgm in bgms:
-            if not bgm:
-                self.bgms.append(None)
-                continue
-            posname = bgm[1].replace('bgm/', '').replace('.mid', '.pos')
-            track = resource_loader.get_track(posname)
-            wavname = join(resource_loader.game_dir, bgm[1].replace('.mid', '.wav'))
-            try:
-                source = InfiniteWaveSource(wavname, track.start, track.end)
-            except IOError:
-                source = None
-            self.bgms.append(source)
-
         Game.__init__(self, resource_loader, players, stage, rank, difficulty,
                       bullet_types, laser_types, item_types, nb_bullets_max,
                       width, height, prng, interface, continues)
--- a/pytouhou/ui/gamerenderer.pyx
+++ b/pytouhou/ui/gamerenderer.pyx
@@ -29,7 +29,8 @@ cdef class GameRenderer(Renderer):
 
     def __init__(self, resource_loader, game=None, background=None):
         Renderer.__init__(self, resource_loader)
-        self.load_game(game, background)
+        if game:
+            self.load_game(game, background)
 
 
     cpdef load_game(self, game=None, background=None):
--- a/pytouhou/ui/gamerunner.py
+++ b/pytouhou/ui/gamerunner.py
@@ -25,11 +25,10 @@ from pyglet.gl import (glMatrixMode, glL
                        GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
                        GL_SCISSOR_TEST)
 
-from pyglet.media import Player as MusicPlayer
-
 from pytouhou.utils.helpers import get_logger
 
 from .gamerenderer import GameRenderer
+from .music import MusicPlayer
 
 
 logger = get_logger(__name__)
@@ -54,7 +53,7 @@ class GameRunner(pyglet.window.Window, G
         self.clock = pyglet.clock.get_default()
 
 
-    def load_game(self, game=None, background=None, replay=None):
+    def load_game(self, game=None, background=None, bgms=None, replay=None):
         GameRenderer.load_game(self, game, background)
         self.replay_level = None
         if not replay or not replay.levels[game.stage-1]:
@@ -63,10 +62,13 @@ class GameRunner(pyglet.window.Window, G
         else:
             self.keys = 0
             self.replay_level = replay.levels[game.stage-1]
-            self.game.players[0].state.lives = self.replay_level.lives
-            self.game.players[0].state.power = self.replay_level.power
-            self.game.players[0].state.bombs = self.replay_level.bombs
-            self.game.difficulty = self.replay_level.difficulty
+            game.players[0].state.lives = self.replay_level.lives
+            game.players[0].state.power = self.replay_level.power
+            game.players[0].state.bombs = self.replay_level.bombs
+            game.difficulty = self.replay_level.difficulty
+
+        game.music = MusicPlayer(game.resource_loader, bgms)
+        game.music.play(0)
 
 
     def start(self, width=None, height=None):
@@ -85,13 +87,6 @@ class GameRunner(pyglet.window.Window, G
         glEnableClientState(GL_VERTEX_ARRAY)
         glEnableClientState(GL_TEXTURE_COORD_ARRAY)
 
-        # Initialize sound
-        self.game.music = MusicPlayer()
-        bgm = self.game.bgms[0]
-        if bgm:
-            self.game.music.queue(bgm)
-        self.game.music.play()
-
         if self.fps_limit > 0:
             pyglet.clock.set_fps_limit(self.fps_limit)
         while not self.has_exit:
--- a/pytouhou/ui/music.py
+++ b/pytouhou/ui/music.py
@@ -13,7 +13,9 @@
 ##
 
 
-from pyglet.media import AudioData, AudioFormat
+from os.path import join
+
+from pyglet.media import AudioData, AudioFormat, Player
 from pyglet.media.riff import WaveSource
 
 
@@ -77,3 +79,32 @@ class ZwavSource(InfiniteWaveSource):
         self._file.seek(self._offset)
         self._start = format.intro + format.start
         self._end = format.intro + format.duration
+
+
+class MusicPlayer(object):
+    def __init__(self, resource_loader, bgms):
+        self.bgms = []
+        for bgm in bgms:
+            if not bgm:
+                self.bgms.append(None)
+                continue
+            posname = bgm[1].replace('bgm/', '').replace('.mid', '.pos')
+            track = resource_loader.get_track(posname)
+            wavname = join(resource_loader.game_dir, bgm[1].replace('.mid', '.wav'))
+            try:
+                source = InfiniteWaveSource(wavname, track.start, track.end)
+            except IOError:
+                source = None
+            self.bgms.append(source)
+
+        self.player = Player()
+
+
+    def play(self, index):
+        bgm = self.bgms[index]
+        if self.player.playing:
+            self.player.next()
+        if bgm:
+            self.player.queue(bgm)
+        self.player.play()
+
--- a/pytouhou/vm/msgrunner.py
+++ b/pytouhou/vm/msgrunner.py
@@ -136,7 +136,7 @@ class MSGRunner(object):
 
     @instruction(7)
     def change_music(self, track):
-        self._game.change_music(track)
+        self._game.music.play(track)
 
 
     @instruction(10)