I am writing a program to create an SDL3 window in C#.
I have compiled SDL3 2.30.5 code from the following repository: https://github.com/libsdl-org/SDL under Win x64 using Rider 2024.1.2.
Here is the code I’m trying to run:
<code>using SDL3;
namespace SDL3Test
{
class Program
{
private static bool _loop;
static void Main(string[] args)
{
if (SDL.Init(SDL.InitFlags.Video) < 0)
{
Console.WriteLine($"SDL could not initialize! SDL_Error: {SDL.GetError()}");
return;
}
var window = SDL.CreateWindow("SDL Create Window", 800, 600, SDL.WindowFlags.OpenGL);
if (window == IntPtr.Zero)
{
Console.WriteLine($"Window could not be created! SDL_Error: {SDL.GetError()}");
return;
}
_loop = true;
while (_loop)
{
while (SDL.PollEvent(out var sdlEvent) != 0)
{
if (sdlEvent == SDL.EventType.Quit)
{
_loop = false;
}
}
}
SDL.DestroyWindow(window);
Console.WriteLine($"SDL_Error: {SDL.GetError()}");
SDL.Quit();
}
}
}
</code>
<code>using SDL3;
namespace SDL3Test
{
class Program
{
private static bool _loop;
static void Main(string[] args)
{
if (SDL.Init(SDL.InitFlags.Video) < 0)
{
Console.WriteLine($"SDL could not initialize! SDL_Error: {SDL.GetError()}");
return;
}
var window = SDL.CreateWindow("SDL Create Window", 800, 600, SDL.WindowFlags.OpenGL);
if (window == IntPtr.Zero)
{
Console.WriteLine($"Window could not be created! SDL_Error: {SDL.GetError()}");
return;
}
_loop = true;
while (_loop)
{
while (SDL.PollEvent(out var sdlEvent) != 0)
{
if (sdlEvent == SDL.EventType.Quit)
{
_loop = false;
}
}
}
SDL.DestroyWindow(window);
Console.WriteLine($"SDL_Error: {SDL.GetError()}");
SDL.Quit();
}
}
}
</code>
using SDL3;
namespace SDL3Test
{
class Program
{
private static bool _loop;
static void Main(string[] args)
{
if (SDL.Init(SDL.InitFlags.Video) < 0)
{
Console.WriteLine($"SDL could not initialize! SDL_Error: {SDL.GetError()}");
return;
}
var window = SDL.CreateWindow("SDL Create Window", 800, 600, SDL.WindowFlags.OpenGL);
if (window == IntPtr.Zero)
{
Console.WriteLine($"Window could not be created! SDL_Error: {SDL.GetError()}");
return;
}
_loop = true;
while (_loop)
{
while (SDL.PollEvent(out var sdlEvent) != 0)
{
if (sdlEvent == SDL.EventType.Quit)
{
_loop = false;
}
}
}
SDL.DestroyWindow(window);
Console.WriteLine($"SDL_Error: {SDL.GetError()}");
SDL.Quit();
}
}
}
Here is the CreateWindow function:
<code>[LibraryImport(SDLLibrary)]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
private static unsafe partial IntPtr SDL_CreateWindow(byte* title, int w, int h, WindowFlags flags);
public static unsafe IntPtr CreateWindow(string? title, int w, int h, WindowFlags flags)
{
var utf8TitleBufSize = Utf8Size(title);
var utf8Title = stackalloc byte[utf8TitleBufSize];
return SDL_CreateWindow(Utf8Encode(title, utf8Title, utf8TitleBufSize), w, h, flags);
}
</code>
<code>[LibraryImport(SDLLibrary)]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
private static unsafe partial IntPtr SDL_CreateWindow(byte* title, int w, int h, WindowFlags flags);
public static unsafe IntPtr CreateWindow(string? title, int w, int h, WindowFlags flags)
{
var utf8TitleBufSize = Utf8Size(title);
var utf8Title = stackalloc byte[utf8TitleBufSize];
return SDL_CreateWindow(Utf8Encode(title, utf8Title, utf8TitleBufSize), w, h, flags);
}
</code>
[LibraryImport(SDLLibrary)]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
private static unsafe partial IntPtr SDL_CreateWindow(byte* title, int w, int h, WindowFlags flags);
public static unsafe IntPtr CreateWindow(string? title, int w, int h, WindowFlags flags)
{
var utf8TitleBufSize = Utf8Size(title);
var utf8Title = stackalloc byte[utf8TitleBufSize];
return SDL_CreateWindow(Utf8Encode(title, utf8Title, utf8TitleBufSize), w, h, flags);
}
And here’s the DestroyWindow function:
<code>[LibraryImport(SDLLibrary, EntryPoint = "SDL_DestroyWindow")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
public static unsafe partial void DestroyWindow(IntPtr window);
</code>
<code>[LibraryImport(SDLLibrary, EntryPoint = "SDL_DestroyWindow")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
public static unsafe partial void DestroyWindow(IntPtr window);
</code>
[LibraryImport(SDLLibrary, EntryPoint = "SDL_DestroyWindow")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
public static unsafe partial void DestroyWindow(IntPtr window);
Helper functions:
<code>private static int Utf8Size(string? str)
{
if (str == null)
{
return 0;
}
return str.Length * 4 + 1;
}
private static unsafe byte* Utf8Encode(string? str, byte* buffer, int bufferSize)
{
if (str == null)
{
return (byte*)0;
}
fixed (char* strPtr = str)
{
Encoding.UTF8.GetBytes(strPtr, str.Length + 1, buffer, bufferSize);
}
return buffer;
}
private static unsafe string? UTF8_ToManaged(IntPtr s, bool freePtr = false)
{
if (s == IntPtr.Zero) return null;
var ptr = (byte*)s;
while (*ptr != 0) ptr++;
var result = Encoding.UTF8.GetString((byte*)s, (int)(ptr - (byte*)s));
if (freePtr) Free(s);
return result;
}
</code>
<code>private static int Utf8Size(string? str)
{
if (str == null)
{
return 0;
}
return str.Length * 4 + 1;
}
private static unsafe byte* Utf8Encode(string? str, byte* buffer, int bufferSize)
{
if (str == null)
{
return (byte*)0;
}
fixed (char* strPtr = str)
{
Encoding.UTF8.GetBytes(strPtr, str.Length + 1, buffer, bufferSize);
}
return buffer;
}
private static unsafe string? UTF8_ToManaged(IntPtr s, bool freePtr = false)
{
if (s == IntPtr.Zero) return null;
var ptr = (byte*)s;
while (*ptr != 0) ptr++;
var result = Encoding.UTF8.GetString((byte*)s, (int)(ptr - (byte*)s));
if (freePtr) Free(s);
return result;
}
</code>
private static int Utf8Size(string? str)
{
if (str == null)
{
return 0;
}
return str.Length * 4 + 1;
}
private static unsafe byte* Utf8Encode(string? str, byte* buffer, int bufferSize)
{
if (str == null)
{
return (byte*)0;
}
fixed (char* strPtr = str)
{
Encoding.UTF8.GetBytes(strPtr, str.Length + 1, buffer, bufferSize);
}
return buffer;
}
private static unsafe string? UTF8_ToManaged(IntPtr s, bool freePtr = false)
{
if (s == IntPtr.Zero) return null;
var ptr = (byte*)s;
while (*ptr != 0) ptr++;
var result = Encoding.UTF8.GetString((byte*)s, (int)(ptr - (byte*)s));
if (freePtr) Free(s);
return result;
}
The problem is that when I call SDL.DestroyWindow(window);, I get the error SDL_Error: Invalid window through GetError.
What could be causing this issue?