1 //
2 // Cross-platform free Puyo-Puyo clone.
3 // Copyright (C) 2006, 2007 Emma's Software
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 #if defined (HAVE_CONFIG_H)
20 #include <config.h>
21 #endif // HAVE_CONFIG_H
22 #include <stdexcept>
23 #include "Sound.h"
24 #include "System.h"
25 
26 using namespace Amoebax;
27 
28 ///
29 /// \brief Sound's default constructor.
30 ///
Sound(void)31 Sound::Sound (void):
32     m_Channel (-1),
33     m_SDLSound (0)
34 {
35 }
36 
37 ///
38 /// \brief Sound's destructor.
39 ///
~Sound(void)40 Sound::~Sound (void)
41 {
42     stop ();
43     Mix_FreeChunk (m_SDLSound);
44 }
45 
46 ///
47 /// \brief Loads a sound from a file.
48 ///
49 /// \param fileName The sound file to load.
50 /// \return A new Sound object with the sound file loaded.
51 ///
52 Sound *
fromFile(const std::string & fileName)53 Sound::fromFile (const std::string &fileName)
54 {
55     Sound *newSound = new Sound;
56     if ( System::getInstance ().isSoundEnabled () )
57     {
58         newSound->m_SDLSound = Mix_LoadWAV (fileName.c_str ());
59         if ( 0 == newSound->m_SDLSound )
60         {
61             delete newSound;
62             throw std::runtime_error (Mix_GetError ());
63         }
64     }
65     return newSound;
66 }
67 
68 ///
69 /// \brief Plays the sound.
70 ///
71 /// \param times The number of times to play the sound. \a -1 means to
72 ///              play the sound forever. Passing \a 1 plays the sound twice.
73 ///
74 void
play(int times)75 Sound::play (int times)
76 {
77     // If the sound is NULL, then the sound subsystem wasn't initialized,
78     // just ignore and continue.
79     if ( 0 != m_SDLSound )
80     {
81         // Ignore errors. If the sound can't be player is bad, but the
82         // game can continue...
83         m_Channel = Mix_PlayChannel (-1, m_SDLSound, times);
84     }
85 }
86 
87 ///
88 /// \brief Stops the sound.
89 ///
90 void
stop(void)91 Sound::stop (void)
92 {
93     // TODO
94 }
95