Browse Source

Initial commit

master
czar 6 months ago
commit
94ba7e5912
Signed by: czar
GPG Key ID: 2DBA9558E9277C37
  1. 16
      CMakeLists.txt
  2. 9
      COPYING
  3. 11
      README.org
  4. 14
      includes/kst/dgs/components/component.hpp
  5. 35
      includes/kst/dgs/core.hpp
  6. 27
      includes/kst/dgs/entity.hpp
  7. 106
      includes/kst/dgs/registry.hpp
  8. 20
      includes/kst/dgs/systems/system.hpp

16
CMakeLists.txt

@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.20)
set(CMAKE_CXX_STANDARD 20)
project(kst_dgs)
find_package(Boost REQUIRED)
add_library(
${PROJECT_NAME} INTERFACE
includes/kst/dgs/entity.hpp
includes/kst/dgs/core.hpp
includes/kst/dgs/components/component.hpp
includes/kst/dgs/systems/system.hpp
includes/kst/dgs/registry.hpp
)
target_include_directories(${PROJECT_NAME} INTERFACE includes)

9
COPYING

@ -0,0 +1,9 @@
Discordian Public License (DPL)
All Rights Reversed Ⓚ 3187 czar <czar@kalli.st>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

11
README.org

@ -0,0 +1,11 @@
#+title: KST.DGS
#+author: [k-] czar
#+email: czar@kalli.st
#+options: toc:nil
Header-only C++ ECS library.
* License
[[file:COPYING][Discordian Public License]]
All Rights Reversed Ⓚ

14
includes/kst/dgs/components/component.hpp

@ -0,0 +1,14 @@
#ifndef KST_DGS_COMPONENT_BASE_HPP
#define KST_DGS_COMPONENT_BASE_HPP
#include "../core.hpp"
namespace kst::dgs::components{
struct component {
};
template<class T>
concept Component = std::is_base_of<components::component, T>::value;
}
#endif //KST_DGS_COMPONENT_BASE_HPP

35
includes/kst/dgs/core.hpp

@ -0,0 +1,35 @@
#ifndef KST_DGS_CORE_HPP
#define KST_DGS_CORE_HPP
#include <bitset>
#include <map>
#include <set>
#include <chrono>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
namespace kst::dgs {
class unique_identifier : public boost::uuids::uuid
{
public:
unique_identifier()
: boost::uuids::uuid(boost::uuids::random_generator()())
{}
auto to_string(){
return boost::uuids::to_string(*this);
}
explicit unique_identifier(boost::uuids::uuid const& u)
: boost::uuids::uuid(u) {}
};
using identifier = size_t;
using deltatick = std::chrono::duration<long double, std::ratio<1>>;
const identifier MAX_COMPONENTS = 64;
using signature = std::bitset<MAX_COMPONENTS>;
}
#endif //KST_DGS_CORE_HPP

27
includes/kst/dgs/entity.hpp

@ -0,0 +1,27 @@
#ifndef KST_DGS_ENTITY_HPP
#define KST_DGS_ENTITY_HPP
#include "core.hpp"
#include <boost/uuid/random_generator.hpp>
namespace kst::dgs {
class entity {
const unique_identifier _id;
public:
entity(): _id() {}
public:
unique_identifier get_id() {
return _id;
}
signature get_signature() {
return _signature;
}
void set_signature(auto id){
_signature.set(id);
}
protected:
signature _signature;
};
}
#endif //KST_DGS_ENTITY_HPP

106
includes/kst/dgs/registry.hpp

@ -0,0 +1,106 @@
#ifndef KST_DGS_REGISTRY_HPP
#define KST_DGS_REGISTRY_HPP
#include <memory>
#include <unordered_set>
#include <ranges>
#include <unordered_map>
#include "core.hpp"
#include "components/component.hpp"
#include "systems/system.hpp"
#include "entity.hpp"
#include <boost/functional/hash.hpp>
namespace kst::dgs{
template<components::Component C>
using component_collection = std::unordered_map<dgs::unique_identifier, std::shared_ptr<C>, boost::hash<dgs::unique_identifier>>;
class registry {
static std::unordered_set<std::shared_ptr<dgs::systems::system>> _systems;
static inline std::set<std::shared_ptr<dgs::entity>> _entities;
static inline dgs::identifier _component_count = 0;
public:
static std::shared_ptr<dgs::entity> create_entity(){
std::shared_ptr<dgs::entity> entity = std::make_shared<dgs::entity>();
_entities.insert(entity);
return entity;
};
static void add_components(const std::shared_ptr<dgs::entity>& entity){
};
template<components::Component C, components::Component...Components>
static void add_components(const std::shared_ptr<dgs::entity>& entity, C&& component, Components&&... fwd){
auto components = get<C>();
entity->set_signature(get_id<C>());
components->insert({entity->get_id(), std::make_shared<C>(std::forward<C>(component))});
add_components(entity, std::forward<Components>(fwd)...);
};
template<systems::System S>
static void add_dependencies(){}
template<systems::System S, components::Component C, components::Component...Components>
static void add_dependencies(){
auto system = get<S>();
system->set_signature(get_id<C>());
add_dependencies<S,Components...>();
}
template<components::Component C>
static auto get_component(const std::shared_ptr<dgs::entity>& entity){
auto components = get<C>();
return components->find(entity->get_id())->second;
}
template<systems::System S>
static auto get_entities(){
auto system = get<S>();
return _entities | std::views::filter([system](auto v){ return test_signature(v->get_signature(),system->get_signature());});
}
static bool test_signature(signature key, signature target){
for(int i; i < key.size(); i++)
if (key.test(i))
if(!target.test(i))
return false;
return true;
}
template<components::Component C>
static void add() {
get<C>();
}
template<components::Component C>
static auto get(){
static auto components = std::make_shared<component_collection<C>>();
return components;
}
template<components::Component C>
static identifier get_id(){
static identifier id = _component_count++;
return id;
}
template<systems::System S>
static auto get(){
static auto system = std::make_shared<S>();
return system;
};
template<systems::System S>
static dgs::signature get_signature(){
static dgs::signature signature(get_id<S>());
return signature;
}
} static engine;
}
#endif //KST_DGS_REGISTRY_HPP

20
includes/kst/dgs/systems/system.hpp

@ -0,0 +1,20 @@
#ifndef KST_DGS_SYSTEM_BASE_HPP
#define KST_DGS_SYSTEM_BASE_HPP
namespace kst::dgs::systems {
class system {
protected:
dgs::signature _signature;
public:
signature get_signature(){
return _signature;
}
void set_signature(auto id){
_signature.set(id);
}
};
template<class T>
concept System = std::is_base_of<dgs::systems::system, T>::value;
}
#endif //KST_DGS_SYSTEM_BASE_HPP
Loading…
Cancel
Save