From a6ac4f029e7d64af23819f1a8ea6c70752f1e2b7 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Thu, 5 Dec 2024 15:48:27 -0600 Subject: [PATCH] Scene context improving but not finished. --- assets/prefabs/rosa.json | 7 +- assets/scenes/test_rpg_scene.json | 5 -- src/dawn/asset/loader/CMakeLists.txt | 8 +-- src/dawn/asset/loader/scene/CMakeLists.txt | 12 ++++ .../{ => scene}/LoaderForSceneItems.cpp | 35 ++++++--- .../{ => scene}/LoaderForSceneItems.hpp | 6 +- .../asset/loader/{ => scene}/PrefabLoader.cpp | 16 ++--- .../asset/loader/{ => scene}/PrefabLoader.hpp | 1 - .../asset/loader/scene/SceneLoadContext.cpp | 6 ++ .../asset/loader/scene/SceneLoadContext.hpp | 51 +++++++++++++ .../asset/loader/{ => scene}/SceneLoader.cpp | 32 ++++----- .../asset/loader/{ => scene}/SceneLoader.hpp | 0 src/dawn/component/display/Camera.cpp | 72 +++++++++---------- src/dawn/component/display/Camera.hpp | 2 +- .../material/SimpleTexturedMaterial.cpp | 12 ++-- .../material/SimpleTexturedMaterial.hpp | 2 +- .../display/mesh/CubeMeshComponent.cpp | 9 +-- .../display/mesh/CubeMeshComponent.hpp | 2 +- src/dawn/game/IGame.cpp | 2 +- src/dawn/scene/SceneComponent.cpp | 5 +- src/dawn/scene/SceneComponent.hpp | 41 ++--------- src/dawn/scene/SceneItem.cpp | 29 ++++---- src/dawn/scene/SceneItem.hpp | 4 +- src/dawnrpg/component/RPGPlayer.cpp | 8 +-- src/dawnrpg/component/RPGPlayer.hpp | 2 +- 25 files changed, 206 insertions(+), 163 deletions(-) create mode 100644 src/dawn/asset/loader/scene/CMakeLists.txt rename src/dawn/asset/loader/{ => scene}/LoaderForSceneItems.cpp (62%) rename src/dawn/asset/loader/{ => scene}/LoaderForSceneItems.hpp (85%) rename src/dawn/asset/loader/{ => scene}/PrefabLoader.cpp (87%) rename src/dawn/asset/loader/{ => scene}/PrefabLoader.hpp (94%) create mode 100644 src/dawn/asset/loader/scene/SceneLoadContext.cpp create mode 100644 src/dawn/asset/loader/scene/SceneLoadContext.hpp rename src/dawn/asset/loader/{ => scene}/SceneLoader.cpp (80%) rename src/dawn/asset/loader/{ => scene}/SceneLoader.hpp (100%) diff --git a/assets/prefabs/rosa.json b/assets/prefabs/rosa.json index 16dc7542..d56ebb82 100644 --- a/assets/prefabs/rosa.json +++ b/assets/prefabs/rosa.json @@ -2,7 +2,7 @@ "name": "Rosa", "assets": { - "rosa": { + "rosa_texture": { "type": "texture", "path": "rosa.texture" } @@ -11,7 +11,7 @@ "components": { "material": { "type": "SimpleTexturedMaterial", - "texture": "rosa" + "texture": "rosa_texture" }, "meshRenderer": { "type": "MeshRenderer" @@ -20,8 +20,7 @@ "type": "RPGEntity" }, "player": { - "type": "RPGPlayer", - "camera": "camera" + "type": "RPGPlayer" } } } \ No newline at end of file diff --git a/assets/scenes/test_rpg_scene.json b/assets/scenes/test_rpg_scene.json index 184240d5..af2545ef 100644 --- a/assets/scenes/test_rpg_scene.json +++ b/assets/scenes/test_rpg_scene.json @@ -20,11 +20,6 @@ "rosa": { "prefab": "rosa", "position": [ 0, 0, 0 ] - }, - - "rosa2": { - "prefab": "rosa", - "position": [ 2, 0, 0 ] } } } \ No newline at end of file diff --git a/src/dawn/asset/loader/CMakeLists.txt b/src/dawn/asset/loader/CMakeLists.txt index 263f40b1..74e37390 100644 --- a/src/dawn/asset/loader/CMakeLists.txt +++ b/src/dawn/asset/loader/CMakeLists.txt @@ -9,7 +9,7 @@ target_sources(${DAWN_TARGET_NAME} TextureLoader.cpp JSONLoader.cpp TrueTypeLoader.cpp - SceneLoader.cpp - LoaderForSceneItems.cpp - PrefabLoader.cpp -) \ No newline at end of file +) + +# Subdirs +add_subdirectory(scene) \ No newline at end of file diff --git a/src/dawn/asset/loader/scene/CMakeLists.txt b/src/dawn/asset/loader/scene/CMakeLists.txt new file mode 100644 index 00000000..8ce94116 --- /dev/null +++ b/src/dawn/asset/loader/scene/CMakeLists.txt @@ -0,0 +1,12 @@ +# Copyright (c) 2025 Dominic Msters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + SceneLoader.cpp + LoaderForSceneItems.cpp + PrefabLoader.cpp +) \ No newline at end of file diff --git a/src/dawn/asset/loader/LoaderForSceneItems.cpp b/src/dawn/asset/loader/scene/LoaderForSceneItems.cpp similarity index 62% rename from src/dawn/asset/loader/LoaderForSceneItems.cpp rename to src/dawn/asset/loader/scene/LoaderForSceneItems.cpp index c4ddb073..99ed28a7 100644 --- a/src/dawn/asset/loader/LoaderForSceneItems.cpp +++ b/src/dawn/asset/loader/scene/LoaderForSceneItems.cpp @@ -5,17 +5,24 @@ #include "LoaderForSceneItems.hpp" #include "asset/loader/TextureLoader.hpp" -#include "asset/loader/PrefabLoader.hpp" +#include "asset/loader/scene/PrefabLoader.hpp" using namespace Dawn; LoaderForSceneItems::LoaderForSceneItems( const std::shared_ptr assetManager, const std::string name -) : AssetLoader(assetManager, name) { +) : + AssetLoader(assetManager, name), + ctx(std::make_shared()) +{ } void LoaderForSceneItems::setupDependencies() { + assertNotNull(this->jsonLoader, "JSON Loader is NULL?"); + assertNotNull(ctx, "SceneLoadContext is NULL?"); + assertTrue(this->jsonLoader->loaded, "JSON loader not loaded?"); + // Begin loading dependencies. auto &data = this->jsonLoader->data; if(data.contains("assets")) { @@ -27,6 +34,18 @@ void LoaderForSceneItems::setupDependencies() { auto type = assetData["type"].get(); auto path = assetData["path"].get(); + // Is this asset already named? + if(ctx->assets.find(assetName) != ctx->assets.end()) { + // Check if path and type already the same. + auto &existing = ctx->assets[assetName]; + assertTrue( + existing->name == path && existing->getAssetType() == type, + "Asset already exists with different path or type: %s", + assetName.c_str() + ); + continue; + } + std::shared_ptr loader; if(type == "texture") { loader = getAssetManager()->get(path); @@ -36,24 +55,18 @@ void LoaderForSceneItems::setupDependencies() { } else if(type == "prefab") { auto prefabLoader = getAssetManager()->get(path); - prefabLoader->parentLoader = weak_from_this(); + prefabLoader->ctx->parent = ctx; loader = prefabLoader; } else { assertUnreachable("Unknown asset type: %s", type.c_str()); } - - assertMapNotHasKey( - ctx.assets, - assetName, - "Asset already exists: %s", assetName.c_str() - ); - ctx.assets[assetName] = loader; + ctx->assets[assetName] = loader; } } } LoaderForSceneItems::~LoaderForSceneItems() { - + jsonLoader = nullptr; } \ No newline at end of file diff --git a/src/dawn/asset/loader/LoaderForSceneItems.hpp b/src/dawn/asset/loader/scene/LoaderForSceneItems.hpp similarity index 85% rename from src/dawn/asset/loader/LoaderForSceneItems.hpp rename to src/dawn/asset/loader/scene/LoaderForSceneItems.hpp index f6fadd6b..3ca2ee07 100644 --- a/src/dawn/asset/loader/LoaderForSceneItems.hpp +++ b/src/dawn/asset/loader/scene/LoaderForSceneItems.hpp @@ -4,8 +4,8 @@ // https://opensource.org/licenses/MIT #pragma once -#include "JSONLoader.hpp" -#include "scene/Scene.hpp" +#include "asset/loader/JSONLoader.hpp" +#include "SceneLoadContext.hpp" namespace Dawn { class LoaderForSceneitems; @@ -24,7 +24,7 @@ namespace Dawn { void setupDependencies(); public: - struct SceneComponentLoadContext ctx; + const std::shared_ptr ctx; LoaderForSceneItems( const std::shared_ptr assetManager, diff --git a/src/dawn/asset/loader/PrefabLoader.cpp b/src/dawn/asset/loader/scene/PrefabLoader.cpp similarity index 87% rename from src/dawn/asset/loader/PrefabLoader.cpp rename to src/dawn/asset/loader/scene/PrefabLoader.cpp index d550a644..cf32b0bb 100644 --- a/src/dawn/asset/loader/PrefabLoader.cpp +++ b/src/dawn/asset/loader/scene/PrefabLoader.cpp @@ -42,7 +42,7 @@ void PrefabLoader::updateSync() { assertTrue(this->jsonLoader->loaded, "JSON loader not loaded?"); // Check if all dependencies are loaded. - for(auto &asset : ctx.assets) { + for(auto &asset : ctx->assets) { if(!asset.second->loaded) return; } @@ -65,14 +65,10 @@ std::string PrefabLoader::getAssetType() const { void PrefabLoader::stagePrefab(std::shared_ptr item) { assertTrue(this->loaded, "Prefab not loaded"); - // Can we merge with the parent context? - auto parentLoaderLock = this->parentLoader.lock(); - if(parentLoaderLock) this->ctx.merge(parentLoaderLock->ctx); - // Force-set new context values - ctx.scene = item->getScene(); - ctx.data = this->jsonLoader->data; - + ctx->data = this->jsonLoader->data; + + // Load the scene item. item->load(ctx); auto &itemData = this->jsonLoader->data; @@ -90,8 +86,8 @@ void PrefabLoader::stagePrefab(std::shared_ptr item) { auto cmp = SceneComponentRegistry::createComponent( cmpType, item ); - ctx.data = cmpData; - ctx.components[cmpName] = cmp; + ctx->data = cmpData; + ctx->components[cmpName] = cmp; cmp->load(ctx); } } diff --git a/src/dawn/asset/loader/PrefabLoader.hpp b/src/dawn/asset/loader/scene/PrefabLoader.hpp similarity index 94% rename from src/dawn/asset/loader/PrefabLoader.hpp rename to src/dawn/asset/loader/scene/PrefabLoader.hpp index b10172e9..910f64da 100644 --- a/src/dawn/asset/loader/PrefabLoader.hpp +++ b/src/dawn/asset/loader/scene/PrefabLoader.hpp @@ -24,7 +24,6 @@ namespace Dawn { public: const static std::string ASSET_TYPE; - std::weak_ptr parentLoader; PrefabLoader( const std::shared_ptr assetManager, diff --git a/src/dawn/asset/loader/scene/SceneLoadContext.cpp b/src/dawn/asset/loader/scene/SceneLoadContext.cpp new file mode 100644 index 00000000..0d94250d --- /dev/null +++ b/src/dawn/asset/loader/scene/SceneLoadContext.cpp @@ -0,0 +1,6 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "SceneLoadContext.hpp" \ No newline at end of file diff --git a/src/dawn/asset/loader/scene/SceneLoadContext.hpp b/src/dawn/asset/loader/scene/SceneLoadContext.hpp new file mode 100644 index 00000000..94cb563f --- /dev/null +++ b/src/dawn/asset/loader/scene/SceneLoadContext.hpp @@ -0,0 +1,51 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawn.hpp" +#include "assert/assert.hpp" + +namespace Dawn { + class Scene; + class SceneItem; + class SceneComponent; + class AssetLoader; + class SceneLoadContext; + + class SceneLoadContext { + public: + std::weak_ptr parent; + std::unordered_map> items; + std::unordered_map> components; + std::unordered_map> assets; + + json data; + + std::shared_ptr currentScene; + std::shared_ptr currentItem; + std::shared_ptr currentComponent; + + /** + * Gets an asset from the context. + * + * @param j Name of the asset to get. + * @return Asset from the context. + */ + template + std::shared_ptr getAsset(const std::string &j) const { + auto it = assets.find(j); + if(it == assets.end()) { + auto parent = this->parent.lock(); + assertNotNull(parent, "Couldn't find asset."); + return parent->getAsset(j); + } + auto asset = std::dynamic_pointer_cast(it->second); + assertNotNull(asset, "Asset is not of the correct type."); + return asset; + } + + + }; +} \ No newline at end of file diff --git a/src/dawn/asset/loader/SceneLoader.cpp b/src/dawn/asset/loader/scene/SceneLoader.cpp similarity index 80% rename from src/dawn/asset/loader/SceneLoader.cpp rename to src/dawn/asset/loader/scene/SceneLoader.cpp index d44105c9..0cab200f 100644 --- a/src/dawn/asset/loader/SceneLoader.cpp +++ b/src/dawn/asset/loader/scene/SceneLoader.cpp @@ -33,6 +33,7 @@ void SceneLoader::updateAsync() { case SceneLoaderState::LOADING_JSON: assertNotNull(this->jsonLoader, "JSON Loader is NULL?"); if(!this->jsonLoader->loaded) return; + this->ctx->data = this->jsonLoader->data; this->setupDependencies(); this->state = SceneLoaderState::LOADING_DEPENDENCIES; break; @@ -40,10 +41,12 @@ void SceneLoader::updateAsync() { case SceneLoaderState::LOADING_DEPENDENCIES: assertTrue(this->jsonLoader->loaded, "JSON loader not loaded?"); // Check if all dependencies are loaded. - for(auto &asset : ctx.assets) { + for(auto &asset : ctx->assets) { if(!asset.second->loaded) return; } - ctx.scene = std::make_shared(this->getAssetManager()->getGame()); + ctx->currentScene = std::make_shared( + this->getAssetManager()->getGame() + ); this->state = SceneLoaderState::PENDING_STAGE; break; @@ -54,6 +57,8 @@ void SceneLoader::updateAsync() { void SceneLoader::updateSync() { if(this->state != SceneLoaderState::PENDING_STAGE) return; + assertNotNull(this->jsonLoader, "JSON Loader is NULL?"); + assertNotNull(ctx, "SceneLoadContext is NULL?"); assertTrue(this->jsonLoader->loaded, "JSON loader not loaded?"); auto &data = this->jsonLoader->data; @@ -62,17 +67,17 @@ void SceneLoader::updateSync() { for(auto &item : data["items"].items()) { auto &itemName = item.key(); auto &itemData = item.value(); - auto sceneItem = ctx.scene->createSceneItem(); - ctx.items[itemName] = sceneItem; + auto sceneItem = ctx->currentScene->createSceneItem(); + ctx->items[itemName] = sceneItem; } // Add components to each scene item for(auto &item : data["items"].items()) { auto &itemName = item.key(); auto &itemData = item.value(); - auto sceneItem = ctx.items[itemName]; + auto sceneItem = ctx->items[itemName]; - ctx.data = itemData; + ctx->data = itemData; sceneItem->load(ctx); if(itemData.contains("components")) { @@ -89,8 +94,8 @@ void SceneLoader::updateSync() { auto cmp = SceneComponentRegistry::createComponent( cmpType, sceneItem ); - ctx.data = cmpData; - ctx.components[cmpName] = cmp; + ctx->data = cmpData; + ctx->components[cmpName] = cmp; cmp->load(ctx); } } @@ -100,11 +105,6 @@ void SceneLoader::updateSync() { this->jsonLoader = nullptr; this->state = SceneLoaderState::DONE; this->loaded = true; - - ctx.assets.clear(); - ctx.components.clear(); - ctx.items.clear(); - ctx.data = {}; } std::string SceneLoader::getAssetType() const { @@ -112,10 +112,10 @@ std::string SceneLoader::getAssetType() const { } std::shared_ptr SceneLoader::getScene() { - return ctx.scene; + assertNotNull(ctx, "Context is NULL?"); + assertNotNull(ctx->currentScene, "Scene not loaded?"); + return ctx->currentScene; } SceneLoader::~SceneLoader() { - ctx = {}; - jsonLoader = nullptr; } \ No newline at end of file diff --git a/src/dawn/asset/loader/SceneLoader.hpp b/src/dawn/asset/loader/scene/SceneLoader.hpp similarity index 100% rename from src/dawn/asset/loader/SceneLoader.hpp rename to src/dawn/asset/loader/scene/SceneLoader.hpp diff --git a/src/dawn/component/display/Camera.cpp b/src/dawn/component/display/Camera.cpp index 091ca64f..085624dc 100644 --- a/src/dawn/component/display/Camera.cpp +++ b/src/dawn/component/display/Camera.cpp @@ -21,65 +21,65 @@ void Camera::onDispose() { renderTarget = nullptr; } -void Camera::load(SceneComponentLoadContext &ctx) { +void Camera::load(std::shared_ptr ctx) { SceneComponent::load(ctx); - if(ctx.data.contains("fov")) { - this->fov = Math::deg2rad(ctx.data["fov"].get()); + if(ctx->data.contains("fov")) { + this->fov = Math::deg2rad(ctx->data["fov"].get()); } - if(ctx.data.contains("cameraType")) { + if(ctx->data.contains("cameraType")) { if( - ctx.data["cameraType"] == "orthogonal" || - ctx.data["cameraType"] == "orthographic" || - ctx.data["cameraType"] == "ortho" + ctx->data["cameraType"] == "orthogonal" || + ctx->data["cameraType"] == "orthographic" || + ctx->data["cameraType"] == "ortho" ) { this->type = CameraType::ORTHOGONAL; - } else if(ctx.data["cameraType"] == "perspective") { + } else if(ctx->data["cameraType"] == "perspective") { this->type = CameraType::PERSPECTIVE; } else { assertUnreachable("Invalid Camera Type!"); } } - if(ctx.data.contains("orthoLeft")) { - this->orthoLeft = ctx.data["orthoLeft"].get(); - } else if(ctx.data.contains("left")) { - this->orthoLeft = ctx.data["left"].get(); + if(ctx->data.contains("orthoLeft")) { + this->orthoLeft = ctx->data["orthoLeft"].get(); + } else if(ctx->data.contains("left")) { + this->orthoLeft = ctx->data["left"].get(); } - if(ctx.data.contains("orthoRight")) { - this->orthoRight = ctx.data["orthoRight"].get(); - } else if(ctx.data.contains("right")) { - this->orthoRight = ctx.data["right"].get(); + if(ctx->data.contains("orthoRight")) { + this->orthoRight = ctx->data["orthoRight"].get(); + } else if(ctx->data.contains("right")) { + this->orthoRight = ctx->data["right"].get(); } - if(ctx.data.contains("orthoBottom")) { - this->orthoBottom = ctx.data["orthoBottom"].get(); - } else if(ctx.data.contains("bottom")) { - this->orthoBottom = ctx.data["bottom"].get(); + if(ctx->data.contains("orthoBottom")) { + this->orthoBottom = ctx->data["orthoBottom"].get(); + } else if(ctx->data.contains("bottom")) { + this->orthoBottom = ctx->data["bottom"].get(); } - if(ctx.data.contains("orthoTop")) { - this->orthoTop = ctx.data["orthoTop"].get(); - } else if(ctx.data.contains("top")) { - this->orthoTop = ctx.data["top"].get(); + if(ctx->data.contains("orthoTop")) { + this->orthoTop = ctx->data["orthoTop"].get(); + } else if(ctx->data.contains("top")) { + this->orthoTop = ctx->data["top"].get(); } - if(ctx.data.contains("clipNear")) { - this->clipNear = ctx.data["clipNear"].get(); - } else if(ctx.data.contains("near")) { - this->clipNear = ctx.data["near"].get(); - } else if(ctx.data.contains("zNear")) { - this->clipNear = ctx.data["zNear"].get(); + if(ctx->data.contains("clipNear")) { + this->clipNear = ctx->data["clipNear"].get(); + } else if(ctx->data.contains("near")) { + this->clipNear = ctx->data["near"].get(); + } else if(ctx->data.contains("zNear")) { + this->clipNear = ctx->data["zNear"].get(); } - if(ctx.data.contains("clipFar")) { - this->clipFar = ctx.data["clipFar"].get(); - } else if(ctx.data.contains("far")) { - this->clipFar = ctx.data["far"].get(); - } else if(ctx.data.contains("zFar")) { - this->clipFar = ctx.data["zFar"].get(); + if(ctx->data.contains("clipFar")) { + this->clipFar = ctx->data["clipFar"].get(); + } else if(ctx->data.contains("far")) { + this->clipFar = ctx->data["far"].get(); + } else if(ctx->data.contains("zFar")) { + this->clipFar = ctx->data["zFar"].get(); } } diff --git a/src/dawn/component/display/Camera.hpp b/src/dawn/component/display/Camera.hpp index deaa4d63..c04e6e9c 100644 --- a/src/dawn/component/display/Camera.hpp +++ b/src/dawn/component/display/Camera.hpp @@ -33,7 +33,7 @@ namespace Dawn { void onInit() override; void onDispose() override; - void load(SceneComponentLoadContext &ctx) override; + void load(std::shared_ptr ctx) override; /** * Returns the aspect ratio that the camera is using. In future I may diff --git a/src/dawn/component/display/material/SimpleTexturedMaterial.cpp b/src/dawn/component/display/material/SimpleTexturedMaterial.cpp index f882a5f8..ef2bbad7 100644 --- a/src/dawn/component/display/material/SimpleTexturedMaterial.cpp +++ b/src/dawn/component/display/material/SimpleTexturedMaterial.cpp @@ -9,14 +9,14 @@ using namespace Dawn; -void SimpleTexturedMaterial::load(SceneComponentLoadContext &ctx) { - if(ctx.data.contains("color")) { - this->setColor(JSON::color(ctx.data["color"])); +void SimpleTexturedMaterial::load(std::shared_ptr ctx) { + if(ctx->data.contains("color")) { + this->setColor(JSON::color(ctx->data["color"])); } - if(ctx.data.contains("texture")) { - auto asset = ctx.getAsset( - ctx.data["texture"].get() + if(ctx->data.contains("texture")) { + auto asset = ctx->getAsset( + ctx->data["texture"].get() ); this->setTexture(asset->getTexture()); } diff --git a/src/dawn/component/display/material/SimpleTexturedMaterial.hpp b/src/dawn/component/display/material/SimpleTexturedMaterial.hpp index f96f4f4b..ac13e8b4 100644 --- a/src/dawn/component/display/material/SimpleTexturedMaterial.hpp +++ b/src/dawn/component/display/material/SimpleTexturedMaterial.hpp @@ -15,7 +15,7 @@ namespace Dawn { std::shared_ptr texture; public: - void load(SceneComponentLoadContext &ctx) override; + void load(std::shared_ptr ctx) override; /** * Returns the color of this material. diff --git a/src/dawn/component/display/mesh/CubeMeshComponent.cpp b/src/dawn/component/display/mesh/CubeMeshComponent.cpp index 74b13140..c862bc42 100644 --- a/src/dawn/component/display/mesh/CubeMeshComponent.cpp +++ b/src/dawn/component/display/mesh/CubeMeshComponent.cpp @@ -10,10 +10,7 @@ using namespace Dawn; void CubeMeshComponent::onInit() { - if(!mesh) { - mesh = std::make_shared(); - } - + if(!mesh) mesh = std::make_shared(); mesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); CubeMesh::buffer( mesh, glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(1.0f, 1.0f, 1.0f), 0, 0 @@ -27,7 +24,7 @@ void CubeMeshComponent::onDispose() { mesh = nullptr; } -void CubeMeshComponent::load(SceneComponentLoadContext &ctx) { +void CubeMeshComponent::load(std::shared_ptr ctx) { + SceneComponent::load(ctx); if(!mesh) mesh = std::make_shared(); - } \ No newline at end of file diff --git a/src/dawn/component/display/mesh/CubeMeshComponent.hpp b/src/dawn/component/display/mesh/CubeMeshComponent.hpp index b4532e1f..ac62c2f9 100644 --- a/src/dawn/component/display/mesh/CubeMeshComponent.hpp +++ b/src/dawn/component/display/mesh/CubeMeshComponent.hpp @@ -14,6 +14,6 @@ namespace Dawn { void onInit() override; void onDispose() override; - void load(SceneComponentLoadContext &ctx) override; + void load(std::shared_ptr ctx) override; }; } \ No newline at end of file diff --git a/src/dawn/game/IGame.cpp b/src/dawn/game/IGame.cpp index 2b5e9d49..868d646f 100644 --- a/src/dawn/game/IGame.cpp +++ b/src/dawn/game/IGame.cpp @@ -6,7 +6,7 @@ #include "game/Game.hpp" #include "scene/Scene.hpp" #include "util/Flag.hpp" -#include "asset/loader/SceneLoader.hpp" +#include "asset/loader/scene/SceneLoader.hpp" #include "component/SceneComponentRegistry.hpp" // Components to register diff --git a/src/dawn/scene/SceneComponent.cpp b/src/dawn/scene/SceneComponent.cpp index a2e6985c..86e2aa31 100644 --- a/src/dawn/scene/SceneComponent.cpp +++ b/src/dawn/scene/SceneComponent.cpp @@ -59,8 +59,11 @@ std::shared_ptr SceneComponent::getGame() { return this->getScene()->getGame(); } -void SceneComponent::load(SceneComponentLoadContext &context) { +void SceneComponent::load(std::shared_ptr context) { // Override this method to load data from a JSON object. + if(context->data.contains("name")) { + this->name = context->data["name"].get(); + } } SceneComponent::~SceneComponent() { diff --git a/src/dawn/scene/SceneComponent.hpp b/src/dawn/scene/SceneComponent.hpp index 0d941b02..475d0181 100644 --- a/src/dawn/scene/SceneComponent.hpp +++ b/src/dawn/scene/SceneComponent.hpp @@ -4,15 +4,12 @@ // https://opensource.org/licenses/MIT #pragma once -#include "dawn.hpp" -#include "assert/assert.hpp" +#include "asset/loader/scene/SceneLoadContext.hpp" namespace Dawn { class Game; class Scene; class SceneItem; - class SceneComponent; - class AssetLoader; enum class SceneComponentState : flag_t { INITIAL = FLAG(0), @@ -20,38 +17,6 @@ namespace Dawn { DISPOSED = FLAG(2) }; - struct SceneComponentLoadContext { - json data; - std::shared_ptr scene; - std::unordered_map> items; - std::unordered_map> components; - std::unordered_map> assets; - - template - std::shared_ptr getAsset(const std::string &j) const { - auto it = assets.find(j); - assertTrue(it != assets.end(), "Asset %s not found.", j.c_str()); - auto asset = std::dynamic_pointer_cast(it->second); - assertNotNull(asset, "Asset is not of the correct type."); - return asset; - } - - void merge(const struct SceneComponentLoadContext &ctx) { - for(auto &item : ctx.items) { - if(items.find(item.first) != items.end()) continue; - this->items[item.first] = item.second; - } - for(auto &cmp : ctx.components) { - if(components.find(cmp.first) != components.end()) continue; - this->components[cmp.first] = cmp.second; - } - for(auto &asset : ctx.assets) { - if(assets.find(asset.first) != assets.end()) continue; - this->assets[asset.first] = asset.second; - } - } - }; - class SceneComponent : std::enable_shared_from_this { private: std::weak_ptr item; @@ -73,6 +38,8 @@ namespace Dawn { virtual void onDispose() = 0; public: + std::string name; + /** * Initializes this scene component. * @@ -119,7 +86,7 @@ namespace Dawn { * * @param json JSON Data that this object needs to load. */ - virtual void load(SceneComponentLoadContext &context); + virtual void load(std::shared_ptr context); /** * Disposes this scene component. diff --git a/src/dawn/scene/SceneItem.cpp b/src/dawn/scene/SceneItem.cpp index 3037dac2..d3af012f 100644 --- a/src/dawn/scene/SceneItem.cpp +++ b/src/dawn/scene/SceneItem.cpp @@ -7,7 +7,7 @@ #include "scene/Scene.hpp" #include "util/JSON.hpp" #include "assert/assert.hpp" -#include "asset/loader/PrefabLoader.hpp" +#include "asset/loader/scene/PrefabLoader.hpp" using namespace Dawn; @@ -76,22 +76,25 @@ void SceneItem::deinit() { this->components.clear(); } -void SceneItem::load(SceneComponentLoadContext &ctx) { +void SceneItem::load(std::shared_ptr ctx) { // Load prefab first, it can be overriden by other properties. - if(ctx.data.contains("prefab")) { - auto prefabLoader = ctx.getAsset( - ctx.data["prefab"].get() - ); + if(ctx->data.contains("prefab")) { + this->name = ctx->data["prefab"].get(); + auto prefabLoader = ctx->getAsset(this->name); prefabLoader->stagePrefab(shared_from_this()); } - // Transforms - if(ctx.data.contains("position")) { - this->setLocalPosition(JSON::vec3(ctx.data["position"])); + if(ctx->data.contains("name")) { + this->name = ctx->data["name"].get(); } - if(ctx.data.contains("lookAt")) { - auto &lookAt = ctx.data["lookAt"]; + // Transforms + if(ctx->data.contains("position")) { + this->setLocalPosition(JSON::vec3(ctx->data["position"])); + } + + if(ctx->data.contains("lookAt")) { + auto &lookAt = ctx->data["lookAt"]; glm::vec3 pos = glm::vec3(3, 3, 3); glm::vec3 look = glm::vec3(0, 0, 0); glm::vec3 up = glm::vec3(0, 1, 0); @@ -102,8 +105,8 @@ void SceneItem::load(SceneComponentLoadContext &ctx) { this->lookAt(pos, look, up); } - if(ctx.data.contains("scale")) { - this->setLocalScale(JSON::vec3(ctx.data["scale"])); + if(ctx->data.contains("scale")) { + this->setLocalScale(JSON::vec3(ctx->data["scale"])); } } diff --git a/src/dawn/scene/SceneItem.hpp b/src/dawn/scene/SceneItem.hpp index 06d7fcb8..bd8a4bd9 100644 --- a/src/dawn/scene/SceneItem.hpp +++ b/src/dawn/scene/SceneItem.hpp @@ -29,6 +29,8 @@ namespace Dawn { std::shared_ptr sceneItemComponentsSelf() override; public: + std::string name; + /** * Constructs a scene item. * @@ -55,7 +57,7 @@ namespace Dawn { * * @param ctx Context to load this scene item from. */ - void load(SceneComponentLoadContext &ctx); + void load(std::shared_ptr ctx); /** * Returns the scene that this scene item belongs to. diff --git a/src/dawnrpg/component/RPGPlayer.cpp b/src/dawnrpg/component/RPGPlayer.cpp index c11de14b..37903d50 100644 --- a/src/dawnrpg/component/RPGPlayer.cpp +++ b/src/dawnrpg/component/RPGPlayer.cpp @@ -71,12 +71,12 @@ void RPGPlayer::onDispose() { this->rpgEntity = nullptr; } -void RPGPlayer::load(SceneComponentLoadContext &ctx) { +void RPGPlayer::load(std::shared_ptr ctx) { SceneComponent::load(ctx); - if(ctx.data.contains("camera")) { - assertMapHasKey(ctx.components, ctx.data["camera"], "Camera not found!"); - const auto component = ctx.components[ctx.data["camera"].get()]; + if(ctx->data.contains("camera")) { + assertMapHasKey(ctx->components, ctx->data["camera"], "Camera not found!"); + const auto component = ctx->components[ctx->data["camera"].get()]; this->camera = std::dynamic_pointer_cast(component); assertNotNull(this->camera, "Camera not found!"); } diff --git a/src/dawnrpg/component/RPGPlayer.hpp b/src/dawnrpg/component/RPGPlayer.hpp index 51a55a02..a2a9a358 100644 --- a/src/dawnrpg/component/RPGPlayer.hpp +++ b/src/dawnrpg/component/RPGPlayer.hpp @@ -18,6 +18,6 @@ namespace Dawn { void onInit() override; void onDispose() override; - void load(SceneComponentLoadContext &ctx) override; + void load(std::shared_ptr ctx) override; }; } \ No newline at end of file