Added CAPI files
I forgot to add CAPI files to .gitignore with previous commits :/
This commit is contained in:
parent
6e45e7b053
commit
0002852303
4 changed files with 1569 additions and 1 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
|
@ -12,4 +12,8 @@
|
||||||
!compile_wasm.py
|
!compile_wasm.py
|
||||||
!compile_android.py
|
!compile_android.py
|
||||||
!.gitlab-ci.yml
|
!.gitlab-ci.yml
|
||||||
!LICENSE
|
!LICENSE
|
||||||
|
!c-api
|
||||||
|
!c-api/*.h
|
||||||
|
!c-api/*.d
|
||||||
|
!c-api/*.c
|
||||||
207
c-api/becs.h
Normal file
207
c-api/becs.h
Normal file
|
|
@ -0,0 +1,207 @@
|
||||||
|
#ifndef __BECS__
|
||||||
|
#define __BECS__
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdalign.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct BECSSystem
|
||||||
|
{
|
||||||
|
|
||||||
|
}BECSSystem;
|
||||||
|
|
||||||
|
typedef struct BECSArray
|
||||||
|
{
|
||||||
|
size_t size;
|
||||||
|
void* ptr;
|
||||||
|
}BECSArray;
|
||||||
|
|
||||||
|
typedef struct EntityID
|
||||||
|
{
|
||||||
|
///Index to entity in IDManager.
|
||||||
|
uint32_t id;
|
||||||
|
///Counter required for reusing ID.
|
||||||
|
uint32_t counter;
|
||||||
|
}EntityID;
|
||||||
|
|
||||||
|
typedef struct Entity
|
||||||
|
{
|
||||||
|
///Entity ID.
|
||||||
|
EntityID id;
|
||||||
|
}Entity;
|
||||||
|
|
||||||
|
typedef struct ComponentRef
|
||||||
|
{
|
||||||
|
///pointer to component
|
||||||
|
void* ptr;
|
||||||
|
///component index
|
||||||
|
uint16_t id;
|
||||||
|
}ComponentRef;
|
||||||
|
|
||||||
|
typedef ComponentRef EventRef;
|
||||||
|
|
||||||
|
typedef struct BECSComponentInfo
|
||||||
|
{
|
||||||
|
///Component size
|
||||||
|
uint16_t size;
|
||||||
|
///Component data alignment
|
||||||
|
uint16_t alignment;
|
||||||
|
///Initialization data
|
||||||
|
BECSArray init_data;
|
||||||
|
///Pointer to component destroy callback
|
||||||
|
void* destroy_callback;
|
||||||
|
///Pointer to component create callback
|
||||||
|
void* create_callback;
|
||||||
|
//void* create_callback;
|
||||||
|
}BECSComponentInfo;
|
||||||
|
|
||||||
|
typedef struct BECSEventRegisterInfo
|
||||||
|
{
|
||||||
|
///Component size
|
||||||
|
uint16_t size;
|
||||||
|
///Component data alignment
|
||||||
|
uint16_t alignment;
|
||||||
|
}BECSEventRegisterInfo;
|
||||||
|
|
||||||
|
typedef struct EntityTemplate
|
||||||
|
{
|
||||||
|
/*///Entity components data
|
||||||
|
BECSArray entity_data;
|
||||||
|
///Pointer to entity type info.
|
||||||
|
void* info;*/
|
||||||
|
}EntityTemplate;
|
||||||
|
|
||||||
|
enum BECSSystemComponentAttribute
|
||||||
|
{
|
||||||
|
required = 0,
|
||||||
|
optional = 1,
|
||||||
|
excluded = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
enum BECSSystemComponentAccess
|
||||||
|
{
|
||||||
|
writable = 0,
|
||||||
|
readonly = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct BECSSystemComponentInfo
|
||||||
|
{
|
||||||
|
uint16_t id;
|
||||||
|
enum BECSSystemComponentAttribute attribute;
|
||||||
|
enum BECSSystemComponentAccess access;
|
||||||
|
}BECSSystemComponentInfo;
|
||||||
|
|
||||||
|
///C-API structure conatin only subset of real data which can be accessed directly. Component arrays can be retrived using function calls.
|
||||||
|
typedef struct BECSListenerCallData
|
||||||
|
{
|
||||||
|
uint32_t count;
|
||||||
|
void* context;
|
||||||
|
}BECSListenerCallData;
|
||||||
|
|
||||||
|
///C-API structure conatin only subset of real data which can be accessed directly. Component arrays can be retrived using function calls.
|
||||||
|
typedef struct BECSSystemCallData
|
||||||
|
{
|
||||||
|
uint32_t count;
|
||||||
|
uint32_t thread_id;
|
||||||
|
uint32_t job_id;
|
||||||
|
void* context;
|
||||||
|
}BECSSystemCallData;
|
||||||
|
|
||||||
|
///C-API structure conatin only subset of real data which can be accessed directly. Component arrays can be retrived using function calls.
|
||||||
|
typedef struct BECSEventCallData
|
||||||
|
{
|
||||||
|
Entity* entity;
|
||||||
|
void* event;
|
||||||
|
void* context;
|
||||||
|
}BECSEventCallData;
|
||||||
|
|
||||||
|
typedef struct BECSEventCallback
|
||||||
|
{
|
||||||
|
uint16_t id;
|
||||||
|
void (*callback)(BECSEventCallData* data);
|
||||||
|
}BECSEventCallback;
|
||||||
|
|
||||||
|
typedef struct BECSSystemRegisterInfo
|
||||||
|
{
|
||||||
|
uint32_t pass_id;
|
||||||
|
int32_t priority;
|
||||||
|
uint32_t max_jobs;
|
||||||
|
|
||||||
|
size_t components_count;
|
||||||
|
BECSSystemComponentInfo* components;
|
||||||
|
|
||||||
|
size_t event_handlers_count;
|
||||||
|
BECSEventCallback* event_handlers;
|
||||||
|
|
||||||
|
size_t system_size;
|
||||||
|
void* init_data;
|
||||||
|
|
||||||
|
void (*on_update)(BECSSystemCallData* call_data);
|
||||||
|
void (*on_create)(void* system_pointer);
|
||||||
|
void (*on_destroy)(void* system_pointer);
|
||||||
|
void (*on_enable)(void* system_pointer);
|
||||||
|
void (*on_disable)(void* system_pointer);
|
||||||
|
char (*on_begin)(void* system_pointer);
|
||||||
|
void (*on_end)(void* system_pointer);
|
||||||
|
void (*on_add_entity)(BECSListenerCallData*);
|
||||||
|
void (*on_remove_entity)(BECSListenerCallData*);
|
||||||
|
void (*on_change_entity)(BECSListenerCallData*);
|
||||||
|
void (*filter_entity)(void* system_pointer, void* info);
|
||||||
|
}BECSSystemRegisterInfo;
|
||||||
|
|
||||||
|
void becsInitialize();
|
||||||
|
void becsDestroy();
|
||||||
|
|
||||||
|
void becsBeginRegister();
|
||||||
|
void becsEndRegister();
|
||||||
|
|
||||||
|
void becsBegin();
|
||||||
|
void becsEnd();
|
||||||
|
void becsCommit();
|
||||||
|
|
||||||
|
void becsUpdate(uint16_t pass_id);
|
||||||
|
void becsUpdateMT(uint16_t pass_id);
|
||||||
|
|
||||||
|
uint16_t becsRegisterComponent(const char* name, BECSComponentInfo info);
|
||||||
|
uint16_t becsRegisterEvent(const char* name, BECSEventRegisterInfo info);
|
||||||
|
uint16_t becsRegisterSystem(const char* name, BECSSystemRegisterInfo info);
|
||||||
|
uint16_t becsRegisterPass(const char* name);
|
||||||
|
|
||||||
|
Entity* becsAddEntity(EntityTemplate* template);
|
||||||
|
Entity* becsAddEntityCopy(EntityID id);
|
||||||
|
void becsRemoveEntity(EntityID id);
|
||||||
|
|
||||||
|
Entity* becsGetEntity(EntityID id);
|
||||||
|
|
||||||
|
void becsAddComponents(const EntityID entity_id, size_t length, ComponentRef* comps);
|
||||||
|
void becsRemoveComponents(const EntityID entity_id, size_t length, uint16_t* comps);
|
||||||
|
|
||||||
|
EntityTemplate* becsAllocateTemplate(uint32_t count, uint16_t* components);
|
||||||
|
EntityTemplate* becsAllocateTemplateFromEntity(EntityID id, uint8_t fill_default);
|
||||||
|
EntityTemplate* becsAllocateTemplateCopy(EntityTemplate* tmpl);
|
||||||
|
EntityTemplate* becsAllocateTemplateFromTemplate(EntityTemplate* tmpl, size_t new_count, uint16_t* components, size_t remove_count, uint16_t* remove_components);
|
||||||
|
void becsFreeTemplate(EntityTemplate* tmpl);
|
||||||
|
|
||||||
|
void* becsEntityGetComponent(Entity* entity, uint16_t component_id);
|
||||||
|
|
||||||
|
void* becsSystemCallDataGetComponentArray(BECSSystemCallData* data, uint16_t component_id);
|
||||||
|
Entity* becsSystemCallDataGetEntitiesArray(BECSSystemCallData* data);
|
||||||
|
|
||||||
|
void* becsListenerCallDataGetComponentArray(BECSListenerCallData* data, uint16_t component_id);
|
||||||
|
Entity* becsListenerCallDataGetEntitiesArray(BECSListenerCallData* data);
|
||||||
|
|
||||||
|
void becsCallEntitiesFunction(uint16_t system_id, void (*on_update)(BECSSystemCallData* call_data), void* context);
|
||||||
|
|
||||||
|
void becsSendEvent(EntityID id, EventRef event);
|
||||||
|
|
||||||
|
#define BECS_REGISTER_COMPONENT(comp) \
|
||||||
|
({ \
|
||||||
|
void* mem_ptr = malloc(sizeof(comp)); \
|
||||||
|
memcpy(mem_ptr, &comp, sizeof(comp)); \
|
||||||
|
becsRegisterComponent(#comp, (BECSComponentInfo){sizeof(comp), alignof(comp), (BECSArray){sizeof(comp), mem_ptr}, 0, 0}); \
|
||||||
|
})
|
||||||
|
|
||||||
|
//BECSComponentInfo(sizeof(comp), 4, 0, 0, 0));
|
||||||
|
|
||||||
|
#endif //__BECS__
|
||||||
306
c-api/manager.d
Normal file
306
c-api/manager.d
Normal file
|
|
@ -0,0 +1,306 @@
|
||||||
|
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);
|
||||||
|
}
|
||||||
1051
c-api/test.c
Normal file
1051
c-api/test.c
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue