-changes in EntityAlocator
* renamed to BlockAllocator * now it's template with compile time assingable block_size and block_in_allocation variables -WIP Event management * probably working sendSelfEvent() * registerEvent() -added win_dll.d as Windows DLL require functionality
This commit is contained in:
parent
d0fcdba6cd
commit
4ad81fe116
6 changed files with 264 additions and 57 deletions
|
|
@ -10,11 +10,12 @@ import core.stdc.stdlib;
|
|||
import core.stdc.string;
|
||||
|
||||
import ecs.entity;
|
||||
import ecs.entity_allocator;
|
||||
import ecs.block_allocator;
|
||||
import ecs.hash_map;
|
||||
import ecs.id_manager;
|
||||
import ecs.system;
|
||||
import ecs.vector;
|
||||
import ecs.events;
|
||||
|
||||
alias gEM = EntityManager.instance;
|
||||
alias SerializeVector = ecs.vector.Vector!ubyte;
|
||||
|
|
@ -24,11 +25,14 @@ class EntityManager
|
|||
|
||||
export static void initialize()
|
||||
{
|
||||
instance = Mallocator.instance.make!EntityManager;
|
||||
if (instance is null)
|
||||
instance = Mallocator.instance.make!EntityManager;
|
||||
}
|
||||
|
||||
export static void destroy()
|
||||
{
|
||||
if (instance is null)
|
||||
return;
|
||||
|
||||
foreach (ref system; instance.systems)
|
||||
{
|
||||
|
|
@ -45,6 +49,12 @@ class EntityManager
|
|||
instance = null;
|
||||
}
|
||||
|
||||
this()
|
||||
{
|
||||
//event_manager = EventManager(this);
|
||||
//event_manager.manager = this;
|
||||
}
|
||||
|
||||
void registerSystem(Sys)(int priority)
|
||||
{
|
||||
alias types = Parameters!(Sys.update);
|
||||
|
|
@ -205,14 +215,14 @@ class EntityManager
|
|||
mixin(genCompList());
|
||||
|
||||
ushort sys_id = systems_map.get(Sys.stringof, ushort.max);
|
||||
if(sys_id < systems.length)
|
||||
if (sys_id < systems.length)
|
||||
{
|
||||
system.enable();
|
||||
systems[sys_id] = system;
|
||||
}
|
||||
else
|
||||
{
|
||||
systems_map.add(Sys.stringof,cast(ushort)systems.length);
|
||||
systems_map.add(Sys.stringof, cast(ushort) systems.length);
|
||||
|
||||
systems.add(system);
|
||||
|
||||
|
|
@ -232,7 +242,6 @@ class EntityManager
|
|||
|
||||
void registerComponent(Comp)()
|
||||
{
|
||||
|
||||
ComponentInfo info;
|
||||
|
||||
static if (!(hasMember!(Comp, "component_id")) || !is(typeof(Comp.component_id) == ushort))
|
||||
|
|
@ -253,12 +262,12 @@ class EntityManager
|
|||
}
|
||||
|
||||
info.size = Comp.sizeof;
|
||||
info.aligment = Comp.alignof; //8;
|
||||
info.alignment = 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)
|
||||
if (comp_id < components.length)
|
||||
{
|
||||
Comp.component_id = comp_id;
|
||||
}
|
||||
|
|
@ -270,6 +279,42 @@ class EntityManager
|
|||
}
|
||||
}
|
||||
|
||||
void registerEvent(Ev)()
|
||||
{
|
||||
EventInfo info;
|
||||
|
||||
static if (!(hasMember!(Ev, "event_id")) || !is(typeof(Ev.event_id) == ushort))
|
||||
{
|
||||
static assert(0, "Event should have \"__gshared ushort event_id");
|
||||
}
|
||||
|
||||
static if (hasMember!(Ev, "onDestroy") && isFunction!(Ev.onDestroy)
|
||||
&& is(ReturnType!(Ev.onDestroy) == void) && Parameters!(Ev.onDestroy).length == 0)
|
||||
{
|
||||
static void callDestroy(void* pointer)
|
||||
{
|
||||
(cast(Ev*) pointer).onDestroy();
|
||||
}
|
||||
|
||||
info.destroy_callback = &callDestroy;
|
||||
}
|
||||
|
||||
info.size = Ev.sizeof;
|
||||
info.alignment = Ev.alignof;
|
||||
|
||||
ushort event_id = events_map.get(Ev.stringof, ushort.max);
|
||||
if (event_id < events.length)
|
||||
{
|
||||
Ev.event_id = event_id;
|
||||
}
|
||||
else
|
||||
{
|
||||
events.add(info);
|
||||
Ev.event_id = cast(ushort)(events.length - 1);
|
||||
events_map.add(Ev.stringof, cast(ushort)(events.length - 1));
|
||||
}
|
||||
}
|
||||
|
||||
export void update()
|
||||
{
|
||||
foreach (info; &entities_infos.byValue)
|
||||
|
|
@ -282,9 +327,9 @@ class EntityManager
|
|||
}
|
||||
}
|
||||
|
||||
static void alignNum(ref ushort num, ushort aligment)
|
||||
static void alignNum(ref ushort num, ushort alignment)
|
||||
{
|
||||
num = cast(ushort)((num + aligment - 1) & (-cast(int) aligment)); //num += aligment - (num & (aligment - 1));
|
||||
num = cast(ushort)((num + alignment - 1) & (-cast(int) alignment)); //num += alignment - (num & (alignment - 1));
|
||||
}
|
||||
|
||||
extern (C) static int compareUShorts(const void* a, const void* b)
|
||||
|
|
@ -352,8 +397,8 @@ class EntityManager
|
|||
|
||||
foreach (i, id; ids)
|
||||
{
|
||||
info.alignment = max(info.alignment, components[id].aligment);
|
||||
alignNum(info.size, components[id].aligment);
|
||||
info.alignment = max(info.alignment, components[id].alignment);
|
||||
alignNum(info.size, components[id].alignment);
|
||||
info.deltas[id] = info.size;
|
||||
info.size += components[id].size;
|
||||
}
|
||||
|
|
@ -874,16 +919,25 @@ class EntityManager
|
|||
updateBlocks();
|
||||
changeEntites();
|
||||
removeEntities();
|
||||
|
||||
clearEvents();
|
||||
}
|
||||
|
||||
struct ComponentInfo
|
||||
{
|
||||
ushort size;
|
||||
ushort aligment;
|
||||
ushort alignment;
|
||||
ubyte[] init_data;
|
||||
void function(void* pointer) destroy_callback;
|
||||
}
|
||||
|
||||
struct EventInfo
|
||||
{
|
||||
ushort size;
|
||||
ushort alignment;
|
||||
void function(void* pointer) destroy_callback;
|
||||
}
|
||||
|
||||
/************************************************************************************************************************
|
||||
*Entity type info.
|
||||
*/
|
||||
|
|
@ -893,7 +947,7 @@ class EntityManager
|
|||
ushort[] components;
|
||||
///deltas in memory for components
|
||||
ushort[] deltas;
|
||||
///aligment of whole entity
|
||||
///alignment of whole entity
|
||||
ushort alignment;
|
||||
///size of entity (with alignment respect)
|
||||
ushort size;
|
||||
|
|
@ -956,13 +1010,20 @@ class EntityManager
|
|||
|
||||
alias SytemFuncType = void function(ref EntityManager.CallData data, void* entity);
|
||||
|
||||
//alias sendSelfEvent = instance.event_manager.sendSelfEvent;
|
||||
|
||||
//alias event_manager this;
|
||||
|
||||
///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;
|
||||
BlockAllocator!(page_size, pages_in_block) allocator;
|
||||
|
||||
//EventManager event_manager;
|
||||
mixin EventManagerCode;
|
||||
|
||||
Vector!EntityID entities_to_remove;
|
||||
Vector!(EntitiesBlock*) blocks_to_update;
|
||||
|
|
@ -971,37 +1032,13 @@ class EntityManager
|
|||
HashMap!(ushort[], EntityInfo*) entities_infos;
|
||||
HashMap!(string, ushort) systems_map;
|
||||
HashMap!(string, ushort) components_map;
|
||||
HashMap!(string, ushort) events_map;
|
||||
Vector!System systems;
|
||||
Vector!ComponentInfo components;
|
||||
__gshared EntityManager instance;
|
||||
|
||||
Vector!EventInfo events;
|
||||
__gshared EntityManager instance = null;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue