Merge Sound.Instance into MainForm.Sound and pass back to Program

now I don't have to feel bad about sweeping a global under the rug
This commit is contained in:
YoshiRulz 2020-12-06 05:57:23 +10:00
parent cf901753ba
commit eeae3f023f
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 21 additions and 13 deletions

View File

@ -273,7 +273,7 @@ namespace BizHawk.Client.EmuHawk
CloseRomContextMenuItem.Image = Properties.Resources.Close;
}
public MainForm(Config config, IGL gl, string[] args, out IMovieSession movieSession)
public MainForm(Config config, IGL gl, Action<Sound> updateGlobalSound, string[] args, out IMovieSession movieSession)
{
//do this threaded stuff early so it has plenty of time to run in background
Database.InitializeDatabase(Path.Combine(PathUtils.ExeDirectoryPath, "gamedb", "gamedb.txt"));
@ -281,6 +281,7 @@ namespace BizHawk.Client.EmuHawk
Config = config;
GL = gl;
_updateGlobalSound = updateGlobalSound;
InputManager = new InputManager
{
@ -894,10 +895,15 @@ namespace BizHawk.Client.EmuHawk
public GameInfo Game { get; private set; }
/// <remarks>don't use this, use <see cref="Sound"/></remarks>
private Sound _sound;
private readonly Action<Sound> _updateGlobalSound;
private Sound Sound
{
get => Sound.Instance;
set => Sound.Instance = value;
get => _sound;
set => _updateGlobalSound(_sound = value);
}
public CheatCollection CheatList { get; }

View File

@ -194,6 +194,8 @@ namespace BizHawk.Client.EmuHawk
var workingGL = TryInitIGL(initialConfig.DispMethod);
Sound globalSound = null;
if (!OSTC.IsUnixHost)
{
//WHY do we have to do this? some intel graphics drivers (ig7icd64.dll 10.18.10.3304 on an unknown chip on win8.1) are calling SetDllDirectory() for the process, which ruins stuff.
@ -212,7 +214,7 @@ namespace BizHawk.Client.EmuHawk
{
try
{
InitAndRunSingleInstance(initialConfig, workingGL, i => exitCode = i, args);
InitAndRunSingleInstance(initialConfig, workingGL, newSound => globalSound = newSound, i => exitCode = i, args);
}
catch (ObjectDisposedException)
{
@ -221,7 +223,7 @@ namespace BizHawk.Client.EmuHawk
}
else
{
var mf = new MainForm(initialConfig, workingGL, args, out var movieSession);
var mf = new MainForm(initialConfig, workingGL, newSound => globalSound = newSound, args, out var movieSession);
// var title = mf.Text;
mf.Show();
// mf.Text = title;
@ -252,8 +254,7 @@ namespace BizHawk.Client.EmuHawk
}
finally
{
Sound.Instance?.Dispose();
Sound.Instance = null;
globalSound?.Dispose();
workingGL.Dispose();
Input.Instance?.Adapter?.DeInitAll();
}
@ -322,13 +323,16 @@ namespace BizHawk.Client.EmuHawk
private readonly Action<int> _setExitCode;
private readonly Action<Sound> _updateGlobalSound;
private readonly string[] cmdArgs;
public SingleInstanceController(Config config, IGL gl, Action<int> setExitCode, string[] args)
public SingleInstanceController(Config config, IGL gl, Action<Sound> updateGlobalSound, Action<int> setExitCode, string[] args)
{
_config = config;
_gl = gl;
_setExitCode = setExitCode;
_updateGlobalSound = updateGlobalSound;
cmdArgs = args;
IsSingleInstance = true;
StartupNextInstance += this_StartupNextInstance;
@ -344,7 +348,7 @@ namespace BizHawk.Client.EmuHawk
protected override void OnCreateMainForm()
{
MainForm = new MainForm(_config, _gl, cmdArgs, out _);
MainForm = new MainForm(_config, _gl, _updateGlobalSound, cmdArgs, out _);
var title = MainForm.Text;
MainForm.Show();
MainForm.Text = title;
@ -352,7 +356,7 @@ namespace BizHawk.Client.EmuHawk
}
}
private static void InitAndRunSingleInstance(Config config, IGL gl, Action<int> setExitCode, string[] args)
=> new SingleInstanceController(config, gl, setExitCode, args).Run();
private static void InitAndRunSingleInstance(Config config, IGL gl, Action<Sound> updateGlobalSound, Action<int> setExitCode, string[] args)
=> new SingleInstanceController(config, gl, updateGlobalSound, setExitCode, args).Run();
}
}

View File

@ -11,8 +11,6 @@ namespace BizHawk.Client.EmuHawk
/// <remarks>TODO rename to <c>HostAudioManager</c></remarks>
public class Sound : IHostAudioManager, IDisposable
{
public static Sound Instance;
public int SampleRate { get; } = 44100;
public int BytesPerSample { get; } = 2;