bubel-ecs/tests/tests.d

270 lines
5 KiB
D

module tests.tests;
import ecs.manager;
import ecs.events;
import ecs.system;
import ecs.entity;
import core.time;
import std.stdio;
int main()
{
alias SerializeVector = ubyte[];
struct TestComp
{
__gshared ushort component_id;
int a;
ulong b;
static void serializeComponent(ref TestComp comp, SerializeVector output)
{
}
static void deserializeComponent(ref TestComp comp, ubyte[] data)
{
}
}
struct TestComp2
{
__gshared ushort component_id;
int b;
int a;
static void serializeComponent(ref TestComp comp, SerializeVector output)
{
}
static void deserializeComponent(ref TestComp comp, ubyte[] data)
{
}
}
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 Entity entity, ref TestComp comp)
{
}
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);
test2.b += 2;
test2.a = 8;
//writeln("Jakis tekst! ",test2.b);
//writeln("Low priority tekst! ");
}
void handleEvent(Event event, ref TestComp comp)
{
}
}
struct TestSystemWithHighPriority
{
void initialize(ref Entity entity, ref TestComp comp)
{
}
void update(ref Entity entity, ref TestComp test) //ref TestComp comp)
{
assert(cast(size_t)&test % TestComp.alignof == 0);
//writeln("High priority tekst! ");
}
void handleEvent(Event event, ref TestComp comp)
{
}
}
struct TestSystem2
{
void onEnable()
{
import std.stdio;
writeln("TestSystem2 enabled");
}
void onDisable()
{
import std.stdio;
writeln("TestSystem2 disabled");
}
void initialize(ref Entity entity, ref TestComp comp)
{
}
void update(ref Entity entity, ref TestComp3 test) //ref TestComp comp)
{
//writeln("TestSystem2 update");
test.gg += 14;
}
void handleEvent(Event event, ref TestComp comp)
{
}
}
EntityManager.initialize();
assert(gEM !is null);
MonoTime time = MonoTime.currTime;
gEM.registerComponent!TestComp2;
gEM.registerComponent!TestComp4;
gEM.registerComponent!TestComp;
gEM.registerComponent!TestComp3;
ulong dur = (MonoTime.currTime - time).total!"usecs";
writeln("Components register: ", dur, " usecs");
time = MonoTime.currTime;
gEM.registerSystem!TestSystemWithHighPriority(100);
gEM.registerSystem!TestSystem(0);
//gEM.registerSystem!TestSystemWithHighPriority(100);
//gEM.registerSystem!TestSystem2(0);
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Systems register: ", dur, " usecs");
time = MonoTime.currTime;
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");
time = MonoTime.currTime;
//foreach(i; 0..1_000_000)gEM.addEntity(tmpl);
//foreach(i; 0..1_000_000)gEM.removeEntity(gEM.addEntity(tmpl).id);
EntityID[1000] idss;
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]);
}
uint blocks = 0;
foreach (info; &gEM.entities_infos.byValue)
{
EntityManager.EntitiesBlock* block = info.first_block;
while (block !is null)
{
block = block.next_block;
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");
//foreach(j; 0..1_000)gEM.addEntity(tmpl);
gEM.registerSystem!TestSystem2(0);
//assert(*(cast(EntityID*)(cast(void*)tmpl.info.first_block+24)) == EntityID(1,1));
//assert(*(cast(EntityID*)(cast(void*)tmpl.info.first_block+48)) == EntityID(1,1));
time = MonoTime.currTime;
//gEM.update();
dur = (MonoTime.currTime - time).total!"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;
}