Skip to content

Commit

Permalink
Update to support latest SDK, NuGet support
Browse files Browse the repository at this point in the history
  • Loading branch information
MackinnonBuck committed Apr 22, 2024
1 parent d970fcd commit 3088bcb
Show file tree
Hide file tree
Showing 8 changed files with 548 additions and 521 deletions.
60 changes: 38 additions & 22 deletions src/Box2D/Box2D.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>BOX2D_OBJECT_TRACKING;BOX2D_VALID_ACCESS_CHECKING;BOX2D_NO_POOLING</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="6.0.1" />
<PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PackageId>Box2D.CSharp</PackageId>
<Version>0.1.0-preview.1</Version>
<RepositoryUrl>https://github.com/MackinnonBuck/Box2D.CSharp.git</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<DebugType>embedded</DebugType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>BOX2D_OBJECT_TRACKING;BOX2D_VALID_ACCESS_CHECKING;BOX2D_NO_POOLING</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
</PropertyGroup>

<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\lib\$(PlatformTarget)\$(Configuration)\box2dwrapper.dll" Pack="true" PackagePath="runtimes/win-x64/native/" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="6.0.1" />
<PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup>

</Project>
194 changes: 97 additions & 97 deletions src/Box2D/Collections/ArrayRef.cs
Original file line number Diff line number Diff line change
@@ -1,97 +1,97 @@
using Box2D.Core;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Box2D.Collections;

/// <summary>
/// Represents a managed reference to an unmanaged Box2D array.
/// </summary>
/// <typeparam name="T">The array item type.</typeparam>
public readonly ref struct ArrayRef<T> where T : struct
{
private static readonly int _elementSize = Marshal.SizeOf<T>();

private readonly IntPtr _native;

/// <summary>
/// Gets the length of the array.
/// </summary>
public int Length { get; }

/// <summary>
/// Gets whether the <see cref="ArrayRef{T}"/> points to a null
/// unmanaged array.
/// </summary>
public bool IsNull => _native == IntPtr.Zero;

/// <summary>
/// Gets or sets the value of an item in the array.
/// </summary>
/// <param name="index">The index of the item in the array.</param>
public T this[int index]
{
get
{
ThrowIfInvalidIndex(index);
return Marshal.PtrToStructure<T>(_native + _elementSize * index);
}
set
{
ThrowIfInvalidIndex(index);
Marshal.StructureToPtr(value, _native + _elementSize * index, false);
}
}

internal ArrayRef(IntPtr native, int length)
{
_native = native;
Length = length;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ThrowIfInvalidIndex(int index)
{
Errors.ThrowIfNull(_native, nameof(ArrayRef<T>));

if (index < 0 || index >= Length)
{
throw new IndexOutOfRangeException();
}
}

/// <summary>
/// Gets an <see cref="Enumerator"/> for the current <see cref="ArrayRef{T}"/> instance.
/// </summary>
public Enumerator GetEnumerator()
=> new(in this);

/// <summary>
/// An enumerator for <see cref="ArrayRef{T}"/> instances.
/// </summary>
public ref struct Enumerator
{
private readonly ArrayRef<T> _source;
private int _index = -1;

/// <summary>
/// Gets the current element.
/// </summary>
public T Current => _source[_index];

/// <summary>
/// Constructs a new <see cref="Enumerator"/> instance.
/// </summary>
public Enumerator(in ArrayRef<T> source)
{
_source = source;
}

/// <summary>
/// Moves to the next element.
/// </summary>
public bool MoveNext()
=> ++_index < _source.Length;
}
}
using Box2D.Core;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Box2D.Collections;

/// <summary>
/// Represents a managed reference to an unmanaged Box2D array.
/// </summary>
/// <typeparam name="T">The array item type.</typeparam>
public readonly ref struct ArrayRef<T> where T : struct
{
private static readonly int _elementSize = Marshal.SizeOf<T>();

private readonly IntPtr _native;

/// <summary>
/// Gets the length of the array.
/// </summary>
public int Length { get; }

/// <summary>
/// Gets whether the <see cref="ArrayRef{T}"/> points to a null
/// unmanaged array.
/// </summary>
public bool IsNull => _native == IntPtr.Zero;

/// <summary>
/// Gets or sets the value of an item in the array.
/// </summary>
/// <param name="index">The index of the item in the array.</param>
public T this[int index]
{
get
{
ThrowIfInvalidIndex(index);
return Marshal.PtrToStructure<T>(_native + _elementSize * index);
}
set
{
ThrowIfInvalidIndex(index);
Marshal.StructureToPtr(value, _native + _elementSize * index, false);
}
}

internal ArrayRef(IntPtr native, int length)
{
_native = native;
Length = length;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ThrowIfInvalidIndex(int index)
{
Errors.ThrowIfNull(_native, nameof(ArrayRef<T>));

if (index < 0 || index >= Length)
{
throw new IndexOutOfRangeException();
}
}

/// <summary>
/// Gets an <see cref="Enumerator"/> for the current <see cref="ArrayRef{T}"/> instance.
/// </summary>
public Enumerator GetEnumerator()
=> new(in this);

/// <summary>
/// An enumerator for <see cref="ArrayRef{T}"/> instances.
/// </summary>
public ref struct Enumerator
{
private readonly ArrayRef<T> _source;
private int _index = -1;

/// <summary>
/// Gets the current element.
/// </summary>
public T Current => _source[_index];

/// <summary>
/// Constructs a new <see cref="Enumerator"/> instance.
/// </summary>
public Enumerator(scoped in ArrayRef<T> source)
{
_source = source;
}

/// <summary>
/// Moves to the next element.
/// </summary>
public bool MoveNext()
=> ++_index < _source.Length;
}
}
98 changes: 49 additions & 49 deletions src/Box2D/Collision/ContactImpulse.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
using Box2D.Collections;
using System;

namespace Box2D.Collision;

using static Interop.NativeMethods;

/// <summary>
/// Contact impulses for reporting. Impulses are used instead of forces because
/// sub-step forces may approach infinity for rigid body collisions. These
/// match up one-to-one with the contact points in <see cref="Manifold"/>.
/// </summary>
public readonly ref struct ContactImpulse
{
private readonly IntPtr _native;

/// <summary>
/// Gets whether this <see cref="ContactImpulse"/> points to a null unmanaged contact impulse.
/// </summary>
public bool IsNull => _native == IntPtr.Zero;

/// <summary>
/// Gets the normal impulses array.
/// </summary>
public ArrayRef<float> NormalImpulses { get; }

/// <summary>
/// Gets the tangent impulses array.
/// </summary>
public ArrayRef<float> TangentImpulses { get; }

internal static ContactImpulse Create(IntPtr native)
{
if (native == IntPtr.Zero)
{
return default;
}

b2ContactImpulse_get_impulses(native, out IntPtr normalImpulses, out IntPtr tangentImpulses, out int count);
return new(native, new(normalImpulses, count), new(tangentImpulses, count));
}

private ContactImpulse(IntPtr native, in ArrayRef<float> normalImpulses, in ArrayRef<float> tangentImpulses)
{
_native = native;
NormalImpulses = normalImpulses;
TangentImpulses = tangentImpulses;
}
}
using Box2D.Collections;
using System;

namespace Box2D.Collision;

using static Interop.NativeMethods;

/// <summary>
/// Contact impulses for reporting. Impulses are used instead of forces because
/// sub-step forces may approach infinity for rigid body collisions. These
/// match up one-to-one with the contact points in <see cref="Manifold"/>.
/// </summary>
public readonly ref struct ContactImpulse
{
private readonly IntPtr _native;

/// <summary>
/// Gets whether this <see cref="ContactImpulse"/> points to a null unmanaged contact impulse.
/// </summary>
public bool IsNull => _native == IntPtr.Zero;

/// <summary>
/// Gets the normal impulses array.
/// </summary>
public ArrayRef<float> NormalImpulses { get; }

/// <summary>
/// Gets the tangent impulses array.
/// </summary>
public ArrayRef<float> TangentImpulses { get; }

internal static ContactImpulse Create(IntPtr native)
{
if (native == IntPtr.Zero)
{
return default;
}

b2ContactImpulse_get_impulses(native, out IntPtr normalImpulses, out IntPtr tangentImpulses, out int count);
return new(native, new(normalImpulses, count), new(tangentImpulses, count));
}

private ContactImpulse(IntPtr native, scoped in ArrayRef<float> normalImpulses, scoped in ArrayRef<float> tangentImpulses)
{
_native = native;
NormalImpulses = normalImpulses;
TangentImpulses = tangentImpulses;
}
}
Loading

0 comments on commit 3088bcb

Please sign in to comment.