diff --git a/source/ecs/entity.d b/source/ecs/entity.d index f267853..e880a64 100644 --- a/source/ecs/entity.d +++ b/source/ecs/entity.d @@ -45,6 +45,7 @@ export struct EntityTemplate T* getComponent(T)() { + if(T.component_id >= info.tmpl_deltas.length)return null; version(LinearLayout)return cast(T*)(entity_data.ptr + info.tmpl_deltas[T.component_id]); else return cast(T*)(entity_data.ptr + info.deltas[T.component_id]); } diff --git a/source/ecs/manager.d b/source/ecs/manager.d index c637768..41776e3 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -83,7 +83,7 @@ class EntityManager i++; if (isPointer!param) { - ret ~= "if(opt_ptr" ~ opt.to!string ~ " !is null)opt_ptr" + ret ~= "if(opt_array" ~ opt.to!string ~ " !is null)opt_ptr" ~ opt.to!string ~ " = &opt_array" ~ opt.to!string ~ "[i];"; opt++; } @@ -151,6 +151,10 @@ class EntityManager { ret ~= "PointerTarget!(types[" ~ i.to!string ~ "])[] opt_array" ~ opt.to!string ~ ";"; + ret ~= "if(info.deltas[types[" ~ i.to!string ~ "].component_id] != ushort.max)opt_array" + ~ opt.to!string ~ " + = (cast(types[" ~ i.to!string ~ "])(cast(void*)block + info.deltas[types[" + ~ i.to!string ~ "].component_id]))[0..block.entities_count];"; ret ~= "types[" ~ i.to!string ~ "] opt_ptr" ~ opt.to!string ~ ";"; opt++; } @@ -452,6 +456,11 @@ class EntityManager num = cast(ushort)((num + alignment - 1) & (-cast(int) alignment)); //num += alignment - (num & (alignment - 1)); } + static ushort alignedNum(ushort num, ushort alignment) + { + return cast(ushort)((num + alignment - 1) & (-cast(int) alignment)); + } + extern (C) static int compareUShorts(const void* a, const void* b) { ushort _a = *cast(ushort*) a; @@ -523,52 +532,60 @@ class EntityManager info.components = Mallocator.instance.makeArray(ids); info.deltas = Mallocator.instance.makeArray!ushort(ids[$ - 1] + 1); - version (LinearLayout) - { - info.tmpl_deltas = Mallocator.instance.makeArray!ushort(ids[$ - 1] + 1); - uint block_memory = page_size - EntitiesBlock.sizeof; - uint entity_comps_size = EntityID.sizeof; - uint mem_begin = EntitiesBlock.sizeof; - - foreach (id; ids) - { - entity_comps_size += components[id].size; - } - - uint entites_in_block = block_memory / entity_comps_size; - info.max_entities = cast(ushort) entites_in_block; - uint current_delta = cast(uint)(mem_begin + entites_in_block * EntityID.sizeof); - } - info.size = EntityID.sizeof; info.alignment = EntityID.alignof; - foreach (i, id; ids) + version (LinearLayout) { - info.alignment = max(info.alignment, components[id].alignment); - alignNum(info.size, components[id].alignment); + info.tmpl_deltas = Mallocator.instance.makeArray!ushort(ids[$ - 1] + 1); + uint components_size = EntityID.sizeof; - version (LinearLayout) + foreach (i, id; ids) { + info.alignment = max(info.alignment, components[id].alignment); + alignNum(info.size, components[id].alignment); info.tmpl_deltas[id] = info.size; + info.size += components[id].size; + components_size += components[id].size; + } + alignNum(info.size, info.alignment); + /**/ + + uint block_memory = cast(uint)( + page_size - EntitiesBlock.sizeof - (info.size - components_size)); + //uint entity_comps_size = EntityID.sizeof; + uint mem_begin = EntitiesBlock.sizeof; + + /*foreach (id; ids) + { + entity_comps_size += components[id].size; + }*/ + + uint entites_in_block = block_memory / info.size; //entity_comps_size; + info.max_entities = cast(ushort) entites_in_block; + ushort current_delta = cast(ushort)(mem_begin + entites_in_block * EntityID.sizeof); + + foreach (i, id; ids) + { + alignNum(current_delta, components[id].alignment); info.deltas[id] = cast(ushort) current_delta; current_delta += entites_in_block * components[id].size; } - else - { - info.deltas[id] = info.size; - } - - info.size += components[id].size; - } - alignNum(info.size, info.alignment); - - version (LinearLayout) - { } else + { + foreach (i, id; ids) + { + info.alignment = max(info.alignment, components[id].alignment); + alignNum(info.size, components[id].alignment); + info.deltas[id] = info.size; + info.size += components[id].size; + } + alignNum(info.size, info.alignment); + info.max_entities = (page_size - EntitiesBlock.sizeof) / info.size; + } foreach (uint i, ref system; systems) { @@ -707,7 +724,10 @@ class EntityManager EntitiesBlock* new_block = findBlockWithFreeSpace(new_info); - void* start = new_block.dataBegin() + new_block.entities_count * new_info.size; + version (LinearLayout) + void* start = new_block.dataBegin() + new_block.entities_count * EntityID.sizeof; + else + void* start = new_block.dataBegin() + new_block.entities_count * new_info.size; Entity* new_entity = cast(Entity*) start; new_entity.id = entity.id; @@ -715,6 +735,7 @@ class EntityManager version (LinearLayout) { + static if (EntityID.sizeof == 8) uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3); else @@ -831,14 +852,17 @@ class EntityManager EntitiesBlock* new_block = findBlockWithFreeSpace(new_info); - void* start = new_block.dataBegin() + new_block.entities_count * new_info.size; + //removeEntityNoID(entity, block); + + version (LinearLayout) + void* start = new_block.dataBegin() + new_block.entities_count * EntityID.sizeof; + else + 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(); - //removeEntityNoID(entity, block); - j = 0; k = 0; version (LinearLayout) diff --git a/source/ecs/vector.d b/source/ecs/vector.d index aeb0a9d..ef6a238 100644 --- a/source/ecs/vector.d +++ b/source/ecs/vector.d @@ -194,7 +194,7 @@ public: } export ref T opIndex(size_t elemNum) { - assert(elemNum < used, "Range violation [index]"); + debug assert(elemNum < used, "Range violation [index]"); return array.ptr[elemNum]; }