GameKit  0.0.1a
C++ gamedev tools
Sound.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Sound.cpp
5  *
6  * Description:
7  *
8  * Created: 15/09/2014 23:03:35
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include "gk/audio/Sound.hpp"
15 #include "gk/core/Exception.hpp"
16 
17 namespace gk {
18 
20 
21 Sound::Sound(const std::string &filename) {
22  openFromFile(filename);
23 }
24 
25 void Sound::openFromFile(const std::string &filename) {
27 
28  m_sfx.reset(Mix_LoadWAV(filename.c_str()));
29  if(!m_sfx) {
30  throw EXCEPTION("Unable to load sound effect:", filename, ":", Mix_GetError());
31  }
32 
33  m_timer.start();
34 }
35 
36 void Sound::play() {
37  Mix_Volume(m_channel, m_volume);
38  Mix_PlayChannel(m_channel, m_sfx.get(), 0);
39 }
40 
41 void Sound::repeat(u16 delay) {
42  if(m_timer.time() > delay) {
43  play();
44 
45  m_timer.reset();
46  m_timer.start();
47  }
48 }
49 
50 } // namespace gk
51 
void start()
Start the timer.
Definition: Timer.cpp:31
void openFromFile(const std::string &filename)
Definition: Sound.cpp:25
Mix_ChunkPtr m_sfx
Definition: Sound.hpp:44
unsigned short u16
Definition: IntTypes.hpp:22
int m_volume
Definition: Sound.hpp:49
int m_channel
Definition: Sound.hpp:48
#define EXCEPTION(args...)
Definition: Exception.hpp:22
void reset()
Reset the timer.
Definition: Timer.cpp:38
Sound()=default
u32 time() const
Get time.
Definition: Timer.cpp:44
void play()
Definition: Sound.cpp:36
gk::Timer m_timer
Definition: Sound.hpp:46
void repeat(u16 delay)
Definition: Sound.cpp:41
static int s_lastUsedChannel
Definition: Sound.hpp:40