-formatted all files with dfmt

-more messy tests
-some documentation
-some useful code moved to functions
-adding components to existing entity
-opportunity to get entity by ID
-fixed calculating deltas
-sorting IDs arrays for allocating Templates
-fixed alignment caluclation
-added compile-time checks for "component_id" member existing in component
This commit is contained in:
Mergul 2018-09-13 00:10:48 +02:00
parent d3222eefbb
commit 4b19907c03
4 changed files with 395 additions and 124 deletions

View file

@ -10,11 +10,11 @@ import std.stdio;
int main()
{
alias SerializeVector = ubyte[];
alias SerializeVector = ubyte[];
struct TestComp
struct TestComp
{
__gshared static int component_id;
__gshared ushort component_id;
int a;
ulong b;
@ -31,9 +31,9 @@ int main()
struct TestComp2
{
__gshared static int component_id;
short b;
ubyte a;
__gshared ushort component_id;
int b;
int a;
static void serializeComponent(ref TestComp comp, SerializeVector output)
{
@ -46,25 +46,60 @@ int main()
}
}
struct TestSystem
struct TestComp3
{
__gshared ushort component_id;
uint gg; //good game
uint bg; //bad game
static void serializeComponent(ref TestComp comp, SerializeVector output)
{
}
static void deserializeComponent(ref TestComp comp, ubyte[] data)
{
}
}
struct TestComp4
{
__gshared ushort component_id;
uint gg; //good game
uint bg; //bad game
static void serializeComponent(ref TestComp comp, SerializeVector output)
{
}
static void deserializeComponent(ref TestComp comp, ubyte[] data)
{
}
}
struct TestSystem
{
void initialize(ref TestComp comp)
{
}
void update(ref TestComp test, ref TestComp2 test2)//ref TestComp comp)
void initialize(ref Entity entity, ref TestComp comp)
{
assert( cast(ptrdiff_t)&test % TestComp.alignof == 0 );
assert( cast(ptrdiff_t)&test2 % TestComp2.alignof == 0 );
}
void update(ref Entity entity, ref TestComp test, ref TestComp2 test2) //ref TestComp comp)
{
assert(cast(size_t)&test % TestComp.alignof == 0);
assert(cast(size_t)&test2 % TestComp2.alignof == 0);
import std.stdio;
//writeln("Jakis tekst! ",test.b);
test.a+=1000;
test.b+=2000;
//writeln("Jakis tekst! ",test.b);
//writeln("Jakis tekst! ",test.b);
test.a += 1000;
test.b += 2000;
//writeln("Jakis tekst! ",test.b);
test2.b += 2;
test2.a = 1;
test2.a = 8;
//writeln("Jakis tekst! ",test2.b);
}
@ -80,23 +115,26 @@ int main()
void onEnable()
{
import std.stdio;
writeln("TestSystem2 enabled");
}
void onDisable()
{
import std.stdio;
writeln("TestSystem2 disabled");
}
void initialize(ref TestComp comp)
{
}
void update(ref TestComp2 test2)//ref TestComp comp)
void initialize(ref Entity entity, ref TestComp comp)
{
test2.b += 1000;
}
void update(ref Entity entity, ref TestComp3 test) //ref TestComp comp)
{
writeln("TestSystem2 update");
test.gg += 14;
}
void handleEvent(Event event, ref TestComp comp)
@ -105,16 +143,18 @@ int main()
}
}
EntityManager.initialize();
assert(gEM !is null);
EntityManager.initialize();
assert(gEM !is null);
MonoTime time = MonoTime.currTime;
gEM.registerComponent!TestComp2;
gEM.registerComponent!TestComp;
gEM.registerComponent!TestComp4;
gEM.registerComponent!TestComp;
gEM.registerComponent!TestComp3;
ulong dur = (MonoTime.currTime - time).total!"usecs";
writeln("Components register: ",dur," usecs");
writeln("Components register: ", dur, " usecs");
time = MonoTime.currTime;
@ -122,16 +162,17 @@ int main()
//gEM.registerSystem!TestSystem2(0);
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Systems register: ",dur," usecs");
writeln("Systems register: ", dur, " usecs");
time = MonoTime.currTime;
ushort[2] ids = [0,1];
EntityTemplate* tmpl = gEM.allocateTemplate(ids);
*cast(EntityID*)tmpl.entity_data.ptr = EntityID(1,1);
ushort[2] ids = [TestComp2.component_id, TestComp.component_id];
EntityTemplate* tmpl = gEM.allocateTemplate(ids);
//writeln(tmpl.info.components[]);
*cast(EntityID*) tmpl.entity_data.ptr = EntityID(1, 1);
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Template allocating: ",dur," usecs");
writeln("Template allocating: ", dur, " usecs");
time = MonoTime.currTime;
@ -141,30 +182,34 @@ int main()
EntityID[1000] idss;
foreach(i; 0..1_000)
foreach (i; 0 .. 1_000)
{
foreach(j; 0..1_000)idss[j] = gEM.addEntity(tmpl).id;
foreach(j; 0..1_000)gEM.removeEntity(idss[j]);
foreach (j; 0 .. 1_000)
idss[j] = gEM.addEntity(tmpl).id;
foreach (j; 0 .. 1_000)
gEM.removeEntity(idss[j]);
}
uint blocks = 0;
foreach(info; &gEM.entities_infos.byValue)
foreach (info; &gEM.entities_infos.byValue)
{
EntityManager.EntitiesBlock* block = info.first_block;
while(block !is null)
while (block !is null)
{
block = block.next_block;
blocks++;
}
}
writeln("Entities blocks: ",blocks);
writeln("Entities blocks: ", blocks);
/*Entity entity = gEM.addEntity(tmpl);
gEM.removeEntity(entity.id);
gEM.addEntity(tmpl);*/
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Entities adding: ",dur," usecs");
writeln("Entities adding: ", dur, " usecs");
//foreach(j; 0..1_000)gEM.addEntity(tmpl);
gEM.registerSystem!TestSystem2(0);
@ -173,13 +218,29 @@ int main()
time = MonoTime.currTime;
gEM.update();
//gEM.update();
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Update: ",dur," usecs");
writeln("Update: ", dur, " usecs");
Entity entity = gEM.addEntity(tmpl);
gEM.update();
Entity* pp = gEM.getEntity(entity.id);
writeln((cast(uint*) pp)[0 .. 14], " ", pp);
gEM.addEntity(tmpl);
gEM.addComponents(entity.id, TestComp3());
pp = gEM.getEntity(entity.id);
gEM.update();
writeln((cast(uint*) pp)[0 .. 14], " ", pp);
//import std.stdio;
//writeln((cast(uint*)tmpl.info.first_block)[0..48]);
gEM.freeTemplate(tmpl);
return 0;
}
}