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/LibDjup.cs | 74 +++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 31 deletions(-) (limited to 'Djup.Native/src/LibDjup.cs') diff --git a/Djup.Native/src/LibDjup.cs b/Djup.Native/src/LibDjup.cs index 4cd99f8..864f330 100644 --- a/Djup.Native/src/LibDjup.cs +++ b/Djup.Native/src/LibDjup.cs @@ -5,50 +5,62 @@ namespace Djup.Native; public static partial class LibDjup { const string LibraryName = "djup"; - const string Prefix = "dp_"; - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(hello_world))] - public static partial void hello_world(); + [LibraryImport(LibraryName, EntryPoint = nameof(vec2_dot))] + public static partial Vec2 vec2_dot(Vec2 a, Vec2 b); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(get_num))] - public static partial int get_num(); + [LibraryImport(LibraryName, EntryPoint = nameof(vec2_length))] + public static partial float vec2_length(Vec2 v); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(get_vec2))] - public static partial Vec2 get_vec2(); + [LibraryImport(LibraryName, EntryPoint = nameof(vec2_normal))] + public static partial Vec2 vec2_normal(Vec2 v); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(vec2_mul))] - public static partial Vec2 vec2_mul(Vec2 v, float scalar); + [LibraryImport(LibraryName, EntryPoint = nameof(sim_log_init))] + public static partial void sim_log_init(IntPtr simulation, [MarshalAs(UnmanagedType.FunctionPtr)] LoggerDelegate logger, [MarshalAs(UnmanagedType.FunctionPtr)] AssertDelegate assert); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(vec2_normal))] - public static partial Vec2 vec2_normal(Vec2 v); + [LibraryImport(LibraryName, EntryPoint = nameof(sim_set_log_level))] + public static partial void sim_set_log_level(IntPtr simulation, LogLevel minLevel); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(vec2_length))] - public static partial float vec2_length(Vec2 v); + [LibraryImport(LibraryName, EntryPoint = nameof(sim_create))] + public static partial IntPtr sim_create(int snapshots, int entities); + + [LibraryImport(LibraryName, EntryPoint = nameof(sim_free))] + public static partial void sim_free(IntPtr simulation); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(log_set_output))] - public static partial void log_set_output([MarshalAs(UnmanagedType.FunctionPtr)] LoggerDelegate logger); + [LibraryImport(LibraryName, EntryPoint = nameof(sim_get_snapshot))] + public unsafe static partial Snapshot* sim_get_snapshot(IntPtr simulation); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(log_set_level))] - public static partial void log_set_level(LogLevel minLevel); + [LibraryImport(LibraryName, EntryPoint = nameof(sim_return_snapshot))] + public unsafe static partial void sim_return_snapshot(IntPtr simulation, Snapshot* snapshot); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(physics_context_create))] - public static partial IntPtr physics_context_create(int snapshots, int players, int balls); + [LibraryImport(LibraryName, EntryPoint = nameof(sim_tick))] + public unsafe static partial void sim_tick(Snapshot* current, Snapshot* next); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(physics_context_free))] - public static partial void physics_context_free(IntPtr context); + [LibraryImport(LibraryName, EntryPoint = nameof(snapshot_create_entity))] + public unsafe static partial EntityId snapshot_create_entity(Snapshot* snapshot); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(physics_context_get_snapshot))] - public static partial IntPtr physics_context_get_snapshot(IntPtr context); + public enum ComponentAddError + { + None, + InvalidArg, + InvalidId, + EntityNotFound + } - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(physics_context_return_snapshot))] - public static partial void physics_context_return_snapshot(IntPtr context, IntPtr snapshot); + [LibraryImport(LibraryName, EntryPoint = nameof(snapshot_add_component))] + public unsafe static partial ComponentAddError snapshot_add_component(Snapshot* snapshot, EntityId entity, Component component, void* init); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(physics_tick))] - public static partial int physics_tick(IntPtr currentSnapshot, double delta, IntPtr nextSnapshot); + public enum ComponentRemoveError + { + None, + InvalidArg, + InvalidId, + EntityNotFound + } - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(snapshot_put_player))] - public static unsafe partial int snapshot_put_player(IntPtr snapshot, int playerId, Player* player); + [LibraryImport(LibraryName, EntryPoint = nameof(snapshot_remove_components))] + public unsafe static partial ComponentRemoveError snapshot_remove_components(Snapshot* snapshot, EntityId entity, Components components); - [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(snapshot_set_player_input))] - public static partial int snapshot_set_player_input(IntPtr snapshot, int playerId, byte input); + [LibraryImport(LibraryName, EntryPoint = nameof(snapshot_reset_entity))] + public unsafe static partial ComponentRemoveError snapshot_reset_entity(Snapshot* snapshot, EntityId entity); } -- cgit v1.2.3