Make the sound engine less dumb about music. Fix the music player not knowing about already playing tracks.

This commit is contained in:
Paul Chote
2011-05-18 18:58:33 +12:00
parent 42d8722cbc
commit c76d2e37dc
5 changed files with 32 additions and 30 deletions

View File

@@ -12,6 +12,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.GameRules;
using OpenRA.Traits;
using Tao.OpenAl;
@@ -24,7 +25,7 @@ namespace OpenRA
static ISoundSource rawSource;
static ISound music;
static ISound video;
static string currentMusic;
static MusicInfo currentMusic;
static ISoundSource LoadSound(string filename)
{
@@ -106,28 +107,30 @@ namespace OpenRA
static Action OnMusicComplete;
public static bool MusicPlaying { get; private set; }
public static MusicInfo CurrentMusic { get { return currentMusic; } }
public static void PlayMusic(string name)
public static void PlayMusic(MusicInfo m)
{
PlayMusicThen(name, () => { });
PlayMusicThen(m, () => { });
}
public static void PlayMusicThen(string name, Action then)
{
OnMusicComplete = then;
if (name == "" || name == null)
public static void PlayMusicThen(MusicInfo m, Action then)
{
if (m == null || !m.Exists)
return;
if (name == currentMusic && music != null)
OnMusicComplete = then;
if (m == currentMusic && music != null)
{
soundEngine.PauseSound(music, false);
return;
}
StopMusic();
currentMusic = name;
currentMusic = m;
MusicPlaying = true;
var sound = sounds[name];
var sound = sounds[m.Filename];
music = soundEngine.Play2D(sound, false, true, float2.Zero, MusicVolume);
}

View File

@@ -51,10 +51,10 @@ namespace OpenRA.Mods.RA
{
if (!Game.Settings.Game.ShellmapMusic ||
Info.Music == null ||
!Rules.Music[Info.Music].Exists)
!Rules.Music.ContainsKey(Info.Music))
return;
Sound.PlayMusicThen(Rules.Music[Info.Music].Filename, () => LoopMusic());
Sound.PlayMusicThen(Rules.Music[Info.Music], () => LoopMusic());
}
int ticks = 0;

View File

@@ -35,7 +35,7 @@ namespace OpenRA.Mods.Cnc
Scripting.Media.PlayFMVFullscreen(w, "gdi1.vqa",
() => Scripting.Media.PlayFMVFullscreen(w, "landing.vqa", () =>
{
Sound.PlayMusic(Rules.Music["aoi"].Filename);
Sound.PlayMusic(Rules.Music["aoi"]);
started = true;
}));
}

View File

@@ -13,6 +13,7 @@ using System.IO;
using System.Linq;
using System.Threading;
using OpenRA.FileFormats;
using OpenRA.GameRules;
using OpenRA.Graphics;
using OpenRA.Traits;
using OpenRA.Widgets;
@@ -22,10 +23,10 @@ namespace OpenRA.Mods.Cnc.Widgets
public class CncMusicPlayerLogic : IWidgetDelegate
{
bool installed;
string currentSong = null;
MusicInfo currentSong = null;
Widget panel;
string[] music;
string[] random;
MusicInfo[] music;
MusicInfo[] random;
[ObjectCreator.UseCtor]
public CncMusicPlayerLogic([ObjectCreator.Param] Widget widget,
@@ -34,7 +35,7 @@ namespace OpenRA.Mods.Cnc.Widgets
panel = widget.GetWidget("MUSIC_PANEL");
BuildMusicTable(panel);
currentSong = GetNextSong();
currentSong = Sound.CurrentMusic ?? GetNextSong();
installed = Rules.Music.Where(m => m.Value.Exists).Any();
Func<bool> noMusic = () => !installed;
@@ -91,13 +92,12 @@ namespace OpenRA.Mods.Cnc.Widgets
panel.GetWidget<LabelWidget>("TIME_LABEL").GetText = () => (currentSong == null) ? "" :
"{0:D2}:{1:D2} / {2:D2}:{3:D2}".F((int)Sound.MusicSeekPosition / 60, (int)Sound.MusicSeekPosition % 60,
Rules.Music[currentSong].Length / 60, Rules.Music[currentSong].Length % 60);
currentSong.Length / 60, currentSong.Length % 60);
}
void BuildMusicTable(Widget panel)
{
music = Rules.Music.Where(a => a.Value.Exists)
.Select(a => a.Key).ToArray();
music = Rules.Music.Where(a => a.Value.Exists).Select(a => a.Value).ToArray();
random = music.Shuffle(Game.CosmeticRandom).ToArray();
var ml = panel.GetWidget<ScrollPanelWidget>("MUSIC_LIST");
@@ -110,7 +110,7 @@ namespace OpenRA.Mods.Cnc.Widgets
currentSong = song;
var item = ScrollItemWidget.Setup(itemTemplate, () => currentSong == song, () => { currentSong = song; Play(); });
item.GetWidget<LabelWidget>("TITLE").GetText = () => Rules.Music[song].Title;
item.GetWidget<LabelWidget>("TITLE").GetText = () => song.Title;
item.GetWidget<LabelWidget>("LENGTH").GetText = () => SongLengthLabel(song);
ml.AddChild(item);
}
@@ -122,7 +122,7 @@ namespace OpenRA.Mods.Cnc.Widgets
if (currentSong == null)
return;
Sound.PlayMusicThen(Rules.Music[currentSong].Filename, () =>
Sound.PlayMusicThen(currentSong, () =>
{
if (!Game.Settings.Sound.Repeat)
currentSong = GetNextSong();
@@ -147,13 +147,12 @@ namespace OpenRA.Mods.Cnc.Widgets
panel.GetWidget("BUTTON_PLAY").Visible = true;
}
string SongLengthLabel(string song)
string SongLengthLabel(MusicInfo song)
{
return "{0:D1}:{1:D2}".F(Rules.Music[song].Length / 60,
Rules.Music[song].Length % 60);
return "{0:D1}:{1:D2}".F(song.Length / 60, song.Length % 60);
}
string GetNextSong()
MusicInfo GetNextSong()
{
if (!music.Any())
return null;
@@ -163,14 +162,14 @@ namespace OpenRA.Mods.Cnc.Widgets
.Skip(1).FirstOrDefault() ?? songs.FirstOrDefault();
}
string GetPrevSong()
MusicInfo GetPrevSong()
{
if (!music.Any())
return null;
var songs = Game.Settings.Sound.Shuffle ? random : music;
return songs.Reverse().SkipWhile(m => m != currentSong)
.Skip(1).FirstOrDefault() ?? songs.FirstOrDefault();
.Skip(1).FirstOrDefault() ?? songs.Reverse().FirstOrDefault();
}
}

View File

@@ -45,7 +45,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
if (CurrentSong == null)
return true;
Sound.PlayMusicThen(Rules.Music[CurrentSong].Filename,
Sound.PlayMusicThen(Rules.Music[CurrentSong],
() => bg.GetWidget(Game.Settings.Sound.Repeat ? "BUTTON_PLAY" : "BUTTON_NEXT").OnMouseUp(new MouseInput()));
bg.GetWidget("BUTTON_PLAY").Visible = false;
bg.GetWidget("BUTTON_PAUSE").Visible = true;