Skip to content

Commit

Permalink
Add more xml comments and edit examples
Browse files Browse the repository at this point in the history
  • Loading branch information
BeanCheeseBurrito committed Aug 24, 2023
1 parent 47cf7f7 commit e7815eb
Show file tree
Hide file tree
Showing 14 changed files with 1,186 additions and 35 deletions.
Binary file added src/Flecs.NET.Bindings/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/Flecs.NET.Examples/Cpp/Entities/Basics.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if CPP_ENTITIES_BASICS
#if Cpp_Entities_Basics

using Flecs.NET.Core;

Expand Down
4 changes: 2 additions & 2 deletions src/Flecs.NET.Examples/Flecs.NET.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup>
<Example>EXAMPLE</Example>
<Example>Example</Example>
<DefineConstants>$(DefineConstants);$(Example)</DefineConstants>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/Flecs.NET.Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if EXAMPLE
#if Example

Console.WriteLine("To run an example, use \"dotnet run --project Flecs.NET.Examples -p\"");
Console.WriteLine("Example: \"dotnet run --project src/Flecs.NET.Examples --property:Example=CPP_ENTITIES_BASICS\"");
Console.WriteLine("Example: \"dotnet run --project src/Flecs.NET.Examples --property:Example=Cpp_Entities_Basics\"");

#endif
47 changes: 47 additions & 0 deletions src/Flecs.NET/Core/AppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,49 +38,87 @@ public AppBuilder(ecs_world_t* world)
Desc.target_fps = 60;
}

/// <summary>
/// Cleans up resources.
/// </summary>
public void Dispose()
{
Managed.FreeGcHandle(_initHandle);
_initHandle = default;
}

/// <summary>
/// Sets the target fps.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public ref AppBuilder TargetFps(float value)
{
Desc.target_fps = value;
return ref this;
}

/// <summary>
/// Sets the delta time.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public ref AppBuilder DeltaTime(float value)
{
Desc.delta_time = value;
return ref this;
}

/// <summary>
/// Sets the number of threads to use.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public ref AppBuilder Threads(int value)
{
Desc.threads = value;
return ref this;
}

/// <summary>
/// Sets the number of frames to run.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public ref AppBuilder Frames(int value)
{
Desc.frames = value;
return ref this;
}

/// <summary>
/// Enable ecs access over http for the explorer.
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
public ref AppBuilder EnableRest(ushort port = 0)
{
Desc.enable_rest = Macros.True;
Desc.port = port;
return ref this;
}

/// <summary>
/// Periodically collect statistics.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public ref AppBuilder EnableMonitor(bool value = true)
{
Desc.enable_monitor = Macros.Bool(value);
return ref this;
}

/// <summary>
/// Sets a callback to be run before starting the main loop.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public ref AppBuilder Init(Ecs.AppInitAction value)
{
Managed.FreeGcHandle(_initHandle);
Expand All @@ -90,12 +128,21 @@ public ref AppBuilder Init(Ecs.AppInitAction value)
return ref this;
}

/// <summary>
/// Context for storing custom data.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public ref AppBuilder Ctx(void* value)
{
Desc.ctx = value;
return ref this;
}

/// <summary>
/// Runs the app.
/// </summary>
/// <returns></returns>
public int Run()
{
fixed (ecs_app_desc_t* appDesc = &Desc)
Expand Down
24 changes: 18 additions & 6 deletions src/Flecs.NET/Core/EnumType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ namespace Flecs.NET.Core
{
public static unsafe class EnumType<T>
{
public static EnumPair[] Data { get; private set; }
private static EnumPair[] _data = Array.Empty<EnumPair>();

/// <summary>
/// Inits entities for an enum type and it's members.
/// </summary>
/// <param name="world"></param>
/// <param name="id"></param>
public static void Init(ecs_world_t* world, ulong id)
{
ecs_cpp_enum_init(world, id);
Expand All @@ -19,7 +24,7 @@ public static void Init(ecs_world_t* world, ulong id)
if (RuntimeFeature.IsDynamicCodeSupported)
{
Array values = type.GetEnumValues();
Data = new EnumPair[values.Length];
_data = new EnumPair[values.Length];

for (int i = 0; i < values.Length; i++)
{
Expand All @@ -31,7 +36,7 @@ public static void Init(ecs_world_t* world, ulong id)
using NativeString nativeName = (NativeString)member.ToString();
ulong enumEntity = ecs_cpp_enum_constant_register(world, id, 0, nativeName, value);

Data[i] = new EnumPair(value, enumEntity);
_data[i] = new EnumPair(value, enumEntity);
}
}
else
Expand All @@ -40,23 +45,30 @@ public static void Init(ecs_world_t* world, ulong id)
}
}

/// <summary>
/// Gets an id for an enum member.
/// </summary>
/// <param name="member"></param>
/// <param name="world"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static ulong Id(T member, ecs_world_t* world)
{
Type<T>.Id(world);

int value = Convert.ToInt32(member, CultureInfo.InvariantCulture);

for (int i = 0; i < Data.Length; i++)
for (int i = 0; i < _data.Length; i++)
{
EnumPair enumPair = Data[i];
EnumPair enumPair = _data[i];
if (enumPair.Value == value)
return enumPair.Id;
}

throw new InvalidOperationException("Failed to find entity associated with enum member.");
}

public struct EnumPair
private struct EnumPair
{
public int Value { get; }
public ulong Id { get; }
Expand Down
42 changes: 42 additions & 0 deletions src/Flecs.NET/Core/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,32 @@

namespace Flecs.NET.Core
{
/// <summary>
/// A filter allows for uncached, adhoc iteration over ECS data.
/// </summary>
public unsafe struct Filter : IDisposable
{
private ecs_world_t* _world;
private ecs_filter_t _filter;

/// <summary>
/// A reference to the world.
/// </summary>
public ref ecs_world_t* World => ref _world;

/// <summary>
/// A pointer to the filter.
/// </summary>
public ecs_filter_t* FilterPtr => (ecs_filter_t*)Unsafe.AsPointer(ref _filter);


/// <summary>
/// Creates a filter.
/// </summary>
/// <param name="world"></param>
/// <param name="name"></param>
/// <param name="filterBuilder"></param>
/// <exception cref="InvalidOperationException"></exception>
public Filter(ecs_world_t* world, string name = "", FilterBuilder filterBuilder = default)
{
Assert.True(world == filterBuilder.World, "Worlds are different");
Expand Down Expand Up @@ -46,6 +64,11 @@ public Filter(ecs_world_t* world, string name = "", FilterBuilder filterBuilder
filterBuilder.Dispose();
}

/// <summary>
/// Creates a filter with the specified world and filter pointer.
/// </summary>
/// <param name="world"></param>
/// <param name="filter"></param>
public Filter(ecs_world_t* world, ecs_filter_t* filter)
{
_world = world;
Expand All @@ -55,6 +78,9 @@ public Filter(ecs_world_t* world, ecs_filter_t* filter)
}
}

/// <summary>
/// Cleans up resources.
/// </summary>
public void Dispose()
{
fixed (ecs_filter_t* mFilter = &_filter)
Expand All @@ -64,21 +90,37 @@ public void Dispose()
}
}

/// <summary>
/// Returns the entity associated with the filter.
/// </summary>
/// <returns></returns>
public Entity Entity()
{
return new Entity(World, ecs_get_entity(FilterPtr));
}

/// <summary>
/// Returns the field count of the filter.
/// </summary>
/// <returns></returns>
public int FieldCount()
{
return _filter.field_count;
}

/// <summary>
/// Returns the string representation of the filter query.
/// </summary>
/// <returns></returns>
public string Str()
{
return NativeString.GetStringAndFree(ecs_filter_str(World, FilterPtr));
}

/// <summary>
/// Iterates the filter using the provided callback.
/// </summary>
/// <param name="func"></param>
public void Iter(Ecs.IterCallback func)
{
ecs_iter_t iter = ecs_filter_iter(World, FilterPtr);
Expand Down
Loading

0 comments on commit e7815eb

Please sign in to comment.