From 32021078111fe01987b9680568316c589a9cdb55 Mon Sep 17 00:00:00 2001 From: Aras Pranckevicius Date: Sun, 23 Sep 2018 22:06:35 +0300 Subject: [PATCH] MoveSystem instead of individual Move updates - 10000,20: 1.8ms update, 6.5ms start - 100000,20: 11.6ms update, 58.8ms start - 1000000,20: 83.1ms update, 502.0ms start, 348.5MB --- source/game.cpp | 86 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/source/game.cpp b/source/game.cpp index 11c417f..db3c886 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -146,8 +146,6 @@ struct WorldBoundsComponent : public Component struct MoveComponent : public Component { float velx, vely; - WorldBoundsComponent* bounds; - PositionComponent* pos; MoveComponent(float minSpeed, float maxSpeed) { @@ -159,44 +157,72 @@ struct MoveComponent : public Component velx = cosf(angle) * speed; vely = sinf(angle) * speed; } + + virtual void Start() override; +}; - virtual void Start() override +struct MoveSystem +{ + WorldBoundsComponent* bounds; + std::vector positionList; + std::vector moveList; + + void AddObjectToSystem(MoveComponent* o) + { + positionList.emplace_back(o->GetGameObject().GetComponent()); + moveList.emplace_back(o); + } + + void Initialize() { bounds = FindOfType(); - // get Position component on our game object - pos = GetGameObject().GetComponent(); } - virtual void Update(double time, float deltaTime) override + void UpdateSystem(double time, float deltaTime) { - // update position based on movement velocity & delta time - pos->x += velx * deltaTime; - pos->y += vely * deltaTime; - - // check against world bounds; put back onto bounds and mirror the velocity component to "bounce" back - if (pos->x < bounds->xMin) - { - velx = -velx; - pos->x = bounds->xMin; - } - if (pos->x > bounds->xMax) - { - velx = -velx; - pos->x = bounds->xMax; - } - if (pos->y < bounds->yMin) - { - vely = -vely; - pos->y = bounds->yMin; - } - if (pos->y > bounds->yMax) + // go through all the objects + for (size_t io = 0, no = positionList.size(); io != no; ++io) { - vely = -vely; - pos->y = bounds->yMax; + PositionComponent* pos = positionList[io]; + MoveComponent* move = moveList[io]; + + // update position based on movement velocity & delta time + pos->x += move->velx * deltaTime; + pos->y += move->vely * deltaTime; + + // check against world bounds; put back onto bounds and mirror the velocity component to "bounce" back + if (pos->x < bounds->xMin) + { + move->velx = -move->velx; + pos->x = bounds->xMin; + } + if (pos->x > bounds->xMax) + { + move->velx = -move->velx; + pos->x = bounds->xMax; + } + if (pos->y < bounds->yMin) + { + move->vely = -move->vely; + pos->y = bounds->yMin; + } + if (pos->y > bounds->yMax) + { + move->vely = -move->vely; + pos->y = bounds->yMax; + } } } }; +static MoveSystem s_MoveSystem; + +void MoveComponent::Start() +{ + s_MoveSystem.AddObjectToSystem(this); +} + + // When present, tells things that have Avoid component to avoid this object struct AvoidThisComponent : public Component @@ -383,6 +409,7 @@ extern "C" void game_initialize(void) // initialize systems s_AvoidanceSystem.Initialize(); + s_MoveSystem.Initialize(); // call Start on all objects/components once they are all created for (auto go : s_Objects) @@ -406,6 +433,7 @@ extern "C" int game_update(sprite_data_t* data, double time, float deltaTime) int objectCount = 0; // update object systems + s_MoveSystem.UpdateSystem(time, deltaTime); s_AvoidanceSystem.UpdateSystem(time, deltaTime); // go through all objects