1 /***************************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011-2013 PariahSoft LLC **
5 ***************************************/
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 #include <Gosu/Math.hpp>
29 #include "client-conf.h"
33 #include "python-bindings-template.cpp"
35 static Music::SongRef
genSong(const std::string
& name
)
37 std::unique_ptr
<Gosu::Buffer
> buffer(Reader::readBuffer(name
));
39 return Music::SongRef();
40 return Music::SongRef(new Gosu::Song(buffer
->frontReader()));
45 Music::SongRef
Music::getSong(const std::string
& name
)
47 if (!conf
.audioEnabled
)
49 return songs
.lifetimeRequest(name
);
54 static const char* stateStr(MUSIC_STATE state)
60 return "PLAYING_INTRO";
62 return "PLAYING_LOOP";
64 return "CHANGED_INTRO";
66 return "CHANGED_LOOP";
73 void Music::setState(MUSIC_STATE state_
)
75 // printf("State changed from %s to %s.\n", stateStr(this->state), stateStr(state));
79 void Music::playIntro()
81 if (musicInst
&& musicInst
->playing())
84 introMusic
->play(false);
85 introMusic
->changeVolume(conf
.musicVolume
/ 100.0);
86 musicInst
= introMusic
;
87 setState(PLAYING_INTRO
);
90 void Music::playLoop()
92 if (musicInst
&& musicInst
->playing())
95 loopMusic
->play(true);
96 loopMusic
->changeVolume(conf
.musicVolume
/ 100.0);
97 musicInst
= loopMusic
;
98 setState(PLAYING_LOOP
);
103 Music::Music() : songs(genSong
), paused(false), state(NOT_PLAYING
)
109 if (musicInst
&& musicInst
->playing())
113 std::string
Music::getIntro()
118 std::string
Music::getLoop()
123 void Music::setIntro(const std::string
& filename
)
125 if (newIntro
== filename
)
132 setState(CHANGED_INTRO
);
137 // Optimize XXX: Don't load until played.
138 introMusic
= filename
.size() ? getSong(filename
) : SongRef();
141 void Music::setLoop(const std::string
& filename
)
143 if (newLoop
== filename
)
150 setState(CHANGED_LOOP
);
155 // Optimize XXX: Don't load until played.
156 loopMusic
= filename
.size() ? getSong(filename
) : SongRef();
159 int Music::getVolume()
161 return conf
.musicVolume
;
164 void Music::setVolume(int level
)
166 if (0 < level
|| level
> 100) {
167 Log::info("Music", "volume can only be set between 0 and 100");
168 level
= Gosu::clamp(level
, 0, 100);
170 conf
.musicVolume
= level
;
172 musicInst
->changeVolume(level
);
175 bool Music::isPaused()
180 void Music::setPaused(bool p
)
208 if (musicInst
&& musicInst
->playing())
212 if (!musicInst
->playing()) {
213 if (newLoop
.size() && loopMusic
)
216 setState(NOT_PLAYING
);
222 if (newIntro
.size() && introMusic
)
224 else if (newLoop
.size() && newLoop
!= curLoop
)
225 setState(CHANGED_LOOP
);
226 else if (newLoop
.size())
227 setState(PLAYING_LOOP
);
229 setState(NOT_PLAYING
);
232 if (newIntro
.size() && loopMusic
)
234 else if (newLoop
.size() && loopMusic
)
237 setState(NOT_PLAYING
);
245 using namespace boost::python
;
247 class_
<Music
>("MusicManager", no_init
)
248 .add_property("intro", &Music::getIntro
, &Music::setIntro
)
249 .add_property("loop", &Music::getLoop
, &Music::setLoop
)
250 .add_property("volume", &Music::getVolume
, &Music::setVolume
)
251 .add_property("paused", &Music::isPaused
, &Music::setPaused
)
252 .def("stop", &Music::stop
)