Switched to Corrade's string over STL

This commit is contained in:
2026-03-07 14:44:29 +02:00
parent 55f12fcd69
commit 88d4695596
6 changed files with 85 additions and 79 deletions

View File

@@ -10,64 +10,60 @@
#ifndef GUARD_TOURMALINE_ECS_H
#define GUARD_TOURMALINE_ECS_H
#include <any>
#include <format>
#include <typeindex>
#include "../Containers/DualkeyMap.hpp"
#include "../Containers/Hashmap.hpp"
#include "../Types.hpp"
#include "ECS/BuiltinComponents.hpp"
#include "Logging.hpp"
namespace Tourmaline::Systems::ECS {
using Entity = Tourmaline::Type::UUID;
class World;
using System = Tourmaline::Type::UUID;
class World {
public:
World() {}
// ====== World controls ======
void Step();
// ======== Entities ========
[[nodiscard]]
Entity CreateEntity();
[[nodiscard("Pointless call of EntityExists")]]
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 <isAComponent component, typename... Args>
component &AddComponent(const Entity &entity,
Args &&...constructionArguments) {
auto newComponent = entityComponentMap.Insert(
entity, typeid(component), component(constructionArguments...));
template <isAComponent Component, typename... ComponentArgs>
Component &AddComponent(const Entity &entity, ComponentArgs &&...args) {
auto newComponent = entityComponentMap.Insert(entity, typeid(Component),
Component(args...));
return std::any_cast<component &>(std::get<2>(newComponent));
return std::any_cast<Component &>(std::get<2>(newComponent));
}
template <isAComponent component>
[[nodiscard("Discarding an expensive operation's result!")]]
component &GetComponent(const Entity &entity) {
auto result = entityComponentMap.Query(entity, typeid(component));
template <isAComponent Component>
[[nodiscard("Pointless call of GetComponent")]]
Component &GetComponent(const Entity &entity) {
auto result = entityComponentMap.Query(entity, typeid(Component));
if (result.empty()) {
Logging::Log(std::format("Entity {} does not have component {}!",
entity.asString(), typeid(component).name()),
"ECS/GetComponent", Logging::LogLevel::Error);
Logging::LogFormatted("Entity {} does not have component {}!",
"ECS/GetComponent", Logging::LogLevel::Error,
entity.asString(), typeid(Component).name());
}
return std::any_cast<component &>(result.begin()->second);
return std::any_cast<Component &>(result.begin()->second);
}
template <isAComponent component>
[[nodiscard("Discarding an expensive operation's result!")]]
template <isAComponent Component>
[[nodiscard("Pointless call of HasComponent")]]
bool HasComponent(const Entity &entity) {
return entityComponentMap.Query(entity, typeid(component)).size();
return entityComponentMap.Query(entity, typeid(Component)).size();
}
template <isAComponent component>
[[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) {
return entityComponentMap.Remove(entity, typeid(component));
template <isAComponent Component> bool RemoveComponent(const Entity &entity) {
return entityComponentMap.Remove(entity, typeid(Component));
}
// Copying is not allowed since the ECS world is meant to be
@@ -76,8 +72,11 @@ public:
World &operator=(const World &) = delete;
private:
Tourmaline::Containers::DualkeyMap<Entity, std::type_index, std::any>
using systemFunction =
std::function<void(const Entity &, std::span<std::any *>)>;
Containers::DualkeyMap<Entity, std::type_index, std::any>
entityComponentMap{};
Containers::Hashmap<System, systemFunction> registeredSystems{};
// ======== Life-cycle ========
void preSystems();

View File

@@ -24,7 +24,7 @@ concept isAComponent = std::derived_from<T, ECS::Component>;
namespace Tourmaline::Systems::Components {
// Builtin
struct Base : public ECS::Component {
Base() {}
double x = 0, y = 0, z = 0;
};
} // namespace Tourmaline::Systems::Components
#endif

View File

@@ -9,33 +9,35 @@
#ifndef GUARD_TOURMALINE_LOGGING_H
#define GUARD_TOURMALINE_LOGGING_H
#include "Corrade/Containers/Array.h"
#include "Corrade/Containers/String.h"
#include "Corrade/Containers/StringView.h"
#include "Corrade/Utility/Format.h"
#include <fstream>
#include <string_view>
namespace Tourmaline::Systems {
class Logging {
public:
enum class LogLevel {
Critical = 0,
Error = 1,
Warning = 2,
Info = 3,
Debug = 4,
Trace = 5
};
enum LogLevel { Critical, Error, Warning, Info, Debug, Trace };
static void LogToFile(std::string File = "");
static void Log(std::string_view message,
std::string_view position = "Unknown",
static void LogToFile(Corrade::Containers::String File = "");
static void Log(Corrade::Containers::StringView message,
Corrade::Containers::StringView position = "Unknown",
LogLevel severity = LogLevel::Info, bool assertion = true);
template <class... Args>
static void LogFormatted(const char *format,
Corrade::Containers::StringView position,
LogLevel severity, const Args &...args) {
Corrade::Containers::String formatted =
Corrade::Utility::format(format, args...);
Log(formatted, position, severity);
}
private:
static std::fstream File;
static Corrade::Containers::Array<
std::pair<const std::string, const std::string>>
LogLevelToString;
static const char *LogLevelToColour[LogLevel::Trace + 1];
static const char *LogLevelToString[LogLevel::Trace + 1];
};
} // namespace Tourmaline::Systems
#endif