-added export attributes (required by windows to make DLL library, not work at now due to DMD bugs) -interface D files to import
1013 lines
32 KiB
D
1013 lines
32 KiB
D
module ecs.manager;
|
|
|
|
import std.algorithm : max;
|
|
import std.conv : to;
|
|
import std.experimental.allocator;
|
|
import std.experimental.allocator.mallocator : AlignedMallocator, Mallocator;
|
|
import std.traits;
|
|
|
|
import core.stdc.stdlib;
|
|
import core.stdc.string;
|
|
|
|
import ecs.entity;
|
|
import ecs.entity_allocator;
|
|
import ecs.hash_map;
|
|
import ecs.id_manager;
|
|
import ecs.system;
|
|
import ecs.vector;
|
|
|
|
alias gEM = EntityManager.instance;
|
|
alias SerializeVector = ecs.vector.Vector!ubyte;
|
|
|
|
class EntityManager
|
|
{
|
|
|
|
export static void initialize()
|
|
{
|
|
instance = Mallocator.instance.make!EntityManager;
|
|
}
|
|
|
|
export static void destroy()
|
|
{
|
|
|
|
foreach (ref system; instance.systems)
|
|
{
|
|
system.disable();
|
|
}
|
|
|
|
foreach (ref system; instance.systems)
|
|
{
|
|
if (system.m_destroy)
|
|
system.m_destroy(system.m_system_pointer);
|
|
}
|
|
|
|
Mallocator.instance.dispose(instance);
|
|
instance = null;
|
|
}
|
|
|
|
void registerSystem(Sys)(int priority)
|
|
{
|
|
alias types = Parameters!(Sys.update);
|
|
alias storages = ParameterStorageClassTuple!(Sys.update);
|
|
|
|
alias STC = ParameterStorageClass;
|
|
|
|
System system;
|
|
|
|
static string genCall()()
|
|
{
|
|
string ret = "s.update(*cast(Entity*)data_pointer,";
|
|
/*foreach (i; 1 .. (Parameters!(Sys.update)).length)
|
|
{
|
|
ret ~= "*cast(types[" ~ i.to!string ~ "]*)(pointers[" ~ (i - 1)
|
|
.to!string ~ "]),";
|
|
}*/
|
|
uint i = 0;
|
|
uint req = 0;
|
|
uint opt = 0;
|
|
static foreach (param; (Parameters!(Sys.update))[1 .. $])
|
|
{
|
|
i++;
|
|
if (isPointer!param)
|
|
{
|
|
ret ~= "cast(types[" ~ i.to!string ~ "])(optional_pointers[" ~ (opt++)
|
|
.to!string ~ "]),";
|
|
}
|
|
else
|
|
{
|
|
ret ~= "*cast(types[" ~ i.to!string ~ "]*)(pointers[" ~ (req++)
|
|
.to!string ~ "]),";
|
|
}
|
|
}
|
|
ret ~= ");";
|
|
return ret;
|
|
}
|
|
|
|
static string genCompList()()
|
|
{
|
|
string ret = "ushort comp;uint req;uint opt;";
|
|
foreach (i; 1 .. (Parameters!(Sys.update)).length)
|
|
{
|
|
ret ~= "
|
|
static if(isPointer!(types[" ~ i.to!string
|
|
~ "]))opt++;
|
|
else static if(storages[" ~ i.to!string ~ "] == STC.ref_)req++;\n
|
|
else static assert(0,\"Can't register system \\\"" ~ Sys.stringof
|
|
~ "\\\". Unsupported parameter type \\\"\"~types["
|
|
~ i.to!string ~ "].stringof~\"\\\".\");";
|
|
}
|
|
ret ~= "system.m_components = Mallocator.instance.makeArray!ushort(req);";
|
|
ret ~= "system.m_optional_components = Mallocator.instance.makeArray!ushort(opt);";
|
|
ret ~= "opt = 0;req = 0;";
|
|
foreach (i; 1 .. (Parameters!(Sys.update)).length)
|
|
{
|
|
ret ~= "
|
|
static if(isPointer!(types[" ~ i.to!string
|
|
~ "]))
|
|
{
|
|
comp = components_map.get(PointerTarget!(types[" ~ i.to!string ~ "]).stringof, ushort.max);\n
|
|
if(comp == ushort.max)assert(0,\"Can't register system \\\"" ~ Sys.stringof
|
|
~ "\\\" due to non existing component \\\"\"~types[" ~ i.to!string ~ "].stringof~\"\\\".\");
|
|
system.m_optional_components[opt++] = comp;
|
|
}
|
|
else static if(storages["
|
|
~ i.to!string ~ "] == STC.ref_)
|
|
{
|
|
comp = components_map.get(types[" ~ i.to!string ~ "].stringof, ushort.max);\n
|
|
if(comp == ushort.max)assert(0,\"Can't register system \\\""
|
|
~ Sys.stringof ~ "\\\" due to non existing component \\\"\"~types["
|
|
~ i.to!string ~ "].stringof~\"\\\".\");
|
|
system.m_components[req++] = comp;
|
|
}";
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
static string catchFunc()(string member, string func)
|
|
{
|
|
string ret = "static if (hasMember!(Sys, \"" ~ func ~ "\"))
|
|
{
|
|
static void call" ~ func
|
|
~ "(void* system_pointer)
|
|
{
|
|
|
|
Sys* s = cast(Sys*) system_pointer;
|
|
s." ~ func ~ "();
|
|
}
|
|
|
|
system."
|
|
~ member ~ " = &call" ~ func ~ ";
|
|
}";
|
|
return ret;
|
|
}
|
|
|
|
static if (hasMember!(Sys, "update"))
|
|
{
|
|
static void callUpdate(ref CallData data, void* entity)
|
|
{
|
|
static if (hasMember!(Sys, "update"))
|
|
{
|
|
Sys* s = cast(Sys*) data.system.m_system_pointer;
|
|
|
|
void*[] pointers = (cast(void**) alloca(data.system.m_components.length * (void*)
|
|
.sizeof))[0 .. data.system.m_components.length];
|
|
void*[] optional_pointers = (cast(void**) alloca(
|
|
data.system.m_optional_components.length * (void*).sizeof))[0
|
|
.. data.system.m_optional_components.length];
|
|
|
|
EntitiesBlock* block = data.info.first_block;
|
|
while (block !is null)
|
|
{
|
|
uint size = block.type_data.size;
|
|
void* data_pointer = block.dataBegin();
|
|
foreach (i, ref pointer; pointers)
|
|
{
|
|
pointer = data_pointer + data.deltas[i];
|
|
}
|
|
foreach (i, ref pointer; optional_pointers)
|
|
{
|
|
uint ind = cast(uint)(i + pointers.length);
|
|
if (data.deltas[ind] != uint.max)
|
|
pointer = data_pointer + data.deltas[ind];
|
|
else
|
|
pointer = null;
|
|
}
|
|
foreach (i; 0 .. block.entities_count)
|
|
{
|
|
mixin(genCall());
|
|
data_pointer += size; //data.info.size;
|
|
foreach (ref pointer; pointers)
|
|
pointer += size;
|
|
foreach (ref pointer; optional_pointers)
|
|
if (pointer != null)
|
|
pointer += size;
|
|
}
|
|
block = block.next_block;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
system.m_update = &callUpdate;
|
|
}
|
|
|
|
mixin(catchFunc("m_enable", "onEnable"));
|
|
mixin(catchFunc("m_disable", "onDisable"));
|
|
mixin(catchFunc("m_create", "onCreate"));
|
|
mixin(catchFunc("m_destroy", "onDestroy"));
|
|
mixin(catchFunc("m_begin", "onBegin"));
|
|
mixin(catchFunc("m_end", "onEnd"));
|
|
|
|
system.m_system_pointer = cast(void*) Mallocator.instance.make!Sys;
|
|
system.m_priority = priority;
|
|
|
|
//system.m_components = Mallocator.instance.makeArray!uint(types.length - 1);
|
|
mixin(genCompList());
|
|
|
|
ushort sys_id = systems_map.get(Sys.stringof, ushort.max);
|
|
if(sys_id < systems.length)
|
|
{
|
|
system.enable();
|
|
systems[sys_id] = system;
|
|
}
|
|
else
|
|
{
|
|
systems_map.add(Sys.stringof,cast(ushort)systems.length);
|
|
|
|
systems.add(system);
|
|
|
|
if (system.m_create)
|
|
system.m_create(system.m_system_pointer);
|
|
|
|
systems[$ - 1].enable();
|
|
|
|
foreach (info; &entities_infos.byValue)
|
|
{
|
|
addEntityCaller(*info, cast(uint) systems.length - 1);
|
|
}
|
|
}
|
|
|
|
updateEntityCallers();
|
|
}
|
|
|
|
void registerComponent(Comp)()
|
|
{
|
|
|
|
ComponentInfo info;
|
|
|
|
static if (!(hasMember!(Comp, "component_id")) || !is(typeof(Comp.component_id) == ushort))
|
|
{
|
|
static assert(0, "Component should have \"__gshared ushort component_id");
|
|
}
|
|
|
|
static if (hasMember!(Comp, "onDestroy") && isFunction!(Comp.onDestroy)
|
|
&& is(ReturnType!(Comp.onDestroy) == void)
|
|
&& Parameters!(Comp.onDestroy).length == 0)
|
|
{
|
|
static void callDestroy(void* pointer)
|
|
{
|
|
(cast(Comp*) pointer).onDestroy();
|
|
}
|
|
|
|
info.destroy_callback = &callDestroy;
|
|
}
|
|
|
|
info.size = Comp.sizeof;
|
|
info.aligment = Comp.alignof; //8;
|
|
info.init_data = Mallocator.instance.makeArray!ubyte(Comp.sizeof);
|
|
*cast(Comp*) info.init_data.ptr = Comp.init; // = Comp();
|
|
|
|
ushort comp_id = components_map.get(Comp.stringof, ushort.max);
|
|
if(comp_id < components.length)
|
|
{
|
|
Comp.component_id = comp_id;
|
|
}
|
|
else
|
|
{
|
|
components.add(info);
|
|
Comp.component_id = cast(ushort)(components.length - 1);
|
|
components_map.add(Comp.stringof, cast(ushort)(components.length - 1));
|
|
}
|
|
}
|
|
|
|
export void update()
|
|
{
|
|
foreach (info; &entities_infos.byValue)
|
|
{
|
|
foreach (data; info.callers)
|
|
{
|
|
if (data.system.enabled)
|
|
(cast(SytemFuncType) data.system.m_update)(data, null); //caller(call_data,null);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void alignNum(ref ushort num, ushort aligment)
|
|
{
|
|
num = cast(ushort)((num + aligment - 1) & (-cast(int) aligment)); //num += aligment - (num & (aligment - 1));
|
|
}
|
|
|
|
extern (C) static int compareUShorts(const void* a, const void* b)
|
|
{
|
|
ushort _a = *cast(ushort*) a;
|
|
ushort _b = *cast(ushort*) b;
|
|
if (_a < _b)
|
|
return -1;
|
|
else if (_a == _b)
|
|
return 0;
|
|
else
|
|
return 1;
|
|
}
|
|
|
|
export EntityTemplate* allocateTemplate(ushort[] components_ids)
|
|
{
|
|
|
|
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * components_ids.length))[0
|
|
.. components_ids.length];
|
|
ids[0 .. $] = components_ids[];
|
|
qsort(ids.ptr, ids.length, ushort.sizeof, &compareUShorts);
|
|
{
|
|
uint j = 1;
|
|
foreach (i; 1 .. ids.length)
|
|
{
|
|
if (ids[i] != ids[j - 1])
|
|
{
|
|
ids[j] = ids[i];
|
|
j++;
|
|
}
|
|
else
|
|
debug assert(0, "Duplicated components in template!!!");
|
|
}
|
|
ids = ids[0 .. j];
|
|
}
|
|
|
|
EntityInfo* info = getEntityInfo(ids);
|
|
|
|
EntityTemplate* temp = Mallocator.instance.make!EntityTemplate;
|
|
temp.entity_data = Mallocator.instance.makeArray!ubyte(info.size);
|
|
temp.info = info;
|
|
|
|
//fill components with default data
|
|
foreach (comp; info.components)
|
|
{
|
|
temp.entity_data[info.deltas[comp] .. info.deltas[comp] + components[comp].size]
|
|
= components[comp].init_data;
|
|
}
|
|
|
|
return temp;
|
|
}
|
|
|
|
export EntityInfo* getEntityInfo(ushort[] ids)
|
|
{
|
|
EntityInfo* info = entities_infos.get(ids, null);
|
|
if (info is null)
|
|
{
|
|
info = Mallocator.instance.make!EntityInfo;
|
|
|
|
info.components = Mallocator.instance.makeArray(ids);
|
|
info.deltas = Mallocator.instance.makeArray!ushort(ids[$ - 1] + 1);
|
|
|
|
info.size = EntityID.sizeof;
|
|
info.alignment = EntityID.alignof;
|
|
|
|
foreach (i, id; ids)
|
|
{
|
|
info.alignment = max(info.alignment, components[id].aligment);
|
|
alignNum(info.size, components[id].aligment);
|
|
info.deltas[id] = info.size;
|
|
info.size += components[id].size;
|
|
}
|
|
alignNum(info.size, info.alignment);
|
|
|
|
foreach (uint i, ref system; systems)
|
|
{
|
|
if (system.m_update is null)
|
|
continue;
|
|
addEntityCaller(*info, i);
|
|
}
|
|
|
|
updateEntityCallers();
|
|
|
|
entities_infos.add(info.components, info);
|
|
}
|
|
return info;
|
|
}
|
|
|
|
export void updateEntityCallers()
|
|
{
|
|
foreach (entity; &entities_infos.byValue)
|
|
{
|
|
foreach (ref caller; entity.callers)
|
|
{
|
|
caller.system = &systems[caller.system_id];
|
|
}
|
|
}
|
|
}
|
|
|
|
export void addEntityCaller(ref EntityInfo entity, uint system_id)
|
|
{
|
|
System* system = &systems[system_id];
|
|
CallData call_data = CallData(system_id, system, &entity, null);
|
|
uint num = cast(uint)(system.m_components.length + system.m_optional_components.length);
|
|
ushort[] deltas = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
|
|
uint delta_id = 0;
|
|
|
|
ushort[] cmp_array = system.m_components;
|
|
add_deltas: foreach (id; cmp_array)
|
|
{
|
|
deltas[delta_id] = ushort.max;
|
|
foreach (i2, id2; entity.components)
|
|
{
|
|
if (id2 == id)
|
|
{
|
|
deltas[delta_id] = entity.deltas[id2];
|
|
break;
|
|
}
|
|
}
|
|
if (deltas[delta_id] == ushort.max)
|
|
{
|
|
deltas = null;
|
|
break;
|
|
}
|
|
delta_id++;
|
|
}
|
|
if (deltas is null)
|
|
return;
|
|
if (cmp_array.ptr == system.m_components.ptr)
|
|
{
|
|
cmp_array = system.m_optional_components;
|
|
goto add_deltas;
|
|
}
|
|
|
|
call_data.deltas = Mallocator.instance.makeArray(deltas); //Mallocator.instance.makeArray!ushort(system.m_components.length);
|
|
|
|
uint index = 0;
|
|
for (; index < entity.callers.length; index++)
|
|
{
|
|
CallData* caller = &entity.callers[index];
|
|
if (caller.system.priority >= call_data.system.priority)
|
|
break;
|
|
}
|
|
entity.callers.add(call_data, index);
|
|
}
|
|
|
|
export Entity* getEntity(EntityID id)
|
|
{
|
|
return cast(Entity*) id_manager.getEntityPointer(id);
|
|
}
|
|
|
|
export void removeComponents(EntityID entity_id, ushort[] del_ids)
|
|
{
|
|
uint num = cast(uint) del_ids.length;
|
|
change_entities_list.add(0);
|
|
change_entities_list.add((cast(ubyte*)&entity_id)[0 .. 8]);
|
|
change_entities_list.add((cast(ubyte*)&num)[0 .. 4]);
|
|
change_entities_list.add(cast(ubyte[]) del_ids);
|
|
}
|
|
|
|
private void __removeComponents(EntityID entity_id, ushort[] del_ids)
|
|
{
|
|
Entity* entity = id_manager.getEntityPointer(entity_id);
|
|
EntitiesBlock* block = getMetaData(entity);
|
|
EntityInfo* info = block.type_data;
|
|
|
|
qsort(del_ids.ptr, del_ids.length, ushort.sizeof, &compareUShorts);
|
|
|
|
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length)))[0
|
|
.. info.components.length];
|
|
|
|
uint j = 0;
|
|
uint k = 0;
|
|
foreach (id; info.components)
|
|
{
|
|
while (k < del_ids.length && del_ids[k] < id)
|
|
k++;
|
|
if (k >= del_ids.length)
|
|
{
|
|
ids[j++] = id;
|
|
}
|
|
else if (del_ids[k] == info.components[j])
|
|
{
|
|
k++;
|
|
}
|
|
else
|
|
ids[j++] = id;
|
|
}
|
|
|
|
if (j == info.components.length)
|
|
return;
|
|
|
|
EntityInfo* new_info = getEntityInfo(ids[0 .. j]);
|
|
|
|
EntitiesBlock* new_block = findBlockWithFreeSpace(new_info);
|
|
|
|
void* start = new_block.dataBegin() + new_block.entities_count * new_info.size;
|
|
|
|
Entity* new_entity = cast(Entity*) start;
|
|
new_entity.id = entity.id;
|
|
new_entity.updateID();
|
|
|
|
foreach (comp; new_info.components)
|
|
{
|
|
memcpy(cast(void*) new_entity + new_info.deltas[comp],
|
|
cast(void*) entity + info.deltas[comp], components[comp].size);
|
|
}
|
|
|
|
removeEntityNoID(entity, block);
|
|
}
|
|
|
|
void removeComponents(Components...)(EntityID entity_id)
|
|
{
|
|
const uint num = Components.length;
|
|
ushort[num] del_ids;
|
|
static foreach (i, comp; Components)
|
|
{
|
|
del_ids[i] = comp.component_id;
|
|
}
|
|
|
|
/*change_entities_list.add(0);
|
|
change_entities_list.add((cast(ubyte*)&entity_id)[0..8]);
|
|
change_entities_list.add((cast(ubyte*)&num)[0..4]);
|
|
change_entities_list.add(cast(ubyte[])del_ids);*/
|
|
removeComponents(entity_id, del_ids);
|
|
}
|
|
|
|
private void __addComponents(EntityID entity_id, ushort[] new_ids, void*[] data_pointers)
|
|
{
|
|
uint num = cast(uint) new_ids.length;
|
|
Entity* entity = id_manager.getEntityPointer(entity_id);
|
|
EntitiesBlock* block = getMetaData(entity);
|
|
EntityInfo* info = block.type_data;
|
|
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length + num)))[0
|
|
.. info.components.length + num];
|
|
/*ushort[num] new_ids;
|
|
|
|
static foreach (i, comp; Components)
|
|
{
|
|
new_ids[i] = comp.component_id;
|
|
}*/
|
|
|
|
/*void*[num] pointers;
|
|
|
|
static foreach (i, comp; comps)
|
|
{
|
|
pointers[i] = ∁
|
|
}*/
|
|
|
|
foreach (int i; 0 .. num)
|
|
{
|
|
ushort min = new_ids[i];
|
|
int pos = i;
|
|
foreach (int j; i .. num)
|
|
{
|
|
if (new_ids[j] < min)
|
|
{
|
|
min = new_ids[j];
|
|
pos = j;
|
|
}
|
|
}
|
|
if (pos != i)
|
|
{
|
|
ushort id = new_ids[i];
|
|
new_ids[i] = new_ids[pos];
|
|
new_ids[pos] = id;
|
|
void* ptr = data_pointers[i];
|
|
data_pointers[i] = data_pointers[pos];
|
|
data_pointers[pos] = ptr;
|
|
}
|
|
}
|
|
|
|
uint j = 0;
|
|
uint k = 0;
|
|
foreach (ref id; ids)
|
|
{
|
|
if (k >= new_ids.length)
|
|
{
|
|
id = info.components[j++];
|
|
continue;
|
|
}
|
|
if (j >= info.components.length)
|
|
{
|
|
id = new_ids[k++];
|
|
continue;
|
|
}
|
|
debug if (new_ids[k] == info.components[j])
|
|
assert(0, "Trying to add already existing component!");
|
|
if (new_ids[k] < info.components[j])
|
|
{
|
|
id = new_ids[k++];
|
|
}
|
|
else
|
|
id = info.components[j++];
|
|
}
|
|
|
|
EntityInfo* new_info = getEntityInfo(ids);
|
|
|
|
EntitiesBlock* new_block = findBlockWithFreeSpace(new_info);
|
|
|
|
void* start = new_block.dataBegin() + new_block.entities_count * new_info.size;
|
|
|
|
Entity* new_entity = cast(Entity*) start;
|
|
new_entity.id = entity.id;
|
|
new_entity.updateID();
|
|
new_block.entities_count++;
|
|
|
|
//removeEntityNoID(entity, block);
|
|
|
|
j = 0;
|
|
k = 0;
|
|
foreach (ref id; ids)
|
|
{
|
|
if (k >= new_ids.length)
|
|
{
|
|
memcpy(cast(void*) new_entity + new_info.deltas[id],
|
|
cast(void*) entity + info.deltas[info.components[j]],
|
|
components[info.components[j]].size); //id = info.components[j++];
|
|
j++;
|
|
}
|
|
else if (j >= info.components.length)
|
|
{
|
|
memcpy(cast(void*) new_entity + new_info.deltas[id],
|
|
data_pointers[k], components[new_ids[k]].size); //id = new_ids[k++];
|
|
k++;
|
|
}
|
|
else if (id == new_ids[k])
|
|
{
|
|
memcpy(cast(void*) new_entity + new_info.deltas[id],
|
|
data_pointers[k], components[new_ids[k]].size); //id = new_ids[k++];
|
|
k++;
|
|
}
|
|
else
|
|
{
|
|
memcpy(cast(void*) new_entity + new_info.deltas[id],
|
|
cast(void*) entity + info.deltas[info.components[j]],
|
|
components[info.components[j]].size); //id = info.components[j++];
|
|
j++;
|
|
}
|
|
}
|
|
|
|
removeEntityNoID(entity, block);
|
|
}
|
|
|
|
void addComponents(Components...)(EntityID entity_id, Components comps)
|
|
{
|
|
const uint num = Components.length;
|
|
Entity* entity = id_manager.getEntityPointer(entity_id);
|
|
EntitiesBlock* block = getMetaData(entity);
|
|
EntityInfo* info = block.type_data;
|
|
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length + num)))[0
|
|
.. info.components.length + num];
|
|
ushort[num] new_ids;
|
|
|
|
static foreach (i, comp; Components)
|
|
{
|
|
new_ids[i] = comp.component_id;
|
|
}
|
|
|
|
/*void*[num] pointers;
|
|
|
|
static foreach (i, comp; comps)
|
|
{
|
|
pointers[i] = ∁
|
|
}*/
|
|
|
|
change_entities_list.add(1);
|
|
change_entities_list.add((cast(ubyte*)&entity_id)[0 .. 8]);
|
|
change_entities_list.add((cast(ubyte*)&num)[0 .. 4]);
|
|
change_entities_list.add(cast(ubyte[]) new_ids);
|
|
static foreach (i, comp; comps)
|
|
{
|
|
change_entities_list.add((cast(ubyte*)&comp)[0 .. comp.sizeof]);
|
|
}
|
|
|
|
//__addComponents(entity_id, new_ids, pointers);
|
|
}
|
|
|
|
export void freeTemplate(EntityTemplate* template_)
|
|
{
|
|
Mallocator.instance.dispose(template_.entity_data);
|
|
Mallocator.instance.dispose(template_);
|
|
}
|
|
|
|
export ref Entity addEntity(EntityTemplate* tmpl)
|
|
{
|
|
EntitiesBlock* block = findBlockWithFreeSpace(tmpl.info);
|
|
|
|
void* start = block.dataBegin() + (block.entities_count + block.added_count)
|
|
* tmpl.info.size;
|
|
memcpy(start, tmpl.entity_data.ptr, tmpl.info.size);
|
|
|
|
if (!block.added_count)
|
|
blocks_to_update.add(block);
|
|
|
|
Entity* entity = cast(Entity*) start;
|
|
entity.id = id_manager.getNewID();
|
|
entity.updateID();
|
|
block.added_count++;
|
|
//block.entities_count++;
|
|
return *entity;
|
|
}
|
|
|
|
private EntitiesBlock* findBlockWithFreeSpace(EntityInfo* info)
|
|
{
|
|
EntitiesBlock* previous_block;
|
|
EntitiesBlock* block = info.first_with_free_space;
|
|
|
|
while (1)
|
|
{
|
|
if (block is null)
|
|
{
|
|
block = cast(EntitiesBlock*) allocator.getBlock(); //AlignedMallocator.instance.alignedAllocate(page_size, page_size);
|
|
*block = EntitiesBlock(info);
|
|
if (previous_block is null)
|
|
{
|
|
info.first_block = block;
|
|
block.id = 0;
|
|
}
|
|
else
|
|
{
|
|
previous_block.next_block = block;
|
|
block.id = previous_block.id + 1;
|
|
}
|
|
info.first_with_free_space = block;
|
|
break; // new block certainly has free space
|
|
}
|
|
// check if there is enought space
|
|
if (block.dataDelta() + (
|
|
block.entities_count + block.added_count + 1) * info.size > page_size)
|
|
{
|
|
previous_block = block;
|
|
block = block.next_block;
|
|
continue;
|
|
}
|
|
|
|
info.first_with_free_space = block;
|
|
break; // block exists and bounds check passed
|
|
}
|
|
|
|
return block;
|
|
}
|
|
|
|
export void removeEntity(EntityID id)
|
|
{
|
|
entities_to_remove.add(id);
|
|
}
|
|
|
|
private void __removeEntity(EntityID id)
|
|
{
|
|
//get entity and block meta data pointers
|
|
Entity* entity = id_manager.getEntityPointer(id);
|
|
if (entity is null)
|
|
return; //return if entity doesn't exist
|
|
EntitiesBlock* block = getMetaData(entity);
|
|
id_manager.releaseID(id); //release id from manager
|
|
|
|
removeEntityNoID(entity, block, true);
|
|
}
|
|
|
|
private void removeEntityNoID(Entity* entity, EntitiesBlock* block,
|
|
bool call_destructors = false)
|
|
{
|
|
//pos is Entity number in block
|
|
void* data_begin = block.dataBegin();
|
|
uint pos = cast(int)(cast(void*) entity - data_begin) / block.type_data.size;
|
|
|
|
block.entities_count--;
|
|
|
|
//set "first_with_free_space" if should it be
|
|
if (block.type_data.first_with_free_space.id > block.id)
|
|
block.type_data.first_with_free_space = block;
|
|
|
|
if (call_destructors)
|
|
{
|
|
void* data = data_begin + pos * block.type_data.size;
|
|
foreach (comp; block.type_data.components)
|
|
{
|
|
if (components[comp].destroy_callback)
|
|
{
|
|
components[comp].destroy_callback(data + block.type_data.deltas[comp]);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (pos == block.entities_count)
|
|
return;
|
|
|
|
//copy memory of last entity to position of removed entity
|
|
void* src = data_begin + block.entities_count * block.type_data.size;
|
|
void* dst = data_begin + pos * block.type_data.size;
|
|
|
|
memcpy(dst, src, block.type_data.size);
|
|
|
|
//update pointer for moved entity ID
|
|
entity = cast(Entity*) dst;
|
|
entity.updateID();
|
|
}
|
|
|
|
/************************************************************************************************************************
|
|
*functions return MetaData of page.
|
|
*
|
|
*params:
|
|
*pointer = pointer to any data of entity (i.e. component data pointer)
|
|
*/
|
|
export EntitiesBlock* getMetaData(void* pointer)
|
|
{
|
|
return cast(EntitiesBlock*)(cast(size_t) pointer & (~cast(size_t)(page_size - 1)));
|
|
}
|
|
|
|
private void changeEntites()
|
|
{
|
|
uint index = 0;
|
|
uint len = cast(uint) change_entities_list.length;
|
|
while (index < len)
|
|
{
|
|
if (!change_entities_list[index++])
|
|
{
|
|
EntityID id = *cast(EntityID*)&change_entities_list[index];
|
|
index += EntityID.sizeof;
|
|
uint num = *cast(uint*)&change_entities_list[index];
|
|
index += uint.sizeof;
|
|
ushort[] ids = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
|
|
ids[0 .. $] = (cast(ushort*)&change_entities_list[index])[0 .. num];
|
|
index += ushort.sizeof * num;
|
|
__removeComponents(id, ids);
|
|
}
|
|
else
|
|
{
|
|
EntityID id = *cast(EntityID*)&change_entities_list[index];
|
|
index += EntityID.sizeof;
|
|
uint num = *cast(uint*)&change_entities_list[index];
|
|
index += uint.sizeof;
|
|
ushort[] ids = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
|
|
ids[0 .. $] = (cast(ushort*)&change_entities_list[index])[0 .. num];
|
|
index += ushort.sizeof * num;
|
|
void*[] pointers = (cast(void**) alloca(num * (void*).sizeof))[0 .. num];
|
|
foreach (i; 0 .. num)
|
|
{
|
|
pointers[i] = &change_entities_list[index];
|
|
index += components[ids[i]].size;
|
|
}
|
|
__addComponents(id, ids, pointers);
|
|
}
|
|
}
|
|
change_entities_list.clear();
|
|
}
|
|
|
|
private void updateBlocks()
|
|
{
|
|
foreach (block; blocks_to_update)
|
|
{
|
|
block.entities_count += block.added_count;
|
|
block.added_count = 0;
|
|
}
|
|
blocks_to_update.clear();
|
|
}
|
|
|
|
private void removeEntities()
|
|
{
|
|
foreach (id; entities_to_remove)
|
|
{
|
|
__removeEntity(id);
|
|
}
|
|
entities_to_remove.clear();
|
|
}
|
|
|
|
export void begin()
|
|
{
|
|
updateBlocks();
|
|
changeEntites();
|
|
removeEntities();
|
|
foreach (ref system; instance.systems)
|
|
{
|
|
if (system.m_begin)
|
|
system.m_begin(system.m_system_pointer);
|
|
}
|
|
}
|
|
|
|
export void end()
|
|
{
|
|
foreach (ref system; instance.systems)
|
|
{
|
|
if (system.m_end)
|
|
system.m_end(system.m_system_pointer);
|
|
}
|
|
updateBlocks();
|
|
changeEntites();
|
|
removeEntities();
|
|
}
|
|
|
|
struct ComponentInfo
|
|
{
|
|
ushort size;
|
|
ushort aligment;
|
|
ubyte[] init_data;
|
|
void function(void* pointer) destroy_callback;
|
|
}
|
|
|
|
/************************************************************************************************************************
|
|
*Entity type info.
|
|
*/
|
|
struct EntityInfo
|
|
{
|
|
///entity components
|
|
ushort[] components;
|
|
///deltas in memory for components
|
|
ushort[] deltas;
|
|
///aligment of whole entity
|
|
ushort alignment;
|
|
///size of entity (with alignment respect)
|
|
ushort size;
|
|
///pointer to first block/page
|
|
EntitiesBlock* first_block;
|
|
///a hint for allocations
|
|
EntitiesBlock* first_with_free_space; // a hint for allocations, should have empty space in it but doesn't have to
|
|
///array of CallData. Contain data for System calls.
|
|
Vector!(CallData) callers;
|
|
}
|
|
|
|
/************************************************************************************************************************
|
|
*Meta data of every block of entities (contained at the begining of block).
|
|
*/
|
|
struct EntitiesBlock
|
|
{
|
|
///return distance (in bytes) from begin of block to data
|
|
uint dataDelta()
|
|
{
|
|
ushort dif = EntitiesBlock.sizeof;
|
|
alignNum(dif, type_data.alignment);
|
|
return dif;
|
|
}
|
|
|
|
///return pointer to first element in block
|
|
export void* dataBegin()
|
|
{
|
|
ushort dif = EntitiesBlock.sizeof;
|
|
alignNum(dif, type_data.alignment);
|
|
return cast(void*)&this + dif;
|
|
}
|
|
|
|
///pointer to Entity type info
|
|
EntityInfo* type_data;
|
|
///number of entities in block
|
|
ushort entities_count;
|
|
///number of new entities in block
|
|
ushort added_count;
|
|
///block id
|
|
uint id;
|
|
///pointer to next block/page
|
|
EntitiesBlock* next_block;
|
|
//there is a loooot of data (4kB, pure magic)
|
|
}
|
|
|
|
/************************************************************************************************************************
|
|
*Structure with data used to calling System calls.
|
|
*/
|
|
struct CallData
|
|
{
|
|
///system ID. Used to update system pointer after system reload.
|
|
uint system_id;
|
|
///pointer to used system
|
|
System* system;
|
|
///poiner to Entity type info
|
|
EntityManager.EntityInfo* info;
|
|
///deltas for components
|
|
ushort[] deltas;
|
|
}
|
|
|
|
alias SytemFuncType = void function(ref EntityManager.CallData data, void* entity);
|
|
|
|
///Single page size. Must be power of two.
|
|
enum page_size = 4096;
|
|
///Number of pages in block.
|
|
enum pages_in_block = 128;
|
|
|
|
IDManager id_manager;
|
|
EntityAllocator allocator;
|
|
|
|
Vector!EntityID entities_to_remove;
|
|
Vector!(EntitiesBlock*) blocks_to_update;
|
|
Vector!ubyte change_entities_list;
|
|
|
|
HashMap!(ushort[], EntityInfo*) entities_infos;
|
|
HashMap!(string, ushort) systems_map;
|
|
HashMap!(string, ushort) components_map;
|
|
Vector!System systems;
|
|
Vector!ComponentInfo components;
|
|
__gshared EntityManager instance;
|
|
|
|
}
|
|
|
|
version(Windows)
|
|
extern(Windows) bool DllMain(void* hInstance, uint ulReason, void*) {
|
|
import core.sys.windows.windows;
|
|
import core.sys.windows.dll;
|
|
switch (ulReason)
|
|
{
|
|
default: assert(0);
|
|
case DLL_PROCESS_ATTACH:
|
|
dll_process_attach( hInstance, true );
|
|
break;
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
dll_process_detach( hInstance, true );
|
|
break;
|
|
|
|
case DLL_THREAD_ATTACH:
|
|
dll_thread_attach( true, true );
|
|
break;
|
|
|
|
case DLL_THREAD_DETACH:
|
|
dll_thread_detach( true, true );
|
|
break;
|
|
}
|
|
return true;
|
|
}
|
|
/*
|
|
static ulong defaultHashFunc(T)(auto ref T t)
|
|
{
|
|
ulong ret = 0;
|
|
foreach(id;t)
|
|
{
|
|
ret = ret
|
|
}
|
|
}*/
|