Okay, here's some of the stuff that I've done.
I added this in the GameForm.cs so when the game loses focus, the music stops playing:
//this part in the run function of GameForm:
// Display is minimized or inactive, wait until it is restored
Application.DoEvents();
if (GameDisplay != null)
GameDisplay.SwapBuffers();
//was replaced by:
// Display is minimized or inactive, wait until it is restored
Application.DoEvents();
if (GameDisplay != null)
GameDisplay.SwapBuffers();
//don't play music if the game is paused
if(CurrentMusic != null)
CustomObjects.FMODBase.StopSound(CurrentMusic);
if(NextMusic != null)
CustomObjects.FMODBase.StopSound(NextMusic);
System.Threading.Thread.Sleep(0);
continue;
//this part:
OutputDebugInfo();
//was replaced by:
CurrentFadeTime = ManageMusic(CurrentFadeTime);
OutputDebugInfo();
I could not add the previous code in a another file (partial class) but the next code I added in another file using the partial class, so this still goes into GameForm.cs
public partial class GameForm : Form
{
#region music extra stuff
public const int FadeDuration = 120; //in frames
public int CurrentFadeTime = FadeDuration;
public const float MaxVolume = 0.60f;
public CustomObjects.FMODBase CurrentMusic;
public CustomObjects.FMODBase NextMusic;
//this part is useful for my project only
//It could be replaced by another function or completely dropped
public CustomObjects.FMODBase GetMusicToPlay()
{
//Verify if music is forced
if (Counter.ForceMusic.CurrentValue != 0)
{
switch(Counter.ForceMusic.CurrentValue)
{
case 1:
return TermogentSwampsNormal.Value;
case 2:
return ElderGodLairNormal.Value;
case 3:
return VoradorMansionNormal.Value;
case 4:
return HumanCityNormal.Value;
case 5:
return VampireCitadelNormal.Value;
case 6:
return HyldenDimensionNormal.Value;
case 7:
return PillarsNosgothNormal.Value;
case 8:
return HyldenFortressNormal.Value;
case 9:
return MenuNormal.Value;
case 10:
return BossNormal.Value;
case 11:
return ElderGodNormal.Value;
case 12:
return IntroNormal.Value;
default:
return NoMusic.Value;
}
}
else
{
//verify if in any menu
if(OverlayMap is MiniMap_Map)
{
//not in menu so play level music
switch(CurrentMap.GetType().Name.Substring(0, 2).ToUpper())
{
case "VM":
return VoradorMansionNormal.Value;
case "TS":
return TermogentSwampsNormal.Value;
case "EL":
return ElderGodLairNormal.Value;
case "HC":
return HumanCityNormal.Value;
case "HD":
return HyldenDimensionNormal.Value;
case "VC":
return VampireCitadelNormal.Value;
case "PN":
return PillarsNosgothNormal.Value;
case "HF":
return HyldenFortressNormal.Value;
default:
return null;
}
}
else
{
//in menu so play menu music
return MenuNormal.Value;
}
}
}
//this is the important part. Plays the music and manages the fades
public int ManageMusic(int CurrentFadeTime)
{
//Find the music to play
CustomObjects.FMODBase Music = GetMusicToPlay(); //this part could be dropped and a custom function added to change the music or to pass it to this function
if(Music != CurrentMusic && CurrentFadeTime == FadeDuration) //don't want to manage multiple fades at once
{
if(CurrentMusic == null)
CurrentMusic = Music;
else
NextMusic = Music;
}
if(CurrentMusic != null)
{
//Assess music situation
if((NextMusic != null) && (CurrentMusic != NextMusic) && (CurrentFadeTime == FadeDuration))
{
//we are changing the music
CurrentFadeTime = 0;
}
//Assess fade situation
if(CurrentFadeTime == FadeDuration)
{
//No fading right now, keep playing current music at full volume
CurrentMusic.Volume = MaxVolume;
FMODBase.PlaySound(CurrentMusic, SoundReplay.Continue);
}
else if(CurrentFadeTime == FadeDuration - 1)
{
//The fading is finishing, switch the musics and play the new current music at full volume
FMODBase.StopSound(CurrentMusic);
CurrentMusic = NextMusic;
CurrentMusic.Volume = MaxVolume;
FMODBase.PlaySound(CurrentMusic, SoundReplay.Continue);
CurrentFadeTime++;
NextMusic = null;
}
else
{
//A fading occurs
//calculate the volume
float FadeAmount = (MaxVolume * CurrentFadeTime) / FadeDuration;
CurrentMusic.Volume = MaxVolume - FadeAmount;
NextMusic.Volume = FadeAmount;
FMODBase.PlaySound(CurrentMusic, SoundReplay.Continue);
FMODBase.PlaySound(NextMusic, SoundReplay.Continue);
CurrentFadeTime++;
}
}
return CurrentFadeTime;
}
}
I also added a field in fmodbase.cs
private float volume;
And modified the Volume property like this
//this part:
// Override this member to override default volume.
protected virtual float Volume
{
get
{
return 1.0f;
}
}
//was replaced by:
public float Volume
{
get
{
return volume;
}
set
{
volume = value;
}
}
//So, of course, I don't override the Volume properties in my music files anymore, but I still do for my sound effects.
It would be even better to add controls in the options menu to set a max volume to sound effects and a max volume to music rather than hardcoding it in GameForm like I did.
I will leave that to your more than able hands bluemonkmn. I suppose you would also prefer to include all of this code into fmodbase.cs only, but I don't see how I could do that. Since SGDK2 is your application, I'll let you see if any of this is of interest to you or not.
I hope it's not too messy!
Thanks bluemonkmn!