I'm to the point where I'm putting sounds in my game. Which is a good sign because it's one of the last things I normally do.
Here's my take on a sound engine:
In my case I don't need a volume slider in the options menu. I'll be using a toggle that will go from:
No Sound (0) - Quiet(0.35) - Normal(0.65) - Loud(1.0)
There's a global master volume and global master music variable.
Here's a question about garbage collection though. Let me set up the environment with some code.
Global Snd1:TSound = LoadSound("mysound.wav")
Const MVol_OFF:Float = 0
Const MVol_LOW:Float = 0.35
Const MVol_MED:Float = 0.65
Const MVol_HI:Float = 1.0
Global MVol:Float = MVol_MED
Type SndFX
Field chn:TChannel
Function Play(snd:TSound,vol:Float=MVol,rate:float=1)
Local s:SndFX = New SndFX
s.chn = AllocChannel()
SetChannelVolume(s.chn,vol)
SetChannelRate(s.chn,rate)
PlaySound(snd,s.chn)
End Function
End Type
Now the above code works extremely well and allows you great control over your sounds. Making a new channel for each sound keeps you from cutting off previous sounds too. It's very easy to call also...like this:
SndFX.Play(Snd1)
My concern is that after the sound is finished, does the garbage collector delete the type or does it hang out and as each new sound is played...eat up memory? I'm assuming it gets deleted.
|