sfxController.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 _SFXCONTROLLER_H_
  23. #define _SFXCONTROLLER_H_
  24. #ifndef _SFXSOURCE_H_
  25. #include "sfx/sfxSource.h"
  26. #endif
  27. #ifndef _SFXCOMMON_H_
  28. #include "sfx/sfxCommon.h"
  29. #endif
  30. #ifndef _SFXSOURCE_H_
  31. #include "sfx/sfxSource.h"
  32. #endif
  33. #ifndef _SFXPLAYLIST_H_
  34. #include "sfx/sfxPlayList.h"
  35. #endif
  36. #ifndef _TVECTOR_H_
  37. #include "core/util/tVector.h"
  38. #endif
  39. class SFXTrack;
  40. class SFXProfile;
  41. class SFXState;
  42. /// SFXSource that drives multi-source playback.
  43. ///
  44. /// Basically, this class is an interpreter for the instruction slots in
  45. /// SFXPlayLists.
  46. ///
  47. /// Controllers can be switched between states. When no state is set, all
  48. /// tracks from playlists that do not have a state set will be played. When
  49. /// setting a state, only tracks with the given state will be played. If
  50. /// currently tracks with a different state are playing, the respective
  51. /// controllers will transition out of their respective slots.
  52. ///
  53. class SFXController : public SFXSource
  54. {
  55. public:
  56. typedef SFXSource Parent;
  57. friend class SFXSystem; // _create
  58. protected:
  59. typedef SFXVariantFloat< 1 > VariantFloat;
  60. enum EOp
  61. {
  62. OP_Delay,
  63. OP_WaitSingle,
  64. OP_WaitAll,
  65. OP_StopSingle,
  66. OP_StopAll,
  67. OP_Play,
  68. OP_Jump,
  69. OP_LoopBegin,
  70. OP_LoopEnd,
  71. };
  72. struct Insn
  73. {
  74. EOp mOpcode;
  75. U32 mSlotIndex;
  76. SFXState* mState;
  77. union
  78. {
  79. VariantFloat mDelayTime;
  80. U32 mJumpIp;
  81. U32 mLoopCount;
  82. } mArg;
  83. Insn() {}
  84. Insn( EOp opcode )
  85. : mOpcode( opcode ), mSlotIndex( U32_MAX ), mState( NULL ) {}
  86. Insn( U32 slotIndex, SFXState* state )
  87. : mSlotIndex( slotIndex ), mState( state ) {}
  88. Insn( EOp opcode, U32 slotIndex, SFXState* state )
  89. : mOpcode( opcode ), mSlotIndex( slotIndex ), mState( state ) {}
  90. };
  91. ///
  92. struct Source
  93. {
  94. /// The play-once source.
  95. SimObjectPtr< SFXSource > mPtr;
  96. /// The state to which the source is tied. Only taken over from
  97. /// the instruction if the state mode is not set to ignored.
  98. SFXState* mState;
  99. /// Index of slot in playlist that this source was spawned on.
  100. U32 mSlotIndex;
  101. /// Volume scale factor to apply to the source. Saved as it may have been
  102. /// randomly generated.
  103. F32 mVolumeScale;
  104. /// Pitch scale factor to apply to the source. Saved as it may have been
  105. /// randomly generated.
  106. F32 mPitchScale;
  107. ///
  108. F32 mFadeInTime;
  109. ///
  110. F32 mFadeOutTime;
  111. Source()
  112. : mState( 0 ) {}
  113. };
  114. /// The current instruction in "mInsns".
  115. U32 mIp;
  116. /// The instruction list. This is compiled from the playlist and then executed
  117. /// in the controller's update.
  118. Vector< Insn > mInsns;
  119. /// The stack of currently playing sources.
  120. ///
  121. /// All sources on this list are play-once sources so we can leave their lifetime
  122. /// management to the SFX system. This is especially convenient in combination
  123. /// with fade-outs where a source cannot be immediately deleted.
  124. Vector< Source > mSources;
  125. ///
  126. bool mTrace;
  127. ///
  128. U32 mDelayEndTime;
  129. ///
  130. U32 mLoopCounter;
  131. ///
  132. SFXController( SFXPlayList* playList );
  133. ///
  134. void _printInsn( Insn& insn );
  135. ///
  136. void _compileList( SFXPlayList* playList );
  137. ///
  138. void _genTransition( Insn& insn, SFXPlayList::ETransitionMode transition );
  139. ///
  140. void _dumpInsns();
  141. ///
  142. void _initInsn();
  143. ///
  144. bool _execInsn();
  145. ///
  146. void _advanceIp();
  147. ///
  148. static SFXController* _create( SFXPlayList* playList );
  149. // SFXSource.
  150. virtual void _play();
  151. virtual void _pause();
  152. virtual void _stop();
  153. virtual void _onParameterEvent( SFXParameter* parameter, SFXParameterEvent event );
  154. virtual void _updateVolume( const MatrixF& listener );
  155. virtual void _updatePitch();
  156. virtual void _updatePriority();
  157. virtual void _update();
  158. public:
  159. ~SFXController();
  160. /// Constructor for the sake of ConsoleObject.
  161. explicit SFXController() {}
  162. /// Return the playlist being played back by the controller.
  163. SFXPlayList* getPlayList() const;
  164. /// Return the index of the playlist slot being processed by the controller.
  165. U32 getCurrentSlot() const;
  166. /// Set the index of the playlist slot to process.
  167. void setCurrentSlot( U32 index );
  168. // SFXSource.
  169. static void initPersistFields();
  170. DECLARE_CONOBJECT( SFXController );
  171. DECLARE_DESCRIPTION( "Controls the playback of an SFXPlayList." );
  172. };
  173. #endif // !_SFXCONTROLLER_H_