00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00077 }
00078
00079
00080
00081 void CSoundAnimation::save()
00082 {
00083
00084 COFile file;
00085 vector<const char*> sounds;
00086
00087
00088 if (!file.open(_Filename.c_str()))
00089 {
00090 throw exception("Can't open the file for writing");
00091 }
00092
00093
00094 COXml output;
00095
00096
00097 if (output.init (&file, "1.0"))
00098 {
00099 xmlDocPtr xmlDoc = output.getDocument();
00100
00101
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
00131 output.flush ();
00132 }
00133
00134
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
00164 if (!file.open(_Filename.c_str()))
00165 {
00166 throw exception("Can't open the file for reading");
00167 }
00168
00169
00170 CIXml input;
00171
00172
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
00216 file.close ();
00217 _Dirty = false;
00218 }
00219
00220 }