Fix tests

This commit is contained in:
hwsmm 2024-08-28 22:46:24 +09:00
parent 15e2c5442d
commit 9f75280505
5 changed files with 30 additions and 41 deletions

View File

@ -22,24 +22,20 @@ namespace SDL.Tests
SDL3.SDL_EnterAppMainCallbacks(0, (byte**)objectHandle.Handle, &AppInit, &AppIterate, &AppEvent, &AppQuit); 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_SetLogPriorities(SDL_LogPriority.SDL_LOG_PRIORITY_VERBOSE);
SDL3.SDL_SetLogOutputFunction(&LogOutput, IntPtr.Zero); SDL3.SDL_SetLogOutputFunction(&LogOutput, IntPtr.Zero);
return CONTINUE; return SDL_AppResult.SDL_APP_CONTINUE;
} }
protected const int TERMINATE_ERROR = -1; protected virtual SDL_AppResult Iterate()
protected const int CONTINUE = 0;
protected const int TERMINATE_SUCCESS = 1;
protected virtual int Iterate()
{ {
Thread.Sleep(10); 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) switch (e.Type)
{ {
@ -47,10 +43,10 @@ namespace SDL.Tests
case SDL_EventType.SDL_EVENT_WINDOW_CLOSE_REQUESTED: case SDL_EventType.SDL_EVENT_WINDOW_CLOSE_REQUESTED:
case SDL_EventType.SDL_EVENT_TERMINATING: case SDL_EventType.SDL_EVENT_TERMINATING:
case SDL_EventType.SDL_EVENT_KEY_DOWN when e.key.key == SDL_Keycode.SDLK_ESCAPE: 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() protected virtual void Quit()
@ -64,7 +60,7 @@ namespace SDL.Tests
} }
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])] [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; IntPtr handle = (IntPtr)argv;
*appState = handle; *appState = handle;
@ -73,27 +69,27 @@ namespace SDL.Tests
if (objectHandle.GetTarget(out var target)) if (objectHandle.GetTarget(out var target))
return target.Init(); return target.Init();
return TERMINATE_ERROR; return SDL_AppResult.SDL_APP_FAILURE;
} }
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])] [UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static int AppIterate(IntPtr appState) private static SDL_AppResult AppIterate(IntPtr appState)
{ {
var objectHandle = new ObjectHandle<MainCallbacksTest>(appState, true); var objectHandle = new ObjectHandle<MainCallbacksTest>(appState, true);
if (objectHandle.GetTarget(out var target)) if (objectHandle.GetTarget(out var target))
return target.Iterate(); return target.Iterate();
return TERMINATE_ERROR; return SDL_AppResult.SDL_APP_FAILURE;
} }
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])] [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); var objectHandle = new ObjectHandle<MainCallbacksTest>(appState, true);
if (objectHandle.GetTarget(out var target)) if (objectHandle.GetTarget(out var target))
return target.Event(*e); return target.Event(*e);
return TERMINATE_ERROR; return SDL_AppResult.SDL_APP_FAILURE;
} }
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])] [UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]

View File

@ -19,7 +19,7 @@ namespace SDL.Tests
public MyWindow() 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()}"); throw new InvalidOperationException($"failed to initialise SDL. Error: {SDL_GetError()}");
initSuccess = true; initSuccess = true;
@ -51,18 +51,18 @@ namespace SDL.Tests
// ReSharper disable once UseCollectionExpression // ReSharper disable once UseCollectionExpression
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })] [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); var handle = new ObjectHandle<MyWindow>(userdata);
if (handle.GetTarget(out var window)) if (handle.GetTarget(out var window))
return window.handleEventFromFilter(e); return window.handleEventFromFilter(e);
return 1; return SDL_bool.SDL_TRUE;
} }
public Action<SDL_Event>? EventFilter; public Action<SDL_Event>? EventFilter;
private int handleEventFromFilter(SDL_Event* e) private SDL_bool handleEventFromFilter(SDL_Event* e)
{ {
switch (e->Type) switch (e->Type)
{ {
@ -76,7 +76,7 @@ namespace SDL.Tests
break; break;
} }
return 1; return SDL_bool.SDL_TRUE;
} }
private void handleKeyFromFilter(SDL_KeyboardEvent e) private void handleKeyFromFilter(SDL_KeyboardEvent e)
@ -105,8 +105,8 @@ namespace SDL.Tests
switch (e.key.key) switch (e.key.key)
{ {
case SDL_Keycode.SDLK_R: case SDL_Keycode.SDLK_R:
bool old = SDL_GetRelativeMouseMode() == SDL_bool.SDL_TRUE; bool old = SDL_GetWindowRelativeMouseMode(sdlWindowHandle) == SDL_bool.SDL_TRUE;
SDL_SetRelativeMouseMode(old ? SDL_bool.SDL_FALSE : SDL_bool.SDL_TRUE); SDL_SetWindowRelativeMouseMode(sdlWindowHandle, old ? SDL_bool.SDL_FALSE : SDL_bool.SDL_TRUE);
break; break;
case SDL_Keycode.SDLK_V: case SDL_Keycode.SDLK_V:
@ -170,14 +170,8 @@ namespace SDL.Tests
Console.WriteLine($"gamepad added: {e.gdevice.which}"); Console.WriteLine($"gamepad added: {e.gdevice.which}");
break; break;
case SDL_EventType.SDL_EVENT_WINDOW_PEN_ENTER: case SDL_EventType.SDL_EVENT_PEN_PROXIMITY_IN:
SDL_PenCapabilityInfo info; Console.WriteLine($"pen proximity in: {e.pproximity.which}");
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);
break; break;
} }
} }

View File

@ -24,7 +24,7 @@ namespace SDL.Tests
[Test] [Test]
public unsafe void TestClipboardData() 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(ret, Is.EqualTo(0), SDL3.SDL_GetError);
Assert.That(SDL3.SDL_HasClipboardData("test/one"), Is.EqualTo(SDL_bool.SDL_TRUE)); Assert.That(SDL3.SDL_HasClipboardData("test/one"), Is.EqualTo(SDL_bool.SDL_TRUE));
@ -46,7 +46,7 @@ namespace SDL.Tests
} }
ret = SDL3.SDL_ClearClipboardData(); 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)); Assert.That(cleanups, Is.EqualTo(1));

View File

@ -11,12 +11,11 @@ namespace SDL.Tests
private SDL_Window* window; private SDL_Window* window;
private SDL_Renderer* renderer; private SDL_Renderer* renderer;
protected override int Init() protected override SDL_AppResult Init()
{ {
// decouple pen, mouse and touch events // decouple pen, mouse and touch events
SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "0"); SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "0");
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_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); SDL_Init(SDL_InitFlags.SDL_INIT_VIDEO);
@ -48,7 +47,7 @@ namespace SDL.Tests
SDL_RenderFillRect(renderer, &r); SDL_RenderFillRect(renderer, &r);
} }
protected override int Iterate() protected override SDL_AppResult Iterate()
{ {
const float gray = 0.1f; const float gray = 0.1f;
SDL_SetRenderDrawColorFloat(renderer, gray, gray, gray, 1.0f); SDL_SetRenderDrawColorFloat(renderer, gray, gray, gray, 1.0f);
@ -86,7 +85,7 @@ namespace SDL.Tests
return base.Iterate(); return base.Iterate();
} }
protected override int Event(SDL_Event e) protected override SDL_AppResult Event(SDL_Event e)
{ {
SDL_ConvertEventToRenderCoordinates(renderer, &e); SDL_ConvertEventToRenderCoordinates(renderer, &e);
@ -117,7 +116,7 @@ namespace SDL.Tests
switch (e.key.key) switch (e.key.key)
{ {
case SDL_Keycode.SDLK_R: 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; break;
case SDL_Keycode.SDLK_F: case SDL_Keycode.SDLK_F:

View File

@ -14,9 +14,9 @@ namespace SDL
{ {
[LibraryImport("SDL3", StringMarshalling = StringMarshalling.Utf8)] [LibraryImport("SDL3", StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
private static unsafe partial int SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, IntPtr userdata, string[] mime_types, UIntPtr num_mime_types); private static unsafe partial SDL_bool SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, IntPtr userdata, string[] mime_types, UIntPtr num_mime_types);
public static unsafe int SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, IntPtr userdata, params string[] mime_types) public static unsafe SDL_bool SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, IntPtr userdata, params string[] mime_types)
=> SDL_SetClipboardData(callback, cleanup, userdata, mime_types, (UIntPtr)mime_types.Length); => SDL_SetClipboardData(callback, cleanup, userdata, mime_types, (UIntPtr)mime_types.Length);
} }
} }