# HG changeset patch # User Neil Muller # Date 1282651627 -7200 # Node ID 5213b45fcc7e7db4d91d8af8501c1719a5827dcf # Parent da547e14853295a81a5ae3177e178c438b08af72 Initial event sound support diff -r da547e148532 -r 5213b45fcc7e gamelib/gamescreen.py --- a/gamelib/gamescreen.py Tue Aug 24 13:53:49 2010 +0200 +++ b/gamelib/gamescreen.py Tue Aug 24 14:07:07 2010 +0200 @@ -86,11 +86,14 @@ def mouse_down(self, event): result = self.state.interact(event.pos) - if result and result.message: - # Display the message as a modal dialog - MessageDialog(result.message, 60).present() - # queue a redraw to show updated state - self.invalidate() + if result: + if result.sound: + result.sound.play() + if result.message: + # Display the message as a modal dialog + MessageDialog(result.message, 60).present() + # queue a redraw to show updated state + self.invalidate() def animate(self): if self.state.animate(): diff -r da547e148532 -r 5213b45fcc7e gamelib/main.py --- a/gamelib/main.py Tue Aug 24 13:53:49 2010 +0200 +++ b/gamelib/main.py Tue Aug 24 14:07:07 2010 +0200 @@ -10,6 +10,7 @@ import os.path right_path = os.path.dirname(os.path.dirname(__file__)) sys.path.insert(0, right_path) +from optparse import OptionParser import pygame from pygame.locals import SWSURFACE @@ -18,7 +19,15 @@ from menu import MenuScreen from gamescreen import GameScreen -from constants import SCREEN, FRAME_RATE +from constants import SCREEN, FRAME_RATE, FREQ, BITSIZE, CHANNELS, BUFFER +from sound import no_sound, disable_sound + +def parse_args(args): + parser = OptionParser() + parser.add_option("--no-sound", action="store_false", default=True, + dest="sound", help="disable sound") + opts, _ = parser.parse_args(args or []) + return opts class MainShell(Shell): def __init__(self, display): @@ -29,8 +38,17 @@ self.show_screen(self.menu_screen) def main(): + opts = parse_args(sys.argv) pygame.display.init() pygame.font.init() + if opts.sound: + try: + pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER) + except pygame.error, exc: + no_sound(exc) + else: + # Ensure get_sound returns nothing, so everything else just works + disable_sound() display = pygame.display.set_mode(SCREEN, SWSURFACE) shell = MainShell(display) shell.run() diff -r da547e148532 -r 5213b45fcc7e gamelib/sound.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamelib/sound.py Tue Aug 24 14:07:07 2010 +0200 @@ -0,0 +1,43 @@ +# Sound management for Suspended Sentence + +# This re-implements some of the albow.resource code to +# a) work around an annoying bugs +# b) add some missing functionality (disable_sound) + +from albow.resource import _resource_path, dummy_sound + +sound_cache = {} + +def get_sound(*names): + if sound_cache is None: + return dummy_sound + path = _resource_path("sounds", names) + sound = sound_cache.get(path) + if not sound: + try: + from pygame.mixer import Sound + except ImportError, e: + no_sound(e) + return dummy_sound + try: + sound = Sound(path) + except pygame.error, e: + missing_sound(e, path) + return dummy_sound + sound_cache[path] = sound + return sound + + +def no_sound(e): + global sound_cache + print "get_sound: %s" % e + print "get_sound: Sound not available, continuing without it" + sound_cache = None + +def disable_sound(): + global sound_cache + sound_cache = None + +def missing_sound(e, name): + print "albow.resource.get_sound: %s: %s" % (name, e) + diff -r da547e148532 -r 5213b45fcc7e gamelib/state.py --- a/gamelib/state.py Tue Aug 24 13:53:49 2010 +0200 +++ b/gamelib/state.py Tue Aug 24 14:07:07 2010 +0200 @@ -1,6 +1,6 @@ """Utilities and base classes for dealing with scenes.""" -from albow.resource import get_image, get_sound +from albow.resource import get_image from albow.utils import frame_rect from widgets import BoomLabel from pygame.locals import BLEND_ADD @@ -8,12 +8,16 @@ from pygame.color import Color import constants +from sound import get_sound class Result(object): """Result of interacting with a thing""" - def __init__(self, message=None): + def __init__(self, message=None, soundfile=None): self.message = message + self.sound = None + if soundfile: + self.sound = get_sound(soundfile) def initial_state():