Skip to content

Commit 71a1b08

Browse files
committed
externalize music definitions
1 parent a931a94 commit 71a1b08

8 files changed

Lines changed: 220 additions & 55 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
musics:
2+
- type: GMDEFEND
3+
catPos: 3
4+
normalization: 0.76
5+
- type: GMENBASE
6+
catPos: 6
7+
normalization: 0.83
8+
- type: GMGEO1
9+
catPos: 0
10+
normalization: 1.19
11+
- type: GMGEO2
12+
catPos: 18
13+
normalization: 1.0
14+
- type: GMGEO3
15+
- type: GMGEO4
16+
- type: GMGEO5
17+
- type: GMGEO6
18+
- type: GMGEO7
19+
- type: GMGEO8
20+
- type: GMGEO9
21+
- type: GMGEO10
22+
- type: GMINTER
23+
catPos: 2
24+
normalization: 0.74
25+
- type: GMINTRO1
26+
catPos: 19
27+
normalization: 0.8
28+
- type: GMINTRO2
29+
catPos: 20
30+
normalization: 0.8
31+
- type: GMINTRO3
32+
catPos: 21
33+
normalization: 0.8
34+
- type: GMLOSE
35+
catPos: 10
36+
normalization: 1.0
37+
- type: GMMARS
38+
catPos: 9
39+
normalization: 0.92
40+
- type: GMNEWMAR
41+
catPos: 8
42+
normalization: 0.81
43+
- type: GMSTORY
44+
catPos: 12
45+
normalization: 1.0
46+
- type: GMTACTIC
47+
catPos: 17
48+
normalization: 1.14
49+
- type: GMTACTIC1
50+
- type: GMTACTIC2
51+
- type: GMTACTIC3
52+
- type: GMTACTIC4
53+
- type: GMTACTIC5
54+
- type: GMTACTIC6
55+
- type: GMTACTIC7
56+
- type: GMTACTIC8
57+
- type: GMTACTIC9
58+
- type: GMTACTIC10
59+
- type: GMWIN
60+
catPos: 11
61+
normalization: 0.84

‎src/OpenXcom.2010.vcxproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@
440440
<ClCompile Include="Ruleset\AlienDeployment.cpp" />
441441
<ClCompile Include="Ruleset\AlienRace.cpp" />
442442
<ClCompile Include="Ruleset\RuleGlobe.cpp" />
443+
<ClCompile Include="Ruleset\RuleMusic.cpp" />
443444
<ClCompile Include="Ruleset\RuleVideo.cpp" />
444445
<ClCompile Include="Ruleset\SoundDefinition.cpp" />
445446
<ClCompile Include="Ruleset\StatString.cpp" />
@@ -752,6 +753,7 @@
752753
<ClInclude Include="Ruleset\Polygon.h" />
753754
<ClInclude Include="Ruleset\Polyline.h" />
754755
<ClInclude Include="Ruleset\RuleGlobe.h" />
756+
<ClInclude Include="Ruleset\RuleMusic.h" />
755757
<ClInclude Include="Ruleset\RuleVideo.h" />
756758
<ClInclude Include="Ruleset\SoundDefinition.h" />
757759
<ClInclude Include="Ruleset\StatString.h" />

‎src/OpenXcom.2010.vcxproj.filters‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,9 @@
907907
<ClCompile Include="Ruleset\RuleVideo.cpp">
908908
<Filter>Ruleset</Filter>
909909
</ClCompile>
910+
<ClCompile Include="Ruleset\RuleMusic.cpp">
911+
<Filter>Ruleset</Filter>
912+
</ClCompile>
910913
</ItemGroup>
911914
<ItemGroup>
912915
<ClInclude Include="resource.h" />
@@ -1842,6 +1845,9 @@
18421845
<ClInclude Include="Ruleset\RuleVideo.h">
18431846
<Filter>Ruleset</Filter>
18441847
</ClInclude>
1848+
<ClInclude Include="Ruleset\RuleMusic.h">
1849+
<Filter>Ruleset</Filter>
1850+
</ClInclude>
18451851
</ItemGroup>
18461852
<ItemGroup>
18471853
<Filter Include="Geoscape">

‎src/Resource/XcomResourcePack.cpp‎

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "../Savegame/Node.h"
3737
#include "../Battlescape/Position.h"
3838
#include "../Ruleset/MapDataSet.h"
39+
#include "../Ruleset/RuleMusic.h"
3940
#include "../Engine/ShaderDraw.h"
4041
#include "../Engine/ShaderMove.h"
4142
#include "../Engine/Exception.h"
@@ -255,22 +256,7 @@ XcomResourcePack::XcomResourcePack(Ruleset *rules) : ResourcePack()
255256
{
256257
#ifndef __NO_MUSIC
257258
// Load musics
258-
std::string mus[] = {"GMDEFEND",
259-
"GMENBASE",
260-
"GMGEO1",
261-
"GMGEO2",
262-
"GMINTER",
263-
"GMINTRO1",
264-
"GMINTRO2",
265-
"GMINTRO3",
266-
"GMLOSE",
267-
"GMMARS",
268-
"GMNEWMAR",
269-
"GMSTORY",
270-
"GMTACTIC",
271-
"GMWIN"};
272-
int tracks[] = {3, 6, 0, 18, 2, 19, 20, 21, 10, 9, 8, 12, 17, 11};
273-
float tracks_normalize[] = {0.76f, 0.83f, 1.19f, 1.0f, 0.74f, 0.8f, 0.8f, 0.8f, 1.0f, 0.92f, 0.81f, 1.0f, 1.14f, 0.84f};
259+
const std::map<std::string, RuleMusic *> musics = *rules->getMusic();
274260

275261
// Check which music version is available
276262
CatFile *adlibcat = 0, *aintrocat = 0;
@@ -292,54 +278,23 @@ XcomResourcePack::XcomResourcePack(Ruleset *rules) : ResourcePack()
292278

293279
// Try the preferred format first, otherwise use the default priority
294280
MusicFormat priority[] = {Options::preferredMusic, MUSIC_FLAC, MUSIC_OGG, MUSIC_MP3, MUSIC_MOD, MUSIC_WAV, MUSIC_ADLIB, MUSIC_MIDI};
295-
296-
for (size_t i = 0; i < sizeof(mus)/sizeof(mus[0]); ++i)
281+
for (std::map<std::string, RuleMusic *>::const_iterator i = musics.begin(); i != musics.end(); ++i)
297282
{
298283
Music *music = 0;
299284
for (size_t j = 0; j < sizeof(priority)/sizeof(priority[0]) && music == 0; ++j)
300285
{
301-
music = loadMusic(priority[j], mus[i], tracks[i], tracks_normalize[i], adlibcat, aintrocat, gmcat);
286+
music = loadMusic(priority[j], (*i).first, (*i).second->getCatPos(), (*i).second->getNormalization(), adlibcat, aintrocat, gmcat);
302287
}
303-
if (!music)
288+
if (music)
304289
{
305-
throw Exception(mus[i] + " not found");
290+
_musics[(*i).first] = music;
306291
}
307-
_musics[mus[i]] = music;
292+
308293
}
294+
309295
delete gmcat;
310296
delete adlibcat;
311297
delete aintrocat;
312-
313-
// Ok, now try to load the optional musics
314-
std::string musOptional[] = {"GMGEO3",
315-
"GMGEO4",
316-
"GMGEO5",
317-
"GMGEO6",
318-
"GMGEO7",
319-
"GMGEO8",
320-
"GMGEO9",
321-
"GMTACTIC1",
322-
"GMTACTIC2",
323-
"GMTACTIC3",
324-
"GMTACTIC4",
325-
"GMTACTIC5",
326-
"GMTACTIC6",
327-
"GMTACTIC7",
328-
"GMTACTIC8",
329-
"GMTACTIC9"};
330-
331-
for (size_t i = 0; i < sizeof(musOptional)/sizeof(musOptional[0]); ++i)
332-
{
333-
Music *music = 0;
334-
for (size_t j = 0; j < sizeof(priority) / sizeof(priority[0]) && music == 0; ++j)
335-
{
336-
music = loadMusic(priority[j], musOptional[i], 0, 0, 0, 0, 0);
337-
}
338-
if (music)
339-
{
340-
_musics[musOptional[i]] = music;
341-
}
342-
}
343298
#endif
344299

345300
if (rules->getSoundDefinitions()->empty())
@@ -1017,15 +972,23 @@ Music *XcomResourcePack::loadMusic(MusicFormat fmt, const std::string &file, int
1017972
else if (aintrocat)
1018973
{
1019974
track -= adlibcat->getAmount();
1020-
music->load(aintrocat->load(track, true), aintrocat->getObjectSize(track));
975+
if (track < aintrocat->getAmount())
976+
{
977+
music->load(aintrocat->load(track, true), aintrocat->getObjectSize(track));
978+
}
979+
else
980+
{
981+
delete music;
982+
music = 0;
983+
}
1021984
}
1022985
}
1023986
}
1024987
// Try MIDI music
1025988
else if (fmt == MUSIC_MIDI)
1026989
{
1027990
// DOS MIDI
1028-
if (gmcat)
991+
if (gmcat && track < gmcat->getAmount())
1029992
{
1030993
music = gmcat->loadMIDI(track);
1031994
}

‎src/Ruleset/RuleMusic.cpp‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2010-2014 OpenXcom Developers.
3+
*
4+
* This file is part of OpenXcom.
5+
*
6+
* OpenXcom is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* OpenXcom is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "RuleMusic.h"
21+
#include <climits>
22+
23+
namespace OpenXcom
24+
{
25+
/**
26+
* initialize catpos as int_max to prevent trying to load files from cats that don't exist,
27+
* but allow for optional music to be listed regardless for loading .ogg or .mp3 versions
28+
* of said files, should they be present.
29+
* default normalization value is 0.76, this only applies to the adlib mixer as far as i know.
30+
* also, 0.76 is roughly optimal for all the TFTD tracks.
31+
*/
32+
RuleMusic::RuleMusic(const std::string id) : _catPos(INT_MAX), _normalization(0.76)
33+
{
34+
}
35+
36+
RuleMusic::~RuleMusic()
37+
{
38+
}
39+
40+
/**
41+
* Loads info about the music track.
42+
* @param node yaml node to read from.
43+
*/
44+
void RuleMusic::load(const YAML::Node& node)
45+
{
46+
_catPos = node["catPos"].as<int>(_catPos);
47+
_normalization = node["normalization"].as<float>(_normalization);
48+
}
49+
50+
/**
51+
* Gets the track's index in the catalog file.
52+
* @return the track's index in the file.
53+
*/
54+
int RuleMusic::getCatPos()
55+
{
56+
return _catPos;
57+
}
58+
59+
/**
60+
* Gets the track's normalization level.
61+
* @return the track's normalization value.
62+
*/
63+
float RuleMusic::getNormalization()
64+
{
65+
return _normalization;
66+
}
67+
68+
}

‎src/Ruleset/RuleMusic.h‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2010-2014 OpenXcom Developers.
3+
*
4+
* This file is part of OpenXcom.
5+
*
6+
* OpenXcom is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* OpenXcom is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef OPENXCOM_RULEMUSIC_H
21+
#define OPENXCOM_RULEMUSIC_H
22+
23+
#include <string>
24+
#include <yaml-cpp/yaml.h>
25+
26+
namespace OpenXcom
27+
{
28+
29+
class RuleMusic
30+
{
31+
private:
32+
int _catPos;
33+
float _normalization;
34+
public:
35+
RuleMusic(const std::string id);
36+
~RuleMusic();
37+
void load(const YAML::Node& node);
38+
int getCatPos();
39+
float getNormalization();
40+
};
41+
42+
}
43+
44+
#endif

‎src/Ruleset/Ruleset.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "ExtraStrings.h"
4949
#include "RuleInterface.h"
5050
#include "SoundDefinition.h"
51+
#include "RuleMusic.h"
5152
#include "../Savegame/SavedGame.h"
5253
#include "../Savegame/Region.h"
5354
#include "../Savegame/Base.h"
@@ -215,6 +216,10 @@ Ruleset::~Ruleset()
215216
{
216217
delete i->second;
217218
}
219+
for (std::map<std::string, RuleMusic *>::const_iterator i = _musics.begin(); i != _musics.end(); ++i)
220+
{
221+
delete i->second;
222+
}
218223
}
219224

220225
/**
@@ -650,6 +655,14 @@ void Ruleset::loadFile(const std::string &filename)
650655
rule->load(*i);
651656
}
652657
}
658+
for (YAML::const_iterator i = doc["musics"].begin(); i != doc["musics"].end(); ++i)
659+
{
660+
RuleMusic *rule = loadRule(*i, &_musics);
661+
if (rule != 0)
662+
{
663+
rule->load(*i);
664+
}
665+
}
653666
}
654667

655668
/**
@@ -1607,4 +1620,9 @@ const std::map<std::string, RuleVideo *> *Ruleset::getVideos() const
16071620
return &_videos;
16081621
}
16091622

1623+
const std::map<std::string, RuleMusic *> *Ruleset::getMusic() const
1624+
{
1625+
return &_musics;
1626+
}
1627+
16101628
}

0 commit comments

Comments
 (0)