From 1e6bcd5306a76d8049024d368e5c6acd076071ca Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 6 Aug 2018 01:30:16 +1000 Subject: [PATCH] Fix problems from last commit --- ClassicalSharp/2D/GuiElement.cs | 156 ++++++++++----------- ClassicalSharp/Audio/AudioPlayer.Sounds.cs | 15 +- ClassicalSharp/Audio/AudioPlayer.cs | 4 + 3 files changed, 80 insertions(+), 95 deletions(-) diff --git a/ClassicalSharp/2D/GuiElement.cs b/ClassicalSharp/2D/GuiElement.cs index 05a1b4e37..28b3fd27c 100644 --- a/ClassicalSharp/2D/GuiElement.cs +++ b/ClassicalSharp/2D/GuiElement.cs @@ -1,84 +1,72 @@ -// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using System.Drawing; -using OpenTK.Input; - -namespace ClassicalSharp.Gui { - public delegate void ClickHandler(Game g, Widget w); - - public abstract class GuiElement : IDisposable { - - protected Game game; - public GuiElement(Game game) { this.game = game; } - - public abstract void Init(); - - public abstract void Render(double delta); - - public abstract void Dispose(); - - /// Causes the gui element to recreate all of its sub-elements and/or textures. - /// Typically used when bitmap font changes. - public virtual void Recreate() { Dispose(); Init(); } - - public virtual bool HandlesKeyDown(Key key) { return false; } - - public virtual bool HandlesKeyPress(char key) { return false; } - - public virtual bool HandlesKeyUp(Key key) { return false; } - - public virtual bool HandlesMouseDown(int mouseX, int mouseY, MouseButton button) { return false; } - - public virtual bool HandlesMouseMove(int mouseX, int mouseY) { return false; } - - public virtual bool HandlesMouseScroll(float delta) { return false; } - - public virtual bool HandlesMouseUp(int mouseX, int mouseY, MouseButton button) { return false; } - - protected static int CalcPos(Anchor anchor, int offset, int size, int axisLen) { - if (anchor == Anchor.Min) return offset; - if (anchor == Anchor.Max) return axisLen - size - offset; - return (axisLen - size) / 2 + offset; - } - - public static bool Contains(int recX, int recY, int width, int height, int x, int y) { - return x >= recX && y >= recY && x < recX + width && y < recY + height; - } - } - - /// Represents a container of widgets and other 2D elements. - /// May cover the entire game window. - public abstract class Screen : GuiElement { - - public Screen(Game game) : base(game) { } - - public bool HandlesAllInput, BlocksWorld, HidesHud, RenderHudOver; - - public abstract void OnResize(); - - protected abstract void ContextLost(); - - protected abstract void ContextRecreated(); - } - - /// Represents an individual 2D gui component. - public abstract class Widget : GuiElement { - - public Widget(Game game) : base(game) { } - - public ClickHandler MenuClick; - public bool Active, Disabled; - public int X, Y, Width, Height; - public Anchor HorizontalAnchor, VerticalAnchor; - public int XOffset, YOffset; - - public virtual void Reposition() { - X = CalcPos(HorizontalAnchor, XOffset, Width, game.Width); - Y = CalcPos(VerticalAnchor, YOffset, Height, game.Height); - } - - public bool Contains(int x, int y) { - return GuiElement.Contains(X, Y, Width, Height, x, y); - } - } -} +// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 +using System; +using System.Drawing; +using OpenTK.Input; + +namespace ClassicalSharp.Gui { + public delegate void ClickHandler(Game g, Widget w); + + public abstract class GuiElement : IDisposable { + public GuiElement(Game game) { this.game = game; } + protected Game game; + + public abstract void Init(); + public abstract void Render(double delta); + public abstract void Dispose(); + + /// Causes the gui element to recreate all of its sub-elements and/or textures. + /// Typically used when bitmap font changes. + public virtual void Recreate() { Dispose(); Init(); } + + public virtual bool HandlesKeyDown(Key key) { return false; } + public virtual bool HandlesKeyPress(char key) { return false; } + public virtual bool HandlesKeyUp(Key key) { return false; } + + public virtual bool HandlesMouseDown(int mouseX, int mouseY, MouseButton button) { return false; } + public virtual bool HandlesMouseMove(int mouseX, int mouseY) { return false; } + public virtual bool HandlesMouseScroll(float delta) { return false; } + public virtual bool HandlesMouseUp(int mouseX, int mouseY, MouseButton button) { return false; } + + protected static int CalcPos(Anchor anchor, int offset, int size, int axisLen) { + if (anchor == Anchor.Min) return offset; + if (anchor == Anchor.Max) return axisLen - size - offset; + return (axisLen - size) / 2 + offset; + } + + public static bool Contains(int recX, int recY, int width, int height, int x, int y) { + return x >= recX && y >= recY && x < recX + width && y < recY + height; + } + } + + /// Represents a container of widgets and other 2D elements. + /// May cover the entire game window. + public abstract class Screen : GuiElement { + public Screen(Game game) : base(game) { } + + public bool HandlesAllInput, BlocksWorld, HidesHud, RenderHudOver; + + public abstract void OnResize(); + protected abstract void ContextLost(); + protected abstract void ContextRecreated(); + } + + /// Represents an individual 2D gui component. + public abstract class Widget : GuiElement { + public Widget(Game game) : base(game) { } + + public ClickHandler MenuClick; + public bool Active, Disabled; + public int X, Y, Width, Height; + public Anchor HorizontalAnchor, VerticalAnchor; + public int XOffset, YOffset; + + public virtual void Reposition() { + X = CalcPos(HorizontalAnchor, XOffset, Width, game.Width); + Y = CalcPos(VerticalAnchor, YOffset, Height, game.Height); + } + + public bool Contains(int x, int y) { + return GuiElement.Contains(X, Y, Width, Height, x, y); + } + } +} diff --git a/ClassicalSharp/Audio/AudioPlayer.Sounds.cs b/ClassicalSharp/Audio/AudioPlayer.Sounds.cs index 08cd25a09..0b0eeaf70 100644 --- a/ClassicalSharp/Audio/AudioPlayer.Sounds.cs +++ b/ClassicalSharp/Audio/AudioPlayer.Sounds.cs @@ -58,7 +58,7 @@ namespace ClassicalSharp.Audio { if (type == SoundType.Metal) format.SampleRate = (format.SampleRate * 6) / 5; else format.SampleRate = (format.SampleRate * 4) / 5; } else { - volume *= 0.50f; + volume *= 0.50f; if (type == SoundType.Metal) format.SampleRate = (format.SampleRate * 7) / 5; } @@ -74,8 +74,9 @@ namespace ClassicalSharp.Audio { for (int i = 0; i < monoOutputs.Length; i++) { IAudioOutput output = outputs[i]; if (output == null) { - outputs[i] = GetPlatformOut(); - output = outputs[i]; + output = GetPlatformOut(); + output.Create(1); + outputs[i] = output; } if (!output.IsFinished()) continue; @@ -95,14 +96,6 @@ namespace ClassicalSharp.Audio { } } - - IAudioOutput MakeSoundOutput(IAudioOutput[] outputs, int i) { - IAudioOutput output = GetPlatformOut(); - output.Create(1); - outputs[i] = output; - return output; - } - void PlaySound(IAudioOutput output, float volume) { try { output.SetVolume(volume); diff --git a/ClassicalSharp/Audio/AudioPlayer.cs b/ClassicalSharp/Audio/AudioPlayer.cs index 488bd0a14..ce577e7bd 100644 --- a/ClassicalSharp/Audio/AudioPlayer.cs +++ b/ClassicalSharp/Audio/AudioPlayer.cs @@ -85,10 +85,13 @@ namespace ClassicalSharp.Audio { musicOut.PlayStreaming(container); } catch (InvalidOperationException ex) { HandleMusicError(ex); + try { musicOut.Dispose(); } catch { } return; } catch (Exception ex) { ErrorHandler.LogError("AudioPlayer.DoMusicThread()", ex); game.Chat.Add("&cError while trying to play music file " + file); + try { musicOut.Dispose(); } catch { } + return; } } if (disposingMusic) break; @@ -120,6 +123,7 @@ namespace ClassicalSharp.Audio { void DisposeMusic() { disposingMusic = true; musicHandle.Set(); + DisposeOf(ref musicOut, ref musicThread); }