Skip to content

Instantly share code, notes, and snippets.

@raizam
Created October 14, 2019 17:56
Show Gist options
  • Save raizam/81f09891569a812be7b63d581fdd2229 to your computer and use it in GitHub Desktop.
Save raizam/81f09891569a812be7b63d581fdd2229 to your computer and use it in GitHub Desktop.
module FlecsSharp;
@nogc:
nothrow:
//Enums
// ecs_type_filter_kind_t: https://github.com/SanderMertens/flecs/blob/612c28635497c1749f8f3e84fa24eabfea58e05a/include/flecs.h#L161
enum TypeFilterKind : int
{
Default = 0,
All = 1,
Any = 2,
Exact = 3,
}
// EcsSystemKind: https://github.com/SanderMertens/flecs/blob/612c28635497c1749f8f3e84fa24eabfea58e05a/include/flecs.h#L76
enum SystemKind : int
{
Onload = 0,
Postload = 1,
Preupdate = 2,
Onupdate = 3,
Onvalidate = 4,
Postupdate = 5,
Prestore = 6,
Onstore = 7,
Manual = 8,
Onadd = 9,
Onremove = 10,
Onset = 11,
}
//Typedefs
public struct OsThread
{
public this(ulong value)
{
Value = value;
}
ulong Value;
}
public struct OsMutex
{
public this(ulong value)
{
Value = value;
}
ulong Value;
}
public struct OsCond
{
public this(ulong value)
{
Value = value;
}
ulong Value;
}
public struct OsDl
{
public this(ulong value)
{
Value = value;
}
ulong Value;
}
public struct EntityId
{
public this(ulong value)
{
Value = value;
}
ulong Value;
}
//Structs
//ecs_os_api_t
public struct OsApi
{
OsApiMallocDelegate malloc; //size: 8, offset:0
OsApiReallocDelegate realloc; //size: 8, offset:8
OsApiCallocDelegate calloc; //size: 8, offset:16
OsApiFreeDelegate free; //size: 8, offset:24
OsApiThreadNewDelegate threadNew; //size: 8, offset:32
OsApiThreadJoinDelegate threadJoin; //size: 8, offset:40
OsApiMutexNewDelegate mutexNew; //size: 8, offset:48
OsApiMutexFreeDelegate mutexFree; //size: 8, offset:56
OsApiMutexLockDelegate mutexLock; //size: 8, offset:64
OsApiMutexLockDelegate mutexUnlock; //size: 8, offset:72
OsApiCondNewDelegate condNew; //size: 8, offset:80
OsApiCondFreeDelegate condFree; //size: 8, offset:88
OsApiCondSignalDelegate condSignal; //size: 8, offset:96
OsApiCondBroadcastDelegate condBroadcast; //size: 8, offset:104
OsApiCondWaitDelegate condWait; //size: 8, offset:112
OsApiSleepDelegate sleep; //size: 8, offset:120
OsApiGetTimeDelegate getTime; //size: 8, offset:128
OsApiLogDelegate log; //size: 8, offset:136
OsApiLogDelegate logError; //size: 8, offset:144
OsApiLogDelegate logDebug; //size: 8, offset:152
OsApiLogDelegate logWarning; //size: 8, offset:160
OsApiAbortDelegate abort; //size: 8, offset:168
OsApiDlopenDelegate dlopen; //size: 8, offset:176
OsApiDlprocDelegate dlproc; //size: 8, offset:184
OsApiDlcloseDelegate dlclose; //size: 8, offset:192
OsApiModuleToDlDelegate moduleToDl; //size: 8, offset:200
}
//ecs_time_t
public struct Time
{
int sec; //size: 4, offset:0
uint nanosec; //size: 4, offset:4
}
//ecs_vector_params_t
public struct VectorParams
{
MoveDelegate moveAction; //size: 8, offset:0
void* moveCtx; //size: 8, offset:8
void* ctx; //size: 8, offset:16
uint elementSize; //size: 4, offset:24
}
//ecs_map_iter_t
public struct MapIter
{
Map map; //size: 8, offset:0
uint bucketIndex; //size: 4, offset:8
uint node; //size: 4, offset:12
}
//ecs_world_stats_t
public struct WorldStats
{
uint systemCount; //size: 4, offset:0
uint tableCount; //size: 4, offset:4
uint componentCount; //size: 4, offset:8
uint entityCount; //size: 4, offset:12
uint threadCount; //size: 4, offset:16
uint tickCount; //size: 4, offset:20
float systemTime; //size: 4, offset:24
float frameTime; //size: 4, offset:28
float mergeTime; //size: 4, offset:32
MemoryStats memory; //size: 64, offset:36
Vector features; //size: 8, offset:104
Vector onLoadSystems; //size: 8, offset:112
Vector postLoadSystems; //size: 8, offset:120
Vector preUpdateSystems; //size: 8, offset:128
Vector onUpdateSystems; //size: 8, offset:136
Vector onValidateSystems; //size: 8, offset:144
Vector postUpdateSystems; //size: 8, offset:152
Vector preStoreSystems; //size: 8, offset:160
Vector onStoreSystems; //size: 8, offset:168
Vector taskSystems; //size: 8, offset:176
Vector inactiveSystems; //size: 8, offset:184
Vector onDemandSystems; //size: 8, offset:192
Vector onAddSystems; //size: 8, offset:200
Vector onRemoveSystems; //size: 8, offset:208
Vector onSetSystems; //size: 8, offset:216
Vector components; //size: 8, offset:224
byte frameProfiling; //size: 1, offset:232
byte systemProfiling; //size: 1, offset:233
}
//EcsMemoryStats
public struct MemoryStats
{
MemoryStat total; //size: 8, offset:0
MemoryStat components; //size: 8, offset:8
MemoryStat entities; //size: 8, offset:16
MemoryStat systems; //size: 8, offset:24
MemoryStat families; //size: 8, offset:32
MemoryStat tables; //size: 8, offset:40
MemoryStat stage; //size: 8, offset:48
MemoryStat world; //size: 8, offset:56
}
//EcsMemoryStat
public struct MemoryStat
{
uint allocd; //size: 4, offset:0
uint used; //size: 4, offset:4
}
//ecs_table_data_t
public struct TableData
{
uint rowCount; //size: 4, offset:0
uint columnCount; //size: 4, offset:4
EntityId* entities; //size: 8, offset:8
EntityId* components; //size: 8, offset:16
TableColumns* columns; //size: 8, offset:24
}
//ecs_type_filter_t
public struct TypeFilter
{
Type include; //size: 8, offset:0
Type exclude; //size: 8, offset:8
TypeFilterKind includeKind; //size: 4, offset:16
TypeFilterKind excludeKind; //size: 4, offset:20
}
//ecs_rows_t
public struct Rows
{
World world; //size: 8, offset:0
EntityId system; //size: 8, offset:8
int* columns; //size: 8, offset:16
ushort columnCount; //size: 2, offset:24
void* table; //size: 8, offset:32
void* tableColumns; //size: 8, offset:40
Reference* references; //size: 8, offset:48
EntityId* components; //size: 8, offset:56
EntityId* entities; //size: 8, offset:64
void* param; //size: 8, offset:72
float deltaTime; //size: 4, offset:80
float worldTime; //size: 4, offset:84
uint frameOffset; //size: 4, offset:88
uint offset; //size: 4, offset:92
uint count; //size: 4, offset:96
EntityId interruptedBy; //size: 8, offset:104
}
//ecs_reference_t
public struct Reference
{
EntityId entity; //size: 8, offset:0
EntityId component; //size: 8, offset:8
void* cachedPtr; //size: 8, offset:16
}
//RefPtr
//type
public struct Type
{
void* ptr;
this(void* ptr) { this.ptr = ptr; }
}
//ecs_table_columns_t
public struct TableColumns
{
void* ptr;
this(void* ptr) { this.ptr = ptr; }
}
//OpaquePtrs
//ecs_vector_t
public struct Vector
{
immutable void* ptr;
public this(immutable void* ptr) { this.ptr = ptr; }
}
//ecs_chunked_t
public struct Chunked
{
immutable void* ptr;
public this(immutable void* ptr) { this.ptr = ptr; }
}
//ecs_map_t
public struct Map
{
immutable void* ptr;
public this(immutable void* ptr) { this.ptr = ptr; }
}
//ecs_world_t
public struct World
{
immutable void* ptr;
public this(immutable void* ptr) { this.ptr = ptr; }
}
//Delegates
public alias OsApiMallocDelegate = void* function(/*size_t*/ulong size);
public alias OsApiReallocDelegate = void* function(void* ptr, /*size_t*/ulong size);
public alias OsApiCallocDelegate = void* function(/*size_t*/ulong num, /*size_t*/ulong size);
public alias OsApiFreeDelegate = void function(void* ptr);
public alias OsApiThreadNewDelegate = OsThread function(OsThread ecsOsThreadT, OsThreadCallbackDelegate callback, void* param);
public alias OsThreadCallbackDelegate = void* function(void* param0);
public alias OsApiThreadJoinDelegate = void* function(OsThread thread);
public alias OsApiMutexNewDelegate = OsMutex function(OsMutex ecsOsMutexT);
public alias OsApiMutexFreeDelegate = void function(OsMutex mutex);
public alias OsApiMutexLockDelegate = void function(OsMutex mutex);
public alias OsApiCondNewDelegate = OsCond function(OsCond ecsOsCondT);
public alias OsApiCondFreeDelegate = void function(OsCond cond);
public alias OsApiCondSignalDelegate = void function(OsCond cond);
public alias OsApiCondBroadcastDelegate = void function(OsCond cond);
public alias OsApiCondWaitDelegate = void function(OsCond cond, OsMutex mutex);
public alias OsApiSleepDelegate = void function(uint sec, uint nanosec);
public alias OsApiGetTimeDelegate = void function(Time* timeOut);
public alias OsApiLogDelegate = void function(char* fmt, void* args);
public alias OsApiAbortDelegate = void function();
public alias OsApiDlopenDelegate = OsDl function(OsDl ecsOsDlT, char* libname);
public alias OsApiDlprocDelegate = OsProcDelegate function(OsProcDelegate ecsOsProcT, OsDl lib, char* procname);
public alias OsProcDelegate = void function();
public alias OsApiDlcloseDelegate = void function(OsDl lib);
public alias OsApiModuleToDlDelegate = ref char function(char* moduleId);
public alias MoveDelegate = void function(Vector array, VectorParams* _params, void* to, void* _from, void* ctx);
public alias ComparatorDelegate = int function(void* p1, void* p2);
public alias ModuleInitActionDelegate = void function(World world, int flags);
public alias SystemActionDelegate = void function(Rows* data);
extern (C) void ecs_os_set_api(out OsApi osApi);
extern (C) void ecs_os_set_api_defaults();
extern (C) void ecs_os_log(in char fmt);
extern (C) void ecs_os_warn(in char fmt);
extern (C) void ecs_os_err(in char fmt);
extern (C) void ecs_os_dbg(in char fmt);
extern (C) void ecs_os_enable_dbg(bool enable);
extern (C) bool ecs_os_dbg_enabled();
extern (C) void ecs_sleepf(double t);
extern (C) double ecs_time_measure(out Time start);
extern (C) Vector ecs_vector_new(out VectorParams _params, uint size);
extern (C) Vector ecs_vector_new_from_buffer(out VectorParams _params, uint size, void* buffer);
extern (C) void ecs_vector_free(Vector array);
extern (C) void ecs_vector_clear(Vector array);
extern (C) void* ecs_vector_add(out Vector arrayInout, out VectorParams _params);
extern (C) void* ecs_vector_addn(out Vector arrayInout, out VectorParams _params, uint count);
extern (C) void* ecs_vector_get(Vector array, out VectorParams _params, uint index);
extern (C) uint ecs_vector_get_index(Vector array, out VectorParams _params, void* elem);
extern (C) void* ecs_vector_last(Vector array, out VectorParams _params);
extern (C) uint ecs_vector_remove(Vector array, out VectorParams _params, void* elem);
extern (C) void ecs_vector_remove_last(Vector array);
extern (C) bool ecs_vector_pop(Vector array, out VectorParams _params, void* _value);
extern (C) uint ecs_vector_move_index(out Vector dstArray, Vector srcArray, out VectorParams _params, uint index);
extern (C) uint ecs_vector_remove_index(Vector array, out VectorParams _params, uint index);
extern (C) void ecs_vector_reclaim(out Vector array, out VectorParams _params);
extern (C) uint ecs_vector_set_size(out Vector array, out VectorParams _params, uint size);
extern (C) uint ecs_vector_set_count(out Vector array, out VectorParams _params, uint size);
extern (C) uint ecs_vector_count(Vector array);
extern (C) uint ecs_vector_size(Vector array);
extern (C) void* ecs_vector_first(Vector array);
extern (C) void ecs_vector_sort(Vector array, out VectorParams _params, ComparatorDelegate compareAction);
extern (C) void ecs_vector_memory(Vector array, out VectorParams _params, out uint allocd, out uint used);
extern (C) void ecs_chunked_free(Chunked chunked);
extern (C) void ecs_chunked_clear(Chunked chunked);
extern (C) uint ecs_chunked_count(Chunked chunked);
extern (C) ref uint ecs_chunked_indices(Chunked chunked);
extern (C) void ecs_chunked_memory(Chunked chunked, out uint allocd, out uint used);
extern (C) Map ecs_map_new(uint size, uint elemSize);
extern (C) void ecs_map_free(Map map);
extern (C) void ecs_map_memory(Map map, out uint total, out uint used);
extern (C) uint ecs_map_count(Map map);
extern (C) uint ecs_map_set_size(Map map, uint size);
extern (C) uint ecs_map_data_size(Map map);
extern (C) uint ecs_map_grow(Map map, uint size);
extern (C) uint ecs_map_bucket_count(Map map);
extern (C) void ecs_map_clear(Map map);
extern (C) void* ecs_map_get_ptr(Map map, ulong keyHash);
extern (C) int ecs_map_remove(Map map, ulong keyHash);
extern (C) MapIter ecs_map_iter(Map map);
extern (C) bool ecs_map_hasnext(out MapIter it);
extern (C) void* ecs_map_next(out MapIter it);
extern (C) void* ecs_map_next_w_size(out MapIter it, /*size_t*/ulong size);
extern (C) void* ecs_map_next_w_key(out MapIter it, out ulong keyOut);
extern (C) void* ecs_map_next_w_key_w_size(out MapIter it, out ulong keyOut, /*size_t*/ulong size);
extern (C) void ecs_get_stats(World world, out WorldStats stats);
extern (C) void ecs_free_stats(out WorldStats stats);
extern (C) void ecs_measure_frame_time(World world, bool enable);
extern (C) void ecs_measure_system_time(World world, bool enable);
extern (C) World ecs_init();
extern (C) World ecs_init_w_args(int argc, out byte argv);
extern (C) int ecs_fini(World world);
extern (C) void ecs_quit(World world);
extern (C) EntityId ecs_import_from_library(World world, in char libraryName, in char moduleName, int flags);
extern (C) bool ecs_progress(World world, float deltaTime);
extern (C) void ecs_merge(World world);
extern (C) void ecs_set_automerge(World world, bool autoMerge);
extern (C) void ecs_set_threads(World world, uint threads);
extern (C) uint ecs_get_threads(World world);
extern (C) ushort ecs_get_thread_index(World world);
extern (C) void ecs_set_target_fps(World world, float fps);
extern (C) uint ecs_get_target_fps(World world);
extern (C) int ecs_enable_admin(World world, ushort port);
extern (C) float ecs_get_delta_time(World world);
extern (C) void ecs_set_context(World world, void* ctx);
extern (C) void* ecs_get_context(World world);
extern (C) uint ecs_get_tick(World world);
extern (C) void ecs_dim(World world, uint entityCount);
extern (C) void ecs_set_entity_range(World world, EntityId idStart, EntityId idEnd);
extern (C) bool ecs_enable_range_check(World world, bool enable);
extern (C) EntityId ecs_set_w_data(World world, out TableData data);
extern (C) EntityId ecs_clone(World world, EntityId entity, bool copyValue);
extern (C) void ecs_delete(World world, EntityId entity);
extern (C) void ecs_delete_w_filter(World world, out TypeFilter filter);
extern (C) void ecs_adopt(World world, EntityId entity, EntityId parent);
extern (C) void ecs_orphan(World world, EntityId entity, EntityId parent);
extern (C) void ecs_inherit(World world, EntityId entity, EntityId _base);
extern (C) void ecs_disinherit(World world, EntityId entity, EntityId _base);
extern (C) bool ecs_has_entity(World world, EntityId entity, EntityId component);
extern (C) bool ecs_contains(World world, EntityId parent, EntityId child);
extern (C) Type ecs_get_type(World world, EntityId entity);
extern (C) ref char ecs_get_id(World world, EntityId entity);
extern (C) bool ecs_is_empty(World world, EntityId entity);
extern (C) EntityId ecs_lookup(World world, in char id);
extern (C) EntityId ecs_lookup_child(World world, EntityId parent, in char id);
extern (C) Type ecs_type_from_entity(World world, EntityId entity);
extern (C) EntityId ecs_type_to_entity(World world, Type type);
extern (C) Type ecs_type_add(World world, Type type, EntityId entity);
extern (C) Type ecs_type_merge(World world, Type type, Type typeAdd, Type typeRemove);
extern (C) Type ecs_type_find(World world, out EntityId array, uint count);
extern (C) EntityId ecs_type_get_entity(World world, Type type, uint index);
extern (C) bool ecs_type_has_entity(World world, Type type, EntityId entity);
extern (C) Type ecs_expr_to_type(World world, in char expr);
extern (C) ref char ecs_type_to_expr(World world, Type type);
extern (C) bool ecs_type_match_w_filter(World world, Type type, out TypeFilter filter);
extern (C) void ecs_enable(World world, EntityId system, bool enabled);
extern (C) void ecs_set_period(World world, EntityId system, float period);
extern (C) bool ecs_is_enabled(World world, EntityId system);
extern (C) EntityId ecs_run(World world, EntityId system, float deltaTime, void* param);
extern (C) void ecs_set_system_context(World world, EntityId system, void* ctx);
extern (C) void* ecs_get_system_context(World world, EntityId system);
extern (C) bool ecs_is_shared(out Rows rows, uint column);
extern (C) EntityId ecs_column_source(out Rows rows, uint column);
extern (C) EntityId ecs_column_entity(out Rows rows, uint column);
extern (C) Type ecs_column_type(out Rows rows, uint column);
extern (C) Type ecs_table_type(out Rows rows);
extern (C) void* ecs_table_column(out Rows rows, uint column);
extern (C) EntityId ecs_new_entity(World world, in char id, in char components);
extern (C) EntityId ecs_new_component(World world, in char id, /*size_t*/ulong size);
extern (C) EntityId ecs_new_system(World world, in char id, SystemKind kind, in char sig, SystemActionDelegate action);
extern (C) EntityId ecs_new_type(World world, in char id, in char components);
extern (C) EntityId ecs_new_prefab(World world, in char id, in char sig);
extern (C) ref char ecs_strerror(uint errorCode);
extern (C) Chunked _ecs_chunked_new(uint elementSize, uint chunkSize, uint chunkCount);
extern (C) void* _ecs_chunked_add(Chunked chunked, uint size);
extern (C) void* _ecs_chunked_remove(Chunked chunked, uint size, uint index);
extern (C) void* _ecs_chunked_get(Chunked chunked, uint size, uint index);
extern (C) void* _ecs_chunked_get_sparse(Chunked chunked, uint size, uint index);
extern (C) void* _ecs_map_set(Map map, ulong keyHash, void* data, uint size);
extern (C) bool _ecs_map_has(Map map, ulong keyHash, void* valueOut, uint size);
extern (C) EntityId _ecs_import(World world, ModuleInitActionDelegate _module, in char moduleName, int flags, void* handlesOut, /*size_t*/ulong handlesSize);
extern (C) void _ecs_dim_type(World world, Type type, uint entityCount);
extern (C) EntityId _ecs_new(World world, Type type);
extern (C) EntityId _ecs_new_w_count(World world, Type type, uint count);
extern (C) EntityId _ecs_new_child(World world, EntityId parent, Type type);
extern (C) EntityId _ecs_new_child_w_count(World world, EntityId parent, Type type, uint count);
extern (C) EntityId _ecs_new_instance(World world, EntityId _base, Type type);
extern (C) EntityId _ecs_new_instance_w_count(World world, EntityId _base, Type type, uint count);
extern (C) void _ecs_add(World world, EntityId entity, Type type);
extern (C) void _ecs_remove(World world, EntityId entity, Type type);
extern (C) void _ecs_add_remove(World world, EntityId entity, Type toAdd, Type toRemove);
extern (C) void _ecs_add_remove_w_filter(World world, Type toAdd, Type toRemove, out TypeFilter filter);
extern (C) void* _ecs_get_ptr(World world, EntityId entity, Type type);
extern (C) EntityId _ecs_set_ptr(World world, EntityId entity, EntityId component, /*size_t*/ulong size, void* ptr);
extern (C) EntityId _ecs_set_singleton_ptr(World world, EntityId component, /*size_t*/ulong size, void* ptr);
extern (C) bool _ecs_has(World world, EntityId entity, Type type);
extern (C) bool _ecs_has_owned(World world, EntityId entity, Type type);
extern (C) bool _ecs_has_any(World world, EntityId entity, Type type);
extern (C) bool _ecs_has_any_owned(World world, EntityId entity, Type type);
extern (C) EntityId _ecs_get_parent(World world, EntityId entity, EntityId component);
extern (C) uint _ecs_count(World world, Type type);
extern (C) EntityId _ecs_run_w_filter(World world, EntityId system, float deltaTime, uint offset, uint limit, Type filter, void* param);
extern (C) void* _ecs_column(out Rows rows, /*size_t*/ulong size, uint column);
extern (C) void* _ecs_field(out Rows rows, /*size_t*/ulong size, uint column, uint row);
extern (C) void _ecs_abort(uint errorCode, in char param, in char file, uint line);
extern (C) void _ecs_assert(bool condition, uint errorCode, in char param, in char conditionStr, in char file, uint line);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment