Compare commits

..

3 Commits

Author SHA1 Message Date
cat
88d4695596 Switched to Corrade's string over STL 2026-03-07 14:44:29 +02:00
cat
55f12fcd69 Fixing quoting on xohiro 2026-03-07 14:43:59 +02:00
cat
27f50ffb35 Decided to dynamically link and ship corrade with tourmaline 2026-03-07 14:43:20 +02:00
10 changed files with 113 additions and 97 deletions

View File

@@ -52,7 +52,7 @@ foreach(dep
add_subdirectory(external/${dep})
endforeach()
# Building SO
# Building
add_library(${PROJECT_NAME} SHARED
"source/Systems/ECS/Components.cpp"
"source/Systems/ECS/World.cpp"
@@ -72,6 +72,16 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
# Module stuff
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/headers>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>
$<BUILD_INTERFACE:${corrade_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}External>
)
FetchContent_GetProperties(Corrade SOURCE_DIR corrade_SOURCE_DIR)
FetchContent_GetProperties(Corrade BINARY_DIR corrade_BINARY_DIR)
@@ -85,21 +95,19 @@ install(
PATTERN "*.hpp"
)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/headers>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>
$<BUILD_INTERFACE:${corrade_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}External>
)
install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
INCLUDES DESTINATION include
INCLUDES DESTINATION include/${PROJECT_NAME}
)
install(
TARGETS CorradeMain CorradeUtility CorradeContainers CorradePluginManager
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
INCLUDES DESTINATION include/${PROJECT_NAME}External
)
install(DIRECTORY headers/ DESTINATION include/${PROJECT_NAME})

View File

@@ -18,7 +18,9 @@ Tormaline Engine is a game engine created for C++23. [Source Code](https://git.t
Tourmaline is by no means currently usable. The project is incredible volatile with constant changes and improvements. Please wait until a release is made.
However if you cannot just help yourself you can compile a tourmaline demo by running
g++ program.cpp -std=c++23 -lTourmaline -I/usr/local/include/TourmalineExternal -o program
```
g++ program.cpp -std=c++23 -lTourmaline -lCorradeUtility -lCorradePluginManager -I/usr/local/include/TourmalineExternal -o program
```
# 3rd Party Libraries Credits
- [Corrade/Magnum](https://magnum.graphics/) - graphics middleware by Vladimír "Mosra" Vondruš.

View File

@@ -1,7 +1,7 @@
include(FetchContent)
# Building options
set(CORRADE_BUILD_STATIC ON)
set(CORRADE_BUILD_STATIC OFF)
# Feature options
set(CORRADE_WITH_MAIN ON)

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

View File

@@ -10,7 +10,7 @@
#ifndef GUARD_TOURMALINE_RANDOM_H
#define GUARD_TOURMALINE_RANDOM_H
#include "../Types.hpp"
#include <TourmalineExternal/random/xoshiro.h>
#include "TourmalineExternal/random/xoshiro.h"
#include <type_traits>

View File

@@ -10,7 +10,9 @@
#ifndef GUARD_TOURMALINE_TYPES_H
#define GUARD_TOURMALINE_TYPES_H
#include "Corrade/Containers/String.h"
#include "TourmalineExternal/random/xoshiro.h"
#include <cstdint>
#include <functional>
#include <string>
@@ -19,7 +21,7 @@ namespace Tourmaline::Type {
class UUID {
public:
[[nodiscard]]
std::string asString() const;
Corrade::Containers::String asString() const;
bool operator==(const UUID &rhs) const;
UUID(uint64_t firstHalf, uint64_t secondHalf);

View File

@@ -8,9 +8,12 @@
*/
#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 <chrono>
#include <cstddef>
@@ -18,65 +21,63 @@
#include <exception>
#include <format>
#include <fstream>
#include <iterator>
#include <print>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
using namespace Tourmaline::Systems;
using namespace Corrade::Containers;
using namespace Corrade::Utility;
// This is what happens when it takes you 50 years to implement
// reflections to a language
Array<std::pair<const std::string, const std::string>>
Logging::LogLevelToString{Corrade::InPlaceInit,
{std::pair{"Critical", "[0;31m"},
{"Error", "[0;91m"},
{"Warning", "[0;33m"},
{"Info", "[0;37m"},
{"Debug", "[0;92m"},
{"Trace", "[0;36m"}}};
const char *Logging::LogLevelToColour[Logging::LogLevel::Trace + 1]{
"[0;31m", "[0;91m", "[0;33m", "[0;37m", "[0;92m", "[0;36m"};
const char *Logging::LogLevelToString[Logging::LogLevel::Trace + 1]{
"Critical", "Error", "Warning", "Info", "Debug", "Trace"};
std::fstream Logging::File;
void Logging::LogToFile(std::string File) {
void Logging::LogToFile(String File) {
if (File == "") {
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()) {
throw std::runtime_error("FAILED! Could not open or create the file: " +
File + "!\n" + strerror(errno));
String error =
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) {
if (assertion) [[likely]] {
static std::string
output; // This is done to stop allocations per std::format
const auto &loglevelData =
Logging::LogLevelToString[static_cast<size_t>(severity)];
std::format_to(std::back_inserter(output), "[{}@{}] {}\n",
loglevelData.first, position, message);
if (assertion) {
static String output{Corrade::ValueInit,
4096}; // This is done to stop allocations
std::size_t formattedSize = formatInto(
output, "[{}@{}] {}\n", LogLevelToString[severity], 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()) {
Logging::File.write(output.c_str(), output.size());
Logging::File.write(output.data(), formattedSize);
Logging::File.flush(); // Terrible but necessary sadly
}
if (severity == Logging::LogLevel::Error) {
throw std::runtime_error(output);
throw std::runtime_error(output.data());
}
if (severity == Logging::LogLevel::Critical) {
std::terminate();
}
output.clear();
}
}

View File

@@ -7,17 +7,19 @@
* obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "Corrade/Utility/Format.h"
#include "Types.hpp"
#include <charconv>
#include <cstdint>
#include <cstring>
#include <format>
#include <string>
using namespace Tourmaline::Type;
std::string UUID::asString() const {
return std::format("{:016X}{:016X}", firstHalf, secondHalf);
using namespace Corrade::Containers;
using namespace Corrade::Utility;
String UUID::asString() const {
return format("{:.16X}{:.16X}", firstHalf, secondHalf);
}
bool UUID::operator==(const UUID &rhs) const {