2 * This file is part of ShapeFusion (Copyright 2000 Tito Dal Canton)
4 * ShapeFusion is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * ShapeFusion is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with ShapeFusion; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
30 #if wxUSE_STD_IOSTREAM
31 #include "wx/ioswrap.h"
33 #include "wx/txtstrm.h"
36 #include "SoundsDocument.h"
37 #include "SoundsView.h"
39 // Construct four-character-code
40 #define FOUR_CHARS_TO_INT(a,b,c,d) (((unsigned int)(a) << 24) | ((unsigned int)(b) << 16) | ((unsigned int)(c) << 8) | (unsigned int)(d))
45 NUMBER_OF_SOUND_SOURCES
48 IMPLEMENT_DYNAMIC_CLASS(SoundsDocument, wxDocument)
50 SoundsDocument::SoundsDocument(): wxDocument(), SoundsElement(true), mM2Demo(false)
52 // We need storage for our sound sources...
53 mSoundDefinitions.resize(NUMBER_OF_SOUND_SOURCES);
56 SoundsDocument::~SoundsDocument()
60 SoundsDefinition *SoundsDocument::GetSoundDefinition(unsigned short source_index, unsigned short sound_index)
62 if (source_index > mSoundDefinitions.size())
64 if (sound_index > mSoundDefinitions[source_index].size())
66 return mSoundDefinitions[source_index][sound_index];
69 SoundsDefinition *SoundsDocument::Get8BitSoundDefinition(unsigned short sound_index)
71 return GetSoundDefinition(_sound_8bit, sound_index);
74 SoundsDefinition *SoundsDocument::Get16BitSoundDefinition(unsigned short sound_index)
76 return GetSoundDefinition(_sound_16bit, sound_index);
79 void SoundsDocument::AddSoundDefinition(void)
81 // As there's always two versions (8-bit/16-bit) of the same sound, we add both there...
82 SoundsDefinition *snd8 = new SoundsDefinition(),
83 *snd16 = new SoundsDefinition();
86 mSoundDefinitions[_sound_8bit].push_back(snd8);
88 mSoundDefinitions[_sound_16bit].push_back(snd16);
90 // We mark ourselves as modified...
94 void SoundsDocument::DeleteSoundDefinition(unsigned int index)
96 // We check we really have that much sounds...
97 if (index > mSoundDefinitions[_sound_8bit].size())
100 // We remove 8-bit version
101 mSoundDefinitions[_sound_8bit].erase(mSoundDefinitions[_sound_8bit].begin() + index);
102 // We remove 16-bit version
103 mSoundDefinitions[_sound_16bit].erase(mSoundDefinitions[_sound_16bit].begin() + index);
105 // We mark ourselves as modified...
109 bool SoundsDocument::DoOpenDocument(const wxString& file)
111 bool wxOpen = wxDocument::DoOpenDocument(file);
113 if (!(wxOpen && mGoodData)) {
114 wxLogError(wxT("[SoundsDocument] There was an error while loading, see log"));
120 unsigned int SoundsDocument::GetSizeInFile(void)
122 unsigned int size = SIZEOF_sound_file_header;
124 for (unsigned int i = 0; i < mSoundDefinitions.size(); i++)
125 for (unsigned int j = 0; j < mSoundDefinitions[i].size(); j++)
126 size += mSoundDefinitions[i][j]->GetSizeInFile();
131 #if wxUSE_STD_IOSTREAM
132 wxSTD ostream& SoundsDocument::SaveObject(wxSTD ostream& stream)
134 wxOutputStream& SoundsDocument::SaveObject(wxOutputStream& stream)
137 BigEndianBuffer filebuffer(GetSizeInFile());
139 unsigned int source_count = mSoundDefinitions.size();
140 unsigned int sound_count = mSoundDefinitions[0].size();
142 filebuffer.WriteLong(mVersion);
143 filebuffer.WriteLong(mTag);
146 filebuffer.WriteShort(mSoundCount);
147 filebuffer.WriteShort(0);
149 filebuffer.WriteShort(source_count);
150 filebuffer.WriteShort(sound_count);
153 filebuffer.Position(SIZEOF_sound_file_header);
155 unsigned int current_sound_offset = SIZEOF_sound_file_header
156 + source_count * sound_count * SIZEOF_sound_definition;
158 for (unsigned int i = 0; i < mSoundDefinitions.size(); i++) {
159 for (unsigned int j = 0; j < mSoundDefinitions[i].size(); j++) {
160 mSoundDefinitions[i][j]->SaveObject(filebuffer, current_sound_offset);
164 #if wxUSE_STD_IOSTREAM
165 stream.write((char *)filebuffer.Data(), filebuffer.Size());
167 stream.Write((char *)filebuffer.Data(), filebuffer.Size());
173 #if wxUSE_STD_IOSTREAM
174 wxSTD istream& SoundsDocument::LoadObject(wxSTD istream& stream)
176 wxInputStream& SoundsDocument::LoadObject(wxInputStream& stream)
179 #if wxUSE_STD_IOSTREAM
180 stream.seekg(0, std::ios::end);
181 wxInt32 filesize = stream.tellg();
182 stream.seekg(0, std::ios::beg);
184 wxInt32 filesize = stream.GetSize();
187 BigEndianBuffer filebuffer(filesize);
189 #if wxUSE_STD_IOSTREAM
190 stream.read((char *)filebuffer.Data(), filebuffer.Size());
192 stream.Read((char *)filebuffer.Data(), filebuffer.Size());
195 mVersion = filebuffer.ReadLong();
196 mTag = filebuffer.ReadLong();
197 mSourceCount = filebuffer.ReadShort();
198 mSoundCount = filebuffer.ReadShort();
200 if ((mVersion != 0 && mVersion != 1) || mTag != FOUR_CHARS_TO_INT('s','n','d','2')) {
201 wxLogError(wxT("[SoundsDocument] Error loading : Incorrect version/tag, (%d/%x)"), mVersion, mTag);
205 if (mSoundCount < 0 || mSourceCount < 0) {
206 wxLogError(wxT("[SoundsDocument] Error loading : Incorrect Sound/Source count (%d/%d)"), mSoundCount, mSourceCount);
210 if (mSoundCount == 0) {
211 /* Handling Marathon 2 Demo
212 * We have to swap our counts
214 mSoundCount = mSourceCount;
220 wxLogDebug(wxT("[SoundsDocument] Version: %d"), mVersion);
221 wxLogDebug(wxT("[SoundsDocument] Tag: %d"), mTag);
222 wxLogDebug(wxT("[SoundsDocument] Source Count: %d"), mSourceCount);
223 wxLogDebug(wxT("[SoundsDocument] Sound Count: %d"), mSoundCount);
226 /* We move to the end of the Sound file header */
227 filebuffer.Position(SIZEOF_sound_file_header);
229 /* Now we load 8-bit and 16-bit sounds */
230 for (int i = 0; i < mSourceCount; i++) {
231 for (int j = 0; j < mSoundCount; j++) {
232 SoundsDefinition *snd = new SoundsDefinition(IsVerbose());
235 wxLogDebug(wxT("[SoundsDocument] Loading source %d, sound %d"), i, j);
237 unsigned int oldpos = filebuffer.Position();
239 snd->LoadObject(filebuffer);
241 if (!snd->IsGood()) {
242 wxLogError(wxT("[SoundsDocument] Error loading sound definition. Skipping..."));
246 filebuffer.Position(oldpos + SIZEOF_sound_definition);
248 mSoundDefinitions[i].push_back(snd);