File indexing completed on 2024-04-21 04:02:24

0001 /*
0002     SPDX-FileCopyrightText: 2011 Ian Wadham <iandw.au at gmail dot com>
0003     SPDX-FileCopyrightText: 2007 Luciano Montanaro <mikelima@cirulla.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "kgrsounds.h"
0009 
0010 #include "kgoldrunner_debug.h"
0011 
0012 KGrSounds::KGrSounds() : 
0013     QObject(),
0014     sounds()
0015 {
0016     t.start();
0017 }
0018 
0019 KGrSounds::~KGrSounds()
0020 {
0021     qDeleteAll(sounds);
0022 }
0023 
0024 int KGrSounds::loadSound (const QString &fileName)
0025 {
0026     //qCDebug(KGOLDRUNNER_LOG) << "Loading sound" << fileName;
0027     sounds << (new KGameSound (fileName));
0028     startTime << 0;
0029     return sounds.count() - 1;
0030 }
0031 
0032 void KGrSounds::setTimedSound (int i)
0033 {
0034     startTime[i] = 1;
0035 }
0036 
0037 void KGrSounds::stopAllSounds()
0038 {
0039     for (int i = 0; i < sounds.count(); i++) {
0040     sounds[i]->stop();
0041     }
0042 }
0043 
0044 void KGrSounds::reset()
0045 {
0046     stopAllSounds();
0047     sounds.clear();
0048 }
0049 
0050 int KGrSounds::play (int effect)
0051 {
0052     if (muted) return -1;
0053 
0054     // Delete all previously playing instances of this sound, but allow gold and
0055     // dig sounds to play for > 1 sec, so that rapid sequences of digging or
0056     // gold collection will have properly overlapping sounds.
0057 
0058     int  started    = startTime[effect];
0059     bool timedSound = (started != 0);
0060     int  current    = timedSound ? t.elapsed() : 0;
0061 
0062     if ((! timedSound) || ((current - started) > 1000)) {
0063         sounds[effect]->stop();
0064     }
0065 
0066     sounds[effect]->start();
0067 
0068     if (timedSound) {
0069         startTime[effect] = current;
0070     }
0071     return effect;
0072 }
0073 
0074 void KGrSounds::stop (int effect)
0075 {
0076     if (muted) return;
0077 
0078     sounds[effect]->stop();
0079 }
0080 
0081 void KGrSounds::setMuted (bool mute)
0082 {
0083     muted = mute;
0084     if (mute) {
0085     stopAllSounds();
0086     }
0087 }
0088 
0089 void KGrSounds::setVolume (int effect, qreal volume)
0090 {
0091     sounds[effect]->setVolume (volume);
0092 }
0093 
0094 #include "moc_kgrsounds.cpp"