1 /******************************************************************************
2 * Warmux is a convivial mass murder game.
3 * Copyright (C) 2001-2011 Warmux Team.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 ******************************************************************************
19 * Sound Sample : stupid class that avoids to take care of channel assigned by
20 * SDL
21 *****************************************************************************/
22
23 #include "sound/sound_sample.h"
24 #include "sound/jukebox.h"
25 #include <SDL_mixer.h>
26
27 std::map<int, SoundSample*> SoundSample::sound_samples_channel;
28
ChannelFinished(int channel)29 void SoundSample::ChannelFinished(int channel)
30 {
31 std::map<int, SoundSample*>::iterator it=sound_samples_channel.find(channel);
32
33 if (it != sound_samples_channel.end()) {
34 SoundSample* s = it->second;
35 s->channel = -1;
36 sound_samples_channel.erase(it);
37 }
38 }
39
~SoundSample()40 SoundSample::~SoundSample()
41 {
42 if (channel == -1)
43 return;
44
45 // removing sample from the table
46 std::map<int, SoundSample*>::iterator it=sound_samples_channel.find(channel);
47
48 if (it != sound_samples_channel.end()) {
49 SoundSample* s = it->second;
50 ASSERT(s == this);
51 sound_samples_channel.erase(it);
52 }
53 }
54
Play(const std::string & category,const std::string & sample,const int loop)55 bool SoundSample::Play(const std::string& category,
56 const std::string& sample,
57 const int loop)
58 {
59 if (!IsPlaying()) {
60 channel = JukeBox::GetInstance()->Play(category, sample, loop);
61 sound_samples_channel.insert(std::make_pair(channel, this));
62 return true;
63 }
64 return false;
65 }
66
Stop()67 void SoundSample::Stop()
68 {
69 if (IsPlaying())
70 JukeBox::GetInstance()->Stop(channel);
71 channel = -1;
72 }
73
IsPlaying()74 bool SoundSample::IsPlaying()
75 {
76 if (channel == -1)
77 return false;
78
79 return Mix_Playing(channel)>0;
80 }
81