# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

sound_animation.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "stdsound.h"
00027 #include "nel/sound/sound_animation.h"
00028 #include "nel/sound/sound_anim_marker.h"
00029 #include "nel/misc/stream.h"
00030 #include "nel/misc/i_xml.h"
00031 #include "nel/misc/o_xml.h"
00032 #include "nel/misc/file.h"
00033 
00034 using namespace std;
00035 using namespace NLSOUND;
00036 using namespace NLMISC;
00037 
00038 namespace NLSOUND {
00039 
00040 
00041 // ********************************************************
00042 
00043 void CSoundAnimation::addMarker(CSoundAnimMarker* marker)
00044 {
00045         if (marker == NULL)
00046                 return;
00047 
00048         _Dirty = true;
00049         _Markers.push_back(marker);
00050         sort();
00051 }
00052 
00053 // ********************************************************
00054 
00055 void CSoundAnimation::removeMarker(CSoundAnimMarker* marker)
00056 {
00057         if (marker == NULL)
00058                 return;
00059 
00060         _Dirty = true;
00061         vector<CSoundAnimMarker*>::iterator iter;
00062         for (iter = _Markers.begin(); iter != _Markers.end(); iter++)
00063         {
00064                 if (*iter == marker)
00065                 {
00066                         _Markers.erase(iter);
00067                         break;
00068                 }
00069         }
00070 }
00071 
00072 // ********************************************************
00073 
00074 void CSoundAnimation::sort()
00075 {
00076         //std::sort<CSoundAnimMarker*, eqmarker>(_Markers.begin(), _Markers.end(), eqmarker());
00077 }
00078 
00079 // ********************************************************
00080 
00081 void CSoundAnimation::save()
00082 {
00083         // File stream
00084         COFile file;
00085         vector<const char*>     sounds;
00086 
00087         // Open the file
00088         if (!file.open(_Filename.c_str()))
00089         {
00090                 throw exception("Can't open the file for writing");
00091         }
00092 
00093         // Create the XML stream
00094         COXml output;
00095 
00096         // Init
00097         if (output.init (&file, "1.0"))
00098         {
00099                 xmlDocPtr xmlDoc = output.getDocument();
00100 
00101                 // Create the first node
00102                 xmlNodePtr root = xmlNewDocNode (xmlDoc, NULL, (const xmlChar*)"SOUNDANIMATION", NULL);
00103                 xmlDocSetRootElement (xmlDoc, root);
00104 
00105                 vector<CSoundAnimMarker*>::iterator iter;
00106                 for (iter = _Markers.begin(); iter != _Markers.end(); iter++)
00107                 {
00108                         CSoundAnimMarker* marker = (*iter);
00109                 
00110                         set<string>::iterator iter;
00111 
00112                         char s[64];
00113                         smprintf(s, 64, "%f", marker->getTime());
00114 
00115                         xmlNodePtr markerNode = xmlNewChild (root, NULL, (const xmlChar*)"MARKER", NULL);
00116                         xmlSetProp (markerNode, (const xmlChar*) "time", (const xmlChar*) s);
00117 
00118                         marker->getSounds(sounds);
00119 
00120                         vector<const char*>::iterator iter2;
00121                         for (iter2 = sounds.begin(); iter2 != sounds.end(); iter2++)
00122                         {
00123                                 xmlNodePtr soundNode = xmlNewChild ( markerNode, NULL, (const xmlChar*)"SOUND", NULL );
00124                                 xmlSetProp (soundNode, (const xmlChar*)"name", (const xmlChar*) (*iter2));
00125                         }
00126 
00127                         sounds.clear();
00128                 }
00129 
00130                 // Flush the stream, write all the output file
00131                 output.flush ();
00132         }
00133 
00134         // Close the file
00135         file.close ();
00136 
00137         _Dirty = false;
00138 }
00139 
00140 // ********************************************************
00141 
00142 void CSoundAnimation::play(UAudioMixer* mixer, float lastTime, float curTime, CSoundContext &context)
00143 {
00144         vector<CSoundAnimMarker*>::iterator iter;
00145         for (iter = _Markers.begin(); iter != _Markers.end(); iter++)
00146         {
00147                 CSoundAnimMarker* marker = *iter;
00148                 nlassert(marker);
00149 
00150                 if ((lastTime <= marker->getTime()) && (marker->getTime() < curTime))
00151                 {
00152                         marker->play(mixer, context);
00153                 }
00154         }
00155 }
00156 
00157 // ********************************************************
00158 
00159 void CSoundAnimation::load()
00160 {
00161         CIFile file;
00162 
00163         // Open the file
00164         if (!file.open(_Filename.c_str()))
00165         {
00166                 throw exception("Can't open the file for reading");
00167         }
00168 
00169         // Create the XML stream
00170         CIXml input;
00171 
00172         // Init
00173         if (input.init (file))
00174         {
00175                 xmlNodePtr animNode = input.getRootNode ();
00176                 xmlNodePtr markerNode = input.getFirstChildNode(animNode, "MARKER");
00177 
00178                 while (markerNode != 0)
00179                 {
00180                         CSoundAnimMarker* marker = new CSoundAnimMarker();
00181 
00182                         const char *time = (const char*) xmlGetProp(markerNode, (xmlChar*) "time");
00183                         if (time == 0)
00184                         {
00185                                 throw exception("Invalid sound animation marker");
00186                         }
00187 
00188                         marker->setTime((float) atof(time));
00189                         xmlFree ((void*)time);
00190 
00191 
00192                         xmlNodePtr soundNode = input.getFirstChildNode(markerNode, "SOUND");
00193 
00194                         while (soundNode != 0)
00195                         {
00196                                 char *name = (char*) xmlGetProp(soundNode, (xmlChar*) "name");
00197                                 if (name == 0)
00198                                 {
00199                                         throw exception("Invalid sound animation marker");
00200                                 }
00201 
00202                                 marker->addSound(string(name));
00203 
00204                                 xmlFree ((void*)name);
00205 
00206                                 soundNode = input.getNextChildNode(soundNode, "SOUND");
00207                         }
00208 
00209                         addMarker(marker);
00210 
00211                         markerNode = input.getNextChildNode(markerNode, "MARKER");
00212                 }
00213         }
00214 
00215         // Close the file
00216         file.close ();
00217         _Dirty = false;
00218 }
00219 
00220 } //namespace NLSOUND