Skip to content

Commit

Permalink
[noirhero] Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
noirhero committed Aug 12, 2021
1 parent 955f88a commit 1875cba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
36 changes: 17 additions & 19 deletions Scenario/Scenario001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace {
struct TransformComponent : public Component {
glm::mat4 value = Math::Mat4::Identity;
};
struct LifeCycleComponent : public Component {
explicit LifeCycleComponent(float initValue) : value(initValue) {}
struct LifeComponent : public Component {
explicit LifeComponent(float initValue) : value(initValue) {}
float value = 0.0f;
};
struct Entity {
Expand All @@ -37,9 +37,9 @@ namespace {
public:
explicit System(Entities& entities) : _entities(entities) {
}
virtual ~System() = default;

virtual void ForEach(float delta, Entity& entity) = 0;

virtual void Run(float delta) {
for (auto* entity : _entities) {
if (std::ranges::any_of(_hashes, [entity](const auto eachHash)->bool {
Expand All @@ -54,41 +54,40 @@ namespace {

protected:
template<typename T>
T* Accept(Entity& entity) const {
__inline T* Accept(Entity& entity) const noexcept {
return reinterpret_cast<T*>(entity.components[typeid(T).hash_code()]);
}

Entities& _entities;
Entities& _entities;
std::vector<uint64_t> _hashes;
};

class PrintScreenSystem final : public System {
public:
PrintScreenSystem(Entities& entities, const Util::Timer& timer, float intervalTime) : System(entities), _timer(timer), _interval(intervalTime) {
explicit PrintScreenSystem(Entities& entities, const Util::Timer& timer, float intervalTime) : System(entities), _timer(timer), _interval(intervalTime) {
}
~PrintScreenSystem() {
~PrintScreenSystem() override {
Print();
fmt::print("Ratio FPS : {}\n", _ratioFrame);
}

void ForEach(float delta, Entity& entity) override {}
void Run(float delta) override {
_checkTime -= delta;
if (0.0f >= _checkTime) {
_checkTime = _interval;
_ratioFrame = 0 == _ratioFrame ? _timer.Frame() : (_ratioFrame + _timer.Frame()) / 2;
Print();
}
}

private:
void Print() {
void Print() {
system("cls");

fmt::print("Total entity count : {}\n", _entities.size());
fmt::print("Total time : {}\n", _timer.Total());
fmt::print("FPS : {}\n", _timer.Frame());

_ratioFrame = 0 == _ratioFrame ? _timer.Frame() : (_ratioFrame + _timer.Frame()) / 2;
fmt::print("Ratio FPS : {}\n", _ratioFrame);
}

const Util::Timer& _timer;
Expand Down Expand Up @@ -117,7 +116,7 @@ namespace {
entity->components.try_emplace(typeid(RotationComponent).hash_code(), new RotationComponent);
entity->components.try_emplace(typeid(TranslateComponent).hash_code(), new TranslateComponent);
entity->components.try_emplace(typeid(TransformComponent).hash_code(), new TransformComponent);
entity->components.try_emplace(typeid(LifeCycleComponent).hash_code(), new LifeCycleComponent{ Util::Random::Distribution(1.0f, 10.0f) });
entity->components.try_emplace(typeid(LifeComponent).hash_code(), new LifeComponent{ Util::Random::Distribution(_minLifeSeconds, _maxLifeSeconds) });
_entities.emplace(entity);
}
}
Expand All @@ -129,12 +128,12 @@ namespace {

class DestroyEntitySystem final : public System {
public:
DestroyEntitySystem(Entities& entities) : System(entities) {
_hashes.emplace_back(typeid(LifeCycleComponent).hash_code());
explicit DestroyEntitySystem(Entities& entities) : System(entities) {
_hashes.emplace_back(typeid(LifeComponent).hash_code());
}

void ForEach(float delta, Entity& entity) override {
auto* lifeCycle = Accept<LifeCycleComponent>(entity);
auto* lifeCycle = Accept<LifeComponent>(entity);
lifeCycle->value -= delta;
if (0.0f >= lifeCycle->value) {
_destroyEntities.emplace_back(&entity);
Expand All @@ -158,7 +157,7 @@ namespace {

class RotationSystem final : public System {
public:
RotationSystem(Entities& entities) : System(entities) {
explicit RotationSystem(Entities& entities) : System(entities) {
_hashes.emplace_back(typeid(RotationComponent).hash_code());
}

Expand All @@ -170,7 +169,7 @@ namespace {

class TransformSystem final : public System {
public:
TransformSystem(Entities& entities) : System(entities) {
explicit TransformSystem(Entities& entities) : System(entities) {
_hashes.emplace_back(typeid(ScaleComponent).hash_code());
_hashes.emplace_back(typeid(RotationComponent).hash_code());
_hashes.emplace_back(typeid(TranslateComponent).hash_code());
Expand All @@ -194,13 +193,12 @@ namespace Scenario {
Util::Timer timer;

PrintScreenSystem printScreenSystem(entities, timer, 1.0f);
CreateEntitySystem createSystem(entities, 100000, 1.0f, 10.0f);
CreateEntitySystem createSystem(entities, NumEntities, 1.0f, 10.0f);
DestroyEntitySystem destroySystem(entities);
RotationSystem rotateSystem(entities);
TransformSystem transformSystem(entities);

while (60.0f >= timer.Total()) {
//std::this_thread::sleep_for(std::chrono::milliseconds(1));
timer.Update();

printScreenSystem.Run(timer.Delta());
Expand Down
44 changes: 21 additions & 23 deletions Scenario/Scenario002.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace {
struct TransformComponent {
glm::mat4 value;
};
struct LifeCycleComponent {
struct LifeComponent {
float value;
};

Expand All @@ -29,7 +29,7 @@ namespace {
typeid(RotationComponent).hash_code(),
typeid(TranslateComponent).hash_code(),
typeid(TransformComponent).hash_code(),
typeid(LifeCycleComponent).hash_code(),
typeid(LifeComponent).hash_code(),
};
}
[[nodiscard]] constexpr ECS::HashSizePairs GetHashSizePairs() noexcept {
Expand All @@ -38,17 +38,16 @@ namespace {
{ typeid(RotationComponent).hash_code(), static_cast<ECS::Size>(sizeof(RotationComponent)) },
{ typeid(TranslateComponent).hash_code(), static_cast<ECS::Size>(sizeof(TranslateComponent)) },
{ typeid(TransformComponent).hash_code(), static_cast<ECS::Size>(sizeof(TransformComponent)) },
{ typeid(LifeCycleComponent).hash_code(), static_cast<ECS::Size>(sizeof(LifeCycleComponent)) },
{ typeid(LifeComponent).hash_code(), static_cast<ECS::Size>(sizeof(LifeComponent)) },
};
}
};

class PrintScreenSystem final : public ECS::System {
public:
explicit PrintScreenSystem(const Util::Timer& timer, float interval) : ECS::System({}), _timer(timer), _interval(interval) {
}
~PrintScreenSystem() override {
fmt::print("Ratio FPS : {}\n", _ratioFrame);
explicit PrintScreenSystem(const Util::Timer& timer, float interval)
: ECS::System({})
, _timer(timer), _interval(interval) {
}

void Run(ECS::Engine& ecsEngine, float delta) override {
Expand All @@ -58,18 +57,16 @@ namespace {
}
_checkTime += _interval;

_ratioFrame = 0 == _ratioFrame ? _timer.Frame() : (_ratioFrame + _timer.Frame()) / 2;

system("cls");

fmt::print("Total entity count : {}\n", ecsEngine.GetNumTotalEntity());
fmt::print("Total time : {}\n", _timer.Total());
fmt::print("FPS : {}\n", _timer.Frame());

_ratioFrame = 0 == _ratioFrame ? _timer.Frame() : (_ratioFrame + _timer.Frame()) / 2;
fmt::print("Ratio FPS : {}\n", _ratioFrame);
}

protected:
void ForEach(const ECS::Collector& collector, float delta) override {}

private:
const Util::Timer& _timer;
const float _interval;
Expand All @@ -79,7 +76,7 @@ namespace {

class CreateEntitySystem final : public ECS::System {
public:
explicit CreateEntitySystem(ECS::Engine& ecsEngine, uint32_t maxCount, float minLifeSeconds, float maxLifeSeconds)
explicit CreateEntitySystem(uint32_t maxCount, float minLifeSeconds, float maxLifeSeconds)
: ECS::System(ArchType::GetHashes())
, _maxCount(maxCount), _minLifeSeconds(minLifeSeconds), _maxLifeSeconds(maxLifeSeconds) {
}
Expand All @@ -89,11 +86,10 @@ namespace {
}

protected:
void ForEach(const ECS::Collector& collector, float delta) override {}
void CreateEntities(ECS::Engine& ecsEngine) const {
void CreateEntities(ECS::Engine& ecsEngine) const {
for (auto i = static_cast<decltype(_maxCount)>(ecsEngine.GetNumTotalEntity()); i < _maxCount; ++i) {
const auto& [scale, rotation, translation, transform, lifeCycle] =
Chunk::Accept<ScaleComponent, RotationComponent, TranslateComponent, TransformComponent, LifeCycleComponent>(ecsEngine.CreateEntity(_hashes)->Get(_hashes));
Chunk::Accept<ScaleComponent, RotationComponent, TranslateComponent, TransformComponent, LifeComponent>(ecsEngine.CreateEntity(_hashes)->Get(_hashes));

scale->value = Math::Vec3::One;
rotation->value = Math::Quat::Identity;
Expand All @@ -111,14 +107,16 @@ namespace {

class DestroyEntitySystem final : public ECS::System {
public:
DestroyEntitySystem(ECS::Engine& ecsEngine) : ECS::System({ typeid(LifeCycleComponent).hash_code() }), _ecsEngine(ecsEngine) {
explicit DestroyEntitySystem(ECS::Engine& ecsEngine)
: ECS::System({ typeid(LifeComponent).hash_code() })
, _ecsEngine(ecsEngine) {
}

protected:
void ForEach(const ECS::Collector& collector, float delta) override {
auto* lifeCycles = Accept<LifeCycleComponent>(collector);
auto* lifeCycles = Accept<LifeComponent>(collector);

for(std::remove_const<decltype(collector.count)>::type i = 0; i < collector.count; ++i) {
for(std::remove_const_t<decltype(collector.count)> i = 0; i < collector.count; ++i) {
lifeCycles[i].value -= delta;
if (0.0f >= lifeCycles[i].value) {
_ecsEngine.DestroyEntity(collector.handler, i);
Expand All @@ -137,7 +135,7 @@ namespace {

void ForEach(const ECS::Collector& collector, float delta) override {
auto* rotations = Accept<RotationComponent>(collector);
for(std::remove_const<decltype(collector.count)>::type i = 0; i < collector.count; ++i) {
for(std::remove_const_t<decltype(collector.count)> i = 0; i < collector.count; ++i) {
rotations[i].value = glm::rotate(rotations[i].value, delta, Math::Vec3::AxisY);
}
}
Expand All @@ -156,7 +154,8 @@ namespace {
void ForEach(const ECS::Collector& collector, float delta) override {
const auto& [scales, rotations, translations, transforms] =
Accept<ScaleComponent, RotationComponent, TranslateComponent, TransformComponent>(collector);
for(std::remove_const<decltype(collector.count)>::type i = 0; i < collector.count; ++i) {

for(std::remove_const_t<decltype(collector.count)> i = 0; i < collector.count; ++i) {
const auto scaleTm = glm::scale(Math::Mat4::Identity, scales[i].value);
const auto rotationTm = glm::toMat4(rotations[i].value);
const auto posTm = glm::translate(Math::Mat4::Identity, translations[i].value);
Expand All @@ -175,13 +174,12 @@ namespace Scenario {
Util::Timer timer;

PrintScreenSystem printScreenSystem(timer, 1.0f);
CreateEntitySystem createSystem(ecsEngine, 100000, 1.0f, 10.0f);
CreateEntitySystem createSystem(NumEntities, 1.0f, 10.0f);
DestroyEntitySystem destroySystem(ecsEngine);
RotationSystem rotationSystem;
TransformSystem transformSystem;

while(60.0f > timer.Total()) {
//std::this_thread::sleep_for(std::chrono::milliseconds(1));
timer.Update();

printScreenSystem.Run(ecsEngine, timer.Delta());
Expand Down
3 changes: 1 addition & 2 deletions pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#define GLM_FORCE_INLINE
#define GLM_FORCE_EXPLICIT_CTOR
#define GLM_FORCE_ALIGNED_GENTYPES
//#define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
#define GLM_FORCE_INTRINSICS
#include <glm/glm.hpp>
#include <glm/vec3.hpp>
Expand All @@ -40,4 +39,4 @@
#include "Mathmatics.h"
#include "Util.h"

using namespace std::string_literals;
constexpr uint32_t NumEntities = 50000;

0 comments on commit 1875cba

Please sign in to comment.