Clean up sound devices on game exit.

This commit is contained in:
Paul Chote
2015-09-20 14:36:45 +01:00
parent ef55d646f7
commit d112083c44
5 changed files with 27 additions and 9 deletions

View File

@@ -21,7 +21,7 @@ using OpenTK.Audio.OpenAL;
namespace OpenRA.Platforms.Default
{
class OpenAlSoundEngine : ISoundEngine
sealed class OpenAlSoundEngine : ISoundEngine
{
public SoundDevice[] AvailableDevices()
{
@@ -49,6 +49,7 @@ namespace OpenRA.Platforms.Default
const int GroupDistanceSqr = GroupDistance * GroupDistance;
const int PoolSize = 32;
IntPtr device;
float volume = 1f;
Dictionary<int, PoolSlot> sourcePool = new Dictionary<int, PoolSlot>();
@@ -88,16 +89,16 @@ namespace OpenRA.Platforms.Default
else
Console.WriteLine("Using default device");
var dev = Alc.OpenDevice(Game.Settings.Sound.Device);
if (dev == IntPtr.Zero)
device = Alc.OpenDevice(Game.Settings.Sound.Device);
if (device == IntPtr.Zero)
{
Console.WriteLine("Failed to open device. Falling back to default");
dev = Alc.OpenDevice(null);
if (dev == IntPtr.Zero)
device = Alc.OpenDevice(null);
if (device == IntPtr.Zero)
throw new InvalidOperationException("Can't create OpenAL device");
}
var ctx = Alc.CreateContext(dev, (int[])null);
var ctx = Alc.CreateContext(device, (int[])null);
if (ctx == ContextHandle.Zero)
throw new InvalidOperationException("Can't create OpenAL context");
Alc.MakeContextCurrent(ctx);
@@ -286,6 +287,15 @@ namespace OpenRA.Platforms.Default
AL.Listener(ALListenerfv.Orientation, ref orientation);
AL.Listener(ALListenerf.EfxMetersPerUnit, .01f);
}
public void Dispose()
{
if (device == IntPtr.Zero)
return;
Alc.CloseDevice(device);
device = IntPtr.Zero;
}
}
class OpenAlSoundSource : ISoundSource