Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
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 "Sound/SoundManager.h"
00008 
00009 //! A simple StateNode that plays a sound upon startup and posts 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 public:
00019   //! constructor, specify a sound file to play
00020   SoundNode(const std::string& soundfilename="") : 
00021   StateNode(), filename(soundfilename), curplay_id(SoundManager::invalid_Play_ID), autostop(false) {}
00022   
00023   //! constructor, specify instance name and sound file to play
00024   SoundNode(const std::string& nodename, const std::string& soundfilename) : 
00025   StateNode(nodename), filename(soundfilename), curplay_id(SoundManager::invalid_Play_ID), autostop(false) {}
00026   
00027   //! activate the node, starts playing the sound
00028   virtual void preStart() {
00029     StateNode::preStart();
00030     if(event!=NULL) {
00031       const DataEvent<std::string>* ev = dynamic_cast<const DataEvent<std::string>*>(event);
00032       if ( ev != NULL )
00033         filename = ev->getData();
00034     }
00035   }
00036   
00037   virtual void postStart() {
00038     StateNode::postStart();
00039     startPlaying();
00040   }
00041   
00042   //! deactivate the node, doesn't stop the sound playback unless the #autostop flag has been set
00043   virtual void stop() {
00044     if(autostop)
00045       stopPlay();
00046     StateNode::stop();
00047   }
00048   
00049   //! receive audioEGID status event and post stateMachineEGID status event
00050   virtual void doEvent() {
00051     curplay_id = SoundManager::invalid_Play_ID;
00052     postStateCompletion();
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 astop) { autostop=astop; }
00072   
00073 protected:
00074   virtual void startPlaying() {
00075     if(filename.size()>0) {
00076       curplay_id = sndman->playFile(filename);
00077       erouter->addListener(this,EventBase::audioEGID,curplay_id,EventBase::deactivateETID);
00078     }
00079   }
00080   
00081   std::string filename; //!< filename of sound to play, accessed through setFileName() and getFileName()
00082   SoundManager::Play_ID curplay_id; //!< holds the playback identification so it can be halted any time
00083   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
00084   
00085 };
00086 
00087 /*! @file
00088  * @brief Defines SoundNode, a simple StateNode that plays a sound and throws a status event upon completion
00089  * @author dst (Creator)
00090  */
00091 
00092 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:51 2016 by Doxygen 1.6.3