From c90ea869407158397e227ebf89b4982d742e6737 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 23 Jun 2022 17:06:38 -0700 Subject: [PATCH] initial commit --- .gitignore | 2 + LICENSE | 23 +++++++ README.md | 9 +++ Silkworm2CS.cs | 157 +++++++++++++++++++++++++++++++++++++++++++++ Silkworm2CS.csproj | 8 +++ 5 files changed, 199 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Silkworm2CS.cs create mode 100644 Silkworm2CS.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd42ee3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d2023f2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,23 @@ +Silkworm2 - C# bindings for the Silkworm2 cloth physics library + +Copyright (c) 2022 Evan Hemsley + +This software is provided 'as-is', without any express or implied warranty. +In no event will the authors be held liable for any damages arising from +the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not +claim that you wrote the original software. If you use this software in a +product, an acknowledgment in the product documentation would be +appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and must not be +misrepresented as being the original software. + +3. This notice may not be removed or altered from any source distribution. + +Evan "cosmonaut" Hemsley diff --git a/README.md b/README.md new file mode 100644 index 0000000..283a1b6 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +This is Silkworm2CS, a C# interop library for the [Silkworm2](https://gitea.moonside.games/MoonsideGames/Silkworm2) cloth physics library. + +License +------- +Silkworm2 and Silkworm2CS are released under the zlib license. See LICENSE for details. + +About Silkworm +---------------- +For more information about Silkworm2, take a look at the Silkworm repository. diff --git a/Silkworm2CS.cs b/Silkworm2CS.cs new file mode 100644 index 0000000..5ef4a99 --- /dev/null +++ b/Silkworm2CS.cs @@ -0,0 +1,157 @@ +/* Silkworm2CS - C# bindings for the Silkworm2 cloth physics library + * + * Copyright (c) 2022 Evan Hemsley + * + * This software is provided 'as-is', without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in a + * product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source distribution. + * + * Evan "cosmonaut" Hemsley + * + */ + +using System; +using System.Runtime.InteropServices; + +namespace Silkworm2CS +{ + public static class Silkworm2 + { + private const string nativeLibName = "Silkworm2"; + + public const uint SILKWORM2_MAJOR_VERSION = 0; + public const uint SILKWORM2_MINOR_VERSION = 1; + public const uint SILKWORM2_PATCH_VERSION = 0; + + [StructLayout(LayoutKind.Sequential)] + public struct NodeCreateInfo + { + int X; + int Y; + float Mass; + float Friction; + float Radius; + bool Pinned; + float PushFactor; + float WindFactor; + bool Destroyable; + }; + + [StructLayout(LayoutKind.Sequential)] + public struct ClothCreateInfo + { + int X; + int Y; + int HorizontalNodeCount; + int VerticalNodeCount; + float Mass; + float Friction; + float WindFactor; + float TearThreshold; + }; + + [StructLayout(LayoutKind.Sequential)] + public struct Vertex + { + float X; + float Y; + float Z; + float U; + float V; + } + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern void Silkworm_Init(); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern void Silkworm_Update( + float delta, + float windSpeedX, + float windSpeedY + ); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern IntPtr Silkworm_CreateNode(NodeCreateInfo* nodeCreateInfo); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern void Silkworm_DestroyNode(IntPtr nodePtr); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr Silkworm_CreateLink( + IntPtr nodeA, + IntPtr nodeB, + float distance, + float tearThreshold + ); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern IntPtr Silkworm_ClothCreate(ClothCreateInfo* clothCreateInfo); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern void Silkworm_ClothNodePin(IntPtr clothPtr, uint i, uint j); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern void Silkworm_ClothNodeUnpin(IntPtr clothPtr, uint i, uint j); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern void Silkworm_ClothNodeDestroy(IntPtr clothPtr, uint i, uint j); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern int Silkworm_ClothFillTriangleBuffer( + IntPtr clothPtr, + IntPtr vertexBufferPtr, + float depth, + float leftUV, + float widthUV, + float topUV, + float heightUV + ); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern void Silkworm_ClothDestroy(IntPtr clothPtr); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern void Silkworm_PinNodesInRadius(float x, float y, float radius); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern void Silkworm_UnpinNodesInRadius(float x, float y, float radius); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern void Silkworm_PushNodesInRadius( + float x, + float y, + float radius, + float xDirection, + float yDirection + ); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern void Silkworm_DestroyNodesInRadius(float x, float y, float radius); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern IntPtr Silkworm_FindClothInRadius(float x, float y, float radius); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern void Silkworm_PerformDestroys(); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern void Silkworm_ClearAll(); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public unsafe static extern void Silkworm_Finish(); + } +} diff --git a/Silkworm2CS.csproj b/Silkworm2CS.csproj new file mode 100644 index 0000000..0786015 --- /dev/null +++ b/Silkworm2CS.csproj @@ -0,0 +1,8 @@ + + + + netstandard2.0 + true + + +