sfxController.h
Engine/source/sfx/sfxController.h
Classes:
class
SFXSource that drives multi-source playback.
class
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _SFXCONTROLLER_H_ 25#define _SFXCONTROLLER_H_ 26 27#ifndef _SFXSOURCE_H_ 28 #include "sfx/sfxSource.h" 29#endif 30#ifndef _SFXCOMMON_H_ 31 #include "sfx/sfxCommon.h" 32#endif 33#ifndef _SFXSOURCE_H_ 34 #include "sfx/sfxSource.h" 35#endif 36#ifndef _SFXPLAYLIST_H_ 37 #include "sfx/sfxPlayList.h" 38#endif 39#ifndef _TVECTOR_H_ 40 #include "core/util/tVector.h" 41#endif 42 43 44class SFXTrack; 45class SFXProfile; 46class SFXState; 47 48 49/// SFXSource that drives multi-source playback. 50/// 51/// Basically, this class is an interpreter for the instruction slots in 52/// SFXPlayLists. 53/// 54/// Controllers can be switched between states. When no state is set, all 55/// tracks from playlists that do not have a state set will be played. When 56/// setting a state, only tracks with the given state will be played. If 57/// currently tracks with a different state are playing, the respective 58/// controllers will transition out of their respective slots. 59/// 60class SFXController : public SFXSource 61{ 62 public: 63 64 typedef SFXSource Parent; 65 friend class SFXSystem; // _create 66 67 protected: 68 69 typedef SFXVariantFloat< 1> VariantFloat; 70 71 enum EOp 72 { 73 OP_Delay, 74 OP_WaitSingle, 75 OP_WaitAll, 76 OP_StopSingle, 77 OP_StopAll, 78 OP_Play, 79 OP_Jump, 80 OP_LoopBegin, 81 OP_LoopEnd, 82 }; 83 84 struct Insn 85 { 86 EOp mOpcode; 87 U32 mSlotIndex; 88 SFXState* mState; 89 90 union 91 { 92 VariantFloat mDelayTime; 93 U32 mJumpIp; 94 U32 mLoopCount; 95 } mArg; 96 97 Insn() 98 : mOpcode(SFXController::OP_Delay), mSlotIndex(0), mState(NULL) {mArg.mLoopCount=0;} 99 Insn( EOp opcode ) 100 : mOpcode( opcode ), mSlotIndex( U32_MAX ), mState( NULL ) {mArg.mLoopCount=0;} 101 Insn( U32 slotIndex, SFXState* state ) 102 : mOpcode(SFXController::OP_Delay), mSlotIndex( slotIndex ), mState( state ){mArg.mLoopCount=0;} 103 Insn( EOp opcode, U32 slotIndex, SFXState* state ) 104 : mOpcode( opcode ), mSlotIndex( slotIndex ), mState( state ) {mArg.mLoopCount=0;} 105 }; 106 107 /// 108 struct Source 109 { 110 /// The play-once source. 111 SimObjectPtr< SFXSource> mPtr; 112 113 /// The state to which the source is tied. Only taken over from 114 /// the instruction if the state mode is not set to ignored. 115 SFXState* mState; 116 117 /// Index of slot in playlist that this source was spawned on. 118 U32 mSlotIndex; 119 120 /// Volume scale factor to apply to the source. Saved as it may have been 121 /// randomly generated. 122 F32 mVolumeScale; 123 124 /// Pitch scale factor to apply to the source. Saved as it may have been 125 /// randomly generated. 126 F32 mPitchScale; 127 128 /// 129 F32 mFadeInTime; 130 131 /// 132 F32 mFadeOutTime; 133 134 Source() 135 : mState( 0 ), mSlotIndex(0), mVolumeScale(1.0f), mPitchScale(1.0f), mFadeInTime(0), mFadeOutTime(0) {} 136 }; 137 138 /// The current instruction in "mInsns". 139 U32 mIp; 140 141 /// The instruction list. This is compiled from the playlist and then executed 142 /// in the controller's update. 143 Vector< Insn> mInsns; 144 145 /// The stack of currently playing sources. 146 /// 147 /// All sources on this list are play-once sources so we can leave their lifetime 148 /// management to the SFX system. This is especially convenient in combination 149 /// with fade-outs where a source cannot be immediately deleted. 150 Vector< Source> mSources; 151 152 /// 153 bool mTrace; 154 155 /// 156 U32 mDelayEndTime; 157 158 /// 159 U32 mLoopCounter; 160 161 /// 162 SFXController( SFXPlayList* playList ); 163 164 /// 165 void _printInsn( Insn& insn ); 166 167 /// 168 void _compileList( SFXPlayList* playList ); 169 170 /// 171 void _genTransition( Insn& insn, SFXPlayList::ETransitionMode transition ); 172 173 /// 174 void _dumpInsns() {}; 175 176 /// 177 void _initInsn(); 178 179 /// 180 bool _execInsn(); 181 182 /// 183 void _advanceIp(); 184 185 /// 186 static SFXController* _create( SFXPlayList* playList ); 187 188 // SFXSource. 189 virtual void _play(); 190 virtual void _pause(); 191 virtual void _stop(); 192 virtual void _onParameterEvent( SFXParameter* parameter, SFXParameterEvent event ); 193 virtual void _updateVolume( const MatrixF& listener ); 194 virtual void _updatePitch(); 195 virtual void _updatePriority(); 196 virtual void _update(); 197 198 public: 199 200 ~SFXController(); 201 202 /// Constructor for the sake of ConsoleObject. 203 explicit SFXController(): mIp(0), mTrace(false), mDelayEndTime(0), mLoopCounter(0) {} 204 205 /// Return the playlist being played back by the controller. 206 SFXPlayList* getPlayList() const; 207 208 /// Return the index of the playlist slot being processed by the controller. 209 U32 getCurrentSlot() const; 210 211 /// Set the index of the playlist slot to process. 212 void setCurrentSlot( U32 index ); 213 214 // SFXSource. 215 static void initPersistFields(); 216 217 DECLARE_CONOBJECT( SFXController ); 218 DECLARE_DESCRIPTION( "Controls the playback of an SFXPlayList." ); 219}; 220 221#endif // !_SFXCONTROLLER_H_ 222 223