Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6382 was 6382, checked in by rgrieder, 14 years ago

Several small changes in sound:

  • Fixed a bug that caused a sound source to be initialised on pause() even it already existed
  • The XML parameter is now called "looping" instead of "loop" because looping was used really all over the code
  • The "play" XML parameter doesn't exist anymore, use "playOnLoad" for AmbientSound only.
  • Added "pitch" XML parameter
  • Changes in synchronisation policy: Ambient sound should not synchronise the state but instead th new playOnLoad parameter
  • Property svn:eol-style set to native
File size: 4.4 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/CoreIncludes.h"
32#include "core/EventIncludes.h"
33#include "core/GameMode.h"
34#include "core/Resource.h"
35#include "core/XMLPort.h"
36#include "SoundManager.h"
37#include "MoodManager.h"
38
39namespace orxonox
40{
41    CreateFactory(AmbientSound);
42
43    AmbientSound::AmbientSound(BaseObject* creator)
44        : BaseObject(creator)
45        , Synchronisable(creator)
46        , bPlayOnLoad_(false)
47    {
48        RegisterObject(AmbientSound);
49
50        // Ambient sounds always fade in
51        this->setVolume(0);
52        this->registerVariables();
53    }
54
55    void AmbientSound::preDestroy()
56    {
57        if (GameMode::playsSound())
58        {
59            // Smoothly fade out by keeping a SmartPtr
60            SoundManager::getInstance().unregisterAmbientSound(this);
61        }
62    }
63   
64    void AmbientSound::registerVariables()
65    {
66        registerVariable(ambientSource_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::ambientSourceChanged));
67        registerVariable(bLooping_,      ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::loopingChanged));
68        registerVariable(pitch_,         ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::pitchChanged));
69        registerVariable(bPlayOnLoad_,   ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::playOnLoadChanged));
70    }
71
72    void AmbientSound::XMLPort(Element& xmlelement, XMLPort::Mode mode)
73    {
74        SUPER(AmbientSound, XMLPort, xmlelement, mode);
75        BaseSound::XMLPortExtern(xmlelement, mode);
76        XMLPortParam(AmbientSound, "ambientSource", setAmbientSource, getAmbientSource, xmlelement, mode);
77        XMLPortParam(AmbientSound, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
78    }
79
80    void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
81    {
82        SUPER(AmbientSound, XMLEventPort, xmlelement, mode);
83        XMLPortEventState(AmbientSound, BaseObject, "play", play, xmlelement, mode);
84    }
85
86    void AmbientSound::play()
87    {
88        if (GameMode::playsSound())
89            SoundManager::getInstance().registerAmbientSound(this);
90    }
91
92    void AmbientSound::stop()
93    {
94        if (GameMode::playsSound())
95            SoundManager::getInstance().unregisterAmbientSound(this);
96    }
97
98    void AmbientSound::pause()
99    {
100        if (GameMode::playsSound())
101            SoundManager::getInstance().pauseAmbientSound(this);
102    }
103   
104    float AmbientSound::getRealVolume()
105    {
106        assert(GameMode::playsSound());
107        return SoundManager::getInstance().getRealVolume(SoundType::Music);
108    }
109
110    void AmbientSound::setAmbientSource(const std::string& source)
111    {
112        this->ambientSource_ = source;
113        if (GameMode::playsSound())
114        {
115            std::string path = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
116            shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path);
117            if (fileInfo != NULL)
118                this->setSource(path);
119            else
120                COUT(3) << "Sound: " << source << ": Not a valid name! Ambient sound will not change." << std::endl;       
121        }
122    }
123
124    void AmbientSound::setPlayOnLoad(bool val)
125    {
126        this->bPlayOnLoad_ = val;
127        if (val)
128            this->play();
129    }
130
131    void AmbientSound::changedActivity()
132    {
133        SUPER(AmbientSound, changedActivity);
134        if (this->isActive())
135            this->play();
136        else 
137            this->stop();
138    }
139}
Note: See TracBrowser for help on using the repository browser.