Compare commits
11 Commits
8174c19f00
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| dda6e25bf9 | |||
| 518995ddd1 | |||
| e8498f9e66 | |||
| 88f5d02b6a | |||
| 31ebef777b | |||
| cb9005bf80 | |||
| b457ecce47 | |||
| e64b8ed194 | |||
| c1efee5b4e | |||
| c36c3468bb | |||
| 6d9edc18b4 |
39
CMakeLists.txt
Normal file
39
CMakeLists.txt
Normal file
@@ -0,0 +1,39 @@
|
||||
# SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
|
||||
# SPDX-License-Identifier: MPL-2.0
|
||||
#
|
||||
# This Source Code Form is subject to the terms of the Mozilla Public License,
|
||||
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
# obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
project(Tourmaline VERSION 1)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address")
|
||||
endif()
|
||||
|
||||
include(GNUInstallDirs)
|
||||
|
||||
include_directories(headers)
|
||||
add_library(${PROJECT_NAME} SHARED
|
||||
"source/Systems/ECS/Component.cpp"
|
||||
"source/Systems/ECS/World.cpp"
|
||||
"source/Systems/Logging.cpp"
|
||||
"source/Systems/Random.cpp"
|
||||
"source/Types/UUID.cpp")
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
|
||||
|
||||
# Nothing to link right now
|
||||
target_link_libraries(${PROJECT_NAME})
|
||||
|
||||
install(
|
||||
TARGETS ${PROJECT_NAME}
|
||||
EXPORT ${PROJECT_NAME}Targets
|
||||
LIBRARY DESTINATION lib
|
||||
ARCHIVE DESTINATION lib
|
||||
RUNTIME DESTINATION bin)
|
||||
install(DIRECTORY headers/ DESTINATION include/${PROJECT_NAME})
|
||||
146
headers/Systems/ECS.hpp
Normal file
146
headers/Systems/ECS.hpp
Normal file
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public License,
|
||||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
#ifndef GUARD_TOURMALINE_ECS_H
|
||||
#define GUARD_TOURMALINE_ECS_H
|
||||
#include <any>
|
||||
#include <concepts>
|
||||
#include <format>
|
||||
#include <typeindex>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#include "../Types.hpp"
|
||||
#include "Logging.hpp"
|
||||
|
||||
namespace Tourmaline::Systems::ECS {
|
||||
using Entity = Tourmaline::Type::UUID;
|
||||
class World;
|
||||
|
||||
struct BaseComponent {
|
||||
public:
|
||||
virtual ~BaseComponent() = default;
|
||||
const Entity &GetOwner();
|
||||
|
||||
private:
|
||||
const Entity *owner;
|
||||
friend World;
|
||||
};
|
||||
|
||||
// Concepts
|
||||
template <typename T>
|
||||
concept Component = std::derived_from<T, BaseComponent>;
|
||||
|
||||
class World {
|
||||
public:
|
||||
// Entity
|
||||
Entity CreateEntity();
|
||||
bool EntityExists(const Entity &entity) noexcept;
|
||||
[[nodiscard("It is not guaranteed that an entity can always be destroyed, "
|
||||
"please make "
|
||||
"sure by checking the returned bool")]]
|
||||
bool DestroyEntity(Entity entity);
|
||||
|
||||
// Components
|
||||
template <Component T, typename... Args>
|
||||
T &AddComponent(const Entity &entity, Args &&...constructionArguments) {
|
||||
auto entityIter = GetEntityIterator(
|
||||
entity,
|
||||
std::format(
|
||||
"Cannot add component \"{}\"! Entity \"{}\" does not exist!",
|
||||
typeid(T).name(), entity.asString()),
|
||||
"AddComponent", Systems::Logging::LogLevel::Error);
|
||||
|
||||
auto [componentIter, success] = entityIter->second.try_emplace(
|
||||
typeid(T), T(std::forward<Args>(constructionArguments)...));
|
||||
Systems::Logging::Log(
|
||||
std::format("Cannot add component! Component \"{}\" already exists "
|
||||
"in entity \"{}\" ",
|
||||
typeid(T).name(), entity.asString()),
|
||||
"AddComponent", Systems::Logging::LogLevel::Error, !success);
|
||||
|
||||
T &component = std::any_cast<T &>(componentIter->second);
|
||||
component.owner = &entity;
|
||||
return component;
|
||||
}
|
||||
|
||||
template <Component T>
|
||||
[[nodiscard("Discarding an expensive operation's result!")]]
|
||||
T &GetComponent(const Entity &entity) {
|
||||
auto iter = GetEntityIterator(
|
||||
entity,
|
||||
std::format("Can't get entity \"{}\"'s component \"{}\", since "
|
||||
"entity does not exist!",
|
||||
entity.asString(), typeid(T).name()),
|
||||
"GetComponent", Systems::Logging::LogLevel::Error);
|
||||
|
||||
auto component = iter->second.find(typeid(T));
|
||||
Systems::Logging::Log(
|
||||
std::format(
|
||||
"Entity \"{}\" does not have component \"{}\", cannot get it!",
|
||||
entity.asString(), typeid(T).name()),
|
||||
"GetComponent", Systems::Logging::LogLevel::Error,
|
||||
component == iter->second.end());
|
||||
|
||||
return std::any_cast<T &>(component->second);
|
||||
}
|
||||
|
||||
template <Component T>
|
||||
[[nodiscard("Discarding an expensive operation's result!")]]
|
||||
bool HasComponent(const Entity &entity) {
|
||||
auto iter = GetEntityIterator(
|
||||
entity,
|
||||
std::format("Can't find if entity \"{}\" has component \"{}\", since "
|
||||
"entity does not exist!",
|
||||
entity.asString(), typeid(T).name()));
|
||||
|
||||
return iter != entityComponentList.end() &&
|
||||
(iter->second.find(typeid(T)) != iter->second.end());
|
||||
}
|
||||
|
||||
template <Component T>
|
||||
[[nodiscard("It is not guaranteed that a component can always be removed, "
|
||||
"please make "
|
||||
"sure by checking the returned bool")]]
|
||||
bool RemoveComponent(const Entity &entity) {
|
||||
auto entityIter = GetEntityIterator(
|
||||
entity,
|
||||
std::format("Cannot remove component {} from entity {}, since entity "
|
||||
"does not exist!",
|
||||
typeid(T).name(), entity.asString()),
|
||||
"RemoveComponent", Systems::Logging::LogLevel::Warning);
|
||||
if (entityIter == entityComponentList.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto componentIter = entityIter->second.find(typeid(T));
|
||||
if (componentIter == entityIter->second.end()) {
|
||||
Systems::Logging::Log(
|
||||
std::format("Cannot remove component {} from entity {}, since entity "
|
||||
"does not have that component",
|
||||
typeid(T).name(), entity.asString()),
|
||||
"RemoveComponent", Systems::Logging::LogLevel::Warning);
|
||||
return false;
|
||||
}
|
||||
|
||||
entityIter->second.erase(componentIter);
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<Entity, std::unordered_map<std::type_index, std::any>>
|
||||
entityComponentList{};
|
||||
|
||||
decltype(entityComponentList)::iterator
|
||||
GetEntityIterator(const Entity &entity, const std::string &errorMessage = "",
|
||||
const std::string &position = "",
|
||||
Tourmaline::Systems::Logging::LogLevel severity =
|
||||
Systems::Logging::LogLevel::Warning);
|
||||
};
|
||||
} // namespace Tourmaline::Systems::ECS
|
||||
#endif
|
||||
@@ -6,7 +6,8 @@
|
||||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#ifndef GUARD_TOURMALINE_LOGGING_H
|
||||
#define GUARD_TOURMALINE_LOGGING_H
|
||||
#include <array>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
@@ -17,9 +18,10 @@ public:
|
||||
enum class LogLevel {
|
||||
Critical = 0,
|
||||
Error = 1,
|
||||
Info = 2,
|
||||
Debug = 3,
|
||||
Trace = 4
|
||||
Warning = 2,
|
||||
Info = 3,
|
||||
Debug = 4,
|
||||
Trace = 5
|
||||
};
|
||||
|
||||
static void LogToFile(std::string File = "");
|
||||
@@ -29,6 +31,8 @@ public:
|
||||
|
||||
private:
|
||||
static std::fstream File;
|
||||
static std::array<const std::string, 5> LogLevelToString;
|
||||
static std::array<std::pair<const std::string, const std::string>, 6>
|
||||
LogLevelToString;
|
||||
};
|
||||
} // namespace Tourmaline::Systems
|
||||
#endif
|
||||
@@ -9,15 +9,18 @@
|
||||
#ifndef GUARD_TOURMALINE_TYPES_H
|
||||
#define GUARD_TOURMALINE_TYPES_H
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
namespace Tourmaline::Type {
|
||||
struct UUID {
|
||||
class UUID {
|
||||
public:
|
||||
constexpr static uint8_t BitLength = 128;
|
||||
constexpr static uint8_t QWORDLength = BitLength / 64;
|
||||
constexpr static uint8_t ByteLength = BitLength / 8;
|
||||
|
||||
[[nodiscard]]
|
||||
std::string asString() const;
|
||||
bool operator==(const UUID &rhs) const;
|
||||
|
||||
UUID(uint64_t firstHalf, uint64_t secondHalf);
|
||||
UUID(const std::string &uuid);
|
||||
@@ -30,6 +33,20 @@ struct UUID {
|
||||
|
||||
private:
|
||||
std::unique_ptr<uint64_t[]> data = std::make_unique<uint64_t[]>(QWORDLength);
|
||||
friend struct std::hash<Tourmaline::Type::UUID>;
|
||||
};
|
||||
} // namespace Tourmaline::Type
|
||||
|
||||
namespace std {
|
||||
template <> struct hash<Tourmaline::Type::UUID> {
|
||||
size_t operator()(const Tourmaline::Type::UUID &uuid) const noexcept {
|
||||
const auto data = uuid.data.get();
|
||||
size_t h1 = std::hash<uint64_t>{}(data[0]),
|
||||
h2 = std::hash<uint64_t>{}(data[1]);
|
||||
return h1 ^ (h2 << 1);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
|
||||
#endif
|
||||
@@ -73,7 +73,7 @@ static inline void jump_state(const int_t jump_table[4], rng_t &rng)
|
||||
int_t s3 = 0;
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
for (int b = 0; b < 8*sizeof(int_t); b++)
|
||||
for (int b = 0; b < static_cast<int>(8*sizeof(int_t)); b++) // static_cast has been added by Tourmaline Engine!
|
||||
{
|
||||
if (jump_table[i] & ((int_t)1) << b)
|
||||
{
|
||||
|
||||
14
source/Systems/ECS/Component.cpp
Normal file
14
source/Systems/ECS/Component.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public License,
|
||||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "../../../headers/Systems/ECS.hpp"
|
||||
|
||||
using namespace Tourmaline::Systems::ECS;
|
||||
|
||||
const Entity &BaseComponent::GetOwner() { return *this->owner; }
|
||||
55
source/Systems/ECS/World.cpp
Normal file
55
source/Systems/ECS/World.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public License,
|
||||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "../../../headers/Systems/ECS.hpp"
|
||||
#include "../../../headers/Systems/Random.hpp"
|
||||
|
||||
using namespace Tourmaline::Systems::ECS;
|
||||
|
||||
Entity World::CreateEntity() {
|
||||
auto [iterator, success] =
|
||||
entityComponentList.try_emplace(Systems::Random::GenerateUUID());
|
||||
|
||||
Systems::Logging::Log("Failed to create an entity! Possibly by incredible "
|
||||
"luck generated already existing UUID?",
|
||||
"CreateEntity", Systems::Logging::LogLevel::Critical,
|
||||
!success);
|
||||
return iterator->first;
|
||||
}
|
||||
|
||||
bool World::EntityExists(const Entity &entity) noexcept {
|
||||
return entityComponentList.find(entity) != entityComponentList.end();
|
||||
}
|
||||
|
||||
bool World::DestroyEntity(Entity entity) {
|
||||
auto entityIter = GetEntityIterator(
|
||||
entity,
|
||||
std::format("Cannot delete entity \"{}\", it does not exist!",
|
||||
entity.asString()),
|
||||
"DestroyEntity");
|
||||
|
||||
if (entityIter == entityComponentList.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
entityComponentList.erase(entityIter);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Very repetitive code
|
||||
decltype(World::entityComponentList)::iterator
|
||||
World::GetEntityIterator(const Entity &entity, const std::string &errorMessage,
|
||||
const std::string &position,
|
||||
Tourmaline::Systems::Logging::LogLevel severity) {
|
||||
auto iter = entityComponentList.find(entity);
|
||||
|
||||
Systems::Logging::Log(errorMessage, "GetEntityIterator/" + position, severity,
|
||||
iter == entityComponentList.end());
|
||||
return iter;
|
||||
}
|
||||
@@ -7,7 +7,8 @@
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "../Logging.hpp"
|
||||
#include "../../headers/Systems/Logging.hpp"
|
||||
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
@@ -17,13 +18,19 @@
|
||||
#include <print>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
using namespace Tourmaline::Systems;
|
||||
|
||||
// This is what happens when it takes you 50 years to implement
|
||||
// reflections to a language
|
||||
std::array<const std::string, 5> Logging::LogLevelToString{
|
||||
"Critical", "Error", "Info", "Debug", "Trace"};
|
||||
std::array<std::pair<const std::string, const std::string>, 6>
|
||||
Logging::LogLevelToString{std::pair{"Critical", "[0;31m"},
|
||||
{"Error", "[0;91m"},
|
||||
{"Warning", "[0;33m"},
|
||||
{"Info", "[0;37m"},
|
||||
{"Debug", "[0;92m"},
|
||||
{"Trace", "[0;36m"}};
|
||||
std::fstream Logging::File;
|
||||
|
||||
void Logging::LogToFile(std::string File) {
|
||||
@@ -42,18 +49,19 @@ void Logging::LogToFile(std::string File) {
|
||||
void Logging::Log(const std::string &message, const std::string &position,
|
||||
Logging::LogLevel severity, bool assertion) {
|
||||
if (assertion) [[likely]] {
|
||||
auto loglevelData =
|
||||
Logging::LogLevelToString[static_cast<size_t>(severity)];
|
||||
std::string output =
|
||||
std::format("[{}@{}] {}\n",
|
||||
Logging::LogLevelToString[static_cast<size_t>(severity)],
|
||||
position, message);
|
||||
std::format("[{}@{}] {}\n", loglevelData.first, position, message);
|
||||
|
||||
std::print("{}", output);
|
||||
std::print("\033{} {}\033[0m", loglevelData.second, output);
|
||||
if (Logging::File.is_open()) {
|
||||
Logging::File.write(output.c_str(), output.size());
|
||||
Logging::File.flush(); // Terrible but necessary sadly
|
||||
}
|
||||
|
||||
if (severity == Logging::LogLevel::Critical) {
|
||||
// Error and Critical
|
||||
if (severity < Logging::LogLevel::Warning) {
|
||||
throw std::runtime_error(output);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,8 @@
|
||||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
#include "../Random.hpp"
|
||||
#include "../../headers/Systems/Random.hpp"
|
||||
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
#include <ctime>
|
||||
@@ -7,7 +7,8 @@
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "../Types.hpp"
|
||||
#include "../../headers/Types.hpp"
|
||||
|
||||
#include <charconv>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
@@ -20,6 +21,16 @@ std::string UUID::asString() const {
|
||||
return std::format("{:016X}{:016X}", data[0], data[1]);
|
||||
}
|
||||
|
||||
bool UUID::operator==(const UUID &rhs) const {
|
||||
// Since size may be increased
|
||||
for (uint8_t index = 0; index < QWORDLength; index++) {
|
||||
if (this->data[index] != rhs.data[index]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
UUID::UUID(const UUID &uuid) {
|
||||
std::memcpy(data.get(), uuid.data.get(), UUID::ByteLength);
|
||||
}
|
||||
Reference in New Issue
Block a user