Skip to content

Commit

Permalink
update c# binding
Browse files Browse the repository at this point in the history
  • Loading branch information
kaesaecracker committed Jun 23, 2024
1 parent 3dc5dae commit edcc142
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 17 deletions.
30 changes: 15 additions & 15 deletions crates/servicepoint_binding_cs/ServicePoint/ByteGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@

namespace ServicePoint;

public sealed class ByteGrid : SpNativeInstance<BindGen.ByteGrid>
public sealed class BrightnessGrid : SpNativeInstance<BindGen.CBrightnessGrid>
{
public static ByteGrid New(int width, int height)
public static BrightnessGrid New(int width, int height)
{
unsafe
{
return new ByteGrid(NativeMethods.sp_byte_grid_new((nuint)width, (nuint)height));
return new BrightnessGrid(NativeMethods.sp_brightness_grid_new((nuint)width, (nuint)height));
}
}

public static ByteGrid Load(int width, int height, Span<byte> bytes)
public static BrightnessGrid Load(int width, int height, Span<byte> bytes)
{
unsafe
{
fixed (byte* bytesPtr = bytes)
{
return new ByteGrid(NativeMethods.sp_byte_grid_load((nuint)width, (nuint)height, bytesPtr,
return new BrightnessGrid(NativeMethods.sp_brightness_grid_load((nuint)width, (nuint)height, bytesPtr,
(nuint)bytes.Length));
}
}
}

public ByteGrid Clone()
public BrightnessGrid Clone()
{
unsafe
{
return new ByteGrid(NativeMethods.sp_byte_grid_clone(Instance));
return new BrightnessGrid(NativeMethods.sp_brightness_grid_clone(Instance));
}
}

Expand All @@ -39,14 +39,14 @@ public ByteGrid Clone()
{
unsafe
{
return NativeMethods.sp_byte_grid_get(Instance, (nuint)x, (nuint)y);
return NativeMethods.sp_brightness_grid_get(Instance, (nuint)x, (nuint)y);
}
}
set
{
unsafe
{
NativeMethods.sp_byte_grid_set(Instance, (nuint)x, (nuint)y, value);
NativeMethods.sp_brightness_grid_set(Instance, (nuint)x, (nuint)y, value);
}
}
}
Expand Down Expand Up @@ -85,7 +85,7 @@ public void Fill(byte value)
{
unsafe
{
NativeMethods.sp_byte_grid_fill(Instance, value);
NativeMethods.sp_brightness_grid_fill(Instance, value);
}
}

Expand All @@ -95,7 +95,7 @@ public int Width
{
unsafe
{
return (int)NativeMethods.sp_byte_grid_width(Instance);
return (int)NativeMethods.sp_brightness_grid_width(Instance);
}
}
}
Expand All @@ -106,7 +106,7 @@ public int Height
{
unsafe
{
return (int)NativeMethods.sp_byte_grid_height(Instance);
return (int)NativeMethods.sp_brightness_grid_height(Instance);
}
}
}
Expand All @@ -117,18 +117,18 @@ public Span<byte> Data
{
unsafe
{
var slice = NativeMethods.sp_byte_grid_unsafe_data_ref(Instance);
var slice = NativeMethods.sp_brightness_grid_unsafe_data_ref(Instance);
return new Span<byte>(slice.start, (int)slice.length);
}
}
}

private unsafe ByteGrid(BindGen.ByteGrid* instance) : base(instance)
private unsafe BrightnessGrid(BindGen.CBrightnessGrid* instance) : base(instance)
{
}

private protected override unsafe void Dealloc()
{
NativeMethods.sp_byte_grid_dealloc(Instance);
NativeMethods.sp_brightness_grid_dealloc(Instance);
}
}
134 changes: 134 additions & 0 deletions crates/servicepoint_binding_cs/ServicePoint/Cp437Grid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System.Text;
using ServicePoint.BindGen;

namespace ServicePoint;

public sealed class Cp437Grid : SpNativeInstance<BindGen.CCp437Grid>
{
public static Cp437Grid New(int width, int height)
{
unsafe
{
return new Cp437Grid(NativeMethods.sp_cp437_grid_new((nuint)width, (nuint)height));
}
}

public static Cp437Grid Load(int width, int height, Span<byte> bytes)
{
unsafe
{
fixed (byte* bytesPtr = bytes)
{
return new Cp437Grid(NativeMethods.sp_cp437_grid_load((nuint)width, (nuint)height, bytesPtr,
(nuint)bytes.Length));
}
}
}

public Cp437Grid Clone()
{
unsafe
{
return new Cp437Grid(NativeMethods.sp_cp437_grid_clone(Instance));
}
}

public byte this[int x, int y]
{
get
{
unsafe
{
return NativeMethods.sp_cp437_grid_get(Instance, (nuint)x, (nuint)y);
}
}
set
{
unsafe
{
NativeMethods.sp_cp437_grid_set(Instance, (nuint)x, (nuint)y, value);
}
}
}

public string this[int y]
{
set
{
var width = Width;
ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Length, width);

var x = 0;
for (; x < value.Length; x++)
this[x, y] = (byte)value[x];

for (; x < width; x++)
this[x, y] = 0;
}

get
{
var sb = new StringBuilder();
for (int x = 0; x < Width; x++)
{
var val = this[x, y];
if (val == 0)
break;
sb.Append((char)val);
}

return sb.ToString();
}
}

public void Fill(byte value)
{
unsafe
{
NativeMethods.sp_cp437_grid_fill(Instance, value);
}
}

public int Width
{
get
{
unsafe
{
return (int)NativeMethods.sp_cp437_grid_width(Instance);
}
}
}

public int Height
{
get
{
unsafe
{
return (int)NativeMethods.sp_cp437_grid_height(Instance);
}
}
}

public Span<byte> Data
{
get
{
unsafe
{
var slice = NativeMethods.sp_cp437_grid_unsafe_data_ref(Instance);
return new Span<byte>(slice.start, (int)slice.length);
}
}
}

private unsafe Cp437Grid(BindGen.CCp437Grid* instance) : base(instance)
{
}

private protected override unsafe void Dealloc()
{
NativeMethods.sp_cp437_grid_dealloc(Instance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -35,7 +34,7 @@
<Exec Command="cargo build"/>
<Exec Command="cargo build --manifest-path ../../../crates/servicepoint_binding_c/Cargo.toml"/>
</Target>

<!-- include native binary in output -->
<ItemGroup Condition="'$(Configuration)'=='Debug'">
<Content Include="../../../target/debug/libservicepoint_binding_c.so" CopyToOutputDirectory="Always">
Expand Down

0 comments on commit edcc142

Please sign in to comment.