diff gamelib/sound.py @ 107:5213b45fcc7e

Initial event sound support
author Neil Muller <neil@dip.sun.ac.za>
date Tue, 24 Aug 2010 14:07:07 +0200
parents
children 18ffaaaa27e7
line wrap: on
line diff
--- /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)
+