From 163a2f1a1076d518b04ee37911bdd40afe8b9b2c Mon Sep 17 00:00:00 2001 From: Sarah B Date: Sat, 20 Jan 2024 15:28:48 -0800 Subject: Added finalizers --- Bitmap.cs | 7 +++++++ Drawable.cs | 16 +++++++++++++++- Main.cs | 1 + Mouse.cs | 3 +-- Oneko.cs | 3 +-- OnekoNet.cs | 4 ++-- 6 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Bitmap.cs b/Bitmap.cs index 0575497..91eabb9 100644 --- a/Bitmap.cs +++ b/Bitmap.cs @@ -123,8 +123,15 @@ class Bitmap : IDisposable return SerializedData; } + bool disposed = false; + public void Dispose() { + if (disposed) return; Raylib.UnloadTexture(Texture); + GC.SuppressFinalize(this); + disposed = true; } + + ~Bitmap() => Dispose(); } \ No newline at end of file diff --git a/Drawable.cs b/Drawable.cs index f01c478..67e4344 100644 --- a/Drawable.cs +++ b/Drawable.cs @@ -34,11 +34,25 @@ abstract class Drawable : IDisposable public static void DisposeAll() { foreach (Drawable drawable in Drawables.ToArray()) drawable?.Dispose(); + Drawables.Clear(); } public abstract void Draw(); public abstract void Update(float delta); - public virtual void Dispose() => Drawables.Remove(this); + bool disposed = false; + + public void Dispose() + { + if (disposed) return; + Unload(); + Drawables.Remove(this); + GC.SuppressFinalize(this); + disposed = true; + } + + protected abstract void Unload(); + + ~Drawable() => Dispose(); } \ No newline at end of file diff --git a/Main.cs b/Main.cs index e7dee63..77a7679 100644 --- a/Main.cs +++ b/Main.cs @@ -64,6 +64,7 @@ static class OnekoOnline Raylib.EndTextureMode(); Raylib.BeginDrawing(); + //Dunno why, but it renders upside down, so I flip it here Raylib.DrawTexturePro(RenderTexture.Texture, new Rectangle(0f,0f,WindowX,-WindowY), new Rectangle(0,0,WindowX*WindowScale,WindowY*WindowScale), Vector2.Zero,0f,Color.WHITE); Raylib.EndDrawing(); } diff --git a/Mouse.cs b/Mouse.cs index 45dbebf..3b82689 100644 --- a/Mouse.cs +++ b/Mouse.cs @@ -48,10 +48,9 @@ abstract class Mouse : Drawable Raylib.DrawTexture(Cursor.Texture, (int)Position.X, (int)Position.Y, Color.WHITE); } - public override void Dispose() + protected override void Unload() { Cursor.Dispose(); allMice.Remove(this); - base.Dispose(); } } \ No newline at end of file diff --git a/Oneko.cs b/Oneko.cs index 6ddefba..0ebb38e 100644 --- a/Oneko.cs +++ b/Oneko.cs @@ -59,11 +59,10 @@ abstract class Oneko : Drawable Raylib.DrawTexturePro(SpriteSheet.Texture, Sprite, new Rectangle(Position.X, Position.Y, Size.X, Size.Y), Size/2, Rotation, ColorTint); } - public override void Dispose() + protected override void Unload() { allNekos.Remove(this); SpriteSheet.Dispose(); - base.Dispose(); } protected struct OnekoAnimation(Rectangle frame1, Rectangle frame2, byte animSpeed) diff --git a/OnekoNet.cs b/OnekoNet.cs index 81829c8..4a01ec8 100644 --- a/OnekoNet.cs +++ b/OnekoNet.cs @@ -49,10 +49,10 @@ class OnekoNet : Oneko } } - public override void Dispose() + protected override void Unload() { NetNekos.Remove(MyUser.Id); - base.Dispose(); + base.Unload(); } public static void DisconnectAll() -- cgit