changeset 487:7f1c42b66aa4

Moved the shooter demo Timer() class to helpers.py. Changed the formula that calculates the length of the audio clip to be correct. The shooter demo now demonstrates the use of the audio clip callbacks correctly. Fixed the fife_timer extension to actually work.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 03 May 2010 19:58:16 +0000
parents 2aaa22475acd
children 23d01971e9c2
files demos/shooter/scripts/common/helpers.py demos/shooter/scripts/scene.py demos/shooter/scripts/soundmanager.py engine/core/audio/soundemitter.h engine/python/fife/extensions/fife_timer.py
diffstat 5 files changed, 66 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/demos/shooter/scripts/common/helpers.py	Mon May 03 15:56:00 2010 +0000
+++ b/demos/shooter/scripts/common/helpers.py	Mon May 03 19:58:16 2010 +0000
@@ -117,4 +117,37 @@
 	newp.y = sintheta * x + costheta * y
 	
 	return newp
+
+class Timer(fife.TimeEvent):
+	def __init__(self,manager, delay=0,callback=None,repeat=0):
+		super(Timer,self).__init__(delay)
+		self._is_registered = False
+		self._callback = callback
+		self._manager = manager
+		self.setPeriod(delay)
+		self._repeat = repeat
+		self._executed = 0
+
+	def start(self):
+		if self._is_registered:
+			return
+		self._is_registered = True
+		self._executed = 0
+		self._manager.registerEvent(self)
+
+	def stop(self):
+		if not self._is_registered:
+			return
+		self._is_registered = False
+		self._manager.unregisterEvent(self)
+
+	def updateEvent(self,delta):
+		if callable(self._callback):
+			self._callback()
+			
+		if self._repeat != 0:
+			self._executed += 1
+			if self._executed >= self._repeat:
+				self.stop()
+
 	
\ No newline at end of file
--- a/demos/shooter/scripts/scene.py	Mon May 03 15:56:00 2010 +0000
+++ b/demos/shooter/scripts/scene.py	Mon May 03 19:58:16 2010 +0000
@@ -185,7 +185,7 @@
 		self.startCamera()
 
 	def musicHasFinished(self):
-		print self._music.name + " has finished playing.\n"
+		print self._music.name + " has finished playing.  Starting it again...\n"
 		
 	def pause(self, time):
 		self._pausedtime = time
--- a/demos/shooter/scripts/soundmanager.py	Mon May 03 15:56:00 2010 +0000
+++ b/demos/shooter/scripts/soundmanager.py	Mon May 03 19:58:16 2010 +0000
@@ -23,37 +23,8 @@
 
 from fife import fife
 
-class Timer(fife.TimeEvent):
-	def __init__(self,manager, delay=0,callback=None,repeat=0):
-		super(Timer,self).__init__(delay)
-		self._is_registered = False
-		self._callback = callback
-		self._manager = manager
-		self.setPeriod(delay)
-		self._repeat = repeat
-		self._executed = 0
-
-	def start(self):
-		if self._is_registered:
-			return
-		self._is_registered = True
-		self._executed = 0
-		self._manager.registerEvent(self)
-
-	def stop(self):
-		if not self._is_registered:
-			return
-		self._is_registered = False
-		self._manager.unregisterEvent(self)
-
-	def updateEvent(self,delta):
-		if callable(self._callback):
-			self._callback()
-			
-		if self._repeat != 0:
-			self._executed += 1
-			if self._executed >= self._repeat:
-				self.stop()
+from scripts.common.helpers import Timer
+from fife.extensions.pychan.tools import callbackWithArguments as cbwa
 
 class SoundClip(object):
 	def __init__(self, soundmanager, clipid, soundname, emitter):
@@ -141,7 +112,7 @@
 			time = fifeemitter.getDuration()
 				
 			self._loadedclips[filename] = SoundClip(self, clipid, filename, fifeemitter)
-			self._loadedclips[filename].duration = time/2
+			self._loadedclips[filename].duration = time
 			self._emitters.append(fifeemitter)
 		
 		return self._loadedclips[filename]
@@ -151,23 +122,41 @@
 			if clip.callback:
 				if clip.timer:
 					clip.timer.stop()
-				clip.timer = Timer(self._engine.getTimeManager(), clip.duration, clip.callback, 1)
+					timer = None
+					
+				if clip.looping:
+					repeat = 0
+					print "setting up callback1"
+
+					def real_callback(c, e):
+						c()
+						print "in here"
+						e.stop()
+						e.play()
+
+					clip.callback = cbwa(real_callback, clip.callback, clip.fifeemitter)
+
+				else:
+					repeat = 1
+
+					
+				clip.timer = Timer(self._engine.getTimeManager(), clip.duration, clip.callback, repeat)
 				clip.timer.start()
 				
 			clip.fifeemitter.setGain(clip.gain)
-			clip.fifeemitter.setLooping(clip.looping)
+			#clip.fifeemitter.setLooping(clip.looping)
 			clip.fifeemitter.play()	
 		else:
 			clip = self.loadSoundClip(clip.name)
+			self.playClip(clip)
 				
 	def stopClip(self, clip):
 		if clip.fifeemitter:
 			clip.fifeemitter.stop()
-		else:
-			clip = self.loadSoundClip(clip.name)
 			
 		if clip.timer:
 			clip.timer.stop()
+			clip.timer = None
 
 	def stopAllSounds(self):
 		for clip in self._loadedclips.values():	
@@ -186,4 +175,3 @@
 			
 		self._emitters = list()
 		self._loadedclips.clear()
-		
\ No newline at end of file
--- a/engine/core/audio/soundemitter.h	Mon May 03 15:56:00 2010 +0000
+++ b/engine/core/audio/soundemitter.h	Mon May 03 19:58:16 2010 +0000
@@ -179,11 +179,11 @@
 		 */
 		unsigned long getDuration() const{
 			if (m_soundclip) {
-				float samplerate = static_cast<float>(getSampleRate());
-				float bitres = static_cast<float>(getBitResolution());
-				float size = static_cast<float>(getDecodedLength());
-				float stereo = (isStereo() ? 2.0f : 1.0f);
-				float time = (( size / samplerate * bitres / 8.0f) * 1000.0f ) / stereo / 2.0f;
+				double samplerate = static_cast<double>(getSampleRate()) / 1000.0;  //convert to milliseconds
+				double bitres = static_cast<double>(getBitResolution());
+				double size = static_cast<double>(getDecodedLength()) * 8.0;  //convert to bits
+				double stereo = (isStereo() ? 2.0 : 1.0);
+				double time = ( size / (samplerate * bitres) ) / stereo;
 
 				return static_cast<unsigned long>(time);
 			}
--- a/engine/python/fife/extensions/fife_timer.py	Mon May 03 15:56:00 2010 +0000
+++ b/engine/python/fife/extensions/fife_timer.py	Mon May 03 19:58:16 2010 +0000
@@ -22,6 +22,7 @@
 # ####################################################################
 
 from fife import fife
+from fife.extensions.pychan.tools import callbackWithArguments as cbwa
 
 """
 Convenient timers
@@ -92,8 +93,7 @@
 	@return The timer.
 	"""
 	timer = Timer(delay)
-	def cbwa(c, *args):
-		c(*args)
+
 	def real_callback(c, t):
 		t.stop()
 		c()