Format files with dfmt
This commit is contained in:
parent
2069e7b420
commit
86e4e57f74
4 changed files with 50 additions and 46 deletions
|
|
@ -30,9 +30,10 @@ class EntityManager
|
||||||
static string genCall()()
|
static string genCall()()
|
||||||
{
|
{
|
||||||
string ret = "s.update(";
|
string ret = "s.update(";
|
||||||
foreach(i,param;Parameters!(Sys.update))
|
foreach (i, param; Parameters!(Sys.update))
|
||||||
{
|
{
|
||||||
ret ~= "*cast(types["~i.to!string~"]*)(data_pointer + data.deltas["~i.to!string~"]),";
|
ret ~= "*cast(types[" ~ i.to!string
|
||||||
|
~ "]*)(data_pointer + data.deltas[" ~ i.to!string ~ "]),";
|
||||||
}
|
}
|
||||||
ret ~= ");";
|
ret ~= ");";
|
||||||
return ret;
|
return ret;
|
||||||
|
|
@ -41,9 +42,10 @@ class EntityManager
|
||||||
static string genCompList()()
|
static string genCompList()()
|
||||||
{
|
{
|
||||||
string ret;
|
string ret;
|
||||||
foreach(i,param;Parameters!(Sys.update))
|
foreach (i, param; Parameters!(Sys.update))
|
||||||
{
|
{
|
||||||
ret ~= "system.components["~i.to!string~"] = components_map.get(types["~i.to!string~"].stringof);\n";
|
ret ~= "system.components[" ~ i.to!string
|
||||||
|
~ "] = components_map.get(types[" ~ i.to!string ~ "].stringof);\n";
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
@ -55,14 +57,15 @@ class EntityManager
|
||||||
Sys* s = cast(Sys*) data.system.system_pointer;
|
Sys* s = cast(Sys*) data.system.system_pointer;
|
||||||
|
|
||||||
EntitiesBlock* block = data.info.first_block;
|
EntitiesBlock* block = data.info.first_block;
|
||||||
while(block !is null)
|
while (block !is null)
|
||||||
{
|
{
|
||||||
void* data_pointer = block.dataBegin();
|
void* data_pointer = block.dataBegin();
|
||||||
foreach(i; 0..block.entities_count){
|
foreach (i; 0 .. block.entities_count)
|
||||||
|
{
|
||||||
mixin(genCall());
|
mixin(genCall());
|
||||||
data_pointer+=data.info.size;
|
data_pointer += data.info.size;
|
||||||
}
|
}
|
||||||
block=block.next_block;
|
block = block.next_block;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -95,7 +98,7 @@ class EntityManager
|
||||||
ushort size = Comp.sizeof;
|
ushort size = Comp.sizeof;
|
||||||
ComponentInfo info;
|
ComponentInfo info;
|
||||||
info.size = size;
|
info.size = size;
|
||||||
info.aligment = Comp.alignof;//8;
|
info.aligment = Comp.alignof; //8;
|
||||||
|
|
||||||
if (components is null)
|
if (components is null)
|
||||||
{
|
{
|
||||||
|
|
@ -108,24 +111,24 @@ class EntityManager
|
||||||
components[$ - 1] = info;
|
components[$ - 1] = info;
|
||||||
}
|
}
|
||||||
|
|
||||||
components_map.add(Comp.stringof,cast(uint)(components.length-1));
|
components_map.add(Comp.stringof, cast(uint)(components.length - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void update()
|
void update()
|
||||||
{
|
{
|
||||||
foreach(info;&entities_infos.byValue)
|
foreach (info; &entities_infos.byValue)
|
||||||
{
|
{
|
||||||
foreach(data;info.callers)
|
foreach (data; info.callers)
|
||||||
{
|
{
|
||||||
data.system.update(data,null);//caller(call_data,null);
|
data.system.update(data, null); //caller(call_data,null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void alignNum(ref ushort num, ushort aligment)
|
static void alignNum(ref ushort num, ushort aligment)
|
||||||
{
|
{
|
||||||
num += aligment - (num & (aligment-1));
|
num += aligment - (num & (aligment - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityTemplate* allocateTemplate(ushort[] components_ids)
|
EntityTemplate* allocateTemplate(ushort[] components_ids)
|
||||||
{
|
{
|
||||||
|
|
@ -142,23 +145,24 @@ class EntityManager
|
||||||
|
|
||||||
foreach (i, id; components_ids)
|
foreach (i, id; components_ids)
|
||||||
{
|
{
|
||||||
info.alignment = max(info.alignment,components[id].aligment);
|
info.alignment = max(info.alignment, components[id].aligment);
|
||||||
alignNum(info.size, components[id].aligment);
|
alignNum(info.size, components[id].aligment);
|
||||||
info.deltas[i] = info.size;
|
info.deltas[i] = info.size;
|
||||||
info.size += components[id].size;
|
info.size += components[id].size;
|
||||||
}
|
}
|
||||||
alignNum(info.size, info.alignment);
|
alignNum(info.size, info.alignment);
|
||||||
|
|
||||||
foreach(ref system;systems)
|
foreach (ref system; systems)
|
||||||
{
|
{
|
||||||
if(system.update is null)continue;
|
if (system.update is null)
|
||||||
CallData call_data = CallData(&system,info, null);
|
continue;
|
||||||
|
CallData call_data = CallData(&system, info, null);
|
||||||
call_data.deltas = Mallocator.instance.makeArray!ushort(system.components.length);
|
call_data.deltas = Mallocator.instance.makeArray!ushort(system.components.length);
|
||||||
foreach(i,id;system.components)
|
foreach (i, id; system.components)
|
||||||
{
|
{
|
||||||
foreach(i2,id2;info.components)
|
foreach (i2, id2; info.components)
|
||||||
{
|
{
|
||||||
if(id2 == id)
|
if (id2 == id)
|
||||||
{
|
{
|
||||||
call_data.deltas[i] = info.deltas[i2];
|
call_data.deltas[i] = info.deltas[i2];
|
||||||
break;
|
break;
|
||||||
|
|
@ -168,7 +172,7 @@ class EntityManager
|
||||||
info.callers.add(call_data);
|
info.callers.add(call_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
entities_infos.add(info.components,info);
|
entities_infos.add(info.components, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityTemplate* temp = Mallocator.instance.make!EntityTemplate;
|
EntityTemplate* temp = Mallocator.instance.make!EntityTemplate;
|
||||||
|
|
@ -184,45 +188,44 @@ class EntityManager
|
||||||
}
|
}
|
||||||
|
|
||||||
void addEntity(EntityTemplate* tmpl)
|
void addEntity(EntityTemplate* tmpl)
|
||||||
{
|
{
|
||||||
EntitiesBlock* previous_block;
|
EntitiesBlock* previous_block;
|
||||||
EntitiesBlock* block=tmpl.info.first_with_free_space;//tmpl.info.first_block;
|
EntitiesBlock* block = tmpl.info.first_with_free_space; //tmpl.info.first_block;
|
||||||
|
|
||||||
// find block with enought space
|
// find block with enought space
|
||||||
while(1)
|
while (1)
|
||||||
{
|
{
|
||||||
if(block is null)
|
if (block is null)
|
||||||
{
|
{
|
||||||
block = cast(EntitiesBlock*) AlignedMallocator.instance.alignedAllocate(4096,4096);
|
block = cast(EntitiesBlock*) AlignedMallocator.instance.alignedAllocate(4096, 4096);
|
||||||
*block = EntitiesBlock(tmpl.info);
|
*block = EntitiesBlock(tmpl.info);
|
||||||
if(previous_block is null)
|
if (previous_block is null)
|
||||||
{
|
{
|
||||||
tmpl.info.first_block=block;
|
tmpl.info.first_block = block;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
previous_block.next_block=block;
|
previous_block.next_block = block;
|
||||||
}
|
}
|
||||||
tmpl.info.first_with_free_space=block;
|
tmpl.info.first_with_free_space = block;
|
||||||
break; // new block certainly has free space
|
break; // new block certainly has free space
|
||||||
}
|
}
|
||||||
// check if there is enought space
|
// check if there is enought space
|
||||||
if(block.dataDelta() + (block.entities_count + 1) * tmpl.info.size > 4096 )
|
if (block.dataDelta() + (block.entities_count + 1) * tmpl.info.size > 4096)
|
||||||
{
|
{
|
||||||
previous_block=block;
|
previous_block = block;
|
||||||
block=block.next_block;
|
block = block.next_block;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl.info.first_with_free_space=block;
|
tmpl.info.first_with_free_space = block;
|
||||||
break; // block exists and bounds check passed
|
break; // block exists and bounds check passed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void* start = block.dataBegin() + block.entities_count * tmpl.info.size;
|
void* start = block.dataBegin() + block.entities_count * tmpl.info.size;
|
||||||
memcpy(start, tmpl.entity_data.ptr, tmpl.info.size);
|
memcpy(start, tmpl.entity_data.ptr, tmpl.info.size);
|
||||||
block.entities_count++;
|
block.entities_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CallData
|
struct CallData
|
||||||
{
|
{
|
||||||
|
|
@ -253,13 +256,14 @@ class EntityManager
|
||||||
uint dataDelta()
|
uint dataDelta()
|
||||||
{
|
{
|
||||||
ushort dif = EntitiesBlock.sizeof;
|
ushort dif = EntitiesBlock.sizeof;
|
||||||
alignNum(dif,type_data.alignment);
|
alignNum(dif, type_data.alignment);
|
||||||
return dif;
|
return dif;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* dataBegin()
|
void* dataBegin()
|
||||||
{
|
{
|
||||||
ushort dif = EntitiesBlock.sizeof;
|
ushort dif = EntitiesBlock.sizeof;
|
||||||
alignNum(dif,type_data.alignment);
|
alignNum(dif, type_data.alignment);
|
||||||
return cast(void*)&this + dif;
|
return cast(void*)&this + dif;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -272,7 +276,7 @@ class EntityManager
|
||||||
alias SytemFuncType = void function(ref EntityManager.CallData data, void* entity);
|
alias SytemFuncType = void function(ref EntityManager.CallData data, void* entity);
|
||||||
|
|
||||||
HashMap!(ushort[], EntityInfo*) entities_infos;
|
HashMap!(ushort[], EntityInfo*) entities_infos;
|
||||||
HashMap!(string,uint) components_map;
|
HashMap!(string, uint) components_map;
|
||||||
System[] systems;
|
System[] systems;
|
||||||
ComponentInfo[] components;
|
ComponentInfo[] components;
|
||||||
__gshared EntityManager instance;
|
__gshared EntityManager instance;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue