bubel-ecs/c-api/manager.d
Mergul 0002852303 Added CAPI files
I forgot to add CAPI files to .gitignore with previous commits :/
2021-11-17 15:22:13 +01:00

306 lines
No EOL
7.7 KiB
D

module manager;
import bubel.ecs.manager;
import bubel.ecs.entity;
import bubel.ecs.events;
import bubel.ecs.system;
import bubel.ecs.std;
import core.stdc.string;
extern (C):
/*
struct BECSComponentInfo
{
///Component size
ushort size;
///Component data alignment
ushort alignment;
///Initialization data
ubyte[] init_data;
///Pointer to component destroy callback
void* destroy_callback;
///Pointer to component create callback
void* create_callback;
//void* create_callback;
}*/
alias BECSComponentInfo = EntityManager.ComponentInfo;
// alias BECSEventCallData = EntityManager.EventCallData;
enum BECSSystemComponentAttribute
{
required = 0,
optional = 1,
excluded = 2
}
enum BECSSystemComponentAccess
{
writable = 0,
readonly = 1
}
struct BECSSystemComponentInfo
{
ushort id;
BECSSystemComponentAttribute attribute;
BECSSystemComponentAccess access;
}
struct BECSEventCallback
{
ushort id;
void function(EntityManager.EventCallData* data) callback;
}
struct BECSSystemRegisterInfo
{
uint pass_id;
int priority;
uint max_jobs;
BECSSystemComponentInfo[] components;
BECSEventCallback[] event_handlers;
byte[] init_data;
void function(EntityManager.SystemCallData* call_data) on_update;
void function(void* system_pointer) on_create;
void function(void* system_pointer) on_destroy;
void function(void* system_pointer) on_enable;
void function(void* system_pointer) on_disable;
bool function(void* system_pointer) on_begin;
void function(void* system_pointer) on_end;
void function(EntityManager.ListenerCallData*) on_add_entity;
void function(EntityManager.ListenerCallData*) on_remove_entity;
void function(EntityManager.ListenerCallData*) on_change_entity;
void function(void* system_pointer, EntityManager.EntityInfo* info) filter_entity;
}
alias BECSEventRegisterInfo = EntityManager.EventRegisterInfo;
void becsInitialize()
{
EntityManager.initialize();
}
void becsDestroy()
{
EntityManager.destroy();
}
void becsBeginRegister()
{
gEntityManager.beginRegister();
}
void becsEndRegister()
{
gEntityManager.endRegister();
}
void becsBegin()
{
gEntityManager.begin();
}
void becsEnd()
{
gEntityManager.end();
}
void becsCommit()
{
gEntityManager.commit();
}
void becsUpdate(ushort pass_id)
{
gEntityManager.update(pass_id);
}
void becsUpdateMT(ushort pass_id)
{
gEntityManager.update(pass_id);
}
ushort becsRegisterPass(const (char)* name)
{
return gEntityManager.registerPass(name[0 .. strlen(name)]);
}
ushort becsRegisterComponent(const(char)* name, BECSComponentInfo info)
{
return gEntityManager.registerComponent(name[0 .. strlen(name)], info);
}
ushort becsRegisterEvent(const(char)* name, BECSEventRegisterInfo info)
{
return gEntityManager.registerEvent(name[0 .. strlen(name)], info);
}
ushort becsRegisterSystem(const(char)* name, BECSSystemRegisterInfo info)
{
System system;
if(info.init_data.length)
{
system.m_system_pointer = malloc(info.init_data.length);
memcpy(system.m_system_pointer, info.init_data.ptr, info.init_data.length);
}
if(info.event_handlers.length)
{
system.m_event_callers = Mallocator.makeArray!(System.EventCaller)(info.event_handlers.length);
foreach(i, BECSEventCallback callback; info.event_handlers)
{
system.m_event_callers[i].id = callback.id;
system.m_event_callers[i].callback = callback.callback;
}
}
system.m_update = info.on_update;
system.m_create = info.on_create;
system.m_destroy = info.on_destroy;
system.m_enable = info.on_enable;
system.m_disable = info.on_disable;
system.m_begin = info.on_begin;
system.m_end = info.on_end;
system.m_add_entity = info.on_add_entity;
system.m_remove_entity = info.on_remove_entity;
system.m_change_entity = info.on_change_entity;
system.m_filter_entity = info.filter_entity;
if(info.components.length)
{
uint req;
uint opt;
foreach(comp; info.components)
{
if(comp.attribute == BECSSystemComponentAttribute.required)req++;
else if(comp.attribute == BECSSystemComponentAttribute.optional)opt++;
}
if(req)system.m_components = Mallocator.makeArray!ushort(req);
if(opt)system.m_optional_components = Mallocator.makeArray!ushort(opt);
req = 0;
opt = 0;
foreach(comp; info.components)
{
if(comp.attribute == BECSSystemComponentAttribute.required)
{
system.m_components[req++] = comp.id;
}
else if(comp.attribute == BECSSystemComponentAttribute.optional)
{
system.m_optional_components[opt++] = comp.id;
}
}
}
else
{
system.m_empty = true;
}
return gEntityManager.registerSystem(name[0 .. strlen(name)], system);
}
EntityTemplate* becsAllocateTemplate(uint count, ushort* components)
{
return gEntityManager.allocateTemplate(components[0 .. count]);
}
EntityTemplate* becsAllocateTemplateFromEntity(EntityID id, bool fill_default)
{
return gEntityManager.allocateTemplate(id, fill_default);
}
EntityTemplate* becsAllocateTemplateCopy(EntityTemplate* tmpl)
{
return gEntityManager.allocateTemplate(tmpl);
}
EntityTemplate* becsAllocateTemplateFromTemplate(EntityTemplate* tmpl, size_t new_count, ushort* components, size_t remove_count, ushort* remove_components)
{
return gEntityManager.allocateTemplate(tmpl, components[0 .. new_count], remove_components[0 .. remove_count]);
}
void becsFreeTemplate(EntityTemplate* tmpl)
{
gEntityManager.freeTemplate(tmpl);
}
Entity* becsAddEntity(EntityTemplate* tmpl)
{
return gEntityManager.addEntity(tmpl);
}
Entity* becsAddEntityCopy(EntityID id)
{
return gEntityManager.addEntityCopy(id);
}
void becsRemoveEntity(EntityID id)
{
gEntityManager.removeEntity(id);
}
Entity* becsGetEntity(EntityID id)
{
return gEntityManager.getEntity(id);
}
void* becsEntityGetComponent(Entity* entity, ushort component_id)
{
return entity.getComponent(component_id);
}
void* becsSystemCallDataGetComponentArray(EntityManager.SystemCallData* data, ushort component_id)
{
if(data.info.deltas.length <= component_id || data.info.deltas[component_id] == 0)return null;
return cast(void*)data.block + data.info.deltas[component_id];
}
Entity* becsSystemCallDataGetEntitiesArray(EntityManager.SystemCallData* data)
{
return cast(Entity*)data.block.dataBegin();
}
void* becsListenerCallDataGetComponentArray(EntityManager.ListenerCallData* data, ushort component_id)
{
if(data.info.deltas.length <= component_id || data.info.deltas[component_id] == 0)return null;
return cast(void*)data.block + data.info.deltas[component_id];
}
Entity* becsListenerCallDataGetEntitiesArray(EntityManager.ListenerCallData* data)
{
return cast(Entity*)data.block.dataBegin();
}
void becsCallEntitiesFunction(ushort system_id, void function(EntityManager.SystemCallData*) callback, void* context)
{
System* system = gEntityManager.getSystem(system_id);
if (!system.m_any_system_caller)
return;
foreach (info; system.m_any_system_caller.infos)
{
EntityManager.CallData data = EntityManager.CallData(system.id, system, info, context, cast(void*)callback);
data.update();
}
}
void becsAddComponents(const EntityID entity_id, ComponentRef[] comps)
{
gEntityManager.addComponents(entity_id, comps);
}
void becsRemoveComponents(const EntityID entity_id, ushort[] comps)
{
gEntityManager.removeComponents(entity_id, comps);
}
void becsSendEvent(EntityID id, EventRef event)
{
gEntityManager.sendEvent(id, event);
}