sfxEmitter.h 9.2 KB

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