Change .clang-format

This commit is contained in:
NukeBird 2025-02-16 21:18:39 +03:00
parent 26353e423b
commit 123aa2b7dc
3 changed files with 92 additions and 104 deletions

View file

@ -3,3 +3,12 @@ BreakBeforeBraces: Allman
AccessModifierOffset: -4 AccessModifierOffset: -4
IndentWidth: 4 IndentWidth: 4
AlwaysBreakTemplateDeclarations: Yes AlwaysBreakTemplateDeclarations: Yes
NamespaceIndentation: All
SpaceAfterTemplateKeyword: false
PointerAlignment: Left
ReferenceAlignment: Left
SpaceAfterControlStatementKeyword: false
AllowShortFunctionsOnASingleLine: false
SpaceBeforeCtorInitializerColon: false
SpaceBeforeInheritanceColon: false
SpaceBeforeRangeBasedForLoopColon: false

View file

@ -1,5 +1,4 @@
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <concepts>
#include <string> #include <string>
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_MAIN
#include <catch2/catch_all.hpp> #include <catch2/catch_all.hpp>
@ -39,7 +38,6 @@ TEST_CASE("Entity #0 should be reserved and never used")
struct ChoosenOne struct ChoosenOne
{ {
}; };
TEST_CASE("Entity shouldn't have a component that wasn't attached to it") TEST_CASE("Entity shouldn't have a component that wasn't attached to it")
@ -60,7 +58,8 @@ TEST_CASE("Attempt of getting non-owned component should throw")
REQUIRE_THROWS(w.get<ChoosenOne>(e)); REQUIRE_THROWS(w.get<ChoosenOne>(e));
} }
TEST_CASE("Attach a simple component to an entity and verify it is correctly associated") TEST_CASE("Attach a simple component to an entity and verify it is correctly "
"associated")
{ {
world w; world w;
@ -80,7 +79,8 @@ struct Name
std::string value; std::string value;
}; };
TEST_CASE("Retrieve a component from an entity and verify its data matches what was set") TEST_CASE("Retrieve a component from an entity and verify its data matches "
"what was set")
{ {
world w; world w;
@ -94,7 +94,8 @@ TEST_CASE("Retrieve a component from an entity and verify its data matches what
REQUIRE(w.get<Name>(e).value == "super-zecsy!"); REQUIRE(w.get<Name>(e).value == "super-zecsy!");
} }
TEST_CASE("Remove a component from an entity and verify it is no longer attached") TEST_CASE(
"Remove a component from an entity and verify it is no longer attached")
{ {
world w; world w;
@ -149,7 +150,8 @@ TEST_CASE("Addresses of removed components should be reused")
} }
} }
TEST_CASE("Attach multiple components to an entity and verify all are correctly stored and retrievable") TEST_CASE("Attach multiple components to an entity and verify all are "
"correctly stored and retrievable")
{ {
world w; world w;
@ -165,7 +167,8 @@ TEST_CASE("Attach multiple components to an entity and verify all are correctly
REQUIRE_FALSE(w.has<Name>(e)); REQUIRE_FALSE(w.has<Name>(e));
} }
TEST_CASE("Create a simple system that processes entities with a specific component and verify it executes correctly") TEST_CASE("Create a simple system that processes entities with a specific "
"component and verify it executes correctly")
{ {
struct Component struct Component
{ {
@ -175,7 +178,7 @@ TEST_CASE("Create a simple system that processes entities with a specific compon
world w; world w;
auto e0 = w.make_entity(), e1 = w.make_entity(); auto e0 = w.make_entity(), e1 = w.make_entity();
w.set<Component>(e0); //or e0.set(Component{}) w.set<Component>(e0); // or e0.set(Component{})
w.set(e1, Component{20}); w.set(e1, Component{20});
REQUIRE(w.get<Component>(e0).value == 0); REQUIRE(w.get<Component>(e0).value == 0);
@ -185,16 +188,14 @@ TEST_CASE("Create a simple system that processes entities with a specific compon
* Really wanna deduce it to w.query([](Component&){}), * Really wanna deduce it to w.query([](Component&){}),
* but I have some troubles with it * but I have some troubles with it
*/ */
w.query<Component>([](Component& c) w.query<Component>([](Component& c) { c.value++; });
{
c.value++;
});
REQUIRE(w.get<Component>(e0).value == 1); REQUIRE(w.get<Component>(e0).value == 1);
REQUIRE(w.get<Component>(e1).value == 21); REQUIRE(w.get<Component>(e1).value == 21);
} }
TEST_CASE("Test a systems ability to query and process only entities with a specific combination of components") TEST_CASE("Test a systems ability to query and process only entities with a "
"specific combination of components")
{ {
struct C0 struct C0
{ {
@ -250,19 +251,13 @@ TEST_CASE("Systems execute at correct frequencies")
int slow_count = 0; int slow_count = 0;
// Add a fast system (60 Hz) // Add a fast system (60 Hz)
scheduler.add_system(60, [&](float dt) scheduler.add_system(60, [&](float dt) { fast_count++; });
{
fast_count++;
});
// Add a slow system (1 Hz) // Add a slow system (1 Hz)
scheduler.add_system(1, [&](float dt) scheduler.add_system(1, [&](float dt) { slow_count++; });
{
slow_count++;
});
// Simulate 2 seconds of updates at 120 FPS // Simulate 2 seconds of updates at 120 FPS
for (int i = 0; i < 240; ++i) for(int i = 0; i < 240; ++i)
{ {
scheduler.update(1.0f / 120.0f); scheduler.update(1.0f / 120.0f);
} }
@ -280,13 +275,10 @@ TEST_CASE("Systems handle zero-frequency gracefully")
int zero_count = 0; int zero_count = 0;
// Add a zero-frequency system (should never execute) // Add a zero-frequency system (should never execute)
scheduler.add_system(0, [&](float dt) scheduler.add_system(0, [&](float dt) { zero_count++; });
{
zero_count++;
});
// Simulate 1 second of updates at 60 FPS // Simulate 1 second of updates at 60 FPS
for (int i = 0; i < 60; ++i) for(int i = 0; i < 60; ++i)
{ {
scheduler.update(1.0f / 60.0f); scheduler.update(1.0f / 60.0f);
} }
@ -303,13 +295,10 @@ TEST_CASE("Systems handle varying update rates")
int varying_count = 0; int varying_count = 0;
// Add a system with varying frequency (10 Hz) // Add a system with varying frequency (10 Hz)
scheduler.add_system(10, [&](float dt) scheduler.add_system(10, [&](float dt) { varying_count++; });
{
varying_count++;
});
// Simulate 1 second of updates at 30 FPS // Simulate 1 second of updates at 30 FPS
for (int i = 0; i < 30; ++i) for(int i = 0; i < 30; ++i)
{ {
scheduler.update(1.0f / 30.0f); scheduler.update(1.0f / 30.0f);
} }
@ -326,10 +315,7 @@ TEST_CASE("Systems handle large time steps")
int large_step_count = 0; int large_step_count = 0;
// Add a system (1 Hz) // Add a system (1 Hz)
scheduler.add_system(1, [&](float dt) scheduler.add_system(1, [&](float dt) { large_step_count++; });
{
large_step_count++;
});
// Simulate a large time step (2 seconds) // Simulate a large time step (2 seconds)
scheduler.update(2.0f); scheduler.update(2.0f);
@ -353,7 +339,7 @@ TEST_CASE("Systems handle multiple frequencies")
scheduler.add_system(1, [&](float dt) { slow_count++; }); scheduler.add_system(1, [&](float dt) { slow_count++; });
// Simulate 1 second of updates at 120 FPS // Simulate 1 second of updates at 120 FPS
for (int i = 0; i < 120; ++i) for(int i = 0; i < 120; ++i)
{ {
scheduler.update(1.0f / 120.0f); scheduler.update(1.0f / 120.0f);
} }
@ -372,18 +358,16 @@ TEST_CASE("Systems handle fractional frequencies")
int fractional_count = 0; int fractional_count = 0;
// Add a system with fractional frequency (0.5 Hz) // Add a system with fractional frequency (0.5 Hz)
scheduler.add_system(0.5f, [&](float dt) scheduler.add_system(0.5f, [&](float dt) { fractional_count++; });
{
fractional_count++;
});
// Simulate 4 seconds of updates at 60 FPS // Simulate 4 seconds of updates at 60 FPS
for (int i = 0; i < 240; ++i) for(int i = 0; i < 240; ++i)
{ {
scheduler.update(1.0f / 60.0f); scheduler.update(1.0f / 60.0f);
} }
// Verify fractional-frequency system executes twice (0.5 Hz = 2 times in 4 seconds) // Verify fractional-frequency system executes twice (0.5 Hz = 2 times in 4
// seconds)
REQUIRE(fractional_count == 2); REQUIRE(fractional_count == 2);
} }
@ -395,10 +379,7 @@ TEST_CASE("Systems handle zero delta time")
int zero_dt_count = 0; int zero_dt_count = 0;
// Add a system (1 Hz) // Add a system (1 Hz)
scheduler.add_system(1, [&](float dt) scheduler.add_system(1, [&](float dt) { zero_dt_count++; });
{
zero_dt_count++;
});
// Simulate zero delta time // Simulate zero delta time
scheduler.update(0.0f); scheduler.update(0.0f);
@ -415,10 +396,7 @@ TEST_CASE("Systems handle negative delta time")
int count = 0; int count = 0;
// Add a system (1 Hz) // Add a system (1 Hz)
scheduler.add_system(1, [&](float dt) scheduler.add_system(1, [&](float dt) { count++; });
{
count++;
});
// Simulate negative delta time // Simulate negative delta time
scheduler.update(-1.0f); scheduler.update(-1.0f);

View file

@ -5,11 +5,10 @@
#include <format> #include <format>
#include <functional> #include <functional>
#include <queue> #include <queue>
#include <set>
#include <stdexcept> #include <stdexcept>
#include <typeindex> #include <typeindex>
#include <unordered_map> #include <unordered_map>
#include <unordered_set>
#include <set>
namespace zecsy namespace zecsy
{ {
@ -18,8 +17,8 @@ namespace zecsy
/* /*
* unordered_set is also an option. But probably sorting ids may be * unordered_set is also an option. But probably sorting ids may be
* beneficial for queries, because it might increase the chance of reusing * beneficial for queries, because it might increase the chance of reusing
* cpu cache (aka "required components were close to each other"). Definitely * cpu cache (aka "required components were close to each other").
* should play around with it * Definitely should play around with it
*/ */
using entities_set = std::set<entity_id>; using entities_set = std::set<entity_id>;
@ -53,19 +52,21 @@ namespace zecsy
template<typename First, typename Second, typename... Rest> template<typename First, typename Second, typename... Rest>
void remove(entity_id e); void remove(entity_id e);
private: private:
using comp_index = std::type_index; using comp_index = std::type_index;
using comp_to_entities_set = std::unordered_map<comp_index, entities_set>; using comp_to_entities_set =
std::unordered_map<comp_index, entities_set>;
using entity_to_index = std::unordered_map<entity_id, size_t>; using entity_to_index = std::unordered_map<entity_id, size_t>;
using comp_to_entity_dict = std::unordered_map<comp_index, using comp_to_entity_dict =
entity_to_index>; std::unordered_map<comp_index, entity_to_index>;
using comp_to_storage = std::unordered_map<comp_index, using comp_to_storage =
std::vector<uint8_t>>; std::unordered_map<comp_index, std::vector<uint8_t>>;
using comp_to_reusable_ids = std::unordered_map<comp_index, using comp_to_reusable_ids =
std::queue<size_t>>; std::unordered_map<comp_index, std::queue<size_t>>;
comp_to_entities_set entities_dict; comp_to_entities_set entities_dict;
comp_to_entity_dict indices_dict; comp_to_entity_dict indices_dict;
@ -81,6 +82,7 @@ namespace zecsy
void add_system(int freq, auto&& func); void add_system(int freq, auto&& func);
void update(float dt); void update(float dt);
private: private:
struct system_handler struct system_handler
{ {
@ -116,6 +118,7 @@ namespace zecsy
template<typename... T> template<typename... T>
void query(auto&& system); void query(auto&& system);
private: private:
entities_set alive_entities; entities_set alive_entities;
entity_id entity_counter = 0; entity_id entity_counter = 0;
@ -155,8 +158,8 @@ namespace zecsy
{ {
if(!has<T>(e)) if(!has<T>(e))
{ {
throw std::runtime_error(std::format("Entity #{} doesn't have {}", throw std::runtime_error(
e, typeid(T).name())); std::format("Entity #{} doesn't have {}", e, typeid(T).name()));
} }
auto index = indices_dict[typeid(T)].at(e); auto index = indices_dict[typeid(T)].at(e);
@ -262,7 +265,8 @@ namespace zecsy
} }
template<typename First, typename Second, typename... Rest> template<typename First, typename Second, typename... Rest>
inline void component_storage::set(entity_id e, const First& comp0, const Second& comp1, inline void component_storage::set(entity_id e, const First& comp0,
const Second& comp1,
const Rest&... rest_comps) const Rest&... rest_comps)
{ {
set(e, comp0); set(e, comp0);
@ -292,11 +296,8 @@ namespace zecsy
inline void system_scheduler::add_system(float freq, auto&& func) inline void system_scheduler::add_system(float freq, auto&& func)
{ {
systems.push_back({ systems.emplace_back(1.0f / freq, 0.0f,
1.0f / freq, std::forward<decltype(func)>(func));
0.0f,
std::forward<decltype(func)>(func)
});
} }
inline void system_scheduler::add_system(int freq, auto&& func) inline void system_scheduler::add_system(int freq, auto&& func)
@ -318,4 +319,4 @@ namespace zecsy
} }
} }
} }
}; }; // namespace zecsy