Skip to content

Commit

Permalink
Merge pull request #23 from richardbiely/master
Browse files Browse the repository at this point in the history
Bring Gaia-ECS up-to-date
  • Loading branch information
abeimler authored Mar 2, 2024
2 parents 2743723 + 599d64a commit 07cc900
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ static ecs::benchmarks::gaia_ecs::GaiaEcsEntityBenchmarkSuite benchmark_suite({.

ECS_ENTITY_BENCHMARKS(benchmark_suite)

/*
static void BM_CreateEntitiesInBulk(benchmark::State& state) {
benchmark_suite.BM_CreateEntitiesInBulk(state);
}
Expand All @@ -14,4 +13,3 @@ static void BM_CreateEmptyEntitiesInBulk(benchmark::State& state) {
benchmark_suite.BM_CreateEmptyEntitiesInBulk(state);
}
BENCHMARK(BM_CreateEmptyEntitiesInBulk)->Apply(ecs::benchmarks::base::BEDefaultArguments);
*/
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class GaiaEcsSoABenchmarkSuite final
[](auto& world) {
return world.query().template all<ComponentOne&>();
},
[&](::gaia::ecs::Iter iter) {
[&](::gaia::ecs::Iter& iter) {
// Position
auto vp = iter.view_mut<ComponentOne>();
auto px = vp.set<0>();
Expand All @@ -50,7 +50,7 @@ class GaiaEcsSoABenchmarkSuite final
[](auto& world) {
return world.query().template all<ComponentOne&, ComponentTwo&>();
},
[&](::gaia::ecs::Iter iter) {
[&](::gaia::ecs::Iter& iter) {
auto vp = iter.view_mut<ComponentOne>();
auto px = vp.set<0>();
auto py = vp.set<1>();
Expand All @@ -74,7 +74,7 @@ class GaiaEcsSoABenchmarkSuite final
[](auto& world) {
return world.query().template all<ComponentOne&, ComponentTwo&, ComponentThree&>();
},
[&](::gaia::ecs::Iter iter) {
[&](::gaia::ecs::Iter& iter) {
auto vp = iter.view_mut<ComponentOne>();
auto px = vp.set<0>();
auto py = vp.set<1>();
Expand Down
4 changes: 2 additions & 2 deletions src/gaia-ecs/gaia-ecs/GaiaEcsApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class GaiaEcsApplication {
~GaiaEcsApplication() = default;
GaiaEcsApplication(const GaiaEcsApplication&) = delete;
GaiaEcsApplication& operator=(const GaiaEcsApplication&) = delete;
GaiaEcsApplication(GaiaEcsApplication&&) = default;
GaiaEcsApplication& operator=(GaiaEcsApplication&&) = default;
GaiaEcsApplication(GaiaEcsApplication&&) = delete;
GaiaEcsApplication& operator=(GaiaEcsApplication&&) = delete;

[[nodiscard]] inline EntityManager& getEntities() { return m_world; }
[[nodiscard]] inline const EntityManager& getEntities() const { return m_world; }
Expand Down
4 changes: 2 additions & 2 deletions src/gaia-ecs/gaia-ecs/SoAGaiaEcsApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class SoAGaiaEcsApplication {
~SoAGaiaEcsApplication() = default;
SoAGaiaEcsApplication(const SoAGaiaEcsApplication&) = delete;
SoAGaiaEcsApplication& operator=(const SoAGaiaEcsApplication&) = delete;
SoAGaiaEcsApplication(SoAGaiaEcsApplication&&) = default;
SoAGaiaEcsApplication& operator=(SoAGaiaEcsApplication&&) = default;
SoAGaiaEcsApplication(SoAGaiaEcsApplication&&) = delete;
SoAGaiaEcsApplication& operator=(SoAGaiaEcsApplication&&) = delete;

[[nodiscard]] inline EntityManager& getEntities() { return m_world; }
[[nodiscard]] inline const EntityManager& getEntities() const { return m_world; }
Expand Down
37 changes: 35 additions & 2 deletions src/gaia-ecs/gaia-ecs/entities/EntityFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,64 @@ EntityFactory::Entity EntityFactory::createEmpty(EntityManager& entities) {
return entities.add();
}

void EntityFactory::createEmptyBulk(EntityManager& entities, size_t nentities) {
entities.add_n(static_cast<uint32_t>(nentities));
}

EntityFactory::Entity EntityFactory::createSingle(EntityManager& entities) {
auto e = entities.add();
entities.add<ecs::benchmarks::base::components::PositionComponent>(e);
return e;
}

void EntityFactory::createSingleBulk(EntityManager& entities, size_t nentities) {
auto e = entities.add();
entities.add<ecs::benchmarks::base::components::PositionComponent>(e);
entities.add_n(e, static_cast<uint32_t>(nentities-1U));
}

EntityFactory::Entity EntityFactory::create(EntityManager& entities) {
auto e = entities.add();
entities.bulk(e)
entities.build(e)
.add<ecs::benchmarks::base::components::PositionComponent>()
.add<ecs::benchmarks::base::components::VelocityComponent>()
.add<ecs::benchmarks::base::components::DataComponent>();
return e;
}

void EntityFactory::createBulk(EntityManager& entities, size_t nentities) {
auto e = entities.add();
entities.build(e)
.add<ecs::benchmarks::base::components::PositionComponent>()
.add<ecs::benchmarks::base::components::VelocityComponent>()
.add<ecs::benchmarks::base::components::DataComponent>();
entities.add_n(e, static_cast<uint32_t>(nentities-1U));
}

EntityFactory::Entity EntityFactory::createMinimal(EntityManager& entities) {
auto e = entities.add();
entities.bulk(e)
entities.build(e)
.add<ecs::benchmarks::base::components::PositionComponent>()
.add<ecs::benchmarks::base::components::VelocityComponent>();
return e;
}

void EntityFactory::createMinimalBulk(EntityManager& entities, size_t nentities) {
auto e = entities.add();
entities.build(e)
.add<ecs::benchmarks::base::components::PositionComponent>()
.add<ecs::benchmarks::base::components::VelocityComponent>();
entities.add_n(e, static_cast<uint32_t>(nentities-1U));
}

void EntityFactory::destroy(EntityManager& entities, Entity entity) {
entities.del(entity);
}

void EntityFactory::destroyBulk(EntityManager& entities, std::vector<Entity>& entities_vec) {
for (auto entity : entities_vec) {
entities.del(entity);
}
}

} // namespace ecs::benchmarks::gaia_ecs::entities
14 changes: 10 additions & 4 deletions src/gaia-ecs/gaia-ecs/entities/EntityFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "base/components/PositionComponent.h"
#include "base/components/VelocityComponent.h"
#include <gaia.h>
#include <optional>

namespace ecs::benchmarks::gaia_ecs::entities {

Expand All @@ -15,18 +14,25 @@ class EntityFactory {
using Entity = ::gaia::ecs::Entity;

static Entity createEmpty(EntityManager& entities);
static void createEmptyBulk(EntityManager& entities, size_t nentities);

static Entity createSingle(EntityManager& entities);
static void createSingleBulk(EntityManager& entities, size_t nentities);

static Entity create(EntityManager& entities);
static void createBulk(EntityManager& entities, size_t nentities);

static Entity createMinimal(EntityManager& entities);
static void createMinimalBulk(EntityManager& entities, size_t nentities);

static void destroy(EntityManager& entities, Entity entity);
static void destroyBulk(EntityManager& entities, std::vector<Entity>& entities_vec);

//[[nodiscard]] static inline auto getEntitiesCount(EntityManager& entities) {
// return entities.size();
//}
template <class C>
[[nodiscard]] static inline auto getComponentCount(EntityManager& entities) {
auto query = entities.query().all<C>();
return query.count();
}

[[nodiscard]] static inline const ecs::benchmarks::base::components::PositionComponent&
getComponentOneConst(EntityManager& entities, Entity entity) {
Expand Down
15 changes: 10 additions & 5 deletions src/gaia-ecs/gaia-ecs/entities/HeroMonsterEntityFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ecs::benchmarks::gaia_ecs::entities {
HeroMonsterEntityFactory::Entity HeroMonsterEntityFactory::createRandom(EntityManager& entities) {
using namespace ecs::benchmarks::base::components;
auto ret = entities.add();
entities.bulk(ret)
entities.build(ret)
.add<ecs::benchmarks::base::components::PositionComponent>()
.add<ecs::benchmarks::base::components::PlayerComponent>()
.add<ecs::benchmarks::base::components::HealthComponent>()
Expand All @@ -18,7 +18,7 @@ HeroMonsterEntityFactory::Entity HeroMonsterEntityFactory::createRandom(EntityMa
HeroMonsterEntityFactory::Entity HeroMonsterEntityFactory::createHero(EntityManager& entities) {
using namespace ecs::benchmarks::base::components;
auto ret = entities.add();
entities.bulk(ret)
entities.build(ret)
.add<ecs::benchmarks::base::components::PositionComponent>()
.add<ecs::benchmarks::base::components::PlayerComponent>()
.add<ecs::benchmarks::base::components::HealthComponent>()
Expand All @@ -30,7 +30,7 @@ HeroMonsterEntityFactory::Entity HeroMonsterEntityFactory::createHero(EntityMana
HeroMonsterEntityFactory::Entity HeroMonsterEntityFactory::createMonster(EntityManager& entities) {
using namespace ecs::benchmarks::base::components;
auto ret = entities.add();
entities.bulk(ret)
entities.build(ret)
.add<ecs::benchmarks::base::components::PositionComponent>()
.add<ecs::benchmarks::base::components::PlayerComponent>()
.add<ecs::benchmarks::base::components::HealthComponent>()
Expand All @@ -41,12 +41,17 @@ HeroMonsterEntityFactory::Entity HeroMonsterEntityFactory::createMonster(EntityM
}

void HeroMonsterEntityFactory::addComponents(EntityManager& entities, Entity entity) {
entities.bulk(entity)
auto builder = entities.build(entity);
builder
.add<ecs::benchmarks::base::components::PlayerComponent>()
.add<ecs::benchmarks::base::components::HealthComponent>()
.add<ecs::benchmarks::base::components::DamageComponent>()
.add<ecs::benchmarks::base::components::PositionComponent>()
.add<ecs::benchmarks::base::components::SpriteComponent>();
if (!entities.has<ecs::benchmarks::base::components::PositionComponent>(entity)) {
builder.add<ecs::benchmarks::base::components::PositionComponent>();
}
builder.commit();

}

} // namespace ecs::benchmarks::gaia_ecs::entities
86 changes: 36 additions & 50 deletions src/gaia-ecs/gaia-ecs/entities/HeroMonsterEntityFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,57 @@

namespace ecs::benchmarks::gaia_ecs::entities {

class HeroMonsterEntityFactory {
class HeroMonsterEntityFactory final
: public ecs::benchmarks::base::entities::HeroMonsterEntityFactory<
::gaia::ecs::World, ::gaia::ecs::Entity, ecs::benchmarks::base::components::PositionComponent,
ecs::benchmarks::base::components::PlayerComponent, ecs::benchmarks::base::components::HealthComponent,
ecs::benchmarks::base::components::DamageComponent, ecs::benchmarks::base::components::SpriteComponent> {
public:
using EntityManager = ::gaia::ecs::World;
using Entity = ::gaia::ecs::Entity;
using BaseHeroMonsterEntityFactory = ecs::benchmarks::base::entities::HeroMonsterEntityFactory<
EntityManager, Entity, ecs::benchmarks::base::components::PositionComponent,
ecs::benchmarks::base::components::PlayerComponent, ecs::benchmarks::base::components::HealthComponent,
ecs::benchmarks::base::components::DamageComponent, ecs::benchmarks::base::components::SpriteComponent>;
// using EntityManager = ::gaia::ecs::World;
// using Entity = ::gaia::ecs::Entity;

Entity createRandom(EntityManager& entities) override;
Entity createHero(EntityManager& entities) override;
Entity createMonster(EntityManager& entities) override;

Entity createRandom(EntityManager& entities);
Entity createHero(EntityManager& entities);
Entity createMonster(EntityManager& entities);
[[nodiscard]] static inline bool valid(EntityManager& entities, Entity entity) { return entities.valid(entity); }

void addComponents(EntityManager& entities, Entity entity);
void addComponents(EntityManager& entities, Entity entity) override;

//[[nodiscard]] static inline auto getEntitiesCount(EntityManager& entities) {
// return entities.size();
//}

[[nodiscard]] inline const ecs::benchmarks::base::components::PositionComponent&
getPositionComponentConst(EntityManager& entities, Entity entity) {
return entities.get<ecs::benchmarks::base::components::PositionComponent>(entity);
template <class C>
[[nodiscard]] static inline auto getComponentCount(EntityManager& entities) {
auto query = entities.query().all<C>();
return query.count();
}

[[nodiscard]] inline const ecs::benchmarks::base::components::PlayerComponent&
getPlayerComponentConst(EntityManager& entities, Entity entity) {
return entities.get<ecs::benchmarks::base::components::PlayerComponent>(entity);
[[nodiscard]] inline ecs::benchmarks::base::components::PositionComponent&
getPositionComponent(EntityManager& entities, Entity entity) override {
return entities.set<ecs::benchmarks::base::components::PositionComponent>(entity);
}

[[nodiscard]] inline const ecs::benchmarks::base::components::HealthComponent&
getHealthComponentConst(EntityManager& entities, Entity entity) {
return entities.get<ecs::benchmarks::base::components::HealthComponent>(entity);
[[nodiscard]] inline ecs::benchmarks::base::components::PlayerComponent&
getPlayerComponent(EntityManager& entities, Entity entity) override {
return entities.set<ecs::benchmarks::base::components::PlayerComponent>(entity);
}

[[nodiscard]] inline const ecs::benchmarks::base::components::DamageComponent&
getDamageComponentConst(EntityManager& entities, Entity entity) {
return entities.get<ecs::benchmarks::base::components::DamageComponent>(entity);
[[nodiscard]] inline ecs::benchmarks::base::components::HealthComponent&
getHealthComponent(EntityManager& entities, Entity entity) override {
return entities.set<ecs::benchmarks::base::components::HealthComponent>(entity);
}

[[nodiscard]] inline const ecs::benchmarks::base::components::SpriteComponent&
getSpriteComponentConst(EntityManager& entities, Entity entity) {
return entities.get<ecs::benchmarks::base::components::SpriteComponent>(entity);
[[nodiscard]] inline ecs::benchmarks::base::components::DamageComponent&
getDamageComponent(EntityManager& entities, Entity entity) override {
return entities.set<ecs::benchmarks::base::components::DamageComponent>(entity);
}

ecs::benchmarks::base::components::PlayerType
initComponents(EntityManager& w, Entity e,
std::optional<ecs::benchmarks::base::components::PlayerType> opt_player_type = std::nullopt) {
using namespace ecs::benchmarks::base::components;

auto position = getPositionComponentConst(w, e);
auto player = getPlayerComponentConst(w, e);
auto health = getHealthComponentConst(w, e);
auto damage = getDamageComponentConst(w, e);
auto sprite = getSpriteComponentConst(w, e);

auto ret = BaseHeroMonsterEntityFactory::setComponents(position, player, health, damage, sprite, opt_player_type);

w.set(e)
.set<PositionComponent>(position)
.set<PlayerComponent>(player)
.set<HealthComponent>(health)
.set<DamageComponent>(damage)
.set<SpriteComponent>(sprite);
[[nodiscard]] inline ecs::benchmarks::base::components::SpriteComponent&
getSpriteComponent(EntityManager& entities, Entity entity) override {
return entities.set<ecs::benchmarks::base::components::SpriteComponent>(entity);
}

return ret;
[[nodiscard]] inline const ecs::benchmarks::base::components::PlayerComponent&
getPlayerComponentConst(EntityManager& entities, Entity entity) override {
return entities.get<ecs::benchmarks::base::components::PlayerComponent>(entity);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/gaia-ecs/gaia-ecs/entities/SoAEntityFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ SoAEntityFactory::Entity SoAEntityFactory::createSingle(EntityManager& entities)

SoAEntityFactory::Entity SoAEntityFactory::create(EntityManager& entities) {
auto e = entities.add();
entities.bulk(e)
entities.build(e)
.add<components::SoAPositionComponent>()
.add<components::SoAVelocityComponent>()
.add<ecs::benchmarks::base::components::DataComponent>();
Expand All @@ -27,7 +27,7 @@ SoAEntityFactory::Entity SoAEntityFactory::create(EntityManager& entities) {

SoAEntityFactory::Entity SoAEntityFactory::createMinimal(EntityManager& entities) {
auto e = entities.add();
entities.bulk(e).add<components::SoAPositionComponent>().add<components::SoAVelocityComponent>();
entities.build(e).add<components::SoAPositionComponent>().add<components::SoAVelocityComponent>();
return e;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ecs::benchmarks::gaia_ecs::entities {
SoAHeroMonsterEntityFactory::Entity SoAHeroMonsterEntityFactory::createRandom(EntityManager& entities) {
using namespace ecs::benchmarks::base::components;
auto ret = entities.add();
entities.bulk(ret)
entities.build(ret)
.add<components::SoAPositionComponent>()
.add<ecs::benchmarks::base::components::PlayerComponent>()
.add<ecs::benchmarks::base::components::HealthComponent>()
Expand All @@ -20,7 +20,7 @@ SoAHeroMonsterEntityFactory::Entity SoAHeroMonsterEntityFactory::createRandom(En
SoAHeroMonsterEntityFactory::Entity SoAHeroMonsterEntityFactory::createHero(EntityManager& entities) {
using namespace ecs::benchmarks::base::components;
auto ret = entities.add();
entities.bulk(ret)
entities.build(ret)
.add<components::SoAPositionComponent>()
.add<ecs::benchmarks::base::components::PlayerComponent>()
.add<ecs::benchmarks::base::components::HealthComponent>()
Expand All @@ -32,7 +32,7 @@ SoAHeroMonsterEntityFactory::Entity SoAHeroMonsterEntityFactory::createHero(Enti
SoAHeroMonsterEntityFactory::Entity SoAHeroMonsterEntityFactory::createMonster(EntityManager& entities) {
using namespace ecs::benchmarks::base::components;
auto ret = entities.add();
entities.bulk(ret)
entities.build(ret)
.add<components::SoAPositionComponent>()
.add<ecs::benchmarks::base::components::PlayerComponent>()
.add<ecs::benchmarks::base::components::HealthComponent>()
Expand All @@ -43,7 +43,7 @@ SoAHeroMonsterEntityFactory::Entity SoAHeroMonsterEntityFactory::createMonster(E
}

void SoAHeroMonsterEntityFactory::addComponents(EntityManager& entities, Entity entity) {
entities.bulk(entity)
entities.build(entity)
.add<ecs::benchmarks::base::components::PlayerComponent>()
.add<ecs::benchmarks::base::components::HealthComponent>()
.add<ecs::benchmarks::base::components::DamageComponent>()
Expand Down
Loading

0 comments on commit 07cc900

Please sign in to comment.