Mostly bugfix update + empty components support and remove EntityID from Event structure

-empty components now take no memory, so flag components is now far better
-added test for critical bug
-fixed critical bug with adding/removing entities form inside events
-fixed small bug with TestRunner
-improve basic tests
-fixed betterC compilation on DMD
-remove EntityID form Event structure
-added "return" attribute to some functions
-moved some code from Tempalte side to actual implementation
-fixed bug with EntityTemplate copying
-commented out some possibliy unused code
-use code formatter
This commit is contained in:
Mergul 2020-05-27 17:03:44 +02:00
parent 15cd57dbcb
commit 6929f5a748
16 changed files with 988 additions and 544 deletions

View file

@ -20,7 +20,7 @@ package struct EventManager
void destroy() nothrow @nogc
{
if(event_block_alloc_mutex)
if (event_block_alloc_mutex)
{
event_block_alloc_mutex.destroy();
Mallocator.dispose(event_block_alloc_mutex);
@ -30,14 +30,14 @@ package struct EventManager
export void sendEvent(Ev)(EntityID id, Ev event, uint thread_id = 0) nothrow @nogc
{
uint block_id = current_index+thread_id;
uint block_id = current_index + thread_id;
EventData* data = &events[Ev.event_id];
EventBlock* block = data.blocks[block_id];
//EntityManager.EventInfo* info = &manager.events[Ev.event_id];
event.entity_id = id;
//event.entity_id = id;
if(block is null)
if (block is null)
{
event_block_alloc_mutex.lock();
block = cast(EventBlock*) allocator.getBlock();
@ -48,35 +48,42 @@ package struct EventManager
data.blocks[block_id] = block;
}
if(block.count >= data.max_events)
if (block.count >= data.max_events)
{
event_block_alloc_mutex.lock();
EventBlock* new_block = cast(EventBlock*) allocator.getBlock();
event_block_alloc_mutex.unlock();
*new_block = EventBlock();
block.next = new_block;
block = new_block;
data.blocks[block_id] = block;
}
Ev* event_array = cast(Ev*)(cast(void*)block + data.data_offset);
event_array[block.count] = event;
uint size = Ev.sizeof + EntityID.sizeof;
void* ptr = cast(void*) block + data.data_offset + block.count * size;
*cast(EntityID*)ptr = id;
*cast(Ev*)(ptr + EntityID.sizeof) = event;
//Ev* event_array = cast(Ev*)(cast(void*) block + data.data_offset);
//event_array[block.count] = event;
block.count++;
}
void swapCurrent() nothrow @nogc
{
uint threads_count = cast(uint)manager.threads.length;
if(current_index == 0)current_index = threads_count;
else current_index = 0;
uint threads_count = cast(uint) manager.threads.length;
if (current_index == 0)
current_index = threads_count;
else
current_index = 0;
foreach(ref event;events)
foreach (ref event; events)
{
foreach(ref first_block; event.first_blocks[current_index .. current_index + threads_count])
foreach (ref first_block; event.first_blocks[current_index
.. current_index + threads_count])
{
EventBlock* block = first_block;
while(block)
while (block)
{
EventBlock* to_dispose = block;
block = block.next;
@ -84,7 +91,7 @@ package struct EventManager
}
first_block = null;
}
foreach(ref block; event.blocks[current_index .. current_index + threads_count])
foreach (ref block; event.blocks[current_index .. current_index + threads_count])
{
block = null;
}
@ -94,12 +101,12 @@ package struct EventManager
void clearEvents() nothrow @nogc
{
//uint threads_count = cast(uint)manager.threads.length;
foreach(ref event;events)
foreach (ref event; events)
{
foreach(ref first_block; event.first_blocks)
foreach (ref first_block; event.first_blocks)
{
EventBlock* block = first_block;
while(block)
while (block)
{
EventBlock* to_dispose = block;
block = block.next;
@ -107,7 +114,7 @@ package struct EventManager
}
first_block = null;
}
foreach(ref block; event.blocks)
foreach (ref block; event.blocks)
{
block = null;
}
@ -118,23 +125,25 @@ package struct EventManager
{
disposeData();
events = Mallocator.makeArray!EventData(manager.events.length);
foreach(i,ref event;events)
foreach (i, ref event; events)
{
event.blocks = Mallocator.makeArray!(EventBlock*)(threads_count*2);
event.first_blocks = Mallocator.makeArray!(EventBlock*)(threads_count*2);
event.data_offset = EventBlock.sizeof;//manager.events[i].
event.blocks = Mallocator.makeArray!(EventBlock*)(threads_count * 2);
event.first_blocks = Mallocator.makeArray!(EventBlock*)(threads_count * 2);
event.data_offset = EventBlock.sizeof; //manager.events[i].
manager.alignNum(event.data_offset, manager.events[i].alignment);
event.max_events = cast(ushort)((events_block_size - event.data_offset) / manager.events[i].size);
uint size = manager.events[i].size + EntityID.sizeof;
event.max_events = cast(ushort)(
(events_block_size - event.data_offset) / size);
}
}
private void disposeData() nothrow @nogc
{
clearEvents();
if(events)
if (events)
{
foreach(ref event;events)
foreach (ref event; events)
{
Mallocator.dispose(event.blocks);
Mallocator.dispose(event.first_blocks);
@ -166,7 +175,7 @@ package struct EventManager
ushort max_events;
EventBlock*[] blocks;
EventBlock*[] first_blocks;
//EventBlock*[] current_blocks;
}