From 3a1f9fe6800ef5fe7bbf1c0a5976720383ba84fe Mon Sep 17 00:00:00 2001 From: Joel Stålnacke Date: Mon, 23 Mar 2026 19:50:18 +0200 Subject: Rewrite native code in Odin and use ECS --- Djup.Native/src/Types.cs | 126 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 87 insertions(+), 39 deletions(-) (limited to 'Djup.Native/src/Types.cs') diff --git a/Djup.Native/src/Types.cs b/Djup.Native/src/Types.cs index 3afce83..e9eb824 100644 --- a/Djup.Native/src/Types.cs +++ b/Djup.Native/src/Types.cs @@ -3,11 +3,85 @@ using System.Runtime.InteropServices; namespace Djup.Native; +[StructLayout(LayoutKind.Sequential)] +public struct EntityId +{ + public int Id { get; } + + public static readonly EntityId Invalid = new(-1); + + public EntityId(int id) + { + Id = id; + } + + public bool IsInvalid => Id < 0; + + public override string ToString() => $"#{Id}"; + + public override bool Equals(object? other) + { + if (other is EntityId id) + { + return this == id; + } + return false; + } + + public override int GetHashCode() => Id.GetHashCode(); + + public static bool operator ==(EntityId a, EntityId b) + { + if (a < 0) + return b < 0; + if (b < 0) + return false; + return a.Id == b.Id; + } + public static bool operator !=(EntityId a, EntityId b) => !(a == b); + public static implicit operator int(EntityId entityId) => entityId.Id; + public static implicit operator EntityId(int id) => new(id); +} + +public enum Component : int +{ + Exists, + Ball, + PlayerInput +} + +[Flags] +public enum Components : int +{ + Exists = 1 << Component.Exists, + Ball = 1 << Component.Ball, + PlayerInput = 1 << Component.PlayerInput +} + +[StructLayout(LayoutKind.Sequential)] +public unsafe struct OdinSlice +{ + public void* Data { get; } + public nint Len { get; } + + public Span AsSpan() => new Span(Data, Len.ToInt32()); + public ReadOnlySpan AsReadOnlySpan() => new ReadOnlySpan(Data, Len.ToInt32()); +} + +[StructLayout(LayoutKind.Sequential)] +public unsafe struct OdinString +{ + public sbyte* Data { get; } + public nint Len { get; } + + public override string ToString() => new string(Data, 0, Len.ToInt32()); +} + [StructLayout(LayoutKind.Sequential)] public struct Vec2 { - public float X { get; } - public float Y { get; } + public float X { get; set; } + public float Y { get; set; } public Vec2(float x, float y) { @@ -21,59 +95,32 @@ public struct Vec2 public override string ToString() => $"({X}, {Y})"; } -public enum EntityState : byte -{ - Inactive = 0, - Active, - Removed, - Dead -} - -public enum PlayerInput : byte -{ - Up = 1 << 0, - Down = 1 << 1, - Left = 1 << 2, - Right = 1 << 3 -} - [StructLayout(LayoutKind.Sequential)] -public struct Player +public struct Ball { - public EntityState State { get; } - private byte input; public Vec2 Position { get; set; } public Vec2 Velocity { get; set; } } [StructLayout(LayoutKind.Sequential)] -public struct Bullet +public struct PlayerInput { - public EntityState State { get; set; } - public Vec2 Position { get; set; } - public Vec2 Velocity { get; set; } + public Vec2 Direction { get; set; } } [StructLayout(LayoutKind.Sequential)] -public unsafe struct Snapshot +public struct Snapshot { - private byte active; + private readonly byte active; public uint Tick { get; } - private int playersLength; - private Player* players; - private int bulletsLength; - private Bullet* bullets; - - public ReadOnlySpan Players => - new ReadOnlySpan(players, playersLength); - - public ReadOnlySpan Bullets => - new ReadOnlySpan(bullets, bulletsLength); + public OdinSlice Metadata { get; } + public OdinSlice Balls { get; } + public OdinSlice PlayerInputs { get; } + private IntPtr ctx; } public enum LogLevel { - Trace, Debug, Info, Warn, @@ -81,4 +128,5 @@ public enum LogLevel Fatal } -public delegate void LoggerDelegate(LogLevel level, string source, int line, string message); +public delegate void LoggerDelegate(LogLevel level, OdinString message, OdinString procedure, OdinString file, int line); +public delegate void AssertDelegate(OdinString prefix, OdinString message, OdinString procedure, OdinString file, int line); -- cgit v1.2.3