1 ///////////////////////////////////////////////////////////////////////////////
2 //            Copyright (C) 2012-2016 by Bertram (Valyria Tear)
3 //                         All Rights Reserved
4 //
5 // This code is licensed under the GNU GPL version 2. It is free software
6 // and you may modify it and/or redistribute it under the terms of this license.
7 // See https://www.gnu.org/copyleft/gpl.html for details.
8 ///////////////////////////////////////////////////////////////////////////////
9 
10 #ifndef __MAP_SOUND_HEADER__
11 #define __MAP_SOUND_HEADER__
12 
13 #include "modes/map/map_objects/map_object.h"
14 
15 #include "engine/audio/audio_descriptor.h"
16 
17 namespace vt_map
18 {
19 
20 namespace private_map
21 {
22 
23 /** ****************************************************************************
24 *** \brief Represents a sound source object on the map
25 *** ***************************************************************************/
26 class SoundObject : public MapObject
27 {
28 public:
29     /** \brief An environmental sound object which sound is played looped and with a volume
30     *** computed against the distance of the object with the camera.
31     *** \param sound_filename The sound filename to play.
32     *** \param x, y The sound map location
33     *** \param strength The "strength" of the sound, the maximal distance
34     in map tiles the sound can be heard within.
35     *** The sound volume will be compute according that distance.
36     **/
37     SoundObject(const std::string& sound_filename, float x, float y, float strength);
38 
~SoundObject()39     virtual ~SoundObject() override
40     {
41     }
42 
43     //! \brief A C++ wrapper made to create a new object from scripting,
44     //! without letting Lua handling the object life-cycle.
45     //! \note We don't permit luabind to use constructors here as it can't currently
46     //! give the object ownership at construction time.
47     static SoundObject* Create(const std::string& sound_filename,
48                                float x, float y, float strength);
49 
50     //! \brief Updates the object's currently desired volume.
51     void UpdateVolume();
52 
53     //! \brief Applies the object's currently desired volume.
54     void ApplyVolume();
55 
56     //! \brief Does nothing
Draw()57     void Draw() override
58     {}
59 
60     //! \brief Stop the ambient sound
61     void Stop();
62 
63     //! \brief Start the ambient sound
64     void Start();
65 
66     //! \brief Tells whether the ambient sound is active
IsActive()67     bool IsActive() const {
68         return _activated;
69     }
70 
71     //! \brief Sets the max sound volume of the ambient sound.
72     //! From  0.0f to 1.0f
73     void SetMaxVolume(float max_volume);
74 
75     //! \brief Gets the sound descriptor of the object.
76     //! Used to apply changes directly to the sound object.
GetSoundDescriptor()77     vt_audio::SoundDescriptor* GetSoundDescriptor() const {
78         return _sound;
79     }
80 
81     //! \brief Gets the current desired sound volume.
82     //! Used by the object manager to determine the best volume to play the sound object at.
GetSoundVolume()83     float GetSoundVolume() const {
84         return (_activated && _playing) ? _sound_volume : 0.0f;
85     }
86 
87 private:
88     //! \brief The sound object reference. Don't delete it.
89     vt_audio::SoundDescriptor* _sound;
90 
91     //! \brief The maximal distance in map tiles the sound can be heard within.
92     float _strength;
93 
94     //! \brief The Volume the sound should currently be played.
95     float _sound_volume;
96 
97     //! \brief The maximal strength of the sound object. (0.0f - 1.0f)
98     float _max_sound_volume;
99 
100     //! \brief The time remaining before next update
101     int32_t _time_remaining;
102 
103     //! \brief Tells whether the sound is activated.
104     bool _activated;
105 
106     //! \brief Tells whether the sound is currently playing or not
107     //! This boolean is here to avoid calling fadeIn()/FadeOut()
108     //! repeatedly on sounds.
109     bool _playing;
110 }; // class SoundObject : public MapObject
111 
112 } // namespace private_map
113 
114 } // namespace vt_map
115 
116 #endif // __MAP_SOUND_HEADER__
117