when focus is lost lower the volume to a minimum
authorkoda
Sun, 08 Apr 2012 18:11:37 +0200
changeset 6862 87e060b69926
parent 6861 7a571b8caaee
child 6863 4d1ce4b4f7c6
when focus is lost lower the volume to a minimum
hedgewars/uSound.pas
hedgewars/uWorld.pas
--- a/hedgewars/uSound.pas	Sat Apr 07 22:13:15 2012 +0200
+++ b/hedgewars/uSound.pas	Sun Apr 08 18:11:37 2012 +0200
@@ -36,6 +36,7 @@
 uses SDLh, uConsts, uTypes, sysutils;
 
 var MusicFN: shortstring; // music file name
+    previousVolume: LongInt; // cached volume value
 
 procedure initModule;
 procedure freeModule;
@@ -90,6 +91,9 @@
 // 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)
+procedure DampenAudio;
+procedure UndampenAudio;
 
 implementation
 uses uVariables, uConsole, uUtils, uCommands, uDebug;
@@ -477,6 +481,17 @@
     ChangeVolume:= Volume * 100 div MIX_MAX_VOLUME
 end;
 
+procedure DampenAudio;
+begin
+previousVolume:= Volume;
+ChangeVolume(-Volume * 7 div 9);
+end;
+
+procedure UndampenAudio;
+begin
+ChangeVolume(previousVolume - Volume);
+end;
+
 procedure PauseMusic;
 begin
     if (MusicFN = '') or (not isMusicEnabled) then
--- a/hedgewars/uWorld.pas	Sat Apr 07 22:13:15 2012 +0200
+++ b/hedgewars/uWorld.pas	Sun Apr 08 18:11:37 2012 +0200
@@ -1712,6 +1712,9 @@
 begin
 if (not cHasFocus) and (GameState <> gsConfirm) then
     ParseCommand('quit', true);
+
+if not cHasFocus then DampenAudio()
+else UndampenAudio();
 end;
 
 procedure animateWidget(widget: POnScreenWidget; fade, showWidget: boolean);