From ad9d6ec5f2ca4312a49cb0f56ed732b9901aaaf4 Mon Sep 17 00:00:00 2001 From: Dorus Date: Thu, 9 May 2024 12:40:44 +0200 Subject: [PATCH] Introduce dispose pattern for DrawingSurface --- Yafc.UI/Core/DrawingSurface.cs | 37 +++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/Yafc.UI/Core/DrawingSurface.cs b/Yafc.UI/Core/DrawingSurface.cs index 0d3bae2f..6ad4a1ae 100644 --- a/Yafc.UI/Core/DrawingSurface.cs +++ b/Yafc.UI/Core/DrawingSurface.cs @@ -46,16 +46,13 @@ protected DrawingSurface(float pixelsPerUnit) { internal static RenderingUtils.BlitMapping[] blitMapping; private SDL.SDL_Rect clipRect; + private bool disposedValue; + internal abstract void DrawIcon(SDL.SDL_Rect position, Icon icon, SchemeColor color); internal abstract void DrawBorder(SDL.SDL_Rect position, RectangleBorder type); public abstract Window window { get; } - public virtual void Dispose() { - SDL.SDL_DestroyRenderer(renderer); - renderer = IntPtr.Zero; - } - public TextureHandle BeginRenderToTexture(out SDL.SDL_Rect textureSize) { _ = SDL.SDL_GetRendererOutputSize(renderer, out int w, out int h); textureSize = new SDL.SDL_Rect { w = w, h = h }; @@ -95,6 +92,32 @@ public TextureHandle CreateTextureFromSurface(IntPtr surface) { public TextureHandle CreateTexture(uint format, int access, int w, int h) { return new TextureHandle(this, SDL.SDL_CreateTexture(renderer, format, access, w, h)); } + + protected virtual void Dispose(bool disposing) { + if (!disposedValue) { + if (disposing) { + // dispose managed state (managed objects) + } + + // free unmanaged resources (unmanaged objects) and override finalizer + SDL.SDL_DestroyRenderer(renderer); + renderer = IntPtr.Zero; + // set large fields to null + + disposedValue = true; + } + } + + ~DrawingSurface() { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: false); + } + + public virtual void Dispose() { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } } public abstract class SoftwareDrawingSurface : DrawingSurface { @@ -158,10 +181,6 @@ public override void Dispose() { GC.SuppressFinalize(this); } - ~MemoryDrawingSurface() { - Dispose(); - } - public override Window window => null; public void SavePng(string filename) {