usound: added function for loops with fade in and out
authorkoda
Sat, 26 Feb 2011 15:56:11 +0100
changeset 4956 48e1f9a04c28
parent 4954 b17aafd93c17
child 4958 84afe376a3b3
child 4961 9075d7effdf2
usound: added function for loops with fade in and out airplane attacks: added stunt sound (issue #54)
hedgewars/GSHandlers.inc
hedgewars/SDLh.pas
hedgewars/uSound.pas
hedgewars/uTypes.pas
hedgewars/uVariables.pas
share/hedgewars/Data/Sounds/plane.ogg
--- a/hedgewars/GSHandlers.inc	Fri Feb 25 16:32:24 2011 -0500
+++ b/hedgewars/GSHandlers.inc	Sat Feb 26 15:56:11 2011 +0100
@@ -2307,13 +2307,19 @@
             //4: FollowGear := AddGear(hwRound(Gear^.X), hwRound(Gear^.Y), gtWaterMelon, 0, cBombsSpeed *
             //                 Gear^.Tag, _0, 5000);
         end;
-        Gear^.dX := Gear^.dX + int2hwFloat(30 * Gear^.Tag)
+        Gear^.dX := Gear^.dX + int2hwFloat(30 * Gear^.Tag);
+        StopSound(Gear^.SoundChannel, 4000);
     end;
 
     if (GameTicks and $3F) = 0 then
         AddVisualGear(hwRound(Gear^.X), hwRound(Gear^.Y), vgtSmokeTrace);
 
-    if (hwRound(Gear^.X) > (LAND_WIDTH+1024)) or (hwRound(Gear^.X) < -1024) then DeleteGear(Gear)
+    if (hwRound(Gear^.X) > (LAND_WIDTH+1024)) or (hwRound(Gear^.X) < -1024) then
+    begin
+        // avoid to play forever (is this necessary?)
+        StopSound(Gear^.SoundChannel);
+        DeleteGear(Gear)
+    end;
 end;
 
 procedure doStepAirAttack(Gear: PGear);
@@ -2344,6 +2350,8 @@
 
     Gear^.Health := 6;
     Gear^.doStep := @doStepAirAttackWork;
+    Gear^.SoundChannel := LoopSound(sndPlane, 4000);
+
 end;
 
 ////////////////////////////////////////////////////////////////////////////////
--- a/hedgewars/SDLh.pas	Fri Feb 25 16:32:24 2011 -0500
+++ b/hedgewars/SDLh.pas	Sat Feb 26 15:56:11 2011 +0100
@@ -826,6 +826,9 @@
 function  Mix_ResumeMusic(music: PMixMusic): LongInt; cdecl; external SDL_MixerLibName;
 function  Mix_HaltChannel(channel: LongInt): LongInt; cdecl; external SDL_MixerLibName;
 
+function  Mix_FadeInChannelTimed(channel: LongInt; chunk: PMixChunk; loops: LongInt; fadems: LongInt; ticks: LongInt): LongInt; cdecl; external SDL_MixerLibName;
+function  Mix_FadeOutChannel(channel: LongInt; fadems: LongInt): LongInt; cdecl; external SDL_MixerLibName;
+
 (*  SDL_image  *)
 function  IMG_Init(flags: LongInt): LongInt; cdecl; external SDL_ImageLibName;
 procedure IMG_Quit; cdecl; external SDL_ImageLibName;
--- a/hedgewars/uSound.pas	Fri Feb 25 16:32:24 2011 -0500
+++ b/hedgewars/uSound.pas	Sat Feb 26 15:56:11 2011 +0100
@@ -35,7 +35,9 @@
 procedure PlaySound(snd: TSound; voicepack: PVoicepack);
 procedure PlaySound(snd: TSound; voicepack: PVoicepack; keepPlaying: boolean);
 function  LoopSound(snd: TSound): LongInt;
+function  LoopSound(snd: TSound; fadems: LongInt): LongInt;
 function  LoopSound(snd: TSound; voicepack: PVoicepack): LongInt;
+function  LoopSound(snd: TSound; voicepack: PVoicepack; fadems: LongInt): LongInt;
 procedure PlayMusic;
 procedure PauseMusic;
 procedure ResumeMusic;
@@ -219,7 +221,17 @@
     LoopSound:= LoopSound(snd, nil)
 end;
 
+function LoopSound(snd: TSound; fadems: LongInt): LongInt;
+begin
+    LoopSound:= LoopSound(snd, nil, fadems)
+end;
+
 function LoopSound(snd: TSound; voicepack: PVoicepack): LongInt;
+begin
+    LoopSound:= LoopSound(snd, nil, 0)
+end;
+
+function LoopSound(snd: TSound; voicepack: PVoicepack; fadems: LongInt): LongInt;
 var s: shortstring;
 begin
     if (not isSoundEnabled) or fastUntilLag then
@@ -252,13 +264,17 @@
             TryDo(defVoicepack^.chunks[snd] <> nil, msgFailed, true);
             WriteLnToConsole(msgOK);
         end;
-        LoopSound:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], -1, -1);
+        if fadems > 0 then
+            LoopSound:= Mix_FadeInChannelTimed(-1, defVoicepack^.chunks[snd], -1, fadems, -1)
+        else
+            LoopSound:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], -1, -1);
     end;
 end;
 
 procedure StopSound(snd: TSound);
 begin
     if not isSoundEnabled then exit;
+
     if (lastChan[snd] <> -1) and (Mix_Playing(lastChan[snd]) <> 0) then
     begin
         Mix_HaltChannel(lastChan[snd]);
@@ -274,6 +290,14 @@
         Mix_HaltChannel(chn);
 end;
 
+procedure StopSound(chn, fadems: LongInt);
+begin
+    if not isSoundEnabled then exit;
+
+    if (chn <> -1) and (Mix_Playing(chn) <> 0) then
+        Mix_FadeOutChannel(chn, fadems);
+end;
+
 procedure PlayMusic;
 var s: shortstring;
 begin
--- a/hedgewars/uTypes.pas	Fri Feb 25 16:32:24 2011 -0500
+++ b/hedgewars/uTypes.pas	Sat Feb 26 15:56:11 2011 +0100
@@ -106,7 +106,7 @@
             sndPoisonCough, sndPoisonMoan, sndBirdyLay, sndWhistle, sndBeeWater,
             sndPiano0, sndPiano1, sndPiano2, sndPiano3, sndPiano4, sndPiano5, sndPiano6, sndPiano7, sndPiano8,
             sndSkip, sndSineGun, sndOoff1, sndOoff2, sndOoff3, sndWhack,
-            sndComeonthen, sndParachute, sndBump, sndResurrector);
+            sndComeonthen, sndParachute, sndBump, sndResurrector, sndPlane);
 
     TAmmoType  = (amNothing, amGrenade, amClusterBomb, amBazooka, amBee, amShotgun, amPickHammer, // 6
             amSkip, amRope, amMine, amDEagle, amDynamite, amFirePunch, amWhip, // 13
--- a/hedgewars/uVariables.pas	Fri Feb 25 16:32:24 2011 -0500
+++ b/hedgewars/uVariables.pas	Sat Feb 26 15:56:11 2011 +0100
@@ -704,7 +704,8 @@
             (FileName:           'Comeonthen.ogg'; Path: ptVoices),// sndComeonthen
             (FileName:            'parachute.ogg'; Path: ptSounds),// sndParachute
             (FileName:                 'bump.ogg'; Path: ptSounds),// sndBump
-            (FileName: 'hogchant3.ogg'; Path: ptSounds) // sndResurrector
+            (FileName:            'hogchant3.ogg'; Path: ptSounds),// sndResurrector
+            (FileName:                'plane.ogg'; Path: ptSounds) // sndPlane
             );
 
     Ammoz: array [TAmmoType] of record
Binary file share/hedgewars/Data/Sounds/plane.ogg has changed