Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update bindings 3 (no merging needed) #129

Merged
merged 12 commits into from
Sep 15, 2024
30 changes: 13 additions & 17 deletions SDL3-CS.Tests/MainCallbacksTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,31 @@ public void TestEnterMainCallbacks()
SDL3.SDL_EnterAppMainCallbacks(0, (byte**)objectHandle.Handle, &AppInit, &AppIterate, &AppEvent, &AppQuit);
}

protected virtual int Init()
protected virtual SDL_AppResult Init()
{
SDL3.SDL_SetLogPriorities(SDL_LogPriority.SDL_LOG_PRIORITY_VERBOSE);
SDL3.SDL_SetLogOutputFunction(&LogOutput, IntPtr.Zero);
return CONTINUE;
return SDL_AppResult.SDL_APP_CONTINUE;
}

protected const int TERMINATE_ERROR = -1;
protected const int CONTINUE = 0;
protected const int TERMINATE_SUCCESS = 1;

protected virtual int Iterate()
protected virtual SDL_AppResult Iterate()
{
Thread.Sleep(10);
return CONTINUE;
return SDL_AppResult.SDL_APP_CONTINUE;
}

protected virtual int Event(SDL_Event e)
protected virtual SDL_AppResult Event(SDL_Event e)
{
switch (e.Type)
{
case SDL_EventType.SDL_EVENT_QUIT:
case SDL_EventType.SDL_EVENT_WINDOW_CLOSE_REQUESTED:
case SDL_EventType.SDL_EVENT_TERMINATING:
case SDL_EventType.SDL_EVENT_KEY_DOWN when e.key.key == SDL_Keycode.SDLK_ESCAPE:
return TERMINATE_SUCCESS;
return SDL_AppResult.SDL_APP_SUCCESS;
}

return CONTINUE;
return SDL_AppResult.SDL_APP_CONTINUE;
}

protected virtual void Quit()
Expand All @@ -64,7 +60,7 @@ private static void LogOutput(IntPtr userdata, SDL_LogCategory category, SDL_Log
}

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static int AppInit(IntPtr* appState, int argc, byte** argv)
private static SDL_AppResult AppInit(IntPtr* appState, int argc, byte** argv)
{
IntPtr handle = (IntPtr)argv;
*appState = handle;
Expand All @@ -73,27 +69,27 @@ private static int AppInit(IntPtr* appState, int argc, byte** argv)
if (objectHandle.GetTarget(out var target))
return target.Init();

return TERMINATE_ERROR;
return SDL_AppResult.SDL_APP_FAILURE;
}

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static int AppIterate(IntPtr appState)
private static SDL_AppResult AppIterate(IntPtr appState)
{
var objectHandle = new ObjectHandle<MainCallbacksTest>(appState, true);
if (objectHandle.GetTarget(out var target))
return target.Iterate();

return TERMINATE_ERROR;
return SDL_AppResult.SDL_APP_FAILURE;
}

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static int AppEvent(IntPtr appState, SDL_Event* e)
private static SDL_AppResult AppEvent(IntPtr appState, SDL_Event* e)
{
var objectHandle = new ObjectHandle<MainCallbacksTest>(appState, true);
if (objectHandle.GetTarget(out var target))
return target.Event(*e);

return TERMINATE_ERROR;
return SDL_AppResult.SDL_APP_FAILURE;
}

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
Expand Down
24 changes: 9 additions & 15 deletions SDL3-CS.Tests/MyWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed unsafe class MyWindow : IDisposable

public MyWindow()
{
if (SDL_InitSubSystem(init_flags) < 0)
if (SDL_InitSubSystem(init_flags) == SDL_bool.SDL_FALSE)
throw new InvalidOperationException($"failed to initialise SDL. Error: {SDL_GetError()}");

initSuccess = true;
Expand Down Expand Up @@ -51,18 +51,18 @@ private static SDL_bool wndProc(IntPtr userdata, MSG* message)

// ReSharper disable once UseCollectionExpression
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static int nativeFilter(IntPtr userdata, SDL_Event* e)
private static SDL_bool nativeFilter(IntPtr userdata, SDL_Event* e)
{
var handle = new ObjectHandle<MyWindow>(userdata);
if (handle.GetTarget(out var window))
return window.handleEventFromFilter(e);

return 1;
return SDL_bool.SDL_TRUE;
}

public Action<SDL_Event>? EventFilter;

private int handleEventFromFilter(SDL_Event* e)
private SDL_bool handleEventFromFilter(SDL_Event* e)
{
switch (e->Type)
{
Expand All @@ -76,7 +76,7 @@ private int handleEventFromFilter(SDL_Event* e)
break;
}

return 1;
return SDL_bool.SDL_TRUE;
}

private void handleKeyFromFilter(SDL_KeyboardEvent e)
Expand Down Expand Up @@ -105,8 +105,8 @@ private void handleEvent(SDL_Event e)
switch (e.key.key)
{
case SDL_Keycode.SDLK_R:
bool old = SDL_GetRelativeMouseMode() == SDL_bool.SDL_TRUE;
SDL_SetRelativeMouseMode(old ? SDL_bool.SDL_FALSE : SDL_bool.SDL_TRUE);
bool old = SDL_GetWindowRelativeMouseMode(sdlWindowHandle) == SDL_bool.SDL_TRUE;
SDL_SetWindowRelativeMouseMode(sdlWindowHandle, old ? SDL_bool.SDL_FALSE : SDL_bool.SDL_TRUE);
break;

case SDL_Keycode.SDLK_V:
Expand Down Expand Up @@ -170,14 +170,8 @@ private void handleEvent(SDL_Event e)
Console.WriteLine($"gamepad added: {e.gdevice.which}");
break;

case SDL_EventType.SDL_EVENT_WINDOW_PEN_ENTER:
SDL_PenCapabilityInfo info;
var cap = SDL_GetPenCapabilities((SDL_PenID)e.window.data1, &info);

if (cap.HasFlag(SDL_PenCapabilityFlags.SDL_PEN_AXIS_XTILT_MASK))
Console.WriteLine("has pen xtilt axis");

Console.WriteLine(info.max_tilt);
case SDL_EventType.SDL_EVENT_PEN_PROXIMITY_IN:
Console.WriteLine($"pen proximity in: {e.pproximity.which}");
break;
}
}
Expand Down
3 changes: 3 additions & 0 deletions SDL3-CS.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public static void Main()
if (OperatingSystem.IsWindows())
Console.OutputEncoding = Encoding.UTF8;

Console.WriteLine($"SDL_FALSE is represented as 0x{SDL_OutOfMemory():X} (expected 0x{SDL_bool.SDL_FALSE:X})");
Console.WriteLine($"SDL_TRUE is represented as 0x{SDL_ClearError():X} (expected 0x{SDL_bool.SDL_TRUE:X})");

SDL_SetHint(SDL_HINT_WINDOWS_CLOSE_ON_ALT_F4, "null byte \0 in string"u8);
Debug.Assert(SDL_GetHint(SDL_HINT_WINDOWS_CLOSE_ON_ALT_F4) == "null byte ");

Expand Down
4 changes: 2 additions & 2 deletions SDL3-CS.Tests/TestClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void SetUp()
[Test]
public unsafe void TestClipboardData()
{
int ret = SDL3.SDL_SetClipboardData(&dataCallback, &cleanupCallback, IntPtr.Zero, "test/one", "test/two");
var ret = SDL3.SDL_SetClipboardData(&dataCallback, &cleanupCallback, IntPtr.Zero, "test/one", "test/two");
Assert.That(ret, Is.EqualTo(0), SDL3.SDL_GetError);

Assert.That(SDL3.SDL_HasClipboardData("test/one"), Is.EqualTo(SDL_bool.SDL_TRUE));
Expand All @@ -46,7 +46,7 @@ public unsafe void TestClipboardData()
}

ret = SDL3.SDL_ClearClipboardData();
Assert.That(ret, Is.EqualTo(0), SDL3.SDL_GetError);
Assert.That(ret, Is.EqualTo(SDL_bool.SDL_TRUE), SDL3.SDL_GetError);

Assert.That(cleanups, Is.EqualTo(1));

Expand Down
9 changes: 4 additions & 5 deletions SDL3-CS.Tests/TestPositionalInputVisualisation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ public unsafe class TestPositionalInputVisualisation : MainCallbacksTest
private SDL_Window* window;
private SDL_Renderer* renderer;

protected override int Init()
protected override SDL_AppResult Init()
{
// decouple pen, mouse and touch events
SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "0");
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
SDL_SetHint(SDL_HINT_PEN_NOT_MOUSE, "2");

SDL_Init(SDL_InitFlags.SDL_INIT_VIDEO);

Expand Down Expand Up @@ -48,7 +47,7 @@ private void fillRect(RectangleF rect)
SDL_RenderFillRect(renderer, &r);
}

protected override int Iterate()
protected override SDL_AppResult Iterate()
{
const float gray = 0.1f;
SDL_SetRenderDrawColorFloat(renderer, gray, gray, gray, 1.0f);
Expand Down Expand Up @@ -86,7 +85,7 @@ protected override int Iterate()
return base.Iterate();
}

protected override int Event(SDL_Event e)
protected override SDL_AppResult Event(SDL_Event e)
{
SDL_ConvertEventToRenderCoordinates(renderer, &e);

Expand Down Expand Up @@ -117,7 +116,7 @@ protected override int Event(SDL_Event e)
switch (e.key.key)
{
case SDL_Keycode.SDLK_R:
SDL_SetRelativeMouseMode(SDL_GetRelativeMouseMode() == SDL_bool.SDL_TRUE ? SDL_bool.SDL_FALSE : SDL_bool.SDL_TRUE);
SDL_SetWindowRelativeMouseMode(window, SDL_GetWindowRelativeMouseMode(window) == SDL_bool.SDL_TRUE ? SDL_bool.SDL_FALSE : SDL_bool.SDL_TRUE);
break;

case SDL_Keycode.SDLK_F:
Expand Down
39 changes: 39 additions & 0 deletions SDL3-CS.Tests/TestSDLBool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using NUnit.Framework;

namespace SDL.Tests
{
[TestFixture]
public class TestSDLBool
{
private SDL_bool invert(SDL_bool value) => value == SDL_bool.SDL_TRUE ? SDL_bool.SDL_FALSE : SDL_bool.SDL_TRUE;

[Test]
public void TestFalse()
{
Assert.That(SDL3.SDL_OutOfMemory(), Is.EqualTo(SDL_bool.SDL_FALSE));
}

[Test]
public void TestTrue()
{
Assert.That(SDL3.SDL_ClearError(), Is.EqualTo(SDL_bool.SDL_TRUE));
}

[Test]
public void TestStoreLoad([Values] SDL_bool value)
{
var props = SDL3.SDL_CreateProperties();
Assume.That(props, Is.Not.EqualTo(0));

var ret = SDL3.SDL_SetBooleanProperty(props, "test", value);
Assume.That(ret, Is.EqualTo(SDL_bool.SDL_TRUE));

Assert.That(SDL3.SDL_GetBooleanProperty(props, "test", invert(value)), Is.EqualTo(value));

SDL3.SDL_DestroyProperties(props);
}
}
}
1 change: 1 addition & 0 deletions SDL3-CS.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -785,5 +785,6 @@ See the LICENCE file in the repository root for full licence text.&#xD;
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002EMemberReordering_002EMigrations_002ECSharpFileLayoutPatternRemoveIsAttributeUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EPredefinedNamingRulesToUserRulesUpgrade/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
20 changes: 8 additions & 12 deletions SDL3-CS/SDL-use-proper-types.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/include/SDL3/SDL.h b/include/SDL3/SDL.h
index 6cda579f7..bc2952a4e 100644
index 4e18f6382..d737c7a9a 100644
--- a/include/SDL3/SDL.h
+++ b/include/SDL3/SDL.h
@@ -28,6 +28,8 @@
Expand All @@ -12,10 +12,10 @@ index 6cda579f7..bc2952a4e 100644
#include <SDL3/SDL_assert.h>
#include <SDL3/SDL_atomic.h>
diff --git a/include/SDL3/SDL_log.h b/include/SDL3/SDL_log.h
index 3ded311ff..42760135f 100644
index 3c0e80f38..44f58add3 100644
--- a/include/SDL3/SDL_log.h
+++ b/include/SDL3/SDL_log.h
@@ -372,7 +372,7 @@ extern DECLSPEC void SDLCALL SDL_LogMessageV(int category,
@@ -407,7 +407,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogMessageV(int category,
*
* \since This datatype is available since SDL 3.0.0.
*/
Expand All @@ -25,28 +25,24 @@ index 3ded311ff..42760135f 100644
/**
* Get the current log output function.
diff --git a/include/SDL3/SDL_stdinc.h b/include/SDL3/SDL_stdinc.h
index 00a54a139..847b50ecb 100644
index 5539f2a81..3155f3f31 100644
--- a/include/SDL3/SDL_stdinc.h
+++ b/include/SDL3/SDL_stdinc.h
@@ -146,7 +146,7 @@ void *alloca(size_t);
@@ -194,7 +194,7 @@ void *alloca(size_t);
*
* \sa SDL_bool
*/
-#define SDL_FALSE 0
-#define SDL_FALSE false
+#define SDL_FALSE (SDL_bool)0

/**
* A boolean true.
@@ -155,7 +155,7 @@ void *alloca(size_t);
@@ -203,7 +203,7 @@ void *alloca(size_t);
*
* \sa SDL_bool
*/
-#define SDL_TRUE 1
-#define SDL_TRUE true
+#define SDL_TRUE (SDL_bool)1

/**
* A boolean type: true or false.

base-commit: 0429f5d6a36fc35b551bcc2acd4a40c2db6dab82
--
2.40.0.windows.1
4 changes: 2 additions & 2 deletions SDL3-CS/SDL3/ClangSharp/SDL_atomic.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ public static unsafe partial class SDL3

[DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: NativeTypeName("void*")]
public static extern IntPtr SDL_AtomicSetPtr([NativeTypeName("void **")] IntPtr* a, [NativeTypeName("void*")] IntPtr v);
public static extern IntPtr SDL_AtomicSetPointer([NativeTypeName("void **")] IntPtr* a, [NativeTypeName("void*")] IntPtr v);

[DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: NativeTypeName("void*")]
public static extern IntPtr SDL_AtomicGetPtr([NativeTypeName("void **")] IntPtr* a);
public static extern IntPtr SDL_AtomicGetPointer([NativeTypeName("void **")] IntPtr* a);
}
}
Loading