Skip to content

Commit

Permalink
Introduce dispose pattern for DrawingSurface
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorus authored and shpaass committed May 10, 2024
1 parent 128596e commit ad9d6ec
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions Yafc.UI/Core/DrawingSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -158,10 +181,6 @@ public override void Dispose() {
GC.SuppressFinalize(this);
}

~MemoryDrawingSurface() {
Dispose();
}

public override Window window => null;

public void SavePng(string filename) {
Expand Down

0 comments on commit ad9d6ec

Please sign in to comment.