-
Notifications
You must be signed in to change notification settings - Fork 1
/
gamesound.cpp
190 lines (155 loc) · 3.9 KB
/
gamesound.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "gamesound.h"
#include "gamewidget.h"
#include <QDebug>
GameSound* sndEngine = 0;
GameSound::GameSound()
{
// load sounds
QString sndpath = GameWidget::getResourcePath() + "sounds/";
loadSound(sndpath + "disappear.wav");
loadSound(sndpath + "smallhammer.wav");
loadSound(sndpath + "unblock.wav");
loadSound(sndpath + "hammer.wav");
loadSound(sndpath + "bighammer.wav");
loadSound(sndpath + "bomb.wav");
loadSound(sndpath + "row.wav");
loadSound(sndpath + "randomkill.wav");
loadSound(sndpath + "mixer.wav");
loadSound(sndpath + "twin.wav");
loadSound(sndpath + "clock.wav");
loadSound(sndpath + "bonusend.wav");
loadSound(sndpath + "newitem.wav");
loadSound(sndpath + "target.wav");
loadSound(sndpath + "levelstart.wav");
loadSound(sndpath + "levelfail.wav");
loadSound(sndpath + "levelwon.wav");
loadSound(sndpath + "beep.wav");
loadSound(sndpath + "bonus.wav");
loadSound(sndpath + "newtool.wav");
// music
music = 0;
musicEnabled = false;
musicPlaying = false;
myTimer = new QTimer(this);
myTimer->setInterval(1000);
connect(myTimer, SIGNAL(timeout()), this, SLOT(checkPlayMusic()));
myTimer->stop();
// setup volume
setChannelVolume(MIX_MAX_VOLUME);
setMusicVolume(MIX_MAX_VOLUME/4);
}
GameSound::~GameSound()
{
Mix_HaltMusic();
for (int i = 0; i < m_sounds.count(); i++)
{
Mix_FreeChunk(m_sounds.at(i));
}
Mix_FreeMusic(music);
}
Mix_Chunk* GameSound::loadSound(const QString &filename)
{
Mix_Chunk *sound = Mix_LoadWAV(filename.toLocal8Bit().constData());
if(sound == NULL) {
qDebug() << filename << ": sound not loaded: " << Mix_GetError();
//fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
}
m_sounds.append(sound);
return sound;
}
void GameSound::playSound(int index, int /*loops*/)
{
if (index >= 0 && index < m_sounds.count())
{
Mix_Chunk *chunk = m_sounds.at(index);
if (!chunk)
return;
Mix_VolumeChunk(chunk, channel_vol);
int channel = Mix_PlayChannel(-1, chunk, 0);
if(channel == -1)
{
qDebug() << "Unable to play sound file: " << Mix_GetError();
//fprintf(stderr, "Unable to play sound file: %s\n", Mix_GetError());
}
else
Mix_Volume(channel, channel_vol);
}
}
void GameSound::stopSound(int /*index*/)
{
// if (index >= 0 && index < m_sounds.count())
// m_sounds.at(index)->stop();
}
void GameSound::stopAllSounds()
{
// for (int i = 0; i < m_sounds.count(); i++)
// m_sounds.at(i)->stop();
}
void GameSound::setChannelVolume(int val, int ch)
{
// if (ch < 0)
// for (int i = 0; i < 8; i++)
// Mix_Volume(i, val);
// else
Mix_Volume(ch, val);
//
// if (ch==-1)
channel_vol = val;
}
void GameSound::loadMusic(const QString &filename)
{
if (music)
Mix_FreeMusic(music);
music = Mix_LoadMUS(filename.toLocal8Bit().constData());
if(music == NULL) {
qDebug() << filename << ": music not loaded: " << Mix_GetError();
//fprintf(stderr, "Unable to load music file: %s\n", Mix_GetError());
}
}
void GameSound::playMusic()
{
musicPlaying = true;
if (!musicEnabled)
return;
int channel = Mix_PlayMusic(music, 0);
if(channel == -1)
{
qDebug() << "Unable to play music file: " << Mix_GetError();
//fprintf(stderr, "Unable to play music file: %s\n", Mix_GetError());
}
Mix_VolumeMusic(music_vol);
myTimer->start();
}
void GameSound::stopMusic()
{
Mix_HaltMusic();
musicPlaying = false;
myTimer->stop();
}
void GameSound::enableMusic(bool on)
{
musicEnabled = on;
if (musicEnabled)
{
if (musicPlaying)
playMusic();
}
else
{
Mix_HaltMusic();
myTimer->stop();
// do NOT set musicPlaying to false!
}
}
void GameSound::checkPlayMusic()
{
// just rewind
if (!Mix_PlayingMusic())
//playMusic();
emit musicFinished();
}
void GameSound::setMusicVolume(int val)
{
music_vol = val;
Mix_VolumeMusic(val);
}