From e660fade9557f26342c41ffc6b6a00286c71282a Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 2 Dec 2024 20:00:54 -0600 Subject: [PATCH] Added prefabs --- assets/test_cube.json | 27 ++++ assets/test_scene.json | 17 ++- src/dawn/CMakeLists.txt | 1 - src/dawn/asset/AssetLoader.hpp | 1 + src/dawn/asset/AssetManager.cpp | 25 ++-- src/dawn/asset/loader/CMakeLists.txt | 2 + src/dawn/asset/loader/JSONLoader.cpp | 1 + src/dawn/asset/loader/LoaderForSceneItems.cpp | 57 +++++++++ src/dawn/asset/loader/LoaderForSceneItems.hpp | 30 +++++ src/dawn/asset/loader/PrefabLoader.cpp | 97 +++++++++++++++ src/dawn/asset/loader/PrefabLoader.hpp | 45 +++++++ src/dawn/asset/loader/SceneLoader.cpp | 62 +++------- src/dawn/asset/loader/SceneLoader.hpp | 16 +-- src/dawn/asset/loader/TextureLoader.cpp | 1 + src/dawn/asset/loader/TrueTypeLoader.cpp | 1 + src/dawn/component/SceneComponentRegistry.cpp | 13 ++ src/dawn/prefab/CMakeLists.txt | 9 -- src/dawn/prefab/SimpleSpinningCube.cpp | 49 -------- src/dawn/prefab/SimpleSpinningCube.hpp | 11 -- src/dawn/scene/SceneItem.cpp | 9 ++ src/dawnglfw/display/RenderHost.cpp | 1 - src/dawnlinux/main.cpp | 3 +- src/dawnopengl/display/Texture.cpp | 1 - src/dawnrpg/CMakeLists.txt | 1 + src/dawnrpg/scene/CMakeLists.txt | 1 - src/dawnrpg/scene/HelloWorldScene.cpp | 117 ------------------ src/dawnrpg/scene/RPGScene.cpp | 1 - src/dawnrpg/scene/SceneList.hpp | 1 - 28 files changed, 332 insertions(+), 268 deletions(-) create mode 100644 assets/test_cube.json create mode 100644 src/dawn/asset/loader/LoaderForSceneItems.cpp create mode 100644 src/dawn/asset/loader/LoaderForSceneItems.hpp create mode 100644 src/dawn/asset/loader/PrefabLoader.cpp create mode 100644 src/dawn/asset/loader/PrefabLoader.hpp delete mode 100644 src/dawn/prefab/CMakeLists.txt delete mode 100644 src/dawn/prefab/SimpleSpinningCube.cpp delete mode 100644 src/dawn/prefab/SimpleSpinningCube.hpp delete mode 100644 src/dawnrpg/scene/HelloWorldScene.cpp diff --git a/assets/test_cube.json b/assets/test_cube.json new file mode 100644 index 00000000..3f0c13fd --- /dev/null +++ b/assets/test_cube.json @@ -0,0 +1,27 @@ +{ + "name": "Test Cube", + + "assets": { + "rosa": { + "type": "texture", + "path": "rosa.texture" + } + }, + + "position": [ 0, 0, 0 ], + + "components": { + "mat": { + "type": "SimpleTexturedMaterial", + "color": "blue" + }, + + "renderer": { + "type": "MeshRenderer" + }, + + "mesh": { + "type": "CubeMesh" + } + } +} \ No newline at end of file diff --git a/assets/test_scene.json b/assets/test_scene.json index 9b85ae46..b7531eb9 100644 --- a/assets/test_scene.json +++ b/assets/test_scene.json @@ -4,32 +4,29 @@ "rosa": { "type": "texture", "path": "rosa.texture" + }, + "cube": { + "type": "prefab", + "path": "test_cube.json" } }, "items": { "camera": { "lookAt": { - "position": [ 5, 5, 5 ], + "position": [ 3, 3, 3 ], "look": [ 0, 0, 0 ], "view": [ 0, 1, 0 ] }, "components": { "camera": { - "type": "Camera", - "fov": 90 + "type": "Camera" } } }, "cube": { - "position": [ 0, 0, 0 ], - "scale": [ 3, 3, 3 ], - "components": { - "mat": { "type": "SimpleTexturedMaterial", "color": "blue", "texture": "rosa" }, - "renderer": { "type": "MeshRenderer" }, - "mesh": { "type": "CubeMesh" } - } + "prefab": "cube" } } } \ No newline at end of file diff --git a/src/dawn/CMakeLists.txt b/src/dawn/CMakeLists.txt index 6ef267b5..5f1f18d3 100644 --- a/src/dawn/CMakeLists.txt +++ b/src/dawn/CMakeLists.txt @@ -28,7 +28,6 @@ add_subdirectory(display) add_subdirectory(environment) add_subdirectory(game) add_subdirectory(locale) -add_subdirectory(prefab) add_subdirectory(save) add_subdirectory(scene) add_subdirectory(time) diff --git a/src/dawn/asset/AssetLoader.hpp b/src/dawn/asset/AssetLoader.hpp index a881bf13..3c4f6be9 100644 --- a/src/dawn/asset/AssetLoader.hpp +++ b/src/dawn/asset/AssetLoader.hpp @@ -14,6 +14,7 @@ namespace Dawn { std::weak_ptr assetManager; public: + std::string typetest; const static std::string ASSET_TYPE; const std::string name; diff --git a/src/dawn/asset/AssetManager.cpp b/src/dawn/asset/AssetManager.cpp index c1d67ec8..9f2e2f27 100644 --- a/src/dawn/asset/AssetManager.cpp +++ b/src/dawn/asset/AssetManager.cpp @@ -14,18 +14,29 @@ void AssetManager::init(const std::shared_ptr &game) { } void AssetManager::update() { - auto itPending = pendingAssetLoaders.begin(); - while(itPending != pendingAssetLoaders.end()) { + auto copyPendingAssets = pendingAssetLoaders; + auto itPending = copyPendingAssets.begin(); + while(itPending != copyPendingAssets.end()) { auto loader = *itPending; + loader->updateSync(); loader->updateAsync(); loader->updateSync(); - if(loader->loaded) { - finishedAssetLoaders.push_back(loader); - itPending = pendingAssetLoaders.erase(itPending); - } else { - itPending++; + + if(!loader->loaded) { + ++itPending; + continue; } + + finishedAssetLoaders.push_back(loader); + auto it = std::find( + pendingAssetLoaders.begin(), + pendingAssetLoaders.end(), + loader + ); + assertTrue(it != pendingAssetLoaders.end(), "Loader not found?"); + pendingAssetLoaders.erase(it); + ++itPending; } } diff --git a/src/dawn/asset/loader/CMakeLists.txt b/src/dawn/asset/loader/CMakeLists.txt index 71ad9480..263f40b1 100644 --- a/src/dawn/asset/loader/CMakeLists.txt +++ b/src/dawn/asset/loader/CMakeLists.txt @@ -10,4 +10,6 @@ target_sources(${DAWN_TARGET_NAME} JSONLoader.cpp TrueTypeLoader.cpp SceneLoader.cpp + LoaderForSceneItems.cpp + PrefabLoader.cpp ) \ No newline at end of file diff --git a/src/dawn/asset/loader/JSONLoader.cpp b/src/dawn/asset/loader/JSONLoader.cpp index 5e77a223..6323ec75 100644 --- a/src/dawn/asset/loader/JSONLoader.cpp +++ b/src/dawn/asset/loader/JSONLoader.cpp @@ -17,6 +17,7 @@ JSONLoader::JSONLoader( loader(name), state(JSONLoaderState::INITIAL) { + this->typetest = this->getAssetType(); } void JSONLoader::updateAsync() { diff --git a/src/dawn/asset/loader/LoaderForSceneItems.cpp b/src/dawn/asset/loader/LoaderForSceneItems.cpp new file mode 100644 index 00000000..451b3f2e --- /dev/null +++ b/src/dawn/asset/loader/LoaderForSceneItems.cpp @@ -0,0 +1,57 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "LoaderForSceneItems.hpp" +#include "asset/loader/TextureLoader.hpp" +#include "asset/loader/PrefabLoader.hpp" + +using namespace Dawn; + +LoaderForSceneItems::LoaderForSceneItems( + const std::shared_ptr assetManager, + const std::string name +) : AssetLoader(assetManager, name) { +} + +void LoaderForSceneItems::setupDependencies() { + // Begin loading dependencies. + auto &data = this->jsonLoader->data; + if(data.contains("assets")) { + for(auto &asset : data["assets"].items()) { + auto &assetName = asset.key(); + auto &assetData = asset.value(); + assertTrue(assetData.contains("type"), "Asset missing type"); + assertTrue(assetData.contains("path"), "Asset missing path"); + auto type = assetData["type"].get(); + auto path = assetData["path"].get(); + + std::shared_ptr loader; + if(type == "texture") { + loader = getAssetManager()->get(path); + + } else if(type == "json") { + loader = getAssetManager()->get(path); + + } else if(type == "prefab") { + loader = getAssetManager()->get(path); + + } else { + assertUnreachable("Unknown asset type: %s", type.c_str()); + } + + assertMapNotHasKey( + ctx.assets, + assetName, + "Asset already exists: %s", assetName.c_str() + ); + + ctx.assets[assetName] = loader; + } + } +} + +LoaderForSceneItems::~LoaderForSceneItems() { + +} \ No newline at end of file diff --git a/src/dawn/asset/loader/LoaderForSceneItems.hpp b/src/dawn/asset/loader/LoaderForSceneItems.hpp new file mode 100644 index 00000000..0a16b2ce --- /dev/null +++ b/src/dawn/asset/loader/LoaderForSceneItems.hpp @@ -0,0 +1,30 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "JSONLoader.hpp" +#include "scene/Scene.hpp" + +namespace Dawn { + class LoaderForSceneItems : public AssetLoader { + protected: + std::shared_ptr jsonLoader; + struct SceneComponentLoadContext ctx; + + /** + * Loads the dependencies into the context for the data available in + * the jsonLoader. + */ + void setupDependencies(); + + public: + LoaderForSceneItems( + const std::shared_ptr assetManager, + const std::string name + ); + + ~LoaderForSceneItems(); + }; +} \ No newline at end of file diff --git a/src/dawn/asset/loader/PrefabLoader.cpp b/src/dawn/asset/loader/PrefabLoader.cpp new file mode 100644 index 00000000..4f7674dc --- /dev/null +++ b/src/dawn/asset/loader/PrefabLoader.cpp @@ -0,0 +1,97 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "PrefabLoader.hpp" +#include "game/Game.hpp" +#include "assert/assert.hpp" +#include "asset/loader/TextureLoader.hpp" +#include "scene/Scene.hpp" +#include "component/SceneComponentRegistry.hpp" + +using namespace Dawn; + +const std::string PrefabLoader::ASSET_TYPE = "prefab"; + +PrefabLoader::PrefabLoader( + const std::shared_ptr assetManager, + const std::string name +) : + LoaderForSceneItems(assetManager, name) +{ + this->typetest = this->getAssetType(); +} + +void PrefabLoader::updateSync() { + switch(this->state) { + case PrefabLoaderState::INITIAL: + jsonLoader = getAssetManager()->get(this->name); + this->state = PrefabLoaderState::LOADING_JSON; + break; + + case PrefabLoaderState::LOADING_JSON: + assertNotNull(this->jsonLoader, "JSON Loader is NULL?"); + if(!this->jsonLoader->loaded) return; + this->state = PrefabLoaderState::LOADING_DEPENDENCIES; + break; + + case PrefabLoaderState::LOADING_DEPENDENCIES: + assertTrue(this->jsonLoader->loaded, "JSON loader not loaded?"); + + // Check if all dependencies are loaded. + for(auto &asset : ctx.assets) { + if(!asset.second->loaded) return; + } + + this->state = PrefabLoaderState::DEPENDENCIES_LOADED; + this->loaded = true; + + + break; + + default: + break; + } +} + +void PrefabLoader::updateAsync() { +} + +std::string PrefabLoader::getAssetType() const { + return PrefabLoader::ASSET_TYPE; +} + +void PrefabLoader::stagePrefab(std::shared_ptr item) { + assertTrue(this->loaded, "Prefab not loaded"); + + ctx.scene = item->getScene(); + ctx.data = this->jsonLoader->data; + + item->load(ctx); + + auto &itemData = this->jsonLoader->data; + if(itemData.contains("components")) { + for(auto &cmpItem : itemData["components"].items()) { + auto &cmpName = cmpItem.key(); + auto &cmpData = cmpItem.value(); + assertTrue( + cmpData.contains("type"), + "Component missing type in %s", + cmpName.c_str() + ); + + auto cmpType = cmpData["type"].get(); + auto cmp = SceneComponentRegistry::createComponent( + cmpType, item + ); + ctx.data = cmpData; + ctx.components[cmpName] = cmp; + cmp->load(ctx); + } + } +} + +PrefabLoader::~PrefabLoader() { + +} \ No newline at end of file diff --git a/src/dawn/asset/loader/PrefabLoader.hpp b/src/dawn/asset/loader/PrefabLoader.hpp new file mode 100644 index 00000000..50f2fedf --- /dev/null +++ b/src/dawn/asset/loader/PrefabLoader.hpp @@ -0,0 +1,45 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "LoaderForSceneItems.hpp" +#include "scene/Scene.hpp" + +namespace Dawn { + enum class PrefabLoaderState { + INITIAL, + LOADING_JSON, + LOADING_DEPENDENCIES, + DEPENDENCIES_LOADED, + DONE + }; + + class PrefabLoader : public LoaderForSceneItems { + protected: + PrefabLoaderState state = PrefabLoaderState::INITIAL; + std::shared_ptr item; + + public: + const static std::string ASSET_TYPE; + + PrefabLoader( + const std::shared_ptr assetManager, + const std::string name + ); + + void updateSync() override; + void updateAsync() override; + std::string getAssetType() const override; + + /** + * Stages this prefab onto a scene item. + * + * @param item The scene item to stage. + */ + void stagePrefab(std::shared_ptr item); + + ~PrefabLoader(); + }; +} \ No newline at end of file diff --git a/src/dawn/asset/loader/SceneLoader.cpp b/src/dawn/asset/loader/SceneLoader.cpp index 01c4d56d..d44105c9 100644 --- a/src/dawn/asset/loader/SceneLoader.cpp +++ b/src/dawn/asset/loader/SceneLoader.cpp @@ -6,7 +6,6 @@ #include "SceneLoader.hpp" #include "game/Game.hpp" #include "assert/assert.hpp" -#include "asset/loader/TextureLoader.hpp" #include "scene/Scene.hpp" #include "component/SceneComponentRegistry.hpp" @@ -18,43 +17,10 @@ SceneLoader::SceneLoader( const std::shared_ptr assetManager, const std::string name ) : - AssetLoader(assetManager, name), + LoaderForSceneItems(assetManager, name), state(SceneLoaderState::INITIAL) { -} - -void SceneLoader::setupDependencies() { - std::cout << "Setting up dependencies" << std::endl; - // Begin loading dependencies. - auto &data = this->jsonLoader->data; - if(data.contains("assets")) { - for(auto &asset : data["assets"].items()) { - auto &assetName = asset.key(); - auto &assetData = asset.value(); - assertTrue(assetData.contains("type"), "Asset missing type"); - assertTrue(assetData.contains("path"), "Asset missing path"); - auto type = assetData["type"].get(); - auto path = assetData["path"].get(); - - std::shared_ptr loader; - if(type == "texture") { - loader = getAssetManager()->get(path); - } else if(type == "json") { - loader = getAssetManager()->get(path); - } else { - assertUnreachable("Unknown asset type: %s", type.c_str()); - } - - assertMapNotHasKey( - ctx.assets, - assetName, - "Asset already exists: %s", assetName.c_str() - ); - - ctx.assets[assetName] = loader; - } - } - this->state = SceneLoaderState::LOADING_DEPENDENCIES; + this->typetest = this->getAssetType(); } void SceneLoader::updateAsync() { @@ -68,18 +34,15 @@ void SceneLoader::updateAsync() { assertNotNull(this->jsonLoader, "JSON Loader is NULL?"); if(!this->jsonLoader->loaded) return; this->setupDependencies(); + this->state = SceneLoaderState::LOADING_DEPENDENCIES; break; case SceneLoaderState::LOADING_DEPENDENCIES: + assertTrue(this->jsonLoader->loaded, "JSON loader not loaded?"); // Check if all dependencies are loaded. for(auto &asset : ctx.assets) { if(!asset.second->loaded) return; } - this->state = SceneLoaderState::DEPENDENCIES_LOADED; - break; - - case SceneLoaderState::DEPENDENCIES_LOADED: - std::cout << "Deps Loaded" << std::endl; ctx.scene = std::make_shared(this->getAssetManager()->getGame()); this->state = SceneLoaderState::PENDING_STAGE; break; @@ -91,6 +54,7 @@ void SceneLoader::updateAsync() { void SceneLoader::updateSync() { if(this->state != SceneLoaderState::PENDING_STAGE) return; + assertTrue(this->jsonLoader->loaded, "JSON loader not loaded?"); auto &data = this->jsonLoader->data; if(data.contains("items")) { @@ -115,10 +79,16 @@ void SceneLoader::updateSync() { for(auto &cmpItem : itemData["components"].items()) { auto &cmpName = cmpItem.key(); auto &cmpData = cmpItem.value(); - assertTrue(cmpData.contains("type"), "Component missing type in %s", itemName.c_str()); + assertTrue( + cmpData.contains("type"), + "Component missing type in %s", + cmpName.c_str() + ); auto cmpType = cmpData["type"].get(); - auto cmp = SceneComponentRegistry::createComponent(cmpType, sceneItem); + auto cmp = SceneComponentRegistry::createComponent( + cmpType, sceneItem + ); ctx.data = cmpData; ctx.components[cmpName] = cmp; cmp->load(ctx); @@ -130,6 +100,11 @@ 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 { @@ -143,5 +118,4 @@ std::shared_ptr SceneLoader::getScene() { SceneLoader::~SceneLoader() { ctx = {}; jsonLoader = nullptr; - std::cout << "Scene Loader removed" << std::endl; } \ No newline at end of file diff --git a/src/dawn/asset/loader/SceneLoader.hpp b/src/dawn/asset/loader/SceneLoader.hpp index 42da2a26..a78ff216 100644 --- a/src/dawn/asset/loader/SceneLoader.hpp +++ b/src/dawn/asset/loader/SceneLoader.hpp @@ -4,7 +4,7 @@ // https://opensource.org/licenses/MIT #pragma once -#include "JSONLoader.hpp" +#include "LoaderForSceneItems.hpp" #include "scene/Scene.hpp" namespace Dawn { @@ -12,24 +12,13 @@ namespace Dawn { INITIAL, LOADING_JSON, LOADING_DEPENDENCIES, - DEPENDENCIES_LOADED, PENDING_STAGE, DONE }; - class SceneLoader : - public AssetLoader, - public std::enable_shared_from_this - { + class SceneLoader : public LoaderForSceneItems { protected: SceneLoaderState state; - std::shared_ptr jsonLoader; - struct SceneComponentLoadContext ctx; - - /** - * Loads the dependencies of the scene. - */ - void setupDependencies(); public: const static std::string ASSET_TYPE; @@ -38,6 +27,7 @@ namespace Dawn { const std::shared_ptr assetManager, const std::string name ); + void updateSync() override; void updateAsync() override; std::string getAssetType() const override; diff --git a/src/dawn/asset/loader/TextureLoader.cpp b/src/dawn/asset/loader/TextureLoader.cpp index fb3b5503..c4b5a2b6 100644 --- a/src/dawn/asset/loader/TextureLoader.cpp +++ b/src/dawn/asset/loader/TextureLoader.cpp @@ -18,6 +18,7 @@ TextureLoader::TextureLoader( loader(name), state(TextureLoaderLoadState::INITIAL) { + this->typetest = this->getAssetType(); texture = std::make_shared(); } diff --git a/src/dawn/asset/loader/TrueTypeLoader.cpp b/src/dawn/asset/loader/TrueTypeLoader.cpp index 60d03f89..0b592cb5 100644 --- a/src/dawn/asset/loader/TrueTypeLoader.cpp +++ b/src/dawn/asset/loader/TrueTypeLoader.cpp @@ -17,6 +17,7 @@ TrueTypeLoader::TrueTypeLoader( AssetLoader(assetManager, name), loader(name) { + this->typetest = this->getAssetType(); // Init the font. auto ret = FT_Init_FreeType(&fontLibrary); assertTrue(ret == 0, "Failed to initialize FreeType library."); diff --git a/src/dawn/component/SceneComponentRegistry.cpp b/src/dawn/component/SceneComponentRegistry.cpp index 8e142446..9c6f2dcb 100644 --- a/src/dawn/component/SceneComponentRegistry.cpp +++ b/src/dawn/component/SceneComponentRegistry.cpp @@ -10,6 +10,11 @@ #include "component/display/MeshRenderer.hpp" #include "component/display/mesh/CubeMeshComponent.hpp" +#ifdef DAWN_ENABLE_PHYSICS + #include "component/physics/CubeCollider.hpp" + #include "component/physics/SphereCollider.hpp" +#endif + using namespace Dawn; std::shared_ptr SceneComponentRegistry::createComponent( @@ -31,6 +36,14 @@ std::shared_ptr SceneComponentRegistry::createComponent( } else if(type == "CubeMesh" || type == "CubeMeshComponent") { return item->addComponent(); + #ifdef DAWN_ENABLE_PHYSICS + } else if(type == "CubeCollider") { + return item->addComponent(); + + } else if(type == "SphereCollider") { + return item->addComponent(); + #endif + } else { assertUnreachable("Unknown component type: %s", type.c_str()); } diff --git a/src/dawn/prefab/CMakeLists.txt b/src/dawn/prefab/CMakeLists.txt deleted file mode 100644 index 013175a6..00000000 --- a/src/dawn/prefab/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -# Copyright (c) 2023 Dominic Masters -# -# This software is released under the MIT License. -# https://opensource.org/licenses/MIT - -target_sources(${DAWN_TARGET_NAME} - PRIVATE - SimpleSpinningCube.cpp -) \ No newline at end of file diff --git a/src/dawn/prefab/SimpleSpinningCube.cpp b/src/dawn/prefab/SimpleSpinningCube.cpp deleted file mode 100644 index 0e32ad5b..00000000 --- a/src/dawn/prefab/SimpleSpinningCube.cpp +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "SimpleSpinningCube.hpp" -#include "component/display/MeshRenderer.hpp" -#include "component/display/material/SimpleTexturedMaterial.hpp" -#include "display/mesh/CubeMesh.hpp" -#include "component/SimpleComponent.hpp" - -using namespace Dawn; - -std::shared_ptr Dawn::createSimpleSpinningCube(Scene &s) { - // Create the scene item. - auto cubeItem = s.createSceneItem(); - - // Create a simple cube mesh. - auto cubeMesh = std::make_shared(); - cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); - CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0); - - // Add a renderer to the scene item. - auto cubeMeshRenderer = cubeItem->addComponent(); - cubeMeshRenderer->mesh = cubeMesh; - - // Add a material to the scene item. - auto cubeMaterial = cubeItem->addComponent(); - cubeMaterial->setColor(COLOR_MAGENTA); - - // Add a simple event listener component to the scene item. - addSimpleComponent(cubeItem, [](auto &cmp, auto &events) { - // Note that add component cannot receive a self reference, so we must - // capture the component by reference instead. - events.push_back(cmp.getScene()->onUnpausedUpdate.listen([&]( - float_t delta - ) { - // Since we captured within the lambda, we can access the component - // directly. - auto item = cmp.getItem(); - item->setLocalRotation( - item->getLocalRotation() * - glm::quat(glm::vec3(1, 1, 0) * delta) - ); - })); - }); - - return cubeItem; -} \ No newline at end of file diff --git a/src/dawn/prefab/SimpleSpinningCube.hpp b/src/dawn/prefab/SimpleSpinningCube.hpp deleted file mode 100644 index 8107683f..00000000 --- a/src/dawn/prefab/SimpleSpinningCube.hpp +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "scene/Scene.hpp" - -namespace Dawn { - std::shared_ptr createSimpleSpinningCube(Scene &s); -} \ No newline at end of file diff --git a/src/dawn/scene/SceneItem.cpp b/src/dawn/scene/SceneItem.cpp index b0e5896d..accafd8b 100644 --- a/src/dawn/scene/SceneItem.cpp +++ b/src/dawn/scene/SceneItem.cpp @@ -7,6 +7,7 @@ #include "scene/Scene.hpp" #include "util/JSON.hpp" #include "assert/assert.hpp" +#include "asset/loader/PrefabLoader.hpp" using namespace Dawn; @@ -76,6 +77,14 @@ void SceneItem::deinit() { } void SceneItem::load(const SceneComponentLoadContext &ctx) { + // Load prefab first, it can be overriden by other properties. + if(ctx.data.contains("prefab")) { + auto prefabLoader = ctx.getAsset( + ctx.data["prefab"].get() + ); + prefabLoader->stagePrefab(shared_from_this()); + } + // Transforms if(ctx.data.contains("position")) { this->setLocalPosition(JSON::vec3(ctx.data["position"])); diff --git a/src/dawnglfw/display/RenderHost.cpp b/src/dawnglfw/display/RenderHost.cpp index ff0187e6..fa0f6471 100644 --- a/src/dawnglfw/display/RenderHost.cpp +++ b/src/dawnglfw/display/RenderHost.cpp @@ -129,7 +129,6 @@ std::shared_ptr RenderHost::getBackBufferRenderTarget() { } RenderHost::~RenderHost() { - std::cout << "RenderHost cleanup" << std::endl; if(this->window != nullptr) { glfwDestroyWindow(this->window); this->window = nullptr; diff --git a/src/dawnlinux/main.cpp b/src/dawnlinux/main.cpp index c1d8ca27..d786f7f3 100644 --- a/src/dawnlinux/main.cpp +++ b/src/dawnlinux/main.cpp @@ -60,8 +60,7 @@ int32_t main(int32_t argc, const char **argv) { }); if(matchingPathIfAny == pathsToTryAndFindAssets.end()) { - std::cout << "Could not find game assets" << std::endl; - return 1; + assertUnreachable("Could not find game assets"); } environment.setVariable("assetsPath", matchingPathIfAny->string()); diff --git a/src/dawnopengl/display/Texture.cpp b/src/dawnopengl/display/Texture.cpp index f124dd30..f3631ff8 100644 --- a/src/dawnopengl/display/Texture.cpp +++ b/src/dawnopengl/display/Texture.cpp @@ -185,7 +185,6 @@ void Texture::buffer(const struct ColorU8 pixels[]) { } void Texture::buffer(const struct Color pixels[]) { - std::cout << "Correct buffer" << std::endl; assertTrue( this->dataFormat == TextureDataFormat::FLOAT, "Texture data format must be float!" diff --git a/src/dawnrpg/CMakeLists.txt b/src/dawnrpg/CMakeLists.txt index 0b0627f9..e66bc1f6 100644 --- a/src/dawnrpg/CMakeLists.txt +++ b/src/dawnrpg/CMakeLists.txt @@ -16,4 +16,5 @@ add_subdirectory(scene) # Assets tool_texture(rosa rosa.png) +tool_copy(test_cube test_cube.json) tool_copy(test_scene test_scene.json) \ No newline at end of file diff --git a/src/dawnrpg/scene/CMakeLists.txt b/src/dawnrpg/scene/CMakeLists.txt index 37c0abe0..ec00be40 100644 --- a/src/dawnrpg/scene/CMakeLists.txt +++ b/src/dawnrpg/scene/CMakeLists.txt @@ -5,6 +5,5 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE - HelloWorldScene.cpp RPGScene.cpp ) \ No newline at end of file diff --git a/src/dawnrpg/scene/HelloWorldScene.cpp b/src/dawnrpg/scene/HelloWorldScene.cpp deleted file mode 100644 index 3f87421c..00000000 --- a/src/dawnrpg/scene/HelloWorldScene.cpp +++ /dev/null @@ -1,117 +0,0 @@ - -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "scene/SceneList.hpp" -#include "component/SimpleComponent.hpp" -#include "component/display/Camera.hpp" -#include "component/display/material/SimpleTexturedMaterial.hpp" -#include "component/display/MeshRenderer.hpp" -#include "display/mesh/CubeMesh.hpp" -#include "display/mesh/SphereMesh.hpp" -#include "component/physics/CubeCollider.hpp" -#include "component/physics/SphereCollider.hpp" - -using namespace Dawn; - -void Dawn::helloWorldScene(Scene &s) { - auto cameraItem = s.createSceneItem(); - auto camera = cameraItem->addComponent(); - cameraItem->lookAt({ 20, 20, 20 }, { 0, 0, 0 }, { 0, 1, 0 }); - camera->clipFar = 99999.99f; - - // Ground - { - // Create the scene item. - auto groundItem = s.createSceneItem(); - groundItem->setLocalPosition(glm::vec3(0, 0, 0)); - - // Create a simple cube mesh. - auto groundMesh = std::make_shared(); - groundMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); - CubeMesh::buffer(groundMesh, glm::vec3(-15, -1, -15), glm::vec3(30, 2, 30), 0, 0); - - // Add a renderer to the scene item. - auto groundMeshRenderer = groundItem->addComponent(); - groundMeshRenderer->mesh = groundMesh; - - // Add a material to the scene item. - auto groundMaterial = groundItem->addComponent(); - groundMaterial->setColor(COLOR_LIGHT_GREY); - - // Add collider - auto groundCollider = groundItem->addComponent(); - groundCollider->setColliderType(ColliderType::STATIC); - groundCollider->setShape(glm::vec3(15, 1, 15)); - } - - // Box - { - // Create the scene item. - auto cubeItem = s.createSceneItem(); - cubeItem->setLocalPosition(glm::vec3(0, 10, 0)); - - // Create a simple cube mesh. - auto cubeMesh = std::make_shared(); - cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); - CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0); - - // Add a renderer to the scene item. - auto cubeMeshRenderer = cubeItem->addComponent(); - cubeMeshRenderer->mesh = cubeMesh; - - // Add a material to the scene item. - auto cubeMaterial = cubeItem->addComponent(); - cubeMaterial->setColor(COLOR_MAGENTA); - - // Add collider - auto cubeCollider = cubeItem->addComponent(); - } - - // Other Box - { - // Create the scene item. - auto cubeItem = s.createSceneItem(); - cubeItem->setLocalPosition(glm::vec3(0.75f, 15, 0.1f)); - - // Create a simple cube mesh. - auto cubeMesh = std::make_shared(); - cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); - CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0); - - // Add a renderer to the scene item. - auto cubeMeshRenderer = cubeItem->addComponent(); - cubeMeshRenderer->mesh = cubeMesh; - - // Add a material to the scene item. - auto cubeMaterial = cubeItem->addComponent(); - cubeMaterial->setColor(COLOR_MAGENTA); - - // Add collider - auto cubeCollider = cubeItem->addComponent(); - } - - // Ball - { - // Create the scene item. - auto sphereItem = s.createSceneItem(); - sphereItem->setLocalPosition(glm::vec3(-1.0f, 13, -0.6f)); - - // Create a simple cube mesh. - auto sphereMesh = std::make_shared(); - SphereMesh::create(sphereMesh, 1.0f); - - // Add a renderer to the scene item. - auto sphereMeshRenderer = sphereItem->addComponent(); - sphereMeshRenderer->mesh = sphereMesh; - - // Add a material to the scene item. - auto sphereMaterial = sphereItem->addComponent(); - sphereMaterial->setColor(COLOR_CYAN); - - // Add collider - auto sphereCollider = sphereItem->addComponent(); - } -} \ No newline at end of file diff --git a/src/dawnrpg/scene/RPGScene.cpp b/src/dawnrpg/scene/RPGScene.cpp index 1631562d..ae90b6d7 100644 --- a/src/dawnrpg/scene/RPGScene.cpp +++ b/src/dawnrpg/scene/RPGScene.cpp @@ -6,7 +6,6 @@ #include "scene/SceneList.hpp" #include "component/display/Camera.hpp" -#include "prefab/SimpleSpinningCube.hpp" #include "component/display/material/SimpleTexturedMaterial.hpp" #include "component/ui/UICanvas.hpp" #include "asset/loader/TextureLoader.hpp" diff --git a/src/dawnrpg/scene/SceneList.hpp b/src/dawnrpg/scene/SceneList.hpp index b47d4de1..5da108fc 100644 --- a/src/dawnrpg/scene/SceneList.hpp +++ b/src/dawnrpg/scene/SceneList.hpp @@ -7,6 +7,5 @@ #include "scene/Scene.hpp" namespace Dawn { - void helloWorldScene(Scene &s); void rpgScene(Scene &s); } \ No newline at end of file