File indexing completed on 2024-04-14 03:59:25

0001 /*******************************************************************************
0002  *   KBlocks, a falling blocks game by KDE                                     *
0003  *   SPDX-FileCopyrightText: 2009-2021 Mauricio Piacentini <mauricio@tabuleiro.com>      *
0004  *                           Zhongjie Cai <squall.leonhart.cai@gmail.com>      *
0005  *                           Julian Helfferich <julian.helfferich@mailbox.org> *
0006  *                                                                             *
0007  *   SPDX-License-Identifier: GPL-2.0-or-later
0008  ******************************************************************************/
0009 //Uses routines from Kapman sound manager (game.cpp)
0010 
0011 #include "KBlocksSound.h"
0012 
0013 #include <KGameSound>
0014 #include <KGameTheme>
0015 
0016 #include <QStandardPaths>
0017 
0018 #include "kblocks_sound_debug.h"
0019 #include "settings.h"
0020 
0021 KBlocksSound::KBlocksSound(const KGameTheme *theme)
0022     : SoundInterface()
0023 {
0024     loadTheme(theme);
0025 }
0026 
0027 KBlocksSound::~KBlocksSound()
0028 {
0029     delete m_blockFallSound;
0030     delete m_blockMoveSound;
0031     delete m_blockRemoveSound;
0032 }
0033 
0034 bool KBlocksSound::loadTheme(const KGameTheme *theme)
0035 {
0036     QString themeMoveSound;
0037     if (!theme->customData(QStringLiteral("Sound_Block_Move")).isEmpty()) {
0038         // TODO: extend KGameTheme to provide look-up of relativ path with custom entries
0039         // For now simply searching all possible locations manually, here and below
0040         themeMoveSound = QStandardPaths::locate(
0041                     QStandardPaths::AppDataLocation, QLatin1String("themes/") + theme->customData(QStringLiteral("Sound_Block_Move")));
0042     } else {
0043         themeMoveSound = QStandardPaths::locate(
0044                     QStandardPaths::AppDataLocation, QStringLiteral("sounds/block-move.ogg"));
0045     }
0046     
0047     QString themeFallSound;
0048     if (!theme->customData(QStringLiteral("Sound_Block_Fall")).isEmpty()) {
0049         themeFallSound = QStandardPaths::locate(
0050                     QStandardPaths::AppDataLocation, QLatin1String("themes/") + theme->customData(QStringLiteral("Sound_Block_Fall")));
0051     } else {
0052         themeFallSound = QStandardPaths::locate(
0053                     QStandardPaths::AppDataLocation, QStringLiteral("sounds/block-fall.ogg"));
0054     }
0055     
0056     QString themeRemoveSound;
0057     if (!theme->customData(QStringLiteral("Sound_Block_Remove")).isEmpty()) {
0058         themeRemoveSound = QStandardPaths::locate(
0059                     QStandardPaths::AppDataLocation, QLatin1String("themes/") + theme->customData(QStringLiteral("Sound_Block_Remove")));
0060     } else {
0061         themeRemoveSound = QStandardPaths::locate(
0062                     QStandardPaths::AppDataLocation, QStringLiteral("sounds/block-remove.ogg"));
0063     }
0064     
0065     delete m_blockFallSound;
0066     delete m_blockMoveSound;
0067     delete m_blockRemoveSound;
0068     m_blockFallSound = new KGameSound(themeFallSound);
0069     m_blockMoveSound = new KGameSound(themeMoveSound);
0070     m_blockRemoveSound = new KGameSound(themeRemoveSound);
0071     return true;
0072 }
0073 
0074 void KBlocksSound::setSoundsEnabled(bool p_enabled)
0075 {
0076     sndActive = p_enabled;
0077 }
0078 
0079 void KBlocksSound::playSound(Sound soundType)
0080 {
0081     if (sndActive) {
0082         switch (soundType) {
0083         case Sound::BlockFall:
0084             m_blockFallSound->start();
0085             break;
0086         case Sound::BlockMove:
0087             m_blockMoveSound->start();
0088             break;
0089         case Sound::BlockRemove:
0090             m_blockRemoveSound->start();
0091             break;
0092         default:
0093             qCWarning(KBSound) << "Unknown Sound requested for playback.";
0094             break;
0095         }
0096     }
0097 }
0098