Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/sound/AmbientSound.cc @ 6987

Last change on this file since 6987 was 6406, checked in by rgrieder, 15 years ago

Mood changes should now be working and with immediate (but delayed because of loading) effect.

  • Property svn:eol-style set to native
File size: 4.5 KB
RevLine 
[3060]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:
[5896]23 *      Reto Grieder
[3060]24 *   Co-authors:
25 *      ...
26 *
27 */
[3196]28
[5896]29#include "AmbientSound.h"
[3196]30
[5914]31#include "core/CoreIncludes.h"
[5896]32#include "core/EventIncludes.h"
[6117]33#include "core/GameMode.h"
34#include "core/Resource.h"
[5896]35#include "core/XMLPort.h"
[6117]36#include "SoundManager.h"
[3060]37
[5738]38namespace orxonox
[3060]39{
[5896]40    CreateFactory(AmbientSound);
41
42    AmbientSound::AmbientSound(BaseObject* creator)
[6382]43        : BaseObject(creator)
44        , Synchronisable(creator)
45        , bPlayOnLoad_(false)
[3060]46    {
[5896]47        RegisterObject(AmbientSound);
[6117]48
49        // Ambient sounds always fade in
50        this->setVolume(0);
[6307]51        this->registerVariables();
[3060]52    }
53
[6349]54    void AmbientSound::preDestroy()
55    {
56        if (GameMode::playsSound())
57        {
58            // Smoothly fade out by keeping a SmartPtr
59            SoundManager::getInstance().unregisterAmbientSound(this);
60        }
61    }
[6387]62
[6307]63    void AmbientSound::registerVariables()
64    {
65        registerVariable(ambientSource_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::ambientSourceChanged));
[6382]66        registerVariable(bLooping_,      ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::loopingChanged));
67        registerVariable(pitch_,         ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::pitchChanged));
68        registerVariable(bPlayOnLoad_,   ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::playOnLoadChanged));
[6307]69    }
[3060]70
[5896]71    void AmbientSound::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[3060]72    {
[5896]73        SUPER(AmbientSound, XMLPort, xmlelement, mode);
[6117]74        BaseSound::XMLPortExtern(xmlelement, mode);
[6382]75        XMLPortParam(AmbientSound, "ambientSource", setAmbientSource, getAmbientSource, xmlelement, mode);
76        XMLPortParam(AmbientSound, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
[3060]77    }
78
[5896]79    void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
[3060]80    {
[5896]81        SUPER(AmbientSound, XMLEventPort, xmlelement, mode);
82        XMLPortEventState(AmbientSound, BaseObject, "play", play, xmlelement, mode);
[3060]83    }
[6117]84
85    void AmbientSound::play()
86    {
87        if (GameMode::playsSound())
88            SoundManager::getInstance().registerAmbientSound(this);
89    }
90
91    void AmbientSound::stop()
92    {
93        if (GameMode::playsSound())
94            SoundManager::getInstance().unregisterAmbientSound(this);
95    }
96
97    void AmbientSound::pause()
98    {
99        if (GameMode::playsSound())
100            SoundManager::getInstance().pauseAmbientSound(this);
101    }
[6387]102
[6370]103    float AmbientSound::getRealVolume()
[6186]104    {
[6322]105        assert(GameMode::playsSound());
[6370]106        return SoundManager::getInstance().getRealVolume(SoundType::Music);
[6186]107    }
[6117]108
109    void AmbientSound::setAmbientSource(const std::string& source)
110    {
111        this->ambientSource_ = source;
[6406]112        this->moodChanged(this->getMood());
113    }
114
115    void AmbientSound::moodChanged(const std::string& mood)
116    {
[6117]117        if (GameMode::playsSound())
118        {
[6406]119            const std::string& path = "ambient/" + MoodManager::getInstance().getMood() + '/' + this->ambientSource_;
[6117]120            shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path);
121            if (fileInfo != NULL)
122                this->setSource(path);
123            else
[6406]124                COUT(3) << "Sound: " << this->ambientSource_ << ": Not a valid name! Ambient sound will not change." << std::endl;
[6117]125        }
126    }
127
[6382]128    void AmbientSound::setPlayOnLoad(bool val)
129    {
130        this->bPlayOnLoad_ = val;
131        if (val)
132            this->play();
133    }
134
[6320]135    void AmbientSound::changedActivity()
[6117]136    {
137        SUPER(AmbientSound, changedActivity);
138        if (this->isActive())
139            this->play();
[6388]140        else
[6117]141            this->stop();
142    }
[5896]143}
Note: See TracBrowser for help on using the repository browser.