GameKit
0.0.1a
C++ gamedev tools
source
audio
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
19
int
Sound::s_lastUsedChannel
= -1;
20
21
Sound::Sound
(
const
std::string &filename) {
22
openFromFile
(filename);
23
}
24
25
void
Sound::openFromFile
(
const
std::string &filename) {
26
m_channel
= ++
s_lastUsedChannel
;
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
gk::Timer::start
void start()
Start the timer.
Definition:
Timer.cpp:31
gk
Definition:
AudioPlayer.hpp:21
gk::Sound::openFromFile
void openFromFile(const std::string &filename)
Definition:
Sound.cpp:25
gk::Sound::m_sfx
Mix_ChunkPtr m_sfx
Definition:
Sound.hpp:44
u16
unsigned short u16
Definition:
IntTypes.hpp:22
gk::Sound::m_volume
int m_volume
Definition:
Sound.hpp:49
gk::Sound::m_channel
int m_channel
Definition:
Sound.hpp:48
EXCEPTION
#define EXCEPTION(args...)
Definition:
Exception.hpp:22
gk::Timer::reset
void reset()
Reset the timer.
Definition:
Timer.cpp:38
Sound.hpp
gk::Sound::Sound
Sound()=default
gk::Timer::time
u32 time() const
Get time.
Definition:
Timer.cpp:44
gk::Sound::play
void play()
Definition:
Sound.cpp:36
gk::Sound::m_timer
gk::Timer m_timer
Definition:
Sound.hpp:46
gk::Sound::repeat
void repeat(u16 delay)
Definition:
Sound.cpp:41
Exception.hpp
gk::Sound::s_lastUsedChannel
static int s_lastUsedChannel
Definition:
Sound.hpp:40
Generated by
1.8.12