-code foramtted with dfmt

This commit is contained in:
Mergul 2019-03-28 17:57:28 +01:00
parent 280d7b8ec4
commit d118adc028
2 changed files with 128 additions and 106 deletions

View file

@ -84,9 +84,10 @@ class EntityManager
assert(register_state, "beginRegister() should be called before endRegister();"); assert(register_state, "beginRegister() should be called before endRegister();");
register_state = false; register_state = false;
foreach(ref info; &entities_infos.byValue) foreach (ref info; &entities_infos.byValue)
{ {
if(info.systems)Mallocator.instance.dispose(info.systems); if (info.systems)
Mallocator.instance.dispose(info.systems);
info.systems = Mallocator.instance.makeArray!bool(systems.length); info.systems = Mallocator.instance.makeArray!bool(systems.length);
} }
@ -94,11 +95,11 @@ class EntityManager
{ {
if (system.m_update is null) if (system.m_update is null)
{ {
if(system.m_entity_added || system.m_entity_removed) if (system.m_entity_added || system.m_entity_removed)
{ {
foreach (info; &entities_infos.byValue) foreach (info; &entities_infos.byValue)
{ {
connectListenerToEntityInfo(*info,system.id); connectListenerToEntityInfo(*info, system.id);
} }
} }
continue; continue;
@ -134,32 +135,35 @@ class EntityManager
event_manager.allocateData(cast(uint) threads.length); event_manager.allocateData(cast(uint) threads.length);
foreach(ref info;events) foreach (ref info; events)
{ {
Mallocator.instance.dispose(info.callers); Mallocator.instance.dispose(info.callers);
} }
ushort[] event_callers = (cast(ushort*)alloca(ushort.sizeof * events.length))[0..events.length]; ushort[] event_callers = (cast(ushort*) alloca(ushort.sizeof * events.length))[0
foreach(ref caller; event_callers)caller = 0; .. events.length];
foreach (ref caller; event_callers)
caller = 0;
foreach(ref system;systems) foreach (ref system; systems)
{ {
foreach(caller;system.m_event_callers) foreach (caller; system.m_event_callers)
{ {
event_callers[caller.id]++; event_callers[caller.id]++;
} }
} }
foreach(i,ref info; events) foreach (i, ref info; events)
{ {
info.callers = Mallocator.instance.makeArray!(EventCaller)(event_callers[i]); info.callers = Mallocator.instance.makeArray!(EventCaller)(event_callers[i]);
} }
foreach(ref caller; event_callers)caller = 0; foreach (ref caller; event_callers)
caller = 0;
foreach(ref system;systems) foreach (ref system; systems)
{ {
foreach(caller;system.m_event_callers) foreach (caller; system.m_event_callers)
{ {
events[caller.id].callers[event_callers[caller.id]].callback = caller.callback; events[caller.id].callers[event_callers[caller.id]].callback = caller.callback;
events[caller.id].callers[event_callers[caller.id]].system = &system; events[caller.id].callers[event_callers[caller.id]].system = &system;
@ -167,7 +171,7 @@ class EntityManager
} }
} }
foreach(info; &entities_infos.byValue) foreach (info; &entities_infos.byValue)
{ {
generateListeners(info); generateListeners(info);
} }
@ -554,8 +558,8 @@ class EntityManager
ret ~= "system.m_read_only_components[" ~ (read_only++) ret ~= "system.m_read_only_components[" ~ (read_only++)
.to!string ~ "] = comp;"; .to!string ~ "] = comp;";
else else
ret ~= "system.m_modified_components[" ~ (modified++) ret ~= "system.m_modified_components[" ~ (modified++)
.to!string ~ "] = comp;"; .to!string ~ "] = comp;";
ret ~= "}"; ret ~= "}";
} }
} }
@ -640,8 +644,8 @@ class EntityManager
//pragma(msg,genFillInputData); //pragma(msg,genFillInputData);
static void fillInputData(ref Sys.EntitiesData input_data, EntityInfo* info, EntitiesBlock* block, static void fillInputData(ref Sys.EntitiesData input_data, EntityInfo* info,
uint offset, uint entities_count, System* system) EntitiesBlock* block, uint offset, uint entities_count, System* system)
{ {
mixin(genFillInputData()); mixin(genFillInputData());
} }
@ -755,8 +759,8 @@ class EntityManager
mixin(catchFunc!(bool)("m_begin", "onBegin")); mixin(catchFunc!(bool)("m_begin", "onBegin"));
mixin(catchFunc("m_end", "onEnd")); mixin(catchFunc("m_end", "onEnd"));
mixin(catchEntityFunc("m_entity_added","onAdd")); mixin(catchEntityFunc("m_entity_added", "onAdd"));
mixin(catchEntityFunc("m_entity_removed","onRemove")); mixin(catchEntityFunc("m_entity_removed", "onRemove"));
system.m_system_pointer = cast(void*) Mallocator.instance.make!Sys; system.m_system_pointer = cast(void*) Mallocator.instance.make!Sys;
system.m_priority = priority; system.m_priority = priority;
@ -845,8 +849,7 @@ class EntityManager
} }
static if (hasMember!(Comp, "onCreate") && isFunction!(Comp.onCreate) static if (hasMember!(Comp, "onCreate") && isFunction!(Comp.onCreate)
&& is(ReturnType!(Comp.onCreate) == void) && is(ReturnType!(Comp.onCreate) == void) && Parameters!(Comp.onCreate).length == 0)
&& Parameters!(Comp.onCreate).length == 0)
{ {
static void callCreate(void* pointer) nothrow @nogc static void callCreate(void* pointer) nothrow @nogc
{ {
@ -1077,7 +1080,7 @@ class EntityManager
nextJob(); nextJob();
caller.job_group.jobs = sys.jobs[0 .. job_id]; caller.job_group.jobs = sys.jobs[0 .. job_id];
(cast(void delegate(JobGroup) nothrow @nogc)m_dispatch_jobs)(caller.job_group); //sys.jobs[0 .. job_id]); (cast(void delegate(JobGroup) nothrow @nogc) m_dispatch_jobs)(caller.job_group); //sys.jobs[0 .. job_id]);
} }
} }
} }
@ -1210,10 +1213,11 @@ class EntityManager
//if(system.m_entity_added || system.m_entity_removed)info.systems[system.id] = true; //if(system.m_entity_added || system.m_entity_removed)info.systems[system.id] = true;
if (system.m_update is null) if (system.m_update is null)
{ {
if(system.m_entity_added || system.m_entity_removed)connectListenerToEntityInfo(*info,cast(uint)i); if (system.m_entity_added || system.m_entity_removed)
connectListenerToEntityInfo(*info, cast(uint) i);
continue; continue;
} }
addSystemCaller(*info, cast(uint)i); addSystemCaller(*info, cast(uint) i);
} }
entities_infos.add(info.components, info); entities_infos.add(info.components, info);
@ -1225,76 +1229,80 @@ class EntityManager
void generateListeners(EntityInfo* info) void generateListeners(EntityInfo* info)
{ {
if(info.add_listeners) if (info.add_listeners)
{ {
Mallocator.instance.dispose(info.add_listeners); Mallocator.instance.dispose(info.add_listeners);
info.add_listeners = null; info.add_listeners = null;
} }
if(info.remove_listeners) if (info.remove_listeners)
{ {
Mallocator.instance.dispose(info.remove_listeners); Mallocator.instance.dispose(info.remove_listeners);
info.remove_listeners = null; info.remove_listeners = null;
} }
//allocate local data //allocate local data
ushort[] tmp_add = (cast(ushort*)alloca(systems.length*ushort.sizeof))[0..systems.length]; ushort[] tmp_add = (cast(ushort*) alloca(systems.length * ushort.sizeof))[0
ushort[] tmp_rem = (cast(ushort*)alloca(systems.length*ushort.sizeof))[0..systems.length]; .. systems.length];
ushort[] tmp_rem = (cast(ushort*) alloca(systems.length * ushort.sizeof))[0
.. systems.length];
int add_len = 0; int add_len = 0;
int rem_len = 0; int rem_len = 0;
//assign listeners to lists //assign listeners to lists
foreach(i;0..systems.length) foreach (i; 0 .. systems.length)
{ {
if(info.systems[i]) if (info.systems[i])
{ {
System* system = &systems[i]; System* system = &systems[i];
//onAddEntity listener //onAddEntity listener
if(system.m_entity_added) if (system.m_entity_added)
{ {
//find listener position by priority //find listener position by priority
int j; int j;
for(j=0;j<add_len;j++) for (j = 0; j < add_len; j++)
{ {
if(systems[i].priority > systems[tmp_add[j]].priority)break; if (systems[i].priority > systems[tmp_add[j]].priority)
break;
} }
add_len++; add_len++;
//move elements after new listener //move elements after new listener
for(int k=add_len;k>j;k--) for (int k = add_len; k > j; k--)
{ {
tmp_add[k] = tmp_add[k-1]; tmp_add[k] = tmp_add[k - 1];
} }
//assign listener //assign listener
tmp_add[j] = cast(ushort)i; tmp_add[j] = cast(ushort) i;
} }
//onRemoveEntity listener //onRemoveEntity listener
if(system.m_entity_removed) if (system.m_entity_removed)
{ {
//find listener position by priority //find listener position by priority
int j; int j;
for(j=0;j<rem_len;j++) for (j = 0; j < rem_len; j++)
{ {
if(systems[i].priority > systems[tmp_rem[j]].priority)break; if (systems[i].priority > systems[tmp_rem[j]].priority)
break;
} }
rem_len++; rem_len++;
//move elements after new listener //move elements after new listener
for(int k=rem_len;k>j;k--) for (int k = rem_len; k > j; k--)
{ {
tmp_rem[k] = tmp_rem[k-1]; tmp_rem[k] = tmp_rem[k - 1];
} }
//assign listener //assign listener
tmp_rem[j] = cast(ushort)i; tmp_rem[j] = cast(ushort) i;
} }
} }
} }
if(add_len) if (add_len)
{ {
info.add_listeners = Mallocator.instance.makeArray!ushort(add_len); info.add_listeners = Mallocator.instance.makeArray!ushort(add_len);
memcpy(info.add_listeners.ptr,tmp_add.ptr,add_len*ushort.sizeof); memcpy(info.add_listeners.ptr, tmp_add.ptr, add_len * ushort.sizeof);
} }
if(rem_len) if (rem_len)
{ {
info.remove_listeners = Mallocator.instance.makeArray!ushort(rem_len); info.remove_listeners = Mallocator.instance.makeArray!ushort(rem_len);
memcpy(info.remove_listeners.ptr,tmp_rem.ptr,rem_len*ushort.sizeof); memcpy(info.remove_listeners.ptr, tmp_rem.ptr, rem_len * ushort.sizeof);
} }
} }
@ -1448,13 +1456,13 @@ class EntityManager
else else
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof()); uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof());
if(info.remove_listeners) if (info.remove_listeners)
{ {
foreach(listener;info.remove_listeners) foreach (listener; info.remove_listeners)
{ {
if(!new_info.systems[listener]) if (!new_info.systems[listener])
{ {
callRemoveEntityListener(&systems[listener],info,block,ind,ind+1); callRemoveEntityListener(&systems[listener], info, block, ind, ind + 1);
} }
} }
} }
@ -1466,13 +1474,14 @@ class EntityManager
cast(void*) block + info.deltas[comp] + ind * comp_size, comp_size); cast(void*) block + info.deltas[comp] + ind * comp_size, comp_size);
} }
if(new_info.add_listeners) if (new_info.add_listeners)
{ {
foreach(listener;new_info.add_listeners) foreach (listener; new_info.add_listeners)
{ {
if(!info.systems[listener]) if (!info.systems[listener])
{ {
callAddEntityListener(&systems[listener],new_info,new_block,new_block.entities_count,new_block.entities_count+1); callAddEntityListener(&systems[listener], new_info, new_block,
new_block.entities_count, new_block.entities_count + 1);
} }
} }
} }
@ -1601,13 +1610,13 @@ class EntityManager
else else
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof()); uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof());
if(info.remove_listeners) if (info.remove_listeners)
{ {
foreach(listener;info.remove_listeners) foreach (listener; info.remove_listeners)
{ {
if(!new_info.systems[listener]) if (!new_info.systems[listener])
{ {
callRemoveEntityListener(&systems[listener],info,block,ind,ind+1); callRemoveEntityListener(&systems[listener], info, block, ind, ind + 1);
} }
} }
} }
@ -1639,13 +1648,14 @@ class EntityManager
} }
} }
if(new_info.add_listeners) if (new_info.add_listeners)
{ {
foreach(listener;new_info.add_listeners) foreach (listener; new_info.add_listeners)
{ {
if(!info.systems[listener]) if (!info.systems[listener])
{ {
callAddEntityListener(&systems[listener],new_info,new_block,new_block.entities_count,new_block.entities_count+1); callAddEntityListener(&systems[listener], new_info, new_block,
new_block.entities_count, new_block.entities_count + 1);
} }
} }
} }
@ -1738,8 +1748,8 @@ class EntityManager
if (components[comp].create_callback) if (components[comp].create_callback)
{ {
components[comp].create_callback(cast( components[comp].create_callback(
void*) block + info.deltas[comp] + id * components[comp].size); cast(void*) block + info.deltas[comp] + id * components[comp].size);
} }
} }
@ -1848,7 +1858,7 @@ class EntityManager
EntitiesBlock* block = getMetaData(entity); EntitiesBlock* block = getMetaData(entity);
EntityInfo* info = block.type_info; EntityInfo* info = block.type_info;
if(info.remove_listeners) if (info.remove_listeners)
{ {
void* data_begin = block.dataBegin(); void* data_begin = block.dataBegin();
static if (EntityID.sizeof == 8) static if (EntityID.sizeof == 8)
@ -1856,7 +1866,7 @@ class EntityManager
else else
uint pos = cast(uint)((cast(void*) entity - data_begin) / EntityID.sizeof()); uint pos = cast(uint)((cast(void*) entity - data_begin) / EntityID.sizeof());
callRemoveEntityListeners(info,block,pos,pos+1); callRemoveEntityListeners(info, block, pos, pos + 1);
} }
id_manager.releaseID(id); //release id from manager id_manager.releaseID(id); //release id from manager
@ -1977,40 +1987,43 @@ class EntityManager
private void callAddEntityListeners(EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow private void callAddEntityListeners(EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow
{ {
foreach(listener;info.add_listeners) foreach (listener; info.add_listeners)
{ {
System* system = &systems[listener]; System* system = &systems[listener];
callAddEntityListener(system,info,block,begin,end); callAddEntityListener(system, info, block, begin, end);
} }
} }
private void callAddEntityListener(System* system, EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow private void callAddEntityListener(System* system, EntityInfo* info,
EntitiesBlock* block, int begin, int end) @nogc nothrow
{ {
ListenerCallData data; ListenerCallData data;
data.system = system; data.system = system;
data.block = block; data.block = block;
data.begin = begin; data.begin = begin;
data.end = end; data.end = end;
(cast(void function (ref ListenerCallData) nothrow @nogc) system.m_entity_added)(data); (cast(void function(ref ListenerCallData) nothrow @nogc) system.m_entity_added)(data);
} }
private void callRemoveEntityListeners(EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow private void callRemoveEntityListeners(EntityInfo* info, EntitiesBlock* block, int begin,
int end) @nogc nothrow
{ {
foreach(listener;info.remove_listeners) foreach (listener; info.remove_listeners)
{ {
System* system = &systems[listener]; System* system = &systems[listener];
callRemoveEntityListener(system,info,block,begin,end); callRemoveEntityListener(system, info, block, begin, end);
} }
} }
private void callRemoveEntityListener(System* system, EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow private void callRemoveEntityListener(System* system, EntityInfo* info,
EntitiesBlock* block, int begin, int end) @nogc nothrow
{ {
ListenerCallData data; ListenerCallData data;
data.system = system; data.system = system;
data.block = block; data.block = block;
data.begin = begin; data.begin = begin;
data.end = end; data.end = end;
(cast(void function (ref ListenerCallData) nothrow @nogc) system.m_entity_removed)(data); (cast(void function(ref ListenerCallData) nothrow @nogc) system.m_entity_removed)(data);
} }
private void updateBlocks() private void updateBlocks()
@ -2028,9 +2041,9 @@ class EntityManager
} }
block.added_count.atomicStore(cast(ushort) 0); block.added_count.atomicStore(cast(ushort) 0);
if(info.add_listeners) if (info.add_listeners)
{ {
callAddEntityListeners(info,block,entities_count,block.entities_count); callAddEntityListeners(info, block, entities_count, block.entities_count);
} }
} }
@ -2053,38 +2066,46 @@ class EntityManager
void updateEvents() nothrow @nogc void updateEvents() nothrow @nogc
{ {
bool empty = true; bool empty = true;
while(1) while (1)
{ {
event_manager.swapCurrent(); event_manager.swapCurrent();
uint current_index; uint current_index;
if(event_manager.current_index == 0)current_index = cast(uint)threads.length; if (event_manager.current_index == 0)
else current_index = 0; current_index = cast(uint) threads.length;
foreach(i,event;event_manager.events) else
current_index = 0;
foreach (i, event; event_manager.events)
{ {
foreach(first_block;event.first_blocks[current_index .. current_index + threads.length]) foreach (first_block; event.first_blocks[current_index
.. current_index + threads.length])
{ {
EventManager.EventBlock* block = first_block; EventManager.EventBlock* block = first_block;
if(block)empty = false; if (block)
while(block) empty = false;
while (block)
{ {
EventCallData call_data; EventCallData call_data;
void* event_pointer = cast(void*)block + event.data_offset; void* event_pointer = cast(void*) block + event.data_offset;
call_data.event = event_pointer; call_data.event = event_pointer;
foreach(j;0..block.count) foreach (j; 0 .. block.count)
{ {
EntityID entity_id = *cast(EntityID*)event_pointer; EntityID entity_id = *cast(EntityID*) event_pointer;
Entity* entity = id_manager.getEntityPointer(entity_id); Entity* entity = id_manager.getEntityPointer(entity_id);
call_data.block = getMetaData(entity); call_data.block = getMetaData(entity);
static if (EntityID.sizeof == 8) static if (EntityID.sizeof == 8)
call_data.id = cast(ushort)((cast(void*)entity - call_data.block.dataBegin()) >> 3); call_data.id = cast(ushort)(
(cast(void*) entity - call_data.block.dataBegin()) >> 3);
else else
call_data.id = cast(ushort)((cast(void*)entity - call_data.block.dataBegin()) / EntityID.sizeof); call_data.id = cast(ushort)(
(cast(void*) entity - call_data.block.dataBegin()) / EntityID
.sizeof);
foreach(caller; events[i].callers) foreach (caller; events[i].callers)
{ {
call_data.system_pointer = caller.system.m_system_pointer; call_data.system_pointer = caller.system.m_system_pointer;
(cast(void function(ref EventCallData) nothrow @nogc)caller.callback)(call_data); (cast(void function(ref EventCallData) nothrow @nogc) caller
.callback)(call_data);
} }
event_pointer += events[i].size; event_pointer += events[i].size;
} }
@ -2092,7 +2113,8 @@ class EntityManager
} }
} }
} }
if(empty)break; if (empty)
break;
empty = true; empty = true;
} }
} }
@ -2142,7 +2164,7 @@ class EntityManager
private void getThreadID() nothrow @nogc private void getThreadID() nothrow @nogc
{ {
if (m_thread_id_func) if (m_thread_id_func)
thread_id = (cast(uint delegate() nothrow @nogc)m_thread_id_func)(); thread_id = (cast(uint delegate() nothrow @nogc) m_thread_id_func)();
else else
thread_id = 0; thread_id = 0;
} }
@ -2243,7 +2265,7 @@ class EntityManager
(SystemCaller*).sizeof, &compareSystems); (SystemCaller*).sizeof, &compareSystems);
foreach (i, caller; pass.system_callers) foreach (i, caller; pass.system_callers)
caller.job_group.id = cast(uint)i; caller.job_group.id = cast(uint) i;
int priority = int.min; int priority = int.min;
uint beg = 0; uint beg = 0;

View file

@ -36,7 +36,7 @@ struct System
export void enable() nothrow @nogc export void enable() nothrow @nogc
{ {
if (!m_enabled && m_enable) if (!m_enabled && m_enable)
(cast(void function(void*) nothrow @nogc)m_enable)(m_system_pointer); (cast(void function(void*) nothrow @nogc) m_enable)(m_system_pointer);
m_enabled = true; m_enabled = true;
} }
@ -46,7 +46,7 @@ struct System
export void disable() nothrow @nogc export void disable() nothrow @nogc
{ {
if (m_enabled && m_disable) if (m_enabled && m_disable)
(cast(void function(void*) nothrow @nogc)m_disable)(m_system_pointer); (cast(void function(void*) nothrow @nogc) m_disable)(m_system_pointer);
m_enabled = false; m_enabled = false;
} }
@ -97,7 +97,7 @@ package:
int m_pass; int m_pass;
///system name ///system name
const (char)[] name; const(char)[] name;
///required components ///required components
ushort[] m_components; ushort[] m_components;