adjust the scope of a few uSound variables and functions
authorkoda
Sat, 05 May 2012 15:17:16 +0100
changeset 7021 0c75512a8920
parent 7019 333afe233886
child 7022 107651b3c5a0
adjust the scope of a few uSound variables and functions
hedgewars/ArgParsers.inc
hedgewars/hwengine.pas
hedgewars/uGame.pas
hedgewars/uGears.pas
hedgewars/uLandObjects.pas
hedgewars/uSound.pas
hedgewars/uVariables.pas
--- a/hedgewars/ArgParsers.inc	Sat May 05 00:01:12 2012 +0400
+++ b/hedgewars/ArgParsers.inc	Sat May 05 15:17:16 2012 +0100
@@ -40,9 +40,10 @@
     val(ParamStr(4), cBits);
     val(ParamStr(5), ipcPort);
     cFullScreen:= ParamStr(6) = '1';
-    isSoundEnabled:= ParamStr(7) = '1';
-    isMusicEnabled:= ParamStr(8) = '1';
-    val(ParamStr(9), cInitVolume);
+    SetSound(ParamStr(7) = '1');
+    SetMusic(ParamStr(8) = '1');
+    val(ParamStr(9), tmp);
+    SetVolume(tmp);
     val(ParamStr(10), cTimerInterval);
     PathPrefix:= ParamStr(11);
     cShowFPS:= ParamStr(12) = '1';
@@ -101,9 +102,9 @@
 
 procedure setAudio(initialVolume: LongInt; musicEnabled: boolean; soundEnabled: boolean);
 begin
-    cInitVolume:= initialVolume;
-    isMusicEnabled:= musicEnabled;
-    isSoundEnabled:= soundEnabled
+    SetVolume(initialVolume);
+    SetMusic(musicEnabled);
+    SetSound(soundEnabled);
 end;
 
 procedure setAudioWithParameters(initialVolumeParam: string; musicEnabledParam: string; soundEnabledParam: string);
@@ -231,8 +232,8 @@
                             if ParamStr(paramIndex) = '--stats-only'  then
                                 begin
                                 cOnlyStats:= true;
-                                isSoundEnabled:= false;
-                                isMusicEnabled:= false;
+                                SetSound(false);
+                                SetMusic(false);
                                 cReducedQuality:= $FFFFFFFF xor rqLowRes; // HACK
                                 paramIndex:= paramIndex + 1
                                 end
--- a/hedgewars/hwengine.pas	Sat May 05 00:01:12 2012 +0400
+++ b/hedgewars/hwengine.pas	Sat May 05 15:17:16 2012 +0100
@@ -71,12 +71,8 @@
             StoreLoad(false);
             InitWorld;
             ResetKbd;
-            SoundLoad;
             if GameType = gmtSave then
-                begin
-                isSEBackup:= isSoundEnabled;
-                isSoundEnabled:= false
-                end;
+                SetSound(false);
             FinishProgress;
             PlayMusic;
             SetScale(zoom);
@@ -310,8 +306,8 @@
         cLocale := Copy(cLocaleFName,1,2);
         
     UserNick:= gameArgs[5];
-    isSoundEnabled:= gameArgs[6] = '1';
-    isMusicEnabled:= gameArgs[7] = '1';
+    SetSound(gameArgs[6] = '1');
+    SetMusic(gameArgs[7] = '1');
     cAltDamage:= gameArgs[8] = '1';
     PathPrefix:= gameArgs[9];
     UserPathPrefix:= '../Documents';
@@ -394,17 +390,14 @@
 
     InitTeams();
     AssignStores();
-
-    if isSoundEnabled then
-        InitSound();
+    InitSound();
 
     isDeveloperMode:= false;
-
     TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
-
     ParseCommand('rotmask', true);
 
     MainLoop();
+
     // clean up SDL and GL context
     OnDestroy();
     // clean up all the other memory allocated
--- a/hedgewars/uGame.pas	Sat May 05 00:01:12 2012 +0400
+++ b/hedgewars/uGame.pas	Sat May 05 15:17:16 2012 +0100
@@ -79,7 +79,7 @@
                         RestoreTeamsFromSave;
                         SetBinds(CurrentTeam^.Binds);
                         //CurrentHedgehog^.Gear^.Message:= 0; <- produces bugs with further save restoring and demos
-                        isSoundEnabled:= isSEBackup;
+                        SetSound;   // restore previous sound state
                         PlayMusic;
                         GameType:= gmtLocal;
                         AddVisualGear(0, 0, vgtTeamHealthSorter);
--- a/hedgewars/uGears.pas	Sat May 05 00:01:12 2012 +0400
+++ b/hedgewars/uGears.pas	Sat May 05 15:17:16 2012 +0100
@@ -76,6 +76,7 @@
     stAfterDelay, stChWin, stWater, stChWin2, stHealth,
     stSpawn, stNTurn);
     upd: Longword;
+    //SDMusic: shortstring;
 
 // For better maintainability the step handlers of gears are stored in
 // separate files.
@@ -313,8 +314,7 @@
                 AddCaption(trmsg[sidSuddenDeath], cWhiteColor, capgrpGameState);
                 playSound(sndSuddenDeath);
                 StopMusic //No SDMusic for now
-                    //MusicFN:= SDMusic;
-                    //ChangeMusic
+                    //ChangeMusic(SDMusic)
                     end
                 else if (TotalRounds < cSuddenDTurns) and (not isInMultiShoot) then
                     begin
@@ -1326,6 +1326,8 @@
     delay2:= 0;
     step:= stDelay;
     upd:= 0;
+
+    //SDMusic:= 'hell.ogg';
 end;
 
 procedure freeModule;
--- a/hedgewars/uLandObjects.pas	Sat May 05 00:01:12 2012 +0400
+++ b/hedgewars/uLandObjects.pas	Sat May 05 15:17:16 2012 +0100
@@ -551,7 +551,7 @@
         SDWaterOpacity:= WaterOpacity
         end
     else if key = 'music' then
-        MusicFN:= Trim(s)
+        SetMusic(Trim(s))
     else if key = 'clouds' then
         begin
         cCloudsNumber:= Word(StrToInt(Trim(s))) * cScreenSpace div LAND_WIDTH;
--- a/hedgewars/uSound.pas	Sat May 05 00:01:12 2012 +0400
+++ b/hedgewars/uSound.pas	Sat May 05 15:17:16 2012 +0100
@@ -35,25 +35,24 @@
 interface
 uses SDLh, uConsts, uTypes, sysutils;
 
-var MusicFN: shortstring; // music file name
-    previousVolume: LongInt; // cached volume value
-
 procedure initModule;
 procedure freeModule;
 
-procedure InitSound; // Initiates sound-system if isSoundEnabled.
-procedure ReleaseSound(complete: boolean); // Releases sound-system and used resources.
-procedure SoundLoad; // Preloads some sounds for performance reasons.
-
+procedure InitSound;                            // Initiates sound-system if isSoundEnabled.
+procedure ReleaseSound(complete: boolean);      // Releases sound-system and used resources.
+procedure SetSound;                             // Reset sound state to the previous state.
+procedure SetSound(enabled: boolean);           // Enable/disable sound-system and backup status.
 
 // MUSIC
 
-// Obvious music commands for music track specified in MusicFN.
-procedure PlayMusic;
-procedure PauseMusic;
-procedure ResumeMusic;
-procedure ChangeMusic; // Replaces music track with current MusicFN and plays it.
-procedure StopMusic; // Stops and releases the current track
+// Obvious music commands for music track
+procedure SetMusic(enabled: boolean);           // Enable/disable music.
+procedure SetMusic(musicname: shortstring);     // Enable/disable music and set name of musicfile to play.
+procedure PlayMusic;                            // Play music from the start.
+procedure PauseMusic;                           // Pause music.
+procedure ResumeMusic;                          // Resume music from pause point.
+procedure ChangeMusic(musicname: shortstring);  // Replaces music track with musicname and plays it.
+procedure StopMusic;                            // Stops and releases the current track.
 
 
 // SOUNDS
@@ -85,13 +84,16 @@
 
 // MISC
 
+// Set the initial volume
+procedure SetVolume(volume: LongInt);
+
 // Modifies the sound volume of the game by voldelta and returns the new volume level.
 function  ChangeVolume(voldelta: LongInt): LongInt;
 
 // Returns a pointer to the voicepack with the given name.
 function  AskForVoicepack(name: shortstring): Pointer;
 
-// Drastically lower the volume when we lose focus (and restore the previous value)
+// Drastically lower the volume when we lose focus (and restore the previous value).
 procedure DampenAudio;
 procedure UndampenAudio;
 
@@ -104,6 +106,13 @@
     voicepacks: array[0..cMaxTeams] of TVoicepack;
     defVoicepack: PVoicepack;
     Mus: PMixMusic = nil;
+    MusicFN: shortstring; // music file name
+    previousVolume: LongInt; // cached volume value
+    isMusicEnabled: boolean;
+    isSoundEnabled: boolean;
+    isSEBackup: boolean;
+    cInitVolume: LongInt;
+
 
 function  AskForVoicepack(name: shortstring): Pointer;
 var i: Longword;
@@ -151,44 +160,43 @@
 end;
 
 procedure InitSound;
-var i: TSound;
-    channels: LongInt;
+const channels: LongInt = {$IFDEF MOBILE}1{$ELSE}2{$ENDIF};
 begin
     if not isSoundEnabled then
         exit;
     WriteToConsole('Init sound...');
     isSoundEnabled:= SDL_InitSubSystem(SDL_INIT_AUDIO) >= 0;
 
-{$IFDEF MOBILE}
-    channels:= 1;
-{$ELSE}
-    channels:= 2;
-{$ENDIF}
-
     if isSoundEnabled then
         isSoundEnabled:= Mix_OpenAudio(44100, $8010, channels, 1024) = 0;
 
-    WriteToConsole('Init SDL_mixer... ');
-    SDLTry(Mix_Init(MIX_INIT_OGG) <> 0, true);
-    WriteLnToConsole(msgOK);
-
     if isSoundEnabled then
         WriteLnToConsole(msgOK)
     else
         WriteLnToConsole(msgFailed);
 
+    WriteToConsole('Init SDL_mixer... ');
+    SDLTry(Mix_Init(MIX_INIT_OGG) <> 0, true);
+    WriteLnToConsole(msgOK);
+
     Mix_AllocateChannels(Succ(chanTPU));
-    if isMusicEnabled then
-        Mix_VolumeMusic(50);
-    for i:= Low(TSound) to High(TSound) do
-        lastChan[i]:= -1;
+    ChangeVolume(cInitVolume);
+    defVoicepack:= AskForVoicepack('Default');
+end;
 
-    Volume:= 0;
-    ChangeVolume(cInitVolume)
+procedure SetSound;
+begin
+    isSoundEnabled:= isSEBackup;
+end;
+
+procedure SetSound(enabled: boolean);
+begin
+    isSEBackup:= isSoundEnabled;
+    isSoundEnabled:= enabled;
 end;
 
 // when complete is false, this procedure just releases some of the chucks on inactive channels
-// this way music is not stopped, nor are chucks currently being plauyed
+// in this way music is not stopped, nor are chucks currently being played
 procedure ReleaseSound(complete: boolean);
 var i: TSound;
     t: Longword;
@@ -224,41 +232,6 @@
         end;
 end;
 
-procedure SoundLoad;
-var i: TSound;
-    t: Longword;
-begin
-    if not isSoundEnabled then
-        exit;
-
-    defVoicepack:= AskForVoicepack('Default');
-
-    // initialize all voices to nil so that they can be loaded when needed
-    for t:= 0 to cMaxTeams do
-        if voicepacks[t].name <> '' then
-            for i:= Low(TSound) to High(TSound) do
-                voicepacks[t].chunks[i]:= nil;
-
-    for i:= Low(TSound) to High(TSound) do
-    begin
-        defVoicepack^.chunks[i]:= nil;
-        (* this is not necessary when SDL_mixer is compiled with USE_OGG_TREMOR
-        // preload all the big sound files (>32k) that would otherwise lockup the game
-        if (i in [sndBeeWater, sndBee, sndCake, sndHellishImpact1, sndHellish, sndHomerun,
-                  sndMolotov, sndMortar, sndRideOfTheValkyries, sndYoohoo])
-            and (Soundz[i].Path <> ptVoices) and (Soundz[i].FileName <> '') then
-        begin
-            s:= UserPathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
-            if not FileExists(s) then s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
-            WriteToConsole(msgLoading + s + ' ');
-            defVoicepack^.chunks[i]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
-            SDLTry(defVoicepack^.chunks[i] <> nil, true);
-            WriteLnToConsole(msgOK);
-        end;*)
-    end;
-
-end;
-
 procedure PlaySound(snd: TSound);
 begin
     PlaySound(snd, nil, false);
@@ -466,6 +439,11 @@
     SDLTry(Mix_FadeInMusic(Mus, -1, 3000) <> -1, false)
 end;
 
+procedure SetVolume(volume: LongInt);
+begin
+    cInitVolume:= volume;
+end;
+
 function ChangeVolume(voldelta: LongInt): LongInt;
 begin
     ChangeVolume:= 0;
@@ -475,7 +453,9 @@
     inc(Volume, voldelta);
     if Volume < 0 then
         Volume:= 0;
+    // apply Volume to all channels
     Mix_Volume(-1, Volume);
+    // get assigned Volume
     Volume:= Mix_Volume(-1, -1);
     if isMusicEnabled then
         Mix_VolumeMusic(Volume * 4 div 8);
@@ -490,7 +470,19 @@
 
 procedure UndampenAudio;
 begin
-ChangeVolume(previousVolume - Volume);
+    ChangeVolume(previousVolume - Volume);
+end;
+
+procedure SetMusic(enabled: boolean);
+begin
+    isMusicEnabled:= enabled;
+    MusicFN:= '';
+end;
+
+procedure SetMusic(musicname: shortstring);
+begin
+    isMusicEnabled:= not (musicname = '');    
+    MusicFN:= musicname;
 end;
 
 procedure PauseMusic;
@@ -511,8 +503,9 @@
         Mix_ResumeMusic(Mus);
 end;
 
-procedure ChangeMusic;
+procedure ChangeMusic(musicname: shortstring);
 begin
+    MusicFN:= musicname;
     if (MusicFN = '') or (not isMusicEnabled) then
         exit;
 
@@ -543,9 +536,32 @@
 end;
 
 procedure initModule;
+var t: LongInt;
+    i: TSound;
 begin
     RegisterVariable('voicepack', @chVoicepack, false);
+
     MusicFN:='';
+    isMusicEnabled:= true;
+    isSoundEnabled:= true;
+    isSEBackup:= isSoundEnabled;
+    cInitVolume:= 100;
+    Volume:= 0;
+
+    for i:= Low(TSound) to High(TSound) do
+        lastChan[i]:= -1;
+
+    // initialize all voices to nil so that they can be loaded lazily
+    for t:= 0 to cMaxTeams do
+        if voicepacks[t].name <> '' then
+            for i:= Low(TSound) to High(TSound) do
+                voicepacks[t].chunks[i]:= nil;
+
+    (* on MOBILE SDL_mixer has to be compiled against Tremor (USE_OGG_TREMOR)
+       or sound files bigger than 32k will lockup the game*)
+    for i:= Low(TSound) to High(TSound) do
+        defVoicepack^.chunks[i]:= nil;
+
 end;
 
 procedure freeModule;
--- a/hedgewars/uVariables.pas	Sat May 05 00:01:12 2012 +0400
+++ b/hedgewars/uVariables.pas	Sat May 05 15:17:16 2012 +0100
@@ -37,11 +37,8 @@
     cBits           : LongInt     = 32;
     ipcPort         : Word        = 0;
     cFullScreen     : boolean     = false;
-    isSoundEnabled  : boolean     = true;
-    isMusicEnabled  : boolean     = false;
     cLocaleFName    : shortstring = 'en.txt';
     cLocale         : shortstring = 'en';
-    cInitVolume     : LongInt     = 100;
     cTimerInterval  : LongInt     = 8;
     PathPrefix      : shortstring = './';
     UserPathPrefix  : shortstring = './';
@@ -66,7 +63,6 @@
     isInLag         : boolean;
     isPaused        : boolean;
 
-    isSEBackup      : boolean;
     isInMultiShoot  : boolean;
     isSpeed         : boolean;
     isFirstFrame    : boolean;
@@ -134,7 +130,6 @@
     LAND_HEIGHT      : Word;
     LAND_WIDTH_MASK  : LongWord;
     LAND_HEIGHT_MASK : LongWord;
-    cMaxCaptions     : LongInt;
 
     cLeftScreenBorder     : LongInt;
     cRightScreenBorder    : LongInt;
@@ -184,7 +179,6 @@
 
     WaterColorArray : array[0..3] of HwColor4f;
     SDWaterColorArray : array[0..3] of HwColor4f;
-    SDMusic         : shortstring;
     SDTint          : LongInt;
 
     CursorPoint     : TPoint;
@@ -2547,7 +2541,6 @@
     SDWaterColorArray[1]:= SDWaterColorArray[0];
     SDWaterColorArray[3]:= SDWaterColorArray[2];
 
-    SDMusic:= 'hell.ogg';
     SDTint:= $80;
 
     cDrownSpeed.QWordValue  := 257698038;       // 0.06
@@ -2615,7 +2608,6 @@
     isSpeed         := false;
     fastUntilLag    := false;
     isFirstFrame    := true;
-    isSEBackup      := true;
     autoCameraOn    := true;
     cScriptName     := '';
     cSeed           := '';
@@ -2638,11 +2630,6 @@
     cRightScreenBorder:= round(cMinZoomLevel * cScreenWidth + LAND_WIDTH);
     cScreenSpace:= cRightScreenBorder - cLeftScreenBorder;
 
-    if isPhone() then
-        cMaxCaptions:= 3
-    else
-        cMaxCaptions:= 4;
-
     vobFrameTicks:= 99999;
     vobFramesCount:= 4;
     vobCount:= 0;
@@ -2676,10 +2663,7 @@
     cBits           := 32;
     ipcPort         := 0;
     cFullScreen     := false;
-    isSoundEnabled  := true;
-    isMusicEnabled  := false;
     cLocaleFName    := 'en.txt';
-    cInitVolume     := 100;
     cTimerInterval  := 8;
     PathPrefix      := './';
     UserPathPrefix  := './';