/Flecs.NET

A C# wrapper for flecs

Primary LanguageC#MIT LicenseMIT

Flecs.NET

MIT

Docs · Examples · Discord

Flecs.NET is a high-level wrapper for flecs. Low-level bindings to the C api are included and generated with Bindgen.NET. Native libraries are cross-compiled with Vezel-Dev's Zig Toolsets.

Warning This repo is a work in progress. Bugs are expected and the API is subject to change.

Nuget

You can download the nuget package and use Flecs.NET right away!

Flecs.NET (Wrapper + bindings + native libraries): Release | Debug

dotnet add PROJECT package Flecs.NET.Release --version *-*

Flecs.NET.Bindings (Bindings + native libraries): Release | Debug

dotnet add PROJECT package Flecs.NET.Bindings.Release --version *-*

Flecs.NET.Native (Native libraries): Release | Debug

dotnet add PROJECT package Flecs.NET.Native.Release --version *-*

Flecs.NET provides both release and debug packages for nuget. To include both of them in your project based on your build configuration, use the packages references below. The latest stable or prerelease versions will be added to your project.

<ItemGroup>
    <PackageReference Include="Flecs.NET.Debug" Version="*-*" Condition="'$(Configuration)' == 'Debug'" />
    <PackageReference Include="Flecs.NET.Release" Version="*-*" Condition="'$(Configuration)' == 'Release'" />
</ItemGroup>

Show me the code!

using Flecs.NET.Core;

using World world = World.Create();

Routine routine = world.Routine(
    filter: world.FilterBuilder().Term<Position>().Term<Velocity>(),
    query: world.QueryBuilder(),
    routine: world.RoutineBuilder(),
    callback: it =>
    {
        Column<Position> p = it.Field<Position>(1);
        Column<Velocity> v = it.Field<Velocity>(2);

        foreach (int i in it)
        {
            p[i].X += v[i].X;
            p[i].Y += v[i].Y;
        }
    }
);

Entity entity = world.Entity("Bob")
    .Set(new Position { X = 10, Y = 20 })
    .Set(new Velocity { X = 1, Y = 2 });

while (world.Progress()) { }

public struct Position
{
    public float X { get; set; }
    public float Y { get; set; }
}

public struct Velocity
{
    public float X { get; set; }
    public float Y { get; set; }
}

Running examples

Note Flecs native libraries need to be built before running any of the examples. See step Compile flecs on how to compile them.

To run any of the example programs, use dotnet runand set the "Example" property to the example's path relative to the Flecs.NET.Examples project. Each level of the path must be separated by an underscore.

Example:

dotnet run --project src/Flecs.NET.Examples --property:Example=Cpp_Entities_Basics

Building from source

Clone the repo

Clone the repo and it's submodules.

git clone --recursive https://github.com/BeanCheeseBurrito/Flecs.NET.git
cd Flecs.NET

Restore dependencies

Run the following command on the solution to restore all project dependencies.

dotnet restore

Generate bindings

Generate the binding code. Bindings are generated with Bindgen.NET.

Note The binding generator needs access to system headers on MacOS. Ensure that XCode is installed.

dotnet run --project src/Flecs.NET.Bindgen

Compile flecs

Compile the native libraries. The zig compiler will automatically be downloaded and cached in your local nuget package folder. Native libraries will be cross-compiled for linux, macos, and windows.

dotnet build src/Flecs.NET.Native

Reference the project

Reference the project and import the native libraries.

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

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net7.0</TargetFramework>
    </PropertyGroup>

    <Import Project="PATH/Flecs.NET/src/Flecs.NET.Native/Flecs.NET.Native.targets" />

    <ItemGroup>
        <ProjectReference Include="PATH/Flecs.NET/src/Flecs.NET/Flecs.NET.csproj" />
    </ItemGroup>

</Project>