.clang-format

This commit is contained in:
NukeBird 2025-02-13 19:47:52 +03:00
parent 432673fa0e
commit d89827046a
6 changed files with 295 additions and 237 deletions

4
.clang-format Normal file
View file

@ -0,0 +1,4 @@
BasedOnStyle: LLVM
BreakBeforeBraces: Allman
AccessModifierOffset: -4
IndentWidth: 4

30
dbc.cpp
View file

@ -1,42 +1,52 @@
#include "dbc.hpp" #include "dbc.hpp"
#include <iostream> #include <iostream>
void dbc::log(const string &message) { void dbc::log(const string &message)
{
std::cerr << "!!!!!!!!!!" << message << std::endl; std::cerr << "!!!!!!!!!!" << message << std::endl;
} }
void dbc::sentinel(const string &message) { void dbc::sentinel(const string &message)
{
string err = fmt::format("[SENTINEL!] {}", message); string err = fmt::format("[SENTINEL!] {}", message);
dbc::log(err); dbc::log(err);
throw dbc::SentinelError{err}; throw dbc::SentinelError{err};
} }
void dbc::pre(const string &message, bool test) { void dbc::pre(const string &message, bool test)
if(!test) { {
if (!test)
{
string err = fmt::format("[PRE!] {}", message); string err = fmt::format("[PRE!] {}", message);
dbc::log(err); dbc::log(err);
throw dbc::PreCondError{err}; throw dbc::PreCondError{err};
} }
} }
void dbc::pre(const string &message, std::function<bool()> tester) { void dbc::pre(const string &message, std::function<bool()> tester)
{
dbc::pre(message, tester()); dbc::pre(message, tester());
} }
void dbc::post(const string &message, bool test) { void dbc::post(const string &message, bool test)
if(!test) { {
if (!test)
{
string err = fmt::format("[POST!] {}", message); string err = fmt::format("[POST!] {}", message);
dbc::log(err); dbc::log(err);
throw dbc::PostCondError{err}; throw dbc::PostCondError{err};
} }
} }
void dbc::post(const string &message, std::function<bool()> tester) { void dbc::post(const string &message, std::function<bool()> tester)
{
dbc::post(message, tester()); dbc::post(message, tester());
} }
void dbc::check(bool test, const string &message) { void dbc::check(bool test, const string &message)
if(!test) { {
if (!test)
{
string err = fmt::format("[CHECK!] {}\n", message); string err = fmt::format("[CHECK!] {}\n", message);
dbc::log(err); dbc::log(err);
throw dbc::CheckError{err}; throw dbc::CheckError{err};

26
dbc.hpp
View file

@ -1,23 +1,33 @@
#pragma once #pragma once
#include <string>
#include <fmt/core.h> #include <fmt/core.h>
#include <functional> #include <functional>
#include <string>
using std::string; using std::string;
namespace dbc { namespace dbc
class Error { {
class Error
{
public: public:
const string message; const string message;
Error(string m) : message{m} {} Error(string m) : message{m} {}
Error(const char *m) : message{m} {} Error(const char *m) : message{m} {}
}; };
class CheckError : public Error {}; class CheckError : public Error
class SentinelError : public Error {}; {
class PreCondError : public Error {}; };
class PostCondError : public Error {}; class SentinelError : public Error
{
};
class PreCondError : public Error
{
};
class PostCondError : public Error
{
};
void log(const string &message); void log(const string &message);
void sentinel(const string &message); void sentinel(const string &message);
@ -26,4 +36,4 @@ namespace dbc {
void post(const string &message, bool test); void post(const string &message, bool test);
void post(const string &message, std::function<bool()> tester); void post(const string &message, std::function<bool()> tester);
void check(bool test, const string &message); void check(bool test, const string &message);
} } // namespace dbc

View file

@ -1,20 +1,22 @@
#pragma once #pragma once
#include "dbc.hpp"
#include <any>
#include <functional> #include <functional>
#include <queue>
#include <tuple>
#include <typeindex> #include <typeindex>
#include <typeinfo> #include <typeinfo>
#include <unordered_map> #include <unordered_map>
#include <any>
#include <tuple>
#include <queue>
#include "dbc.hpp"
namespace DinkyECS { namespace DinkyECS
{
typedef unsigned long Entity; typedef unsigned long Entity;
using EntityMap = std::unordered_map<Entity, std::any>; using EntityMap = std::unordered_map<Entity, std::any>;
struct Event { struct Event
{
int event = 0; int event = 0;
Entity entity = 0; Entity entity = 0;
std::any data; std::any data;
@ -22,110 +24,121 @@ namespace DinkyECS {
typedef std::queue<Event> EventQueue; typedef std::queue<Event> EventQueue;
struct World { struct World
{
unsigned long entity_count = 0; unsigned long entity_count = 0;
std::unordered_map<std::type_index, EntityMap> $components; std::unordered_map<std::type_index, EntityMap> $components;
std::unordered_map<std::type_index, std::any> $facts; std::unordered_map<std::type_index, std::any> $facts;
std::unordered_map<std::type_index, EventQueue> $events; std::unordered_map<std::type_index, EventQueue> $events;
std::vector<Entity> $constants; std::vector<Entity> $constants;
Entity entity() { Entity entity() { return ++entity_count; }
return ++entity_count;
}
void clone_into(DinkyECS::World &to_world) { void clone_into(DinkyECS::World &to_world)
{
to_world.$constants = $constants; to_world.$constants = $constants;
to_world.$facts = $facts; to_world.$facts = $facts;
to_world.entity_count = entity_count; to_world.entity_count = entity_count;
for(auto eid : $constants) { for (auto eid : $constants)
for(const auto &[tid, eid_map] : $components) { {
for (const auto &[tid, eid_map] : $components)
{
auto &their_map = to_world.$components[tid]; auto &their_map = to_world.$components[tid];
if(eid_map.contains(eid)) { if (eid_map.contains(eid))
{
their_map.insert_or_assign(eid, eid_map.at(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); $constants.push_back(entity);
} }
template <typename Comp> template <typename Comp> EntityMap &entity_map_for()
EntityMap& entity_map_for() { {
return $components[std::type_index(typeid(Comp))]; return $components[std::type_index(typeid(Comp))];
} }
template <typename Comp> template <typename Comp> EventQueue &queue_map_for()
EventQueue& queue_map_for() { {
return $events[std::type_index(typeid(Comp))]; return $events[std::type_index(typeid(Comp))];
} }
template <typename Comp> template <typename Comp> void remove(Entity ent)
void remove(Entity ent) { {
EntityMap &map = entity_map_for<Comp>(); EntityMap &map = entity_map_for<Comp>();
map.erase(ent); map.erase(ent);
} }
template <typename Comp> template <typename Comp> void set_the(Comp val)
void set_the(Comp val) { {
$facts.insert_or_assign(std::type_index(typeid(Comp)), val); $facts.insert_or_assign(std::type_index(typeid(Comp)), val);
} }
template <typename Comp> template <typename Comp> Comp &get_the()
Comp &get_the() { {
auto comp_id = std::type_index(typeid(Comp)); auto comp_id = std::type_index(typeid(Comp));
dbc::check($facts.contains(comp_id), 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 // use .at to get std::out_of_range if fact not set
std::any &res = $facts.at(comp_id); std::any &res = $facts.at(comp_id);
return std::any_cast<Comp &>(res); return std::any_cast<Comp &>(res);
} }
template <typename Comp> template <typename Comp> bool has_the()
bool has_the() { {
auto comp_id = std::type_index(typeid(Comp)); auto comp_id = std::type_index(typeid(Comp));
return $facts.contains(comp_id); return $facts.contains(comp_id);
} }
template <typename Comp> template <typename Comp> void set(Entity ent, Comp val)
void set(Entity ent, Comp val) { {
EntityMap &map = entity_map_for<Comp>(); EntityMap &map = entity_map_for<Comp>();
map.insert_or_assign(ent, val); map.insert_or_assign(ent, val);
} }
template <typename Comp> template <typename Comp> Comp &get(Entity ent)
Comp &get(Entity ent) { {
EntityMap &map = entity_map_for<Comp>(); EntityMap &map = entity_map_for<Comp>();
// use .at for bounds checking // use .at for bounds checking
std::any &res = map.at(ent); std::any &res = map.at(ent);
return std::any_cast<Comp &>(res); return std::any_cast<Comp &>(res);
} }
template <typename Comp> template <typename Comp> bool has(Entity ent)
bool has(Entity ent) { {
EntityMap &map = entity_map_for<Comp>(); EntityMap &map = entity_map_for<Comp>();
return map.contains(ent); return map.contains(ent);
} }
template <typename Comp> template <typename Comp>
void query(std::function<void(const Entity&, Comp&)> cb) { void query(std::function<void(const Entity &, Comp &)> cb)
{
EntityMap &map = entity_map_for<Comp>(); EntityMap &map = entity_map_for<Comp>();
for(auto& [entity, any_comp] : map) { for (auto &[entity, any_comp] : map)
{
Comp &res = std::any_cast<Comp &>(any_comp); Comp &res = std::any_cast<Comp &>(any_comp);
cb(entity, res); cb(entity, res);
} }
} }
template <typename CompA, typename CompB> template <typename CompA, typename CompB>
void query(std::function<void(const Entity&, CompA&, CompB&)> cb) { void query(std::function<void(const Entity &, CompA &, CompB &)> cb)
{
EntityMap &map_a = entity_map_for<CompA>(); EntityMap &map_a = entity_map_for<CompA>();
EntityMap &map_b = entity_map_for<CompB>(); EntityMap &map_b = entity_map_for<CompB>();
for(auto& [entity, any_a] : map_a) { for (auto &[entity, any_a] : map_a)
if(map_b.contains(entity)) { {
if (map_b.contains(entity))
{
CompA &res_a = std::any_cast<CompA &>(any_a); CompA &res_a = std::any_cast<CompA &>(any_a);
CompB &res_b = get<CompB>(entity); CompB &res_b = get<CompB>(entity);
cb(entity, res_a, res_b); cb(entity, res_a, res_b);
@ -133,24 +146,24 @@ namespace DinkyECS {
} }
} }
template<typename Comp> template <typename Comp> void send(Comp event, Entity entity, std::any data)
void send(Comp event, Entity entity, std::any data) { {
EventQueue &queue = queue_map_for<Comp>(); EventQueue &queue = queue_map_for<Comp>();
queue.push({event, entity, data}); queue.push({event, entity, data});
} }
template<typename Comp> template <typename Comp> Event recv()
Event recv() { {
EventQueue &queue = queue_map_for<Comp>(); EventQueue &queue = queue_map_for<Comp>();
Event evt = queue.front(); Event evt = queue.front();
queue.pop(); queue.pop();
return evt; return evt;
} }
template<typename Comp> template <typename Comp> bool has_event()
bool has_event() { {
EventQueue &queue = queue_map_for<Comp>(); EventQueue &queue = queue_map_for<Comp>();
return !queue.empty(); return !queue.empty();
} }
}; };
} } // namespace DinkyECS

View file

@ -10,19 +10,22 @@ using namespace fmt;
using DinkyECS::Entity; using DinkyECS::Entity;
using std::string; using std::string;
struct Player { struct Player
{
string name; string name;
Entity eid; Entity eid;
}; };
struct Position { struct Position
{
Point location; Point location;
}; };
// DINKY_HAS_COMPONENT(Point, x, y); // DINKY_HAS_COMPONENT(Point, x, y);
// DINKY_HAS_COMPONENT(Position, location); // DINKY_HAS_COMPONENT(Position, location);
struct Motion { struct Motion
{
int dx; int dx;
int dy; int dy;
bool random = false; bool random = false;
@ -30,23 +33,27 @@ struct Motion {
// DINKY_HAS_COMPONENT(Motion, dx, dy, random); // DINKY_HAS_COMPONENT(Motion, dx, dy, random);
struct Velocity { struct Velocity
{
double x, y; double x, y;
}; };
// DINKY_HAS_COMPONENT(Velocity, x, y); // DINKY_HAS_COMPONENT(Velocity, x, y);
struct Gravity { struct Gravity
{
double level; double level;
}; };
// DINKY_HAS_COMPONENT(Gravity, level); // DINKY_HAS_COMPONENT(Gravity, level);
struct DaGUI { struct DaGUI
{
int event; int event;
}; };
void configure(DinkyECS::World &world, Entity &test) { void configure(DinkyECS::World &world, Entity &test)
{
println("---Configuring the base system."); println("---Configuring the base system.");
Entity test2 = world.entity(); Entity test2 = world.entity();
@ -74,7 +81,8 @@ void configure(DinkyECS::World &world, Entity &test) {
world.set_the<Gravity>({0.9}); world.set_the<Gravity>({0.9});
} }
int main() { int main()
{
DinkyECS::World world; DinkyECS::World world;
Entity test = world.entity(); Entity test = world.entity();
@ -88,20 +96,26 @@ int main() {
REQUIRE(vel.x == 1); REQUIRE(vel.x == 1);
REQUIRE(vel.y == 2); REQUIRE(vel.y == 2);
world.query<Position>([](const auto &ent, auto &pos) { world.query<Position>(
[](const auto &ent, auto &pos)
{
REQUIRE(ent > 0); REQUIRE(ent > 0);
REQUIRE(pos.location.x >= 0); REQUIRE(pos.location.x >= 0);
REQUIRE(pos.location.y >= 0); REQUIRE(pos.location.y >= 0);
}); });
world.query<Velocity>([](const auto &ent, auto &vel) { world.query<Velocity>(
[](const auto &ent, auto &vel)
{
REQUIRE(ent > 0); REQUIRE(ent > 0);
REQUIRE(vel.x >= 0); REQUIRE(vel.x >= 0);
REQUIRE(vel.y >= 0); REQUIRE(vel.y >= 0);
}); });
println("--- Manually get the velocity in position system:"); println("--- Manually get the velocity in position system:");
world.query<Position>([&](const auto &ent, auto &pos) { world.query<Position>(
[&](const auto &ent, auto &pos)
{
Velocity &vel = world.get<Velocity>(ent); Velocity &vel = world.get<Velocity>(ent);
REQUIRE(ent > 0); REQUIRE(ent > 0);
@ -113,7 +127,9 @@ int main() {
}); });
println("--- Query only entities with Position and Velocity:"); println("--- Query only entities with Position and Velocity:");
world.query<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) { world.query<Position, Velocity>(
[&](const auto &ent, auto &pos, auto &vel)
{
Gravity &grav = world.get_the<Gravity>(); Gravity &grav = world.get_the<Gravity>();
REQUIRE(grav.level <= 1.0f); REQUIRE(grav.level <= 1.0f);
REQUIRE(grav.level > 0.5f); REQUIRE(grav.level > 0.5f);
@ -132,7 +148,9 @@ int main() {
REQUIRE(!world.has<Velocity>(test)); REQUIRE(!world.has<Velocity>(test));
println("--- After remove test, should only result in test2:"); println("--- After remove test, should only result in test2:");
world.query<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) { world.query<Position, Velocity>(
[&](const auto &ent, auto &pos, auto &vel)
{
auto &in_position = world.get<Position>(ent); auto &in_position = world.get<Position>(ent);
auto &in_velocity = world.get<Velocity>(ent); auto &in_velocity = world.get<Velocity>(ent);
REQUIRE(pos.location.x >= 0); REQUIRE(pos.location.x >= 0);

View file

@ -1,20 +1,23 @@
#pragma once #pragma once
#include <vector> #include <vector>
struct Point { struct Point
{
size_t x = 0; size_t x = 0;
size_t y = 0; size_t y = 0;
bool operator==(const Point& other) const { bool operator==(const Point &other) const
{
return other.x == x && other.y == y; return other.x == x && other.y == y;
} }
}; };
typedef std::vector<Point> PointList; typedef std::vector<Point> PointList;
template<> struct std::hash<Point> { template <> struct std::hash<Point>
size_t operator()(const Point& p) const { {
size_t operator()(const Point &p) const
{
auto hasher = std::hash<int>(); auto hasher = std::hash<int>();
return hasher(p.x) ^ hasher(p.y); return hasher(p.x) ^ hasher(p.y);
} }