kgrsounds.cpp 2.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/****************************************************************************
 *    Copyright 2011  Ian Wadham <iandw.au at gmail dot com>
 *    Copyright 2007 Luciano Montanaro <mikelima@cirulla.net>               *
 *                                                                          *
 *    This program is free software; you can redistribute it and/or         *
 *    modify it under the terms of the GNU General Public License as        *
 *    published by the Free Software Foundation; either version 2 of        *
 *    the License, or (at your option) any later version.                   *
 *                                                                          *
 *    This program is distributed in the hope that it will be useful,       *
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *    GNU General Public License for more details.                          *
 *                                                                          *
 *    You should have received a copy of the GNU General Public License     *
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ****************************************************************************/

#include "kgrsounds.h"

Laurent Montel's avatar
Laurent Montel committed
21
#include "kgoldrunner_debug.h"
22 23 24 25 26

KGrSounds::KGrSounds() : 
    QObject(),
    sounds()
{
27
    t.start();
28 29 30 31 32 33 34 35
}

KGrSounds::~KGrSounds()
{
}

int KGrSounds::loadSound (const QString &fileName)
{
Laurent Montel's avatar
Laurent Montel committed
36
    //qCDebug(KGOLDRUNNER_LOG) << "Loading sound" << fileName;
37
    sounds << (new KgSound (fileName));
38
    startTime << 0;
39 40 41
    return sounds.count() - 1;
}

42 43 44 45 46
void KGrSounds::setTimedSound (int i)
{
    startTime[i] = 1;
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
void KGrSounds::stopAllSounds()
{
    for (int i = 0; i < sounds.count(); i++) {
	sounds[i]->stop();
    }
}

void KGrSounds::reset()
{
    stopAllSounds();
    sounds.clear();
}

int KGrSounds::play (int effect)
{
    if (muted) return -1;

64 65 66 67 68 69 70 71 72 73 74
    // 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();
    }
75 76

    sounds[effect]->start();
77 78 79 80

    if (timedSound) {
        startTime[effect] = current;
    }
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    return effect;
}

void KGrSounds::stop (int effect)
{
    if (muted) return;

    sounds[effect]->stop();
}

void KGrSounds::setMuted (bool mute)
{
    muted = mute;
    if (mute) {
	stopAllSounds();
    }
}

99 100 101 102 103
void KGrSounds::setVolume (int effect, qreal volume)
{
    sounds[effect]->setVolume (volume);
}

Laurent Montel's avatar
port++  
Laurent Montel committed
104