Homepage Demos Overview Downloads Tutorials Reference
Credits

SoundNode.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_SoundNode_h_
00003 #define INCLUDED_SoundNode_h_
00004 
00005 #include "Behaviors/StateNode.h"
00006 #include "Events/EventRouter.h"
00007 #include "SoundPlay/SoundManager.h"
00008 
00009 //! A simple StateNode that plays a sound upon startup and throws a status event on completion
00010 /*! Doesn't automatically preload the sound buffer - if you want the sound file
00011  *  to be preloaded, you'll have to make the
00012  *  SoundManager::LoadFile() / SoundManager::ReleaseFile() calls yourself.
00013  *  
00014  *  By default, sound playback will continue even after this node has been deactivated.
00015  *  If this is not the behavior you desire, set the #autostop flag (through setAutoStop())
00016  */
00017 class SoundNode : public StateNode {
00018 protected:
00019   std::string filename; //!< filename of sound to play, accessed through setFileName() and getFileName()
00020   SoundManager::Play_ID curplay_id; //!< holds the playback identification so it can be halted any time
00021   bool autostop; //!< if set to true by setAutoStop(), when this node is deactivated, playback will be halted.  Otherwise, playback will continue even after the node is deactivated
00022 
00023 public:
00024   //! constructor, specify a sound file to play
00025   SoundNode(const std::string& soundfilename="") : 
00026     StateNode("SoundNode","SoundNode"), filename(soundfilename), curplay_id(SoundManager::invalid_Play_ID), autostop(false) {}
00027 
00028   //! constructor, specify instance name and sound file to play
00029   SoundNode(const std::string& nodename, const std::string& soundfilename) : 
00030     StateNode("SoundNode",nodename), filename(soundfilename), curplay_id(SoundManager::invalid_Play_ID), autostop(false) {}
00031 
00032   //! activate the node, starts playing the sound
00033   virtual void DoStart() {
00034     StateNode::DoStart();  // don't activate transitions until our listener has been added
00035     if(filename.size()>0) {
00036       curplay_id = sndman->PlayFile(filename);
00037       erouter->addListener(this,EventBase::audioEGID,curplay_id,EventBase::deactivateETID);
00038     }
00039   }
00040 
00041   //! deactivate the node, doesn't stop the sound playback unless the #autostop flag has been set
00042   virtual void DoStop() {
00043     if(autostop)
00044       StopPlay();
00045     erouter->removeListener(this);
00046     StateNode::DoStop();
00047   }
00048 
00049   //! receive audioEGID status event and throw stateMachineEGID status event
00050   virtual void processEvent(const EventBase&) {
00051     curplay_id = SoundManager::invalid_Play_ID;
00052     erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<unsigned int>(this),EventBase::statusETID,0,getName(),1);
00053   }
00054 
00055   //! interrupts playing of the current sound
00056   void StopPlay() {
00057     sndman->StopPlay(curplay_id);
00058     curplay_id = SoundManager::invalid_Play_ID;
00059   }
00060 
00061   //! returns the name of the sound file associated with this node
00062   std::string getFileName() { return filename; }
00063 
00064   //! sets the name of the sound file associated with this node
00065   void setFileName(std::string &soundfilename) { filename = soundfilename; }
00066 
00067   //! returns the current status of the #autostop flag
00068   bool getAutoStop() { return autostop; }
00069 
00070   //! sets the #autostop flag
00071   void setAutoStop(bool stop) { autostop=stop; }
00072 
00073 protected:
00074   //! constructor
00075   SoundNode(const std::string &classname, const std::string &nodename, const std::string &soundfilename) : 
00076     StateNode(classname,nodename), filename(soundfilename), curplay_id(SoundManager::invalid_Play_ID), autostop(false) {}
00077 };
00078 
00079 /*! @file
00080  * @brief Defines SoundNode, a simple StateNode that plays a sound and throws a status event upon completion
00081  * @author dst (Creator)
00082  *
00083  * $Author: ejt $
00084  * $Name: tekkotsu-2_2_2 $
00085  * $Revision: 1.9 $
00086  * $State: Exp $
00087  * $Date: 2004/12/04 00:10:42 $
00088  */
00089 
00090 #endif

Tekkotsu v2.2.2
Generated Tue Jan 4 15:43:15 2005 by Doxygen 1.4.0