sfxEmitter.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SFXEMITTER_H_
  23. #define _SFXEMITTER_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _SFXPROFILE_H_
  28. #include "sfx/sfxProfile.h"
  29. #endif
  30. #ifndef _SFXDESCRIPTION_H_
  31. #include "sfx/sfxDescription.h"
  32. #endif
  33. #ifndef _GFXSTATEBLOCK_H_
  34. #include "gfx/gfxStateBlock.h"
  35. #endif
  36. #include "T3D/assets/SoundAsset.h"
  37. class SFXSource;
  38. class SFXTrack;
  39. //RDTODO: make 3D sound emitters yield their source when being culled
  40. /// The SFXEmitter is used to place 2D or 3D sounds into a
  41. /// mission.
  42. ///
  43. /// If the profile is set then the emitter plays that. If the
  44. /// profile is null and the filename is set then the local emitter
  45. /// options are used.
  46. ///
  47. /// Note that you can call SFXEmitter.play() and SFXEmitter.stop()
  48. /// to control playback from script.
  49. ///
  50. class SFXEmitter : public SceneObject
  51. {
  52. public:
  53. typedef SceneObject Parent;
  54. protected:
  55. /// Network update masks.
  56. enum UpdateMasks
  57. {
  58. InitialUpdateMask = BIT(0),
  59. TransformUpdateMask = BIT(1),
  60. DirtyUpdateMask = BIT(2),
  61. SourcePlayMask = BIT(3),
  62. SourceStopMask = BIT(4),
  63. AllSourceMasks = SourcePlayMask | SourceStopMask,
  64. };
  65. /// Dirty flags used to handle sound property
  66. /// updates locally and across the network.
  67. enum Dirty
  68. {
  69. Track = BIT( 0 ),
  70. Filename = BIT( 2 ),
  71. Volume = BIT( 4 ),
  72. IsLooping = BIT( 5 ),
  73. Is3D = BIT( 6 ),
  74. MinDistance = BIT( 7 ),
  75. MaxDistance = BIT( 8 ),
  76. ConeInsideAngle = BIT( 9 ),
  77. ConeOutsideAngle = BIT( 10 ),
  78. ConeOutsideVolume = BIT( 11 ),
  79. Transform = BIT( 12 ),
  80. SourceGroup = BIT( 13 ),
  81. OutsideAmbient = BIT( 14 ),
  82. IsStreaming = BIT( 15 ),
  83. FadeInTime = BIT( 16 ),
  84. FadeOutTime = BIT( 17 ),
  85. Pitch = BIT( 18 ),
  86. ScatterDistance = BIT( 19 ),
  87. TrackOnly = BIT( 20 ),
  88. AllDirtyMask = 0xFFFFFFFF,
  89. };
  90. /// The current dirty flags.
  91. BitSet32 mDirty;
  92. DECLARE_SOUNDASSET(SFXEmitter, Sound);
  93. DECLARE_ASSET_NET_SETGET(SFXEmitter, Sound, DirtyUpdateMask);
  94. /// The sound source for the emitter.
  95. SFXSource *mSource;
  96. /// Whether to leave sound setup exclusively to the assigned mTrack and not
  97. /// override part of the track's description with emitter properties.
  98. bool mUseTrackDescriptionOnly;
  99. /// A local profile object used to coax the
  100. /// sound system to play a custom sound.
  101. SFXProfile mLocalProfile;
  102. /// The description used by the local profile.
  103. SFXDescription mDescription;
  104. /// If true playback starts when the emitter
  105. /// is added to the scene.
  106. bool mPlayOnAdd;
  107. /// State block for cone rendering in editor.
  108. GFXStateBlockRef mRenderSB;
  109. /// If true, render all emitters when in editor (not only selected one).
  110. static bool smRenderEmitters;
  111. /// Point size for rendering point clouds of emitter cones in editor.
  112. /// @todo Currently not implemented.
  113. static F32 smRenderPointSize;
  114. ///
  115. static F32 smRenderRadialIncrements;
  116. ///
  117. static F32 smRenderSweepIncrements;
  118. ///
  119. static F32 smRenderPointDistance;
  120. /// Point color when emitter is playing and in range of listener.
  121. static ColorI smRenderColorPlayingInRange;
  122. /// Point color when emitter is playing but out of range of listern.
  123. static ColorI smRenderColorPlayingOutOfRange;
  124. /// Point color when emitter is not playing but in range of listener.
  125. static ColorI smRenderColorStoppedInRange;
  126. /// Point color when emitter is not playing and not in range of listener.
  127. static ColorI smRenderColorStoppedOutOfRange;
  128. ///
  129. static ColorI smRenderColorInnerCone;
  130. ///
  131. static ColorI smRenderColorOuterCone;
  132. ///
  133. static ColorI smRenderColorOutsideVolume;
  134. ///
  135. static ColorI smRenderColorRangeSphere;
  136. /// Helper which reads a flag from the stream and
  137. /// updates the mDirty bits.
  138. bool _readDirtyFlag( BitStream *stream, U32 flag );
  139. /// Called when the emitter state has been marked
  140. /// dirty and the source needs to be updated.
  141. void _update();
  142. /// Render emitter object in editor.
  143. void _renderObject( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat );
  144. /// Render visual feedback for 3D sounds in editor.
  145. void _render3DVisualFeedback();
  146. ///
  147. void _renderCone( F32 radialIncrements,
  148. F32 sweepIncrements,
  149. F32 pointDistance,
  150. F32 startAngle,
  151. F32 stopAngle,
  152. F32 startVolume,
  153. F32 stopVolume,
  154. const ColorI& color );
  155. /// Return the playback status of the emitter's associated sound.
  156. /// This should only be called on either the ghost or the server object if the server is running
  157. /// in-process. Otherwise, the method will not return a meaningful value.
  158. SFXStatus _getPlaybackStatus() const;
  159. public:
  160. SFXEmitter();
  161. virtual ~SFXEmitter();
  162. /// Return the sound source object associated with the emitter.
  163. /// @note This will only return a meaningful result when called on ghost objects.
  164. SFXSource* getSource() const { return mSource; }
  165. /// Return true if this object emits a 3D sound.
  166. bool is3D() const;
  167. /// Return true if the SFX system's listener is in range of this emitter.
  168. bool isInRange() const;
  169. /// Sends network event to start playback if
  170. /// the emitter source is not already playing.
  171. void play();
  172. /// Sends network event to stop emitter
  173. /// playback on all ghosted clients.
  174. void stop();
  175. // SimObject
  176. bool onAdd();
  177. void onRemove();
  178. void onStaticModified( const char *slotName, const char *newValue = NULL );
  179. U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
  180. void unpackUpdate( NetConnection *conn, BitStream *stream );
  181. void setTransform( const MatrixF &mat );
  182. void setScale( const VectorF &scale );
  183. bool containsPoint( const Point3F& point ) { return false; }
  184. void prepRenderImage( SceneRenderState* state );
  185. void inspectPostApply();
  186. static void initPersistFields();
  187. static void consoleInit();
  188. DECLARE_CONOBJECT( SFXEmitter );
  189. DECLARE_DESCRIPTION( "A 3D object emitting sound." );
  190. DECLARE_CATEGORY( "3D Sound" );
  191. };
  192. #endif // _SFXEMITTER_H_