Switched to Corrade's string over STL
This commit is contained in:
@@ -10,64 +10,60 @@
|
|||||||
#ifndef GUARD_TOURMALINE_ECS_H
|
#ifndef GUARD_TOURMALINE_ECS_H
|
||||||
#define GUARD_TOURMALINE_ECS_H
|
#define GUARD_TOURMALINE_ECS_H
|
||||||
#include <any>
|
#include <any>
|
||||||
#include <format>
|
|
||||||
#include <typeindex>
|
#include <typeindex>
|
||||||
|
|
||||||
#include "../Containers/DualkeyMap.hpp"
|
#include "../Containers/DualkeyMap.hpp"
|
||||||
|
#include "../Containers/Hashmap.hpp"
|
||||||
#include "../Types.hpp"
|
#include "../Types.hpp"
|
||||||
#include "ECS/BuiltinComponents.hpp"
|
#include "ECS/BuiltinComponents.hpp"
|
||||||
#include "Logging.hpp"
|
#include "Logging.hpp"
|
||||||
|
|
||||||
namespace Tourmaline::Systems::ECS {
|
namespace Tourmaline::Systems::ECS {
|
||||||
using Entity = Tourmaline::Type::UUID;
|
using Entity = Tourmaline::Type::UUID;
|
||||||
class World;
|
using System = Tourmaline::Type::UUID;
|
||||||
|
|
||||||
class World {
|
class World {
|
||||||
public:
|
public:
|
||||||
|
World() {}
|
||||||
// ====== World controls ======
|
// ====== World controls ======
|
||||||
void Step();
|
void Step();
|
||||||
|
|
||||||
// ======== Entities ========
|
// ======== Entities ========
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
Entity CreateEntity();
|
Entity CreateEntity();
|
||||||
|
[[nodiscard("Pointless call of EntityExists")]]
|
||||||
bool EntityExists(const Entity &entity) noexcept;
|
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);
|
bool DestroyEntity(Entity entity);
|
||||||
|
|
||||||
// ======== Components ========
|
// ======== Components ========
|
||||||
template <isAComponent component, typename... Args>
|
template <isAComponent Component, typename... ComponentArgs>
|
||||||
component &AddComponent(const Entity &entity,
|
Component &AddComponent(const Entity &entity, ComponentArgs &&...args) {
|
||||||
Args &&...constructionArguments) {
|
auto newComponent = entityComponentMap.Insert(entity, typeid(Component),
|
||||||
auto newComponent = entityComponentMap.Insert(
|
Component(args...));
|
||||||
entity, typeid(component), component(constructionArguments...));
|
|
||||||
|
|
||||||
return std::any_cast<component &>(std::get<2>(newComponent));
|
return std::any_cast<Component &>(std::get<2>(newComponent));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <isAComponent component>
|
template <isAComponent Component>
|
||||||
[[nodiscard("Discarding an expensive operation's result!")]]
|
[[nodiscard("Pointless call of GetComponent")]]
|
||||||
component &GetComponent(const Entity &entity) {
|
Component &GetComponent(const Entity &entity) {
|
||||||
auto result = entityComponentMap.Query(entity, typeid(component));
|
auto result = entityComponentMap.Query(entity, typeid(Component));
|
||||||
if (result.empty()) {
|
if (result.empty()) {
|
||||||
Logging::Log(std::format("Entity {} does not have component {}!",
|
Logging::LogFormatted("Entity {} does not have component {}!",
|
||||||
entity.asString(), typeid(component).name()),
|
"ECS/GetComponent", Logging::LogLevel::Error,
|
||||||
"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>
|
template <isAComponent Component>
|
||||||
[[nodiscard("Discarding an expensive operation's result!")]]
|
[[nodiscard("Pointless call of HasComponent")]]
|
||||||
bool HasComponent(const Entity &entity) {
|
bool HasComponent(const Entity &entity) {
|
||||||
return entityComponentMap.Query(entity, typeid(component)).size();
|
return entityComponentMap.Query(entity, typeid(Component)).size();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <isAComponent component>
|
template <isAComponent Component> bool RemoveComponent(const Entity &entity) {
|
||||||
[[nodiscard("It is not guaranteed that a component can always be removed, "
|
return entityComponentMap.Remove(entity, typeid(Component));
|
||||||
"please make sure by checking the returned bool")]]
|
|
||||||
bool RemoveComponent(const Entity &entity) {
|
|
||||||
return entityComponentMap.Remove(entity, typeid(component));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copying is not allowed since the ECS world is meant to be
|
// Copying is not allowed since the ECS world is meant to be
|
||||||
@@ -76,8 +72,11 @@ public:
|
|||||||
World &operator=(const World &) = delete;
|
World &operator=(const World &) = delete;
|
||||||
|
|
||||||
private:
|
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{};
|
entityComponentMap{};
|
||||||
|
Containers::Hashmap<System, systemFunction> registeredSystems{};
|
||||||
|
|
||||||
// ======== Life-cycle ========
|
// ======== Life-cycle ========
|
||||||
void preSystems();
|
void preSystems();
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ concept isAComponent = std::derived_from<T, ECS::Component>;
|
|||||||
namespace Tourmaline::Systems::Components {
|
namespace Tourmaline::Systems::Components {
|
||||||
// Builtin
|
// Builtin
|
||||||
struct Base : public ECS::Component {
|
struct Base : public ECS::Component {
|
||||||
Base() {}
|
double x = 0, y = 0, z = 0;
|
||||||
};
|
};
|
||||||
} // namespace Tourmaline::Systems::Components
|
} // namespace Tourmaline::Systems::Components
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -9,33 +9,35 @@
|
|||||||
#ifndef GUARD_TOURMALINE_LOGGING_H
|
#ifndef GUARD_TOURMALINE_LOGGING_H
|
||||||
#define 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 <fstream>
|
||||||
#include <string_view>
|
|
||||||
|
|
||||||
namespace Tourmaline::Systems {
|
namespace Tourmaline::Systems {
|
||||||
class Logging {
|
class Logging {
|
||||||
public:
|
public:
|
||||||
enum class LogLevel {
|
enum LogLevel { Critical, Error, Warning, Info, Debug, Trace };
|
||||||
Critical = 0,
|
|
||||||
Error = 1,
|
|
||||||
Warning = 2,
|
|
||||||
Info = 3,
|
|
||||||
Debug = 4,
|
|
||||||
Trace = 5
|
|
||||||
};
|
|
||||||
|
|
||||||
static void LogToFile(std::string File = "");
|
static void LogToFile(Corrade::Containers::String File = "");
|
||||||
static void Log(std::string_view message,
|
static void Log(Corrade::Containers::StringView message,
|
||||||
std::string_view position = "Unknown",
|
Corrade::Containers::StringView position = "Unknown",
|
||||||
LogLevel severity = LogLevel::Info, bool assertion = true);
|
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:
|
private:
|
||||||
static std::fstream File;
|
static std::fstream File;
|
||||||
static Corrade::Containers::Array<
|
static const char *LogLevelToColour[LogLevel::Trace + 1];
|
||||||
std::pair<const std::string, const std::string>>
|
static const char *LogLevelToString[LogLevel::Trace + 1];
|
||||||
LogLevelToString;
|
|
||||||
};
|
};
|
||||||
} // namespace Tourmaline::Systems
|
} // namespace Tourmaline::Systems
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -10,7 +10,9 @@
|
|||||||
#ifndef GUARD_TOURMALINE_TYPES_H
|
#ifndef GUARD_TOURMALINE_TYPES_H
|
||||||
#define GUARD_TOURMALINE_TYPES_H
|
#define GUARD_TOURMALINE_TYPES_H
|
||||||
|
|
||||||
|
#include "Corrade/Containers/String.h"
|
||||||
#include "TourmalineExternal/random/xoshiro.h"
|
#include "TourmalineExternal/random/xoshiro.h"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -19,7 +21,7 @@ namespace Tourmaline::Type {
|
|||||||
class UUID {
|
class UUID {
|
||||||
public:
|
public:
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
std::string asString() const;
|
Corrade::Containers::String asString() const;
|
||||||
bool operator==(const UUID &rhs) const;
|
bool operator==(const UUID &rhs) const;
|
||||||
|
|
||||||
UUID(uint64_t firstHalf, uint64_t secondHalf);
|
UUID(uint64_t firstHalf, uint64_t secondHalf);
|
||||||
|
|||||||
@@ -8,9 +8,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Systems/Logging.hpp"
|
#include "Systems/Logging.hpp"
|
||||||
#include "Corrade/Containers/Array.h"
|
|
||||||
|
|
||||||
#include <Corrade/Tags.h>
|
#include "Corrade/Containers/String.h"
|
||||||
|
#include "Corrade/Containers/StringView.h"
|
||||||
|
#include "Corrade/Tags.h"
|
||||||
|
#include "Corrade/Utility/Format.h"
|
||||||
|
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
@@ -18,65 +21,63 @@
|
|||||||
#include <exception>
|
#include <exception>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iterator>
|
|
||||||
#include <print>
|
#include <print>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
using namespace Tourmaline::Systems;
|
using namespace Tourmaline::Systems;
|
||||||
using namespace Corrade::Containers;
|
using namespace Corrade::Containers;
|
||||||
|
using namespace Corrade::Utility;
|
||||||
|
|
||||||
// This is what happens when it takes you 50 years to implement
|
// This is what happens when it takes you 50 years to implement
|
||||||
// reflections to a language
|
// reflections to a language
|
||||||
Array<std::pair<const std::string, const std::string>>
|
const char *Logging::LogLevelToColour[Logging::LogLevel::Trace + 1]{
|
||||||
Logging::LogLevelToString{Corrade::InPlaceInit,
|
"[0;31m", "[0;91m", "[0;33m", "[0;37m", "[0;92m", "[0;36m"};
|
||||||
{std::pair{"Critical", "[0;31m"},
|
const char *Logging::LogLevelToString[Logging::LogLevel::Trace + 1]{
|
||||||
{"Error", "[0;91m"},
|
"Critical", "Error", "Warning", "Info", "Debug", "Trace"};
|
||||||
{"Warning", "[0;33m"},
|
|
||||||
{"Info", "[0;37m"},
|
|
||||||
{"Debug", "[0;92m"},
|
|
||||||
{"Trace", "[0;36m"}}};
|
|
||||||
std::fstream Logging::File;
|
std::fstream Logging::File;
|
||||||
|
|
||||||
void Logging::LogToFile(std::string File) {
|
void Logging::LogToFile(String File) {
|
||||||
if (File == "") {
|
if (File == "") {
|
||||||
const auto now = std::chrono::system_clock::now();
|
const auto now = std::chrono::system_clock::now();
|
||||||
File = std::format("Tourmaline-{:%Y-%j}.txt", now);
|
std::chrono::year_month_day ymd{std::chrono::floor<std::chrono::days>(now)};
|
||||||
|
File = String{Corrade::ValueInit, 128};
|
||||||
|
formatInto(File, "Tourmaline-{}-{}-{}.txt", static_cast<int>(ymd.year()),
|
||||||
|
static_cast<unsigned>(ymd.month()),
|
||||||
|
static_cast<unsigned>(ymd.day()));
|
||||||
}
|
}
|
||||||
Logging::File.open(File, std::fstream::out);
|
Logging::File.open(File.data(), std::fstream::out);
|
||||||
|
|
||||||
if (Logging::File.fail()) {
|
if (Logging::File.fail()) {
|
||||||
throw std::runtime_error("FAILED! Could not open or create the file: " +
|
String error =
|
||||||
File + "!\n" + strerror(errno));
|
format("FAILED! Could not open or create the file: {}! Error: {}", File,
|
||||||
|
strerror(errno));
|
||||||
|
throw std::runtime_error(error.data());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Logging::Log(std::string_view message, std::string_view position,
|
void Logging::Log(StringView message, StringView position,
|
||||||
Logging::LogLevel severity, bool assertion) {
|
Logging::LogLevel severity, bool assertion) {
|
||||||
if (assertion) [[likely]] {
|
if (assertion) {
|
||||||
static std::string
|
static String output{Corrade::ValueInit,
|
||||||
output; // This is done to stop allocations per std::format
|
4096}; // This is done to stop allocations
|
||||||
const auto &loglevelData =
|
std::size_t formattedSize = formatInto(
|
||||||
Logging::LogLevelToString[static_cast<size_t>(severity)];
|
output, "[{}@{}] {}\n", LogLevelToString[severity], position, message);
|
||||||
std::format_to(std::back_inserter(output), "[{}@{}] {}\n",
|
|
||||||
loglevelData.first, position, message);
|
|
||||||
|
|
||||||
std::print("\033{} {}\033[0m", loglevelData.second, output);
|
std::print(
|
||||||
|
"\033{} {}\033[0m", LogLevelToColour[severity],
|
||||||
|
std::string_view{output.begin(), output.begin() + formattedSize});
|
||||||
if (Logging::File.is_open()) {
|
if (Logging::File.is_open()) {
|
||||||
Logging::File.write(output.c_str(), output.size());
|
Logging::File.write(output.data(), formattedSize);
|
||||||
Logging::File.flush(); // Terrible but necessary sadly
|
Logging::File.flush(); // Terrible but necessary sadly
|
||||||
}
|
}
|
||||||
|
|
||||||
if (severity == Logging::LogLevel::Error) {
|
if (severity == Logging::LogLevel::Error) {
|
||||||
throw std::runtime_error(output);
|
throw std::runtime_error(output.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (severity == Logging::LogLevel::Critical) {
|
if (severity == Logging::LogLevel::Critical) {
|
||||||
std::terminate();
|
std::terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
output.clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,17 +7,19 @@
|
|||||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "Corrade/Utility/Format.h"
|
||||||
#include "Types.hpp"
|
#include "Types.hpp"
|
||||||
|
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <format>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using namespace Tourmaline::Type;
|
using namespace Tourmaline::Type;
|
||||||
std::string UUID::asString() const {
|
using namespace Corrade::Containers;
|
||||||
return std::format("{:016X}{:016X}", firstHalf, secondHalf);
|
using namespace Corrade::Utility;
|
||||||
|
String UUID::asString() const {
|
||||||
|
return format("{:.16X}{:.16X}", firstHalf, secondHalf);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool UUID::operator==(const UUID &rhs) const {
|
bool UUID::operator==(const UUID &rhs) const {
|
||||||
|
|||||||
Reference in New Issue
Block a user