Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/sound/AmbientSound.cc @ 7854

Last change on this file since 7854 was 7854, checked in by landauf, 13 years ago

detached AmbientSound from BaseObject - AmbientSound can not be placed directly in a level file anymore
instead added WorldAmbientSound, a BaseObject which can be placed in a level file to create and control ambient sound

with this change, the level can be destroyed completely because WorldAmbientSound can be deleted immediately at the end of the level, while the AmbientSound remains active until it faded out.

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "AmbientSound.h"
30
31#include "core/GameMode.h"
32#include "core/Resource.h"
33#include "SoundManager.h"
34
35namespace orxonox
36{
37    AmbientSound::AmbientSound()
38        : bPlayOnLoad_(false)
39    {
40        // Ambient sounds always fade in
41        this->setVolume(0);
42    }
43
44    void AmbientSound::preDestroy()
45    {
46        if (GameMode::playsSound())
47        {
48            // Smoothly fade out by keeping a SmartPtr
49            SoundManager::getInstance().unregisterAmbientSound(this);
50        }
51    }
52
53    void AmbientSound::play()
54    {
55        if (GameMode::playsSound())
56            SoundManager::getInstance().registerAmbientSound(this);
57    }
58
59    void AmbientSound::stop()
60    {
61        if (GameMode::playsSound())
62            SoundManager::getInstance().unregisterAmbientSound(this);
63    }
64
65    void AmbientSound::pause()
66    {
67        if (GameMode::playsSound())
68            SoundManager::getInstance().pauseAmbientSound(this);
69    }
70
71    float AmbientSound::getRealVolume()
72    {
73        assert(GameMode::playsSound());
74        return SoundManager::getInstance().getRealVolume(SoundType::Music);
75    }
76
77    void AmbientSound::setAmbientSource(const std::string& source)
78    {
79        this->ambientSource_ = source;
80        this->moodChanged(this->getMood());
81    }
82
83    void AmbientSound::moodChanged(const std::string& mood)
84    {
85        if (GameMode::playsSound())
86        {
87            const std::string& path = "ambient/" + MoodManager::getInstance().getMood() + '/' + this->ambientSource_;
88            shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path);
89            if (fileInfo != NULL)
90                this->setSource(path);
91            else
92                COUT(3) << "Sound: " << this->ambientSource_ << ": Not a valid name! Ambient sound will not change." << std::endl;
93        }
94    }
95
96    void AmbientSound::setPlayOnLoad(bool val)
97    {
98        this->bPlayOnLoad_ = val;
99        if (val)
100            this->play();
101    }
102}
Note: See TracBrowser for help on using the repository browser.