1 /* Pushover
2  *
3  * Pushover is the legal property of its developers, whose
4  * names are listed in the COPYRIGHT file, which is included
5  * within the source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11 
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 General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
20  */
21 
22 #include "soundsys.h"
23 
24 #include <SDL.h>
25 
26 #include <iostream>
27 
soundSystem_c(void)28 soundSystem_c::soundSystem_c(void)
29 {
30   useSound = false;
31   playSoundSwitch = true;
32   playMusicSwitch = true;
33   music = 0;
34 }
35 
~soundSystem_c(void)36 soundSystem_c::~soundSystem_c(void)
37 {
38   closeSound();
39 
40   for (unsigned int t = 0; t < sounds.size(); t++)
41     if (sounds[t].sound)
42       Mix_FreeChunk(sounds[t].sound);
43 
44 }
45 
addsound(const std::string & fname,int vol)46 void soundSystem_c::addsound(const std::string & fname, int vol)
47 {
48   struct soundDat d;
49 
50   SDL_RWops *file = SDL_RWFromFile(fname.c_str(), "rb");
51   d.sound = Mix_LoadWAV_RW(file, 1);
52 
53   if (d.sound) {
54 
55     d.channel = -1;
56     d.volume = vol;
57 
58     sounds.push_back(d);
59   }
60   else
61   {
62     std::cout << "Can't load sound from file: " << fname << std::endl;
63   }
64 }
65 
startSound(unsigned int snd)66 void soundSystem_c::startSound(unsigned int snd)
67 {
68   if (!useSound) return;
69   if (!playSoundSwitch) return;
70 
71   if (snd >= 0 && snd < sounds.size())
72   {
73     sounds[snd].channel = Mix_PlayChannel(-1, sounds[snd].sound, 0);
74     Mix_Volume(sounds[snd].channel, sounds[snd].volume);
75   }
76 }
77 
instance(void)78 soundSystem_c * soundSystem_c::instance(void) {
79 
80   if (!inst)
81     inst = new soundSystem_c();
82 
83   return inst;
84 }
85 
86 class soundSystem_c *soundSystem_c::inst = 0;
87 
openSound(const std::string & base)88 void soundSystem_c::openSound(const std::string & base) {
89 
90   if(SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) {
91     return;
92   }
93 
94   if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) < 0) {
95     SDL_QuitSubSystem(SDL_INIT_AUDIO);
96     return;
97   }
98 
99   if (sounds.size() == 0)
100   {
101 
102     addsound(base+"/data/01_StandardFalling.ogg", MIX_MAX_VOLUME);     // SE_STANDARD,
103     addsound(base+"/data/02_StopperHit.ogg", MIX_MAX_VOLUME);          // SE_STOPPER,
104     addsound(base+"/data/03_Splitter.ogg", MIX_MAX_VOLUME);            // SE_SPLITTER,
105     addsound(base+"/data/04_Exploder.ogg", MIX_MAX_VOLUME);            // SE_EXPLODER,
106     addsound(base+"/data/05_Delay.ogg", MIX_MAX_VOLUME);               // SE_DELAY,
107     addsound(base+"/data/06_TumblerFalling.ogg", MIX_MAX_VOLUME);      // SE_TUMBLER,
108     addsound(base+"/data/07_BridgerFalling.ogg", MIX_MAX_VOLUME);      // SE_BRIDGER,
109     addsound(base+"/data/06_TumblerFalling.ogg", MIX_MAX_VOLUME);      // SE_VANISH,
110     addsound(base+"/data/09_TriggerFalling.ogg", MIX_MAX_VOLUME);      // SE_TRIGGER,
111     addsound(base+"/data/0A_Ascender.ogg", MIX_MAX_VOLUME);            // SE_ASCENDER,
112     addsound(base+"/data/0B_Falling.ogg", MIX_MAX_VOLUME);             // SE_ANT_FALLING,
113     addsound(base+"/data/0C_Landing.ogg", MIX_MAX_VOLUME);             // SE_ANT_LANDING,
114     addsound(base+"/data/0D_PickUpDomino.ogg", MIX_MAX_VOLUME);        // SE_PICK_UP_DOMINO,
115     addsound(base+"/data/0E_Tapping.ogg", MIX_MAX_VOLUME);             // SE_NU_WHAT,
116     addsound(base+"/data/0F_Schrugging.ogg", MIX_MAX_VOLUME);          // SE_SHRUGGING,
117     addsound(base+"/data/10_DoorClose.ogg", MIX_MAX_VOLUME);           // SE_DOOR_CLOSE,
118     addsound(base+"/data/11_DoorOpen.ogg", MIX_MAX_VOLUME);            // SE_DOOR_OPEN,
119     addsound(base+"/data/13_Victory.ogg", MIX_MAX_VOLUME);             // SE_VICTORY,
120   }
121 
122   useSound = true;
123 }
124 
closeSound(void)125 void soundSystem_c::closeSound(void) {
126 
127   if (!useSound) return;
128   useSound = false;
129 
130   while (Mix_Playing(-1)) SDL_Delay(100);
131 
132   Mix_CloseAudio();
133   SDL_QuitSubSystem(SDL_INIT_AUDIO);
134 }
135 
playMusic(const std::string & fname)136 void soundSystem_c::playMusic(const std::string & fname) {
137 
138   if (!useSound) return;
139 
140   static std::string currentlyPlaying = "";
141 
142   if (fname == currentlyPlaying) return;
143 
144   if (music)
145   {
146     Mix_FadeOutMusic(100);
147     SDL_Delay(130);
148     Mix_HaltMusic();
149 
150     Mix_FreeMusic(music);
151 
152     music = 0;
153   }
154 
155   if (!playMusicSwitch)
156   {
157     currentlyPlaying = fname;
158     return;
159   }
160 
161   // when no name was given, set the name to the currently played music
162   if (fname == "")
163   {
164     music = Mix_LoadMUS(currentlyPlaying.c_str());
165 
166     if (music)
167     {
168       Mix_PlayMusic(music, -1);
169     }
170     else
171     {
172       currentlyPlaying = "";
173     }
174   }
175   else
176   {
177     music = Mix_LoadMUS(fname.c_str());
178 
179     if (music)
180     {
181       Mix_PlayMusic(music, -1);
182       currentlyPlaying = fname;
183     }
184     else
185     {
186       currentlyPlaying = "";
187     }
188   }
189 }
190 
191