From be946eb23c7bafa9cfccc723a2011a4a6fd7b09a Mon Sep 17 00:00:00 2001 From: Susko3 Date: Tue, 17 Dec 2024 17:18:31 +0100 Subject: [PATCH] Add `SDLOpaquePointerArray` and use it for SDL_GetWindows() `SDLPointerArray` is inadequate as it does `*(SDL_Window*)ptr` which doesn't make sense as SDL_Window* is an opaque pointer type --- SDL3-CS/SDL3/SDL_video.cs | 4 +-- SDL3-CS/SDLArray.cs | 10 +++++++ SDL3-CS/SDLOpaquePointerArray.cs | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 SDL3-CS/SDLOpaquePointerArray.cs diff --git a/SDL3-CS/SDL3/SDL_video.cs b/SDL3-CS/SDL3/SDL_video.cs index 0b952ab..2641bb6 100644 --- a/SDL3-CS/SDL3/SDL_video.cs +++ b/SDL3-CS/SDL3/SDL_video.cs @@ -107,11 +107,11 @@ namespace SDL } [MustDisposeResource] - public static unsafe SDLPointerArray? SDL_GetWindows() + public static unsafe SDLOpaquePointerArray? SDL_GetWindows() { int count; var array = SDL_GetWindows(&count); - return SDLArray.Create(array, count); + return SDLArray.CreateOpaque(array, count); } } } diff --git a/SDL3-CS/SDLArray.cs b/SDL3-CS/SDLArray.cs index 91f1d60..1494025 100644 --- a/SDL3-CS/SDLArray.cs +++ b/SDL3-CS/SDLArray.cs @@ -63,5 +63,15 @@ namespace SDL return new SDLPointerArray(array, count); } + + [MustDisposeResource] + internal static SDLOpaquePointerArray? CreateOpaque(T** array, int count) + where T : unmanaged + { + if (array == null) + return null; + + return new SDLOpaquePointerArray(array, count); + } } } diff --git a/SDL3-CS/SDLOpaquePointerArray.cs b/SDL3-CS/SDLOpaquePointerArray.cs new file mode 100644 index 0000000..4123e91 --- /dev/null +++ b/SDL3-CS/SDLOpaquePointerArray.cs @@ -0,0 +1,46 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Diagnostics; +using JetBrains.Annotations; + +namespace SDL +{ + [MustDisposeResource] + public sealed unsafe class SDLOpaquePointerArray : IDisposable + where T : unmanaged + { + private readonly T** array; + public readonly int Count; + private bool isDisposed; + + internal SDLOpaquePointerArray(T** array, int count) + { + this.array = array; + Count = count; + } + + public T* this[int index] + { + get + { + ObjectDisposedException.ThrowIf(isDisposed, this); + ArgumentOutOfRangeException.ThrowIfNegative(index); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); + Debug.Assert(array[index] != null); + return array[index]; + } + } + + public void Dispose() + { + if (isDisposed) + return; + + isDisposed = true; + + SDL3.SDL_free(array); + } + } +}