Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7996 was 7856, checked in by landauf, 14 years ago

fixed (?) an endless loop in SoundManager::preUpdate if a non-looping ambient sound faded out. maybe solved the problem by returning a bool from stop() depending on whether or not the sound source was destroyed.

someone who knows more about the sound system should probably have a look at this.

  • Property svn:eol-style set to native
File size: 2.9 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    bool AmbientSound::stop()
60    {
61        if (GameMode::playsSound())
62            SoundManager::getInstance().unregisterAmbientSound(this);
63        return false; // sound source not (yet) destroyed - return false
64    }
65
66    void AmbientSound::pause()
67    {
68        if (GameMode::playsSound())
69            SoundManager::getInstance().pauseAmbientSound(this);
70    }
71
72    float AmbientSound::getRealVolume()
73    {
74        assert(GameMode::playsSound());
75        return SoundManager::getInstance().getRealVolume(SoundType::Music);
76    }
77
78    void AmbientSound::setAmbientSource(const std::string& source)
79    {
80        this->ambientSource_ = source;
81        this->moodChanged(this->getMood());
82    }
83
84    void AmbientSound::moodChanged(const std::string& mood)
85    {
86        if (GameMode::playsSound())
87        {
88            const std::string& path = "ambient/" + MoodManager::getInstance().getMood() + '/' + this->ambientSource_;
89            shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path);
90            if (fileInfo != NULL)
91                this->setSource(path);
92            else
93                COUT(3) << "Sound: " << this->ambientSource_ << ": Not a valid name! Ambient sound will not change." << std::endl;
94        }
95    }
96
97    void AmbientSound::setPlayOnLoad(bool val)
98    {
99        this->bPlayOnLoad_ = val;
100        if (val)
101            this->play();
102    }
103}
Note: See TracBrowser for help on using the repository browser.