Add F1 debug menu with paused diagnostics and scene controls
This commit is contained in:
1 parent
8d60a68511
commit
3078b8afd0
21 files changed
+809
-36
No files matched your search
@@ -10,6 +10,7 @@ public sealed class SceneAudio : IDisposable
|
||||
private (string Database, int Id)? selectedMusic;
|
||||
private readonly List<SdlAudio> effects = [];
|
||||
private readonly Dictionary<string, SdlAudio> loops = new(StringComparer.OrdinalIgnoreCase);
|
||||
public bool Paused { get; private set; }
|
||||
public int EffectCount => effects.Count;
|
||||
public int LoopCount => loops.Count;
|
||||
public int QueuedEffectBytes => effects.Sum(effect => effect.QueuedBytes) + loops.Values.Sum(effect => effect.QueuedBytes);
|
||||
@@ -26,13 +27,13 @@ public sealed class SceneAudio : IDisposable
|
||||
{
|
||||
StopMusic();
|
||||
if (selectedMusic is { } selection && resources.Wave(selection.Database, selection.Id) is { } wave)
|
||||
music = new(wave.Path, true);
|
||||
music = new(wave.Path, true, Paused);
|
||||
}
|
||||
public void Speech(string database, int id)
|
||||
{
|
||||
StopSpeech();
|
||||
var wave = resources.Wave(database, id) ?? throw new FileNotFoundException($"Missing prepared speech: {database}:{id}");
|
||||
speech = new(wave.Path);
|
||||
speech = new(wave.Path, paused: Paused);
|
||||
}
|
||||
public void StopSpeech() { speech?.Dispose(); speech = null; }
|
||||
// Default current bank set by Jimmy PostLoad423642..656; positional attenuation
|
||||
@@ -41,11 +42,11 @@ public sealed class SceneAudio : IDisposable
|
||||
{
|
||||
var wave = resources.Wave("SoundEffects.omt", id)
|
||||
?? throw new FileNotFoundException($"Missing original default sound effect {id}");
|
||||
if (loopOwner == null) effects.Add(new(wave.Path));
|
||||
if (loopOwner == null) effects.Add(new(wave.Path, paused: Paused));
|
||||
else
|
||||
{
|
||||
StopLoop(loopOwner);
|
||||
loops.Add(loopOwner, new(wave.Path, true));
|
||||
loops.Add(loopOwner, new(wave.Path, true, Paused));
|
||||
}
|
||||
}
|
||||
public void StopLoop(string owner)
|
||||
@@ -60,10 +61,19 @@ public sealed class SceneAudio : IDisposable
|
||||
}
|
||||
public void Pump()
|
||||
{
|
||||
if (Paused) return;
|
||||
music?.Pump(); speech?.Pump();
|
||||
foreach (var effect in loops.Values) effect.Pump();
|
||||
for (var i = effects.Count - 1; i >= 0; i--)
|
||||
if (effects[i].QueuedBytes == 0) { effects[i].Dispose(); effects.RemoveAt(i); }
|
||||
}
|
||||
/// <summary>Modern debug pause; new streams also inherit this state.</summary>
|
||||
public void SetPaused(bool paused)
|
||||
{
|
||||
music?.SetPaused(paused); speech?.SetPaused(paused);
|
||||
foreach (var effect in effects) effect.SetPaused(paused);
|
||||
foreach (var effect in loops.Values) effect.SetPaused(paused);
|
||||
Paused = paused;
|
||||
}
|
||||
public void Dispose() { StopEffects(); StopSpeech(); StopMusic(); }
|
||||
}
|
||||
@@ -9,6 +9,8 @@ public sealed class SdlAudio : IDisposable
|
||||
private nint data;
|
||||
private int length;
|
||||
private readonly bool loop;
|
||||
private bool audioInitialized;
|
||||
public bool Paused { get; private set; }
|
||||
public int QueuedBytes
|
||||
{
|
||||
get
|
||||
@@ -17,10 +19,11 @@ public sealed class SdlAudio : IDisposable
|
||||
return bytes >= 0 ? bytes : throw new InvalidOperationException(Sdl.Error);
|
||||
}
|
||||
}
|
||||
public SdlAudio(string path,bool loop=false)
|
||||
public SdlAudio(string path,bool loop=false,bool paused=false)
|
||||
{
|
||||
this.loop=loop;
|
||||
if(!Sdl.SDL_InitSubSystem(Sdl.InitAudio))throw new InvalidOperationException(Sdl.Error);
|
||||
audioInitialized = true;
|
||||
try
|
||||
{
|
||||
if(!Sdl.SDL_LoadWAV(Path.GetFullPath(path),out var spec,out data,out var size))throw new InvalidOperationException(Sdl.Error);
|
||||
@@ -28,17 +31,28 @@ public sealed class SdlAudio : IDisposable
|
||||
stream=Sdl.SDL_OpenAudioDeviceStream(uint.MaxValue,ref spec,0,0);
|
||||
if(stream==0)throw new InvalidOperationException(Sdl.Error);
|
||||
Queue();
|
||||
if(!Sdl.SDL_ResumeAudioStreamDevice(stream))throw new InvalidOperationException(Sdl.Error);
|
||||
// SDL_OpenAudioDeviceStream starts paused. Keep it paused from its
|
||||
// first sample when created while a debug menu has stopped playback.
|
||||
Paused = true;
|
||||
SetPaused(paused);
|
||||
Console.WriteLine($"SDL3 WAV stream: {spec.Frequency} Hz, {spec.Channels} channel(s), {length} source bytes.");
|
||||
}
|
||||
catch{Dispose();throw;}
|
||||
}
|
||||
public void SetPaused(bool paused)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(stream == 0, this);
|
||||
if (Paused == paused) return;
|
||||
var changed = paused ? Sdl.SDL_PauseAudioStreamDevice(stream) : Sdl.SDL_ResumeAudioStreamDevice(stream);
|
||||
if (!changed) throw new InvalidOperationException(Sdl.Error);
|
||||
Paused = paused;
|
||||
}
|
||||
private void Queue(){if(!Sdl.SDL_PutAudioStreamData(stream,data,length))throw new InvalidOperationException(Sdl.Error);}
|
||||
public void Pump(){if(loop && QueuedBytes<length)Queue();}
|
||||
public void Pump(){if(!Paused && loop && QueuedBytes<length)Queue();}
|
||||
public void Dispose()
|
||||
{
|
||||
if(stream!=0){Sdl.SDL_DestroyAudioStream(stream);stream=0;}
|
||||
if(data!=0){Sdl.SDL_free(data);data=0;}
|
||||
Sdl.SDL_QuitSubSystem(Sdl.InitAudio);
|
||||
if(audioInitialized){Sdl.SDL_QuitSubSystem(Sdl.InitAudio);audioInitialized=false;}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user