From 95c99a0eff14cfcf5bcb2464011ca11532343666 Mon Sep 17 00:00:00 2001 From: Ricardo Antunes Date: Wed, 13 Sep 2023 21:06:04 +0100 Subject: [PATCH] docs(engine): add assets.saving --- docs/pages/3_examples/2_engine/main.md | 3 +- engine/samples/CMakeLists.txt | 3 +- engine/samples/assets/page.md | 7 +++ engine/samples/assets/saving/main.cpp | 86 ++++++++++++++++++++++++++ engine/samples/assets/saving/page.md | 38 ++++++++++++ 5 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 engine/samples/assets/page.md create mode 100644 engine/samples/assets/saving/main.cpp create mode 100644 engine/samples/assets/saving/page.md diff --git a/docs/pages/3_examples/2_engine/main.md b/docs/pages/3_examples/2_engine/main.md index 55ed35d12..794b16e02 100644 --- a/docs/pages/3_examples/2_engine/main.md +++ b/docs/pages/3_examples/2_engine/main.md @@ -8,5 +8,4 @@ multiple plugins of the engine: - @subpage examples-engine-settings - @copybrief examples-engine-settings - @subpage examples-engine-renderer - @copybrief examples-engine-renderer - @subpage examples-engine-scene - @copybrief examples-engine-scene -- @subpage examples-engine-assets-bridge - @copybrief examples-engine-assets-bridge -- @subpage examples-engine-assets-json - @copybrief examples-engine-assets-json +- @subpage examples-engine-assets - @copybrief examples-engine-assets diff --git a/engine/samples/CMakeLists.txt b/engine/samples/CMakeLists.txt index d21a922f8..9a8541cae 100644 --- a/engine/samples/CMakeLists.txt +++ b/engine/samples/CMakeLists.txt @@ -40,8 +40,9 @@ make_sample(DIR "settings") make_sample(DIR "events") make_sample(DIR "systems") make_sample(DIR "input" ASSETS) -make_sample(DIR "assets/json" ASSETS) make_sample(DIR "assets/bridge" ASSETS) +make_sample(DIR "assets/json" ASSETS) +make_sample(DIR "assets/saving" ASSETS) make_sample(DIR "renderer") make_sample(DIR "collisions" COMPONENTS) make_sample(DIR "scene" COMPONENTS ASSETS) diff --git a/engine/samples/assets/page.md b/engine/samples/assets/page.md new file mode 100644 index 000000000..046d79c15 --- /dev/null +++ b/engine/samples/assets/page.md @@ -0,0 +1,7 @@ +# Assets {#examples-engine-assets} + +@brief How to use the @ref assets-plugin plugin. + +- @subpage examples-engine-assets-bridge - @copybrief examples-engine-assets-bridge +- @subpage examples-engine-assets-json - @copybrief examples-engine-assets-json +- @subpage examples-engine-assets-saving - @copybrief examples-engine-assets-saving diff --git a/engine/samples/assets/saving/main.cpp b/engine/samples/assets/saving/main.cpp new file mode 100644 index 000000000..d58c93747 --- /dev/null +++ b/engine/samples/assets/saving/main.cpp @@ -0,0 +1,86 @@ +#include +#include + +#include +#include + +using cubos::core::Settings; +using cubos::core::data::Deserializer; +using cubos::core::data::FileSystem; +using cubos::core::data::Serializer; +using cubos::core::ecs::Read; +using cubos::core::ecs::Write; +using cubos::core::memory::Stream; +using namespace cubos::engine; + +/// [Asset type] +struct IntegerAsset +{ + int value; +}; +/// [Asset type] + +template <> +void cubos::core::data::serialize(Serializer& ser, const IntegerAsset& obj, const char* name) +{ + ser.beginObject(name); + ser.write(obj.value, "value"); + ser.endObject(); +} + +template <> +void cubos::core::data::deserialize(Deserializer& des, IntegerAsset& obj) +{ + des.beginObject(); + des.read(obj.value); + des.endObject(); +} + +/// [Setting] +static void configSystem(Write settings) +{ + // If we want to save assets, we must set this to false. + settings->setBool("assets.io.readOnly", false); + /// [Setting] + + settings->setString("assets.io.path", SAMPLE_ASSETS_FOLDER); +} + +static void bridgeSystem(Write assets) +{ + assets->registerBridge(".int", std::make_unique>()); +} + +/// [Create a new asset] +static void saveSystem(Write assets) +{ + // Create a new asset (with a random UUID). + auto handle = assets->create(IntegerAsset{1337}); + /// [Create a new asset] + + /// [Save the asset] + assets->writeMeta(handle)->set("path", "/assets/sample/sample.int"); + assets->save(handle); + /// [Save the asset] + + // Wait for input before exiting. + Stream::stdOut.print("You can now check the contents of the file!\nPress enter to exit..."); + Stream::stdIn.get(); + + // Cleanup the created asset. + FileSystem::destroy("/assets/sample"); +} + +int main() +{ + Cubos cubos{}; + + /// [Configure] + cubos.addPlugin(assetsPlugin); + cubos.startupSystem(saveSystem).tagged("cubos.assets"); + /// [Configure] + + cubos.startupSystem(configSystem).tagged("cubos.settings"); + cubos.startupSystem(bridgeSystem).tagged("cubos.assets.bridge"); + cubos.run(); +} diff --git a/engine/samples/assets/saving/page.md b/engine/samples/assets/saving/page.md new file mode 100644 index 000000000..f36dbfe36 --- /dev/null +++ b/engine/samples/assets/saving/page.md @@ -0,0 +1,38 @@ +# Creating and Saving {#examples-engine-assets-saving} + +@brief Creating and saving assets. + +This example demonstrates how a new asset be created programatically and how it +can be saved to the assets directory, which is useful while working on tools +such as **TESSERATOS.** + +Before we go any further, if we want to save assets to the filesystem, we must +allow assets to be modified. This is done through the following setting: + +@snippet assets/saving/main.cpp Setting + +We'll use the following asset type as an example, with a @ref +cubos::engine::JSONBridge "JSONBridge" registered for it with the extension +`.int`. + +@snippet assets/saving/main.cpp Asset type + +First, we'll create an asset of this type: + +@snippet assets/saving/main.cpp Create a new asset + +Then, we'll assign it a path and save it. Its important that the path ends with +the correct extension, so that @ref cubos::engine::Assets "Assets" knows which +bridge to use when loading it. + +@snippet assets/saving/main.cpp Save the asset + +With this, the files `sample/sample.int` and `sample/sample.int.meta` should +have appeared on the `assets/` directory. The `.meta` file contains the UUID of +the asset, which is used by the engine to identify it. + +Finally, the engine is configured the following way: + +@snippet assets/saving/main.cpp Configure + +Try running the sample yourself to see the files being created!