sfxEmitter.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. DefineConsoleType(TypeSoundControls, bool)
  40. class GuiInspectorTypeSoundControls : public GuiInspectorField
  41. {
  42. typedef GuiInspectorField Parent;
  43. public:
  44. GuiBitmapButtonCtrl* mPlayButton;
  45. GuiBitmapButtonCtrl* mPauseButton;
  46. GuiBitmapButtonCtrl* mStopButton;
  47. DECLARE_CONOBJECT(GuiInspectorTypeSoundControls);
  48. static void consoleInit();
  49. GuiControl* constructEditControl() override;
  50. bool updateRects() override;
  51. };
  52. //RDTODO: make 3D sound emitters yield their source when being culled
  53. /// The SFXEmitter is used to place 2D or 3D sounds into a
  54. /// mission.
  55. ///
  56. /// If the profile is set then the emitter plays that. If the
  57. /// profile is null and the filename is set then the local emitter
  58. /// options are used.
  59. ///
  60. /// Note that you can call SFXEmitter.play() and SFXEmitter.stop()
  61. /// to control playback from script.
  62. ///
  63. class SFXEmitter : public SceneObject
  64. {
  65. public:
  66. typedef SceneObject Parent;
  67. protected:
  68. /// Network update masks.
  69. enum UpdateMasks
  70. {
  71. InitialUpdateMask = BIT(0),
  72. TransformUpdateMask = BIT(1),
  73. DirtyUpdateMask = BIT(2),
  74. SourcePlayMask = BIT(3),
  75. SourcePauseMask = BIT(4),
  76. SourceStopMask = BIT(5),
  77. AllSourceMasks = SourcePlayMask | SourceStopMask,
  78. };
  79. /// Dirty flags used to handle sound property
  80. /// updates locally and across the network.
  81. enum Dirty
  82. {
  83. Track = BIT( 0 ),
  84. Volume = BIT( 1 ),
  85. IsLooping = BIT( 3 ),
  86. Is3D = BIT( 4 ),
  87. MinDistance = BIT( 5 ),
  88. MaxDistance = BIT( 6 ),
  89. ConeInsideAngle = BIT( 7 ),
  90. ConeOutsideAngle = BIT( 8 ),
  91. ConeOutsideVolume = BIT( 9 ),
  92. Transform = BIT( 10 ),
  93. SourceGroup = BIT( 11 ),
  94. OutsideAmbient = BIT( 12 ),
  95. IsStreaming = BIT( 13 ),
  96. FadeInTime = BIT( 14 ),
  97. FadeOutTime = BIT( 15 ),
  98. Pitch = BIT( 16 ),
  99. ScatterDistance = BIT( 17 ),
  100. TrackOnly = BIT( 18 ),
  101. AllDirtyMask = 0xFFFFFFFF,
  102. };
  103. /// The current dirty flags.
  104. BitSet32 mDirty;
  105. DECLARE_SOUNDASSET(SFXEmitter, Sound);
  106. DECLARE_ASSET_NET_SETGET(SFXEmitter, Sound, DirtyUpdateMask);
  107. /// returns the shape asset used for this object
  108. StringTableEntry getTypeHint() const override { return (getSoundAsset()) ? getSoundAsset()->getAssetName() : StringTable->EmptyString(); }
  109. /// The sound source for the emitter.
  110. SFXSource *mSource;
  111. /// Whether to leave sound setup exclusively to the assigned mTrack and not
  112. /// override part of the track's description with emitter properties.
  113. bool mUseTrackDescriptionOnly;
  114. /// A local profile object used to coax the
  115. /// sound system to play a custom sound.
  116. SFXTrack* mLocalProfile;
  117. /// The description used by the local profile.
  118. SFXDescription mDescription;
  119. /// The description used by the local profile.
  120. SFXDescription *mInstanceDescription;
  121. /// If true playback starts when the emitter
  122. /// is added to the scene.
  123. bool mPlayOnAdd;
  124. /// State block for cone rendering in editor.
  125. GFXStateBlockRef mRenderSB;
  126. /// If true, render all emitters when in editor (not only selected one).
  127. static bool smRenderEmitters;
  128. /// Point size for rendering point clouds of emitter cones in editor.
  129. /// @todo Currently not implemented.
  130. static F32 smRenderPointSize;
  131. ///
  132. static F32 smRenderRadialIncrements;
  133. ///
  134. static F32 smRenderSweepIncrements;
  135. ///
  136. static F32 smRenderPointDistance;
  137. /// Point color when emitter is playing and in range of listener.
  138. static ColorI smRenderColorPlayingInRange;
  139. /// Point color when emitter is playing but out of range of listern.
  140. static ColorI smRenderColorPlayingOutOfRange;
  141. /// Point color when emitter is not playing but in range of listener.
  142. static ColorI smRenderColorStoppedInRange;
  143. /// Point color when emitter is not playing and not in range of listener.
  144. static ColorI smRenderColorStoppedOutOfRange;
  145. ///
  146. static ColorI smRenderColorInnerCone;
  147. ///
  148. static ColorI smRenderColorOuterCone;
  149. ///
  150. static ColorI smRenderColorOutsideVolume;
  151. ///
  152. static ColorI smRenderColorRangeSphere;
  153. /// Helper which reads a flag from the stream and
  154. /// updates the mDirty bits.
  155. bool _readDirtyFlag( BitStream *stream, U32 flag );
  156. /// Called when the emitter state has been marked
  157. /// dirty and the source needs to be updated.
  158. void _update();
  159. /// Render emitter object in editor.
  160. void _renderObject( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat );
  161. /// Render visual feedback for 3D sounds in editor.
  162. void _render3DVisualFeedback();
  163. ///
  164. void _renderCone( F32 radialIncrements,
  165. F32 sweepIncrements,
  166. F32 pointDistance,
  167. F32 startAngle,
  168. F32 stopAngle,
  169. F32 startVolume,
  170. F32 stopVolume,
  171. const ColorI& color );
  172. /// Return the playback status of the emitter's associated sound.
  173. /// This should only be called on either the ghost or the server object if the server is running
  174. /// in-process. Otherwise, the method will not return a meaningful value.
  175. SFXStatus _getPlaybackStatus() const;
  176. public:
  177. SFXEmitter();
  178. virtual ~SFXEmitter();
  179. /// Return the sound source object associated with the emitter.
  180. /// @note This will only return a meaningful result when called on ghost objects.
  181. SFXSource* getSource() const { return mSource; }
  182. /// Return true if this object emits a 3D sound.
  183. bool is3D() const;
  184. /// Return true if the SFX system's listener is in range of this emitter.
  185. bool isInRange() const;
  186. /// Sends network event to start playback if
  187. /// the emitter source is not already playing.
  188. void play();
  189. /// Sends network event to pause playback if
  190. /// the emitter source is already playing.
  191. void pause();
  192. /// Sends network event to stop emitter
  193. /// playback on all ghosted clients.
  194. void stop();
  195. // SimObject
  196. bool onAdd() override;
  197. void onRemove() override;
  198. void onStaticModified( const char *slotName, const char *newValue = NULL ) override;
  199. U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream ) override;
  200. void unpackUpdate( NetConnection *conn, BitStream *stream ) override;
  201. void setTransform( const MatrixF &mat ) override;
  202. void setScale( const VectorF &scale ) override;
  203. bool containsPoint( const Point3F& point ) override { return false; }
  204. void prepRenderImage( SceneRenderState* state ) override;
  205. void inspectPostApply() override;
  206. static void initPersistFields();
  207. static void consoleInit();
  208. DECLARE_CONOBJECT( SFXEmitter );
  209. DECLARE_DESCRIPTION( "A 3D object emitting sound." );
  210. DECLARE_CATEGORY("Environment \t FX");
  211. };
  212. #endif // _SFXEMITTER_H_