From d89827046ab6a653dab03ecbcd422d5f91740acf Mon Sep 17 00:00:00 2001 From: NukeBird Date: Thu, 13 Feb 2025 19:47:52 +0300 Subject: [PATCH] .clang-format --- .clang-format | 4 + dbc.cpp | 70 +++++++++-------- dbc.hpp | 50 ++++++++----- dinkyecs.hpp | 177 +++++++++++++++++++++++-------------------- main.cpp | 204 +++++++++++++++++++++++++++----------------------- point.hpp | 27 ++++--- 6 files changed, 295 insertions(+), 237 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..56e0e43 --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: LLVM +BreakBeforeBraces: Allman +AccessModifierOffset: -4 +IndentWidth: 4 diff --git a/dbc.cpp b/dbc.cpp index 482ed9b..60236e1 100644 --- a/dbc.cpp +++ b/dbc.cpp @@ -1,44 +1,54 @@ #include "dbc.hpp" #include -void dbc::log(const string &message) { - std::cerr << "!!!!!!!!!!" << message << std::endl; +void dbc::log(const string &message) +{ + std::cerr << "!!!!!!!!!!" << message << std::endl; } -void dbc::sentinel(const string &message) { - string err = fmt::format("[SENTINEL!] {}", message); - dbc::log(err); - throw dbc::SentinelError{err}; -} - -void dbc::pre(const string &message, bool test) { - if(!test) { - string err = fmt::format("[PRE!] {}", message); +void dbc::sentinel(const string &message) +{ + string err = fmt::format("[SENTINEL!] {}", message); dbc::log(err); - throw dbc::PreCondError{err}; - } + throw dbc::SentinelError{err}; } -void dbc::pre(const string &message, std::function tester) { - dbc::pre(message, tester()); +void dbc::pre(const string &message, bool test) +{ + if (!test) + { + string err = fmt::format("[PRE!] {}", message); + dbc::log(err); + throw dbc::PreCondError{err}; + } } -void dbc::post(const string &message, bool test) { - if(!test) { - string err = fmt::format("[POST!] {}", message); - dbc::log(err); - throw dbc::PostCondError{err}; - } +void dbc::pre(const string &message, std::function tester) +{ + dbc::pre(message, tester()); } -void dbc::post(const string &message, std::function tester) { - dbc::post(message, tester()); +void dbc::post(const string &message, bool test) +{ + if (!test) + { + string err = fmt::format("[POST!] {}", message); + dbc::log(err); + throw dbc::PostCondError{err}; + } } -void dbc::check(bool test, const string &message) { - if(!test) { - string err = fmt::format("[CHECK!] {}\n", message); - dbc::log(err); - throw dbc::CheckError{err}; - } -} \ No newline at end of file +void dbc::post(const string &message, std::function tester) +{ + dbc::post(message, tester()); +} + +void dbc::check(bool test, const string &message) +{ + if (!test) + { + string err = fmt::format("[CHECK!] {}\n", message); + dbc::log(err); + throw dbc::CheckError{err}; + } +} diff --git a/dbc.hpp b/dbc.hpp index 919d729..3392e70 100644 --- a/dbc.hpp +++ b/dbc.hpp @@ -1,29 +1,39 @@ #pragma once -#include #include #include +#include using std::string; -namespace dbc { - class Error { - public: - const string message; - Error(string m) : message{m} {} - Error(const char *m) : message{m} {} - }; +namespace dbc +{ +class Error +{ +public: + const string message; + Error(string m) : message{m} {} + Error(const char *m) : message{m} {} +}; - class CheckError : public Error {}; - class SentinelError : public Error {}; - class PreCondError : public Error {}; - class PostCondError : public Error {}; +class CheckError : public Error +{ +}; +class SentinelError : public Error +{ +}; +class PreCondError : public Error +{ +}; +class PostCondError : public Error +{ +}; - void log(const string &message); - void sentinel(const string &message); - void pre(const string &message, bool test); - void pre(const string &message, std::function tester); - void post(const string &message, bool test); - void post(const string &message, std::function tester); - void check(bool test, const string &message); -} +void log(const string &message); +void sentinel(const string &message); +void pre(const string &message, bool test); +void pre(const string &message, std::function tester); +void post(const string &message, bool test); +void post(const string &message, std::function tester); +void check(bool test, const string &message); +} // namespace dbc diff --git a/dinkyecs.hpp b/dinkyecs.hpp index c8dc3bc..946a0c0 100644 --- a/dinkyecs.hpp +++ b/dinkyecs.hpp @@ -1,156 +1,169 @@ #pragma once +#include "dbc.hpp" +#include #include +#include +#include #include #include #include -#include -#include -#include -#include "dbc.hpp" -namespace DinkyECS { - typedef unsigned long Entity; +namespace DinkyECS +{ +typedef unsigned long Entity; - using EntityMap = std::unordered_map; +using EntityMap = std::unordered_map; - struct Event { +struct Event +{ int event = 0; Entity entity = 0; std::any data; - }; +}; - typedef std::queue EventQueue; +typedef std::queue EventQueue; - struct World { +struct World +{ unsigned long entity_count = 0; std::unordered_map $components; std::unordered_map $facts; std::unordered_map $events; std::vector $constants; - Entity entity() { - return ++entity_count; - } + Entity entity() { return ++entity_count; } - void clone_into(DinkyECS::World &to_world) { - to_world.$constants = $constants; - to_world.$facts = $facts; - to_world.entity_count = entity_count; + void clone_into(DinkyECS::World &to_world) + { + to_world.$constants = $constants; + to_world.$facts = $facts; + to_world.entity_count = entity_count; - for(auto eid : $constants) { - for(const auto &[tid, eid_map] : $components) { - auto& their_map = to_world.$components[tid]; - if(eid_map.contains(eid)) { - their_map.insert_or_assign(eid, eid_map.at(eid)); - } + for (auto eid : $constants) + { + for (const auto &[tid, eid_map] : $components) + { + auto &their_map = to_world.$components[tid]; + if (eid_map.contains(eid)) + { + their_map.insert_or_assign(eid, eid_map.at(eid)); + } + } } - } } - void make_constant(DinkyECS::Entity entity) { + void make_constant(DinkyECS::Entity entity) + { $constants.push_back(entity); } - template - EntityMap& entity_map_for() { + template EntityMap &entity_map_for() + { return $components[std::type_index(typeid(Comp))]; - } + } - template - EventQueue& queue_map_for() { + template EventQueue &queue_map_for() + { return $events[std::type_index(typeid(Comp))]; - } + } - template - void remove(Entity ent) { + template void remove(Entity ent) + { EntityMap &map = entity_map_for(); map.erase(ent); - } + } - template - void set_the(Comp val) { + template void set_the(Comp val) + { $facts.insert_or_assign(std::type_index(typeid(Comp)), val); - } + } - template - Comp &get_the() { + template Comp &get_the() + { auto comp_id = std::type_index(typeid(Comp)); dbc::check($facts.contains(comp_id), - fmt::format("!!!! ATTEMPT to access world fact that hasn't been set yet: {}", typeid(Comp).name())); + fmt::format("!!!! ATTEMPT to access world fact that hasn't " + "been set yet: {}", + typeid(Comp).name())); // use .at to get std::out_of_range if fact not set std::any &res = $facts.at(comp_id); - return std::any_cast(res); - } + return std::any_cast(res); + } - template - bool has_the() { + template bool has_the() + { auto comp_id = std::type_index(typeid(Comp)); return $facts.contains(comp_id); - } + } - template - void set(Entity ent, Comp val) { + template void set(Entity ent, Comp val) + { EntityMap &map = entity_map_for(); map.insert_or_assign(ent, val); - } + } - template - Comp &get(Entity ent) { + template Comp &get(Entity ent) + { EntityMap &map = entity_map_for(); // use .at for bounds checking std::any &res = map.at(ent); - return std::any_cast(res); - } + return std::any_cast(res); + } - template - bool has(Entity ent) { + template bool has(Entity ent) + { EntityMap &map = entity_map_for(); return map.contains(ent); - } + } - template - void query(std::function cb) { + template + void query(std::function cb) + { EntityMap &map = entity_map_for(); - for(auto& [entity, any_comp] : map) { - Comp &res = std::any_cast(any_comp); - cb(entity, res); + for (auto &[entity, any_comp] : map) + { + Comp &res = std::any_cast(any_comp); + cb(entity, res); } - } + } - template - void query(std::function cb) { + template + void query(std::function cb) + { EntityMap &map_a = entity_map_for(); EntityMap &map_b = entity_map_for(); - for(auto& [entity, any_a] : map_a) { - if(map_b.contains(entity)) { - CompA &res_a = std::any_cast(any_a); - CompB &res_b = get(entity); - cb(entity, res_a, res_b); - } + for (auto &[entity, any_a] : map_a) + { + if (map_b.contains(entity)) + { + CompA &res_a = std::any_cast(any_a); + CompB &res_b = get(entity); + cb(entity, res_a, res_b); + } } - } + } - template - void send(Comp event, Entity entity, std::any data) { + template void send(Comp event, Entity entity, std::any data) + { EventQueue &queue = queue_map_for(); queue.push({event, entity, data}); - } + } - template - Event recv() { + template Event recv() + { EventQueue &queue = queue_map_for(); Event evt = queue.front(); queue.pop(); return evt; - } + } - template - bool has_event() { + template bool has_event() + { EventQueue &queue = queue_map_for(); return !queue.empty(); - } - }; -} + } +}; +} // namespace DinkyECS diff --git a/main.cpp b/main.cpp index d0fda46..3353c74 100644 --- a/main.cpp +++ b/main.cpp @@ -10,137 +10,155 @@ using namespace fmt; using DinkyECS::Entity; using std::string; -struct Player { - string name; - Entity eid; +struct Player +{ + string name; + Entity eid; }; -struct Position { - Point location; +struct Position +{ + Point location; }; // DINKY_HAS_COMPONENT(Point, x, y); // DINKY_HAS_COMPONENT(Position, location); -struct Motion { - int dx; - int dy; - bool random=false; +struct Motion +{ + int dx; + int dy; + bool random = false; }; // DINKY_HAS_COMPONENT(Motion, dx, dy, random); -struct Velocity { - double x, y; +struct Velocity +{ + double x, y; }; // DINKY_HAS_COMPONENT(Velocity, x, y); -struct Gravity { - double level; +struct Gravity +{ + double level; }; // DINKY_HAS_COMPONENT(Gravity, level); -struct DaGUI { - int event; +struct DaGUI +{ + int event; }; -void configure(DinkyECS::World &world, Entity &test) { - println("---Configuring the base system."); - Entity test2 = world.entity(); +void configure(DinkyECS::World &world, Entity &test) +{ + println("---Configuring the base system."); + Entity test2 = world.entity(); - world.set(test, {10,20}); - world.set(test, {1,2}); + world.set(test, {10, 20}); + world.set(test, {1, 2}); - world.set(test2, {1,1}); - world.set(test2, {9,19}); + world.set(test2, {1, 1}); + world.set(test2, {9, 19}); - println("---- Setting up the player as a fact in the system."); + println("---- Setting up the player as a fact in the system."); - auto player_eid = world.entity(); - Player player_info{"Zed", player_eid}; - // just set some player info as a fact with the entity id - world.set_the(player_info); + auto player_eid = world.entity(); + Player player_info{"Zed", player_eid}; + // just set some player info as a fact with the entity id + world.set_the(player_info); - world.set(player_eid, {0,0}); - world.set(player_eid, {0,0}); + world.set(player_eid, {0, 0}); + world.set(player_eid, {0, 0}); - auto enemy = world.entity(); - world.set(enemy, {0,0}); - world.set(enemy, {0,0}); + auto enemy = world.entity(); + world.set(enemy, {0, 0}); + world.set(enemy, {0, 0}); - println("--- Creating facts (singletons)"); - world.set_the({0.9}); + println("--- Creating facts (singletons)"); + world.set_the({0.9}); } -int main() { - DinkyECS::World world; - Entity test = world.entity(); +int main() +{ + DinkyECS::World world; + Entity test = world.entity(); - configure(world, test); + configure(world, test); - Position &pos = world.get(test); - REQUIRE(pos.location.x == 10); - REQUIRE(pos.location.y == 20); + Position &pos = world.get(test); + REQUIRE(pos.location.x == 10); + REQUIRE(pos.location.y == 20); - Velocity &vel = world.get(test); - REQUIRE(vel.x == 1); - REQUIRE(vel.y == 2); + Velocity &vel = world.get(test); + REQUIRE(vel.x == 1); + REQUIRE(vel.y == 2); - world.query([](const auto &ent, auto &pos) { - REQUIRE(ent > 0); - REQUIRE(pos.location.x >= 0); - REQUIRE(pos.location.y >= 0); - }); + world.query( + [](const auto &ent, auto &pos) + { + REQUIRE(ent > 0); + REQUIRE(pos.location.x >= 0); + REQUIRE(pos.location.y >= 0); + }); - world.query([](const auto &ent, auto &vel) { - REQUIRE(ent > 0); - REQUIRE(vel.x >= 0); - REQUIRE(vel.y >= 0); - }); + world.query( + [](const auto &ent, auto &vel) + { + REQUIRE(ent > 0); + REQUIRE(vel.x >= 0); + REQUIRE(vel.y >= 0); + }); - println("--- Manually get the velocity in position system:"); - world.query([&](const auto &ent, auto &pos) { - Velocity &vel = world.get(ent); + println("--- Manually get the velocity in position system:"); + world.query( + [&](const auto &ent, auto &pos) + { + Velocity &vel = world.get(ent); - REQUIRE(ent > 0); - REQUIRE(pos.location.x >= 0); - REQUIRE(pos.location.y >= 0); - REQUIRE(ent > 0); - REQUIRE(vel.x >= 0); - REQUIRE(vel.y >= 0); - }); + REQUIRE(ent > 0); + REQUIRE(pos.location.x >= 0); + REQUIRE(pos.location.y >= 0); + REQUIRE(ent > 0); + REQUIRE(vel.x >= 0); + REQUIRE(vel.y >= 0); + }); - println("--- Query only entities with Position and Velocity:"); - world.query([&](const auto &ent, auto &pos, auto &vel) { - Gravity &grav = world.get_the(); - REQUIRE(grav.level <= 1.0f); - REQUIRE(grav.level > 0.5f); - REQUIRE(ent > 0); - REQUIRE(pos.location.x >= 0); - REQUIRE(pos.location.y >= 0); - REQUIRE(ent > 0); - REQUIRE(vel.x >= 0); - REQUIRE(vel.y >= 0); - }); + println("--- Query only entities with Position and Velocity:"); + world.query( + [&](const auto &ent, auto &pos, auto &vel) + { + Gravity &grav = world.get_the(); + REQUIRE(grav.level <= 1.0f); + REQUIRE(grav.level > 0.5f); + REQUIRE(ent > 0); + REQUIRE(pos.location.x >= 0); + REQUIRE(pos.location.y >= 0); + REQUIRE(ent > 0); + REQUIRE(vel.x >= 0); + REQUIRE(vel.y >= 0); + }); - // now remove Velocity - REQUIRE(world.has(test)); - world.remove(test); - //REQUIRE_THROWS(world.get(test)); - REQUIRE(!world.has(test)); + // now remove Velocity + REQUIRE(world.has(test)); + world.remove(test); + // REQUIRE_THROWS(world.get(test)); + REQUIRE(!world.has(test)); - println("--- After remove test, should only result in test2:"); - world.query([&](const auto &ent, auto &pos, auto &vel) { - auto &in_position = world.get(ent); - auto &in_velocity = world.get(ent); - REQUIRE(pos.location.x >= 0); - REQUIRE(pos.location.y >= 0); - REQUIRE(in_position.location.x == pos.location.x); - REQUIRE(in_position.location.y == pos.location.y); - REQUIRE(in_velocity.x == vel.x); - REQUIRE(in_velocity.y == vel.y); - }); - return 0; + println("--- After remove test, should only result in test2:"); + world.query( + [&](const auto &ent, auto &pos, auto &vel) + { + auto &in_position = world.get(ent); + auto &in_velocity = world.get(ent); + REQUIRE(pos.location.x >= 0); + REQUIRE(pos.location.y >= 0); + REQUIRE(in_position.location.x == pos.location.x); + REQUIRE(in_position.location.y == pos.location.y); + REQUIRE(in_velocity.x == vel.x); + REQUIRE(in_velocity.y == vel.y); + }); + return 0; } diff --git a/point.hpp b/point.hpp index db754c0..526dfe0 100644 --- a/point.hpp +++ b/point.hpp @@ -1,21 +1,24 @@ - #pragma once #include -struct Point { - size_t x = 0; - size_t y = 0; +struct Point +{ + size_t x = 0; + size_t y = 0; - bool operator==(const Point& other) const { - return other.x == x && other.y == y; - } + bool operator==(const Point &other) const + { + return other.x == x && other.y == y; + } }; typedef std::vector PointList; -template<> struct std::hash { - size_t operator()(const Point& p) const { - auto hasher = std::hash(); - return hasher(p.x) ^ hasher(p.y); - } +template <> struct std::hash +{ + size_t operator()(const Point &p) const + { + auto hasher = std::hash(); + return hasher(p.x) ^ hasher(p.y); + } };