Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / src / sound / sound_animation.cpp
blobc6b49aea7298ffd008e8d10e9b1aba8085573db3
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "stdsound.h"
21 #include "nel/sound/sound_animation.h"
22 #include "nel/sound/sound_anim_marker.h"
23 #include "nel/misc/stream.h"
24 #include "nel/misc/i_xml.h"
25 #include "nel/misc/o_xml.h"
26 #include "nel/misc/file.h"
28 using namespace std;
29 using namespace NLSOUND;
30 using namespace NLMISC;
32 namespace NLSOUND {
35 // ********************************************************
37 void CSoundAnimation::addMarker(CSoundAnimMarker* marker)
39 if (marker == NULL)
40 return;
42 _Dirty = true;
43 _Markers.push_back(marker);
44 sort();
47 // ********************************************************
49 void CSoundAnimation::removeMarker(CSoundAnimMarker* marker)
51 if (marker == NULL)
52 return;
54 _Dirty = true;
55 vector<CSoundAnimMarker*>::iterator iter;
56 for (iter = _Markers.begin(); iter != _Markers.end(); iter++)
58 if (*iter == marker)
60 _Markers.erase(iter);
61 break;
66 // ********************************************************
68 void CSoundAnimation::sort()
70 //std::sort<CSoundAnimMarker*, eqmarker>(_Markers.begin(), _Markers.end(), eqmarker());
73 // ********************************************************
75 void CSoundAnimation::save()
77 // File stream
78 COFile file;
79 vector<NLMISC::TStringId> sounds;
81 // Open the file
82 if (!file.open(_Filename.c_str()))
84 throw NLMISC::Exception("Can't open the file for writing");
87 // Create the XML stream
88 COXml output;
90 // Init
91 if (output.init (&file, "1.0"))
93 xmlDocPtr xmlDoc = output.getDocument();
95 // Create the first node
96 xmlNodePtr root = xmlNewDocNode (xmlDoc, NULL, (const xmlChar*)"SOUNDANIMATION", NULL);
97 xmlDocSetRootElement (xmlDoc, root);
99 vector<CSoundAnimMarker*>::iterator iter;
100 for (iter = _Markers.begin(); iter != _Markers.end(); iter++)
102 CSoundAnimMarker* marker = (*iter);
104 set<string>::iterator iter;
106 char s[64];
107 smprintf(s, 64, "%f", marker->getTime());
109 xmlNodePtr markerNode = xmlNewChild (root, NULL, (const xmlChar*)"MARKER", NULL);
110 xmlSetProp (markerNode, (const xmlChar*) "time", (const xmlChar*) s);
112 marker->getSounds(sounds);
114 vector<NLMISC::TStringId>::iterator iter2;
115 for (iter2 = sounds.begin(); iter2 != sounds.end(); iter2++)
117 xmlNodePtr soundNode = xmlNewChild ( markerNode, NULL, (const xmlChar*)"SOUND", NULL );
118 xmlSetProp (soundNode, (const xmlChar*)"name", (const xmlChar*) CStringMapper::unmap(*iter2).c_str());
121 sounds.clear();
124 // Flush the stream, write all the output file
125 output.flush ();
128 // Close the file
129 file.close ();
131 _Dirty = false;
134 // ********************************************************
136 void CSoundAnimation::play(UAudioMixer* mixer, float lastTime, float curTime, NL3D::CCluster *cluster, CSoundContext &context)
138 vector<CSoundAnimMarker*>::iterator iter;
139 for (iter = _Markers.begin(); iter != _Markers.end(); iter++)
141 CSoundAnimMarker* marker = *iter;
142 nlassert(marker);
144 if ((lastTime <= marker->getTime()) && (marker->getTime() < curTime))
146 marker->play(mixer, cluster, context);
151 // ********************************************************
153 void CSoundAnimation::load()
155 CIFile file;
157 // Open the file
158 if (!file.open(_Filename.c_str()))
160 throw NLMISC::Exception("Can't open the file for reading");
163 // Create the XML stream
164 CIXml input;
166 // Init
167 if (input.init (file))
169 xmlNodePtr animNode = input.getRootNode ();
170 xmlNodePtr markerNode = input.getFirstChildNode(animNode, "MARKER");
172 while (markerNode != 0)
174 CSoundAnimMarker* marker = new CSoundAnimMarker();
176 const char *time = (const char*) xmlGetProp(markerNode, (xmlChar*) "time");
177 if (time == 0)
179 throw NLMISC::Exception("Invalid sound animation marker");
182 float val;
183 NLMISC::fromString(time, val);
185 marker->setTime(val);
186 xmlFree ((void*)time);
189 xmlNodePtr soundNode = input.getFirstChildNode(markerNode, "SOUND");
191 while (soundNode != 0)
193 char *name = (char*) xmlGetProp(soundNode, (xmlChar*) "name");
194 if (name == 0)
196 throw NLMISC::Exception("Invalid sound animation marker");
199 marker->addSound(CStringMapper::map(string(name)));
201 xmlFree ((void*)name);
203 soundNode = input.getNextChildNode(soundNode, "SOUND");
206 addMarker(marker);
208 markerNode = input.getNextChildNode(markerNode, "MARKER");
212 // Close the file
213 file.close ();
214 _Dirty = false;
217 } //namespace NLSOUND