Commit bbf44612 authored by Ian Wadham's avatar Ian Wadham

Fix the relative volumes of the sounds, the overlapping of rapid sequences of...

Fix the relative volumes of the sounds, the overlapping of rapid sequences of gold and dig sounds and the cutoff of all sounds when the game is paused.

svn path=/trunk/KDE/kdegames/kgoldrunner/; revision=1234202
parent dc44c998
......@@ -1175,6 +1175,12 @@ void KGrGame::freeze (const bool userAction, const bool on_off)
kDebug() << "PAUSE:" << type << on_off;
kDebug() << "gameFrozen" << gameFrozen << "programFreeze" << programFreeze;
#ifdef ENABLE_SOUND_SUPPORT
if (on_off && effects) { // If pausing and sounds are loaded, cut
effects->stopAllSounds(); // off all sounds that are in progress.
}
#endif
if (! userAction) {
// The program needs to freeze the game during a message, dialog, etc.
if (on_off) {
......@@ -2147,6 +2153,7 @@ bool KGrGame::loadRecording (const QString & dir, const QString & prefix,
void KGrGame::loadSounds()
{
#ifdef ENABLE_SOUND_SUPPORT
const qreal volumes [NumSounds] = {0.6, 0.3, 0.3, 0.6, 0.6, 1.8, 1.0, 1.0, 1.0, 1.0};
#ifdef KGOLDRUNNER_USE_OPENAL
effects = new KGrSounds();
#else
......@@ -2174,6 +2181,17 @@ void KGrGame::loadSounds()
"themes/default/gameover.wav"));
fx[VictorySound] = effects->loadSound (KStandardDirs::locate ("appdata",
"themes/default/victory.wav"));
#ifdef KGOLDRUNNER_USE_OPENAL
// Gold and dig sounds are timed and are allowed to play for at least one
// second, so that rapid sequences of those sounds are heard as overlapping.
effects->setTimedSound (fx[GoldSound]);
effects->setTimedSound (fx[DigSound]);
// Adjust the relative volumes of sounds to improve the overall balance.
for (int i = 0; i < NumSounds; i++) {
effects->setVolume (fx [i], volumes [i]);
}
#endif
#endif
}
......
......@@ -19,12 +19,12 @@
#include "kgrsounds.h"
#include <KDebug>
#include <stdio.h>
KGrSounds::KGrSounds() :
QObject(),
sounds()
{
t.start();
}
KGrSounds::~KGrSounds()
......@@ -35,9 +35,15 @@ int KGrSounds::loadSound (const QString &fileName)
{
kDebug() << "Loading sound" << fileName;
sounds << (new Tagaro::Sound (fileName));
startTime << 0;
return sounds.count() - 1;
}
void KGrSounds::setTimedSound (int i)
{
startTime[i] = 1;
}
void KGrSounds::stopAllSounds()
{
for (int i = 0; i < sounds.count(); i++) {
......@@ -55,9 +61,23 @@ int KGrSounds::play (int effect)
{
if (muted) return -1;
sounds[effect]->stop(); // Delete previous OpenAL/Tagaro instances.
// Delete all previously playing instances of this sound, but allow gold and
// dig sounds to play for > 1 sec, so that rapid sequences of digging or
// gold collection will have properly overlapping sounds.
int started = startTime[effect];
bool timedSound = (started != 0);
int current = timedSound ? t.elapsed() : 0;
if ((! timedSound) || ((current - started) > 1000)) {
sounds[effect]->stop();
}
sounds[effect]->start();
if (timedSound) {
startTime[effect] = current;
}
return effect;
}
......@@ -76,4 +96,9 @@ void KGrSounds::setMuted (bool mute)
}
}
void KGrSounds::setVolume (int effect, qreal volume)
{
sounds[effect]->setVolume (volume);
}
#include "kgrsounds.moc"
......@@ -21,6 +21,7 @@
#include <QObject>
#include <QVector>
#include <QTime>
#include "TagaroAudio/sound.h"
......@@ -53,6 +54,11 @@ public:
*/
int loadSound (const QString &fileName);
/**
* Set up a sound to have its latest start-time recorded.
*/
void setTimedSound (int i);
/**
* Stop sound and discard the loaded sound effects.
*/
......@@ -77,9 +83,17 @@ public:
void setMuted (bool mute);
/**
* Change the volume of one type of sound (e.g. footstep) by a given factor.
* \param volume 0.0 for mute, > 1.0 to increase, < 1.0 to decrease.
*/
void setVolume (int effect, qreal volume);
private:
QVector<Tagaro::Sound *> sounds;
bool muted;
QVector<int> startTime; // Start times of timed sounds, else 0.
bool muted;
QTime t;
};
#endif // KGRSOUNDS_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment