Skip to content

Commit

Permalink
docs(engine): switch samples to new Cubos syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Jan 31, 2024
1 parent c194273 commit 0229e9f
Show file tree
Hide file tree
Showing 26 changed files with 669 additions and 896 deletions.
49 changes: 20 additions & 29 deletions engine/samples/assets/bridge/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,42 +47,33 @@ class TextBridge : public FileBridge
};
/// [TextBridge::saveToFile]

static void configSystem(Settings& settings)
int main()
{
settings.setString("assets.io.path", SAMPLE_ASSETS_FOLDER);
}
auto cubos = Cubos();

/// [Registering the bridge]
static void bridgeSystem(Assets& assets)
{
// Add a custom bridge to load .txt files.
assets.registerBridge(".txt", std::make_unique<TextBridge>());
}
/// [Registering the bridge]
cubos.addPlugin(assetsPlugin);

/// [Loading the asset]
// Assets are identified through UUIDs which are defined in their .meta files.
static const Asset<std::string> SampleAsset = AnyAsset("6f42ae5a-59d1-5df3-8720-83b8df6dd536");
cubos.startupSystem("configure Assets plugin").tagged("cubos.settings").call([](Settings& settings) {
settings.setString("assets.io.path", SAMPLE_ASSETS_FOLDER);
});

static void loadSystem(const Assets& assets)
{
// Access the text asset - will be loaded automatically.
auto text = assets.read(SampleAsset);
Stream::stdOut.print(*text);
}
/// [Loading the asset]
/// [Registering the bridge]
cubos.startupSystem("setup bridge to load .txt files").tagged("cubos.assets.bridge").call([](Assets& assets) {
assets.registerBridge(".txt", std::make_unique<TextBridge>());
});
/// [Registering the bridge]

int main()
{
auto cubos = Cubos();
/// [Loading the asset]
// Assets are identified through UUIDs which are defined in their .meta files.
static const Asset<std::string> SampleAsset = AnyAsset("6f42ae5a-59d1-5df3-8720-83b8df6dd536");

/// [Configuration]
cubos.addPlugin(assetsPlugin);
cubos.startupSystem(bridgeSystem).tagged("cubos.assets.bridge");
cubos.startupSystem(loadSystem).tagged("cubos.assets");
/// [Configuration]
cubos.startupSystem("access .txt asset").tagged("cubos.assets").call([](const Assets& assets) {
// Access the text asset - will be loaded automatically.
auto text = assets.read(SampleAsset);
Stream::stdOut.print(*text);
});
/// [Loading the asset]

cubos.startupSystem(configSystem).tagged("cubos.settings");
cubos.run();
return 0;
}
2 changes: 0 additions & 2 deletions engine/samples/assets/bridge/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,3 @@ startup phase. Systems which add bridges should be tagged with
`cubos.assets.bridge` so that they run before any assets are loaded.
Similarly, startup systems which load assets should be tagged with
`cubos.assets` so that they run after all bridges have been registered.

@snippet assets/bridge/main.cpp Configuration
70 changes: 21 additions & 49 deletions engine/samples/assets/json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <cubos/engine/settings/settings.hpp>

using cubos::core::data::FileSystem;
using cubos::core::data::old::Deserializer;
using cubos::core::data::old::Serializer;
using cubos::core::memory::Stream;
using cubos::core::reflection::FieldsTrait;
using cubos::core::reflection::Type;
Expand All @@ -34,59 +32,33 @@ CUBOS_REFLECT_IMPL(Strings)
}
/// [Asset type]

/// [Serialization definition]
template <>
void cubos::core::data::old::serialize<Strings>(Serializer& ser, const Strings& obj, const char* name)
{
ser.beginObject(name);
ser.write(obj.strings, "strings");
ser.endObject();
}

template <>
void cubos::core::data::old::deserialize<Strings>(Deserializer& des, Strings& obj)
{
des.beginObject();
des.read(obj.strings);
des.endObject();
}
/// [Serialization definition]

static void configSystem(Settings& settings)
int main()
{
settings.setString("assets.io.path", SAMPLE_ASSETS_FOLDER);
}
Cubos cubos{};

/// [Register bridge]
static void bridgeSystem(Assets& assets)
{
assets.registerBridge(".strings", std::make_unique<JSONBridge<Strings>>());
}
/// [Register bridge]
cubos.addPlugin(assetsPlugin);

/// [Loading the asset]
static const Asset<Strings> SampleAsset = AnyAsset("6f42ae5a-59d1-5df3-8720-83b8df6dd536");
cubos.startupSystem("configure Assets plugin").tagged("cubos.settings").call([](Settings& settings) {
settings.setString("assets.io.path", SAMPLE_ASSETS_FOLDER);
});

static void loadSystem(Assets& assets)
{
auto read = assets.read(SampleAsset);
for (const auto& str : read->strings)
{
Stream::stdOut.printf("String: {}\n", str);
}
}
/// [Loading the asset]
/// [Register bridge]
cubos.startupSystem("setup bridge to load .strings files").tagged("cubos.assets.bridge").call([](Assets& assets) {
assets.registerBridge(".strings", std::make_unique<JSONBridge<Strings>>());
});
/// [Register bridge]

int main()
{
Cubos cubos{};
/// [Loading the asset]
static const Asset<Strings> SampleAsset = AnyAsset("6f42ae5a-59d1-5df3-8720-83b8df6dd536");

/// [Configuration]
cubos.addPlugin(assetsPlugin);
cubos.startupSystem(bridgeSystem).tagged("cubos.assets.bridge");
cubos.startupSystem(loadSystem).tagged("cubos.assets");
/// [Configuration]
cubos.startupSystem("access .strings asset").tagged("cubos.assets").call([](Assets& assets) {
auto read = assets.read(SampleAsset);
for (const auto& str : read->strings)
{
Stream::stdOut.printf("String: {}\n", str);
}
});
/// [Loading the asset]

cubos.startupSystem(configSystem).tagged("cubos.settings");
cubos.run();
}
12 changes: 3 additions & 9 deletions engine/samples/assets/json/page.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
# Loading Serializable Assets {#examples-engine-assets-json}
# Loading Reflectable Assets {#examples-engine-assets-json}

@brief Loading serializable assets from JSON.
@brief Loading reflectable assets from JSON.

@see Full source code [here](https://github.com/GameDevTecnico/cubos/tree/main/engine/samples/assets/json).

We'll use the following type as an example:

@snippet assets/json/main.cpp Asset type

We can make it serializable by implementing the following specializations:

@snippet assets/json/main.cpp Serialization definition

Then, we must register a bridge for this type. We provide @ref
cubos::engine::JSONBridge "JSONBridge" for easily loading and saving
serializable assets as JSON.
reflectable assets as JSON.

@snippet assets/json/main.cpp Register bridge

Expand All @@ -24,5 +20,3 @@ With the bridge registered, we can just load it from its handle:

These sytems are configured the usual way, as explained in @ref
examples-engine-assets-bridge.

@snippet assets/json/main.cpp Configuration
78 changes: 26 additions & 52 deletions engine/samples/assets/saving/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <cubos/engine/settings/settings.hpp>

using cubos::core::data::FileSystem;
using cubos::core::data::old::Deserializer;
using cubos::core::data::old::Serializer;
using cubos::core::memory::Stream;
using cubos::core::reflection::FieldsTrait;
using cubos::core::reflection::Type;
Expand All @@ -30,67 +28,43 @@ CUBOS_REFLECT_IMPL(IntegerAsset)
}
/// [Asset type]

template <>
void cubos::core::data::old::serialize<IntegerAsset>(Serializer& ser, const IntegerAsset& obj, const char* name)
int main()
{
ser.beginObject(name);
ser.write(obj.value, "value");
ser.endObject();
}
Cubos cubos{};

template <>
void cubos::core::data::old::deserialize<IntegerAsset>(Deserializer& des, IntegerAsset& obj)
{
des.beginObject();
des.read(obj.value);
des.endObject();
}
cubos.addPlugin(assetsPlugin);

/// [Setting]
static void configSystem(Settings& settings)
{
// If we want to save assets, we must set this to false.
settings.setBool("assets.io.readOnly", false);
/// [Setting]
cubos.startupSystem("configure Assets plugin").tagged("cubos.settings").call([](Settings& 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);
}
settings.setString("assets.io.path", SAMPLE_ASSETS_FOLDER);
});

static void bridgeSystem(Assets& assets)
{
assets.registerBridge(".int", std::make_unique<JSONBridge<IntegerAsset>>());
}
cubos.startupSystem("setup bridge to load and save .int assets")
.tagged("cubos.assets.bridge")
.call([](Assets& assets) { assets.registerBridge(".int", std::make_unique<JSONBridge<IntegerAsset>>()); });

/// [Create a new asset]
static void saveSystem(Assets& assets)
{
// Create a new asset (with a random UUID).
auto handle = assets.create(IntegerAsset{1337});
/// [Create a new asset]
cubos.startupSystem("create and save asset").tagged("cubos.assets").call([](Assets& 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]
/// [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();
// 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]
// Cleanup the created asset.
FileSystem::destroy("/assets/sample");
});

cubos.startupSystem(configSystem).tagged("cubos.settings");
cubos.startupSystem(bridgeSystem).tagged("cubos.assets.bridge");
cubos.run();
}
10 changes: 3 additions & 7 deletions engine/samples/assets/saving/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

@see Full source code [here](https://github.com/GameDevTecnico/cubos/tree/main/engine/samples/assets/saving).

This example demonstrates how a new asset be created programatically and how it
This example demonstrates how a new asset can be created programatically and how it
can be saved to the assets directory, which is useful while working on tools
such as **TESSERATOS.**
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:
Expand All @@ -23,7 +23,7 @@ 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
Then, we'll assign it a path and save it. It's important that the path ends with
the correct extension, so that @ref cubos::engine::Assets "Assets" knows which
bridge to use when loading it.

Expand All @@ -33,8 +33,4 @@ 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!
Loading

0 comments on commit 0229e9f

Please sign in to comment.