changeset 107:5213b45fcc7e

Initial event sound support
author Neil Muller <neil@dip.sun.ac.za>
date Tue, 24 Aug 2010 14:07:07 +0200
parents da547e148532
children ab11689aec36
files gamelib/gamescreen.py gamelib/main.py gamelib/sound.py gamelib/state.py
diffstat 4 files changed, 76 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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():
--- 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()
--- /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)
+
--- 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():