Template allocation update and some tests

-test runner support before/after everty test callback funcion
-added some basic tests for template allocation
-added function to allocate new template from different template and list of addition and removed components
This commit is contained in:
Mergul 2020-04-09 22:14:51 +02:00
parent 8f5f2f3baf
commit 998240f7be
3 changed files with 243 additions and 3 deletions

View file

@ -1374,8 +1374,6 @@ export struct EntityManager
{
memcpy(temp.entity_data.ptr + info.tmpl_deltas[comp],
components[comp].init_data.ptr, components[comp].size);
/*temp.entity_data[info.tmpl_deltas[comp] .. info.tmpl_deltas[comp] + components[comp].size]
= components[comp].init_data;*/
}
}
else
@ -1383,7 +1381,7 @@ export struct EntityManager
ushort index = block.entityIndex(entity);
foreach (comp; info.components)
{
memcpy(cast(void*) temp.entity_data + info.tmpl_deltas[comp],
memcpy(cast(void*) temp.entity_data.ptr + info.tmpl_deltas[comp],
cast(void*) block + info.deltas[comp] + components[comp].size * index,
components[comp].size);
}
@ -1440,6 +1438,74 @@ export struct EntityManager
return temp;
}
/************************************************************************************************************************
*Allocate EntityTemplate with specifed components and returns pointer to it.
*
*Params:
*components_ids = array of components allocated with template
*/
export EntityTemplate* allocateTemplate(EntityTemplate* base_tmpl, ushort[] components_ids, ushort[] remove_components_ids = null)
{
size_t len = base_tmpl.info.components.length + components_ids.length;
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * len))[0
.. len];
memcpy(ids.ptr, base_tmpl.info.components.ptr, ushort.sizeof * base_tmpl.info.components.length);
memcpy(ids.ptr + base_tmpl.info.components.length, components_ids.ptr, ushort.sizeof * components_ids.length);
qsort(ids.ptr, ids.length, ushort.sizeof, &compareUShorts);
qsort(remove_components_ids.ptr, remove_components_ids.length, ushort.sizeof, &compareUShorts);
{
uint k = 0;
uint j = 1;
foreach (i; 1 .. ids.length)
{
assert(ids[i] != ushort.max);
if(k < remove_components_ids.length)
{
while(k < remove_components_ids.length && remove_components_ids[k] < ids[i])
{
k++;
}
if(k < remove_components_ids.length)
{
if(remove_components_ids[k] == ids[i])continue;
}
}
if (ids[i] != ids[j - 1])
{
ids[j] = ids[i];
j++;
}
else
debug assert(0, "Duplicated components in template!!!");
}
ids = ids[0 .. j];
}
EntityInfo* info = getEntityInfo(ids);
EntityTemplate* temp = Mallocator.make!EntityTemplate;
temp.entity_data = Mallocator.makeArray!ubyte(info.size);
temp.info = info;
//fill components with default data and copy from base template
foreach (comp; info.components)
{
if(comp < base_tmpl.info.deltas.length && base_tmpl.info.deltas[comp] != ushort.max) //copy data from base component
{
memcpy(temp.entity_data.ptr + info.tmpl_deltas[comp],
base_tmpl.entity_data.ptr + base_tmpl.info.tmpl_deltas[comp], components[comp].size);
}
else //fill with default data
{
memcpy(temp.entity_data.ptr + info.tmpl_deltas[comp],
components[comp].init_data.ptr, components[comp].size);
}
}
return temp;
}
/************************************************************************************************************************
*Returns entity type info.
*