From 4dccd7d969175c697ea4a7acfa9219425aa4b725 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 1 Dec 2024 13:27:36 -0600 Subject: [PATCH] Starting scene loader. --- assets/test_scene.json | 0 src/dawn/asset/AssetLoader.cpp | 15 +- src/dawn/asset/AssetLoader.hpp | 18 ++- src/dawn/asset/AssetManager.hpp | 33 +--- src/dawn/asset/loader/CMakeLists.txt | 1 + src/dawn/asset/loader/JSONLoader.cpp | 7 +- src/dawn/asset/loader/JSONLoader.hpp | 5 +- src/dawn/asset/loader/SceneLoader.cpp | 44 ++++++ src/dawn/asset/loader/SceneLoader.hpp | 40 +++++ src/dawn/asset/loader/TextureLoader.cpp | 7 +- src/dawn/asset/loader/TextureLoader.hpp | 16 +- src/dawn/asset/loader/TrueTypeLoader.cpp | 7 +- src/dawn/asset/loader/TrueTypeLoader.hpp | 15 +- src/dawn/dawn.hpp | 3 +- src/dawn/game/IGame.cpp | 13 +- src/dawnhelloworld/scene/HelloWorldScene.cpp | 158 ++++++++++--------- src/dawnrpg/CMakeLists.txt | 3 +- src/dawnrpg/game/Game.cpp | 2 +- 18 files changed, 251 insertions(+), 136 deletions(-) create mode 100644 assets/test_scene.json create mode 100644 src/dawn/asset/loader/SceneLoader.cpp create mode 100644 src/dawn/asset/loader/SceneLoader.hpp diff --git a/assets/test_scene.json b/assets/test_scene.json new file mode 100644 index 00000000..e69de29b diff --git a/src/dawn/asset/AssetLoader.cpp b/src/dawn/asset/AssetLoader.cpp index 7d8be236..69ebb4cc 100644 --- a/src/dawn/asset/AssetLoader.cpp +++ b/src/dawn/asset/AssetLoader.cpp @@ -8,10 +8,23 @@ using namespace Dawn; -AssetLoader::AssetLoader(const std::string name) : name(name) { +AssetLoader::AssetLoader( + const std::shared_ptr assetManager, + const std::string name +) : + assetManager(assetManager), + name(name) +{ + assertNotNull(assetManager, "AssetManager cannot be null"); assertTrue(name.size() > 0, "Name cannot be empty"); } +std::shared_ptr AssetLoader::getAssetManager() { + auto am = this->assetManager.lock(); + assertNotNull(am, "AssetManager is null"); + return am; +} + AssetLoader::~AssetLoader() { this->loaded = false; } \ No newline at end of file diff --git a/src/dawn/asset/AssetLoader.hpp b/src/dawn/asset/AssetLoader.hpp index a9d78a2e..142ebe9e 100644 --- a/src/dawn/asset/AssetLoader.hpp +++ b/src/dawn/asset/AssetLoader.hpp @@ -7,17 +7,26 @@ #include "dawn.hpp" namespace Dawn { + class AssetManager; + class AssetLoader { + private: + std::weak_ptr assetManager; + public: const std::string name; bool_t loaded = false; + /** * Create an abstract Asset object. * * @param name Name of the asset. */ - AssetLoader(const std::string name); + AssetLoader( + const std::shared_ptr assetManager, + const std::string name + ); /** * Virtual function that will be called by the asset manager on a @@ -32,6 +41,13 @@ namespace Dawn { * false. */ virtual void updateAsync() = 0; + + /** + * Returns the asset manager. + * + * @return The asset manager. + */ + std::shared_ptr getAssetManager(); /** * Dispose the asset item. diff --git a/src/dawn/asset/AssetManager.hpp b/src/dawn/asset/AssetManager.hpp index 757e8852..abf28611 100644 --- a/src/dawn/asset/AssetManager.hpp +++ b/src/dawn/asset/AssetManager.hpp @@ -8,7 +8,7 @@ #include "asset/AssetLoader.hpp" namespace Dawn { - class AssetManager final { + class AssetManager final : public std::enable_shared_from_this { private: std::vector> pendingAssetLoaders; std::vector> finishedAssetLoaders; @@ -88,39 +88,16 @@ namespace Dawn { auto existing = this->getExisting(filename); if(existing) return existing; - std::shared_ptr loader = std::make_shared(filename); + std::shared_ptr loader = std::make_shared( + shared_from_this(), + filename + ); pendingAssetLoaders.push_back( std::static_pointer_cast(loader) ); return loader; } - /** - * Returns the asset loader for the given asset. - * - * @param filename The filename of the asset to get. - * @param fontSize The font size to get the truetype asset of. - * @return The asset loader for the given asset. - */ - // std::shared_ptr get( - // const std::string filename, - // const uint32_t fontSize - // ) { - // auto existing = this->getExisting(filename); - // if(existing) { - // // Check pointer hasn't gone stale, if it has remove it and create new. - // auto texture = existing->getTexture(fontSize); - // if(texture) return texture; - // this->removeExisting(filename); - // } - - // std::shared_ptr loader = std::make_shared( - // filename - // ); - // pendingAssetLoaders.push_back(std::static_pointer_cast(loader)); - // return loader->getTexture(fontSize); - // } - /** * Dispose the asset manager, and all attached assets. */ diff --git a/src/dawn/asset/loader/CMakeLists.txt b/src/dawn/asset/loader/CMakeLists.txt index 21f66773..71ad9480 100644 --- a/src/dawn/asset/loader/CMakeLists.txt +++ b/src/dawn/asset/loader/CMakeLists.txt @@ -9,4 +9,5 @@ target_sources(${DAWN_TARGET_NAME} TextureLoader.cpp JSONLoader.cpp TrueTypeLoader.cpp + SceneLoader.cpp ) \ No newline at end of file diff --git a/src/dawn/asset/loader/JSONLoader.cpp b/src/dawn/asset/loader/JSONLoader.cpp index b713dd2d..ef109212 100644 --- a/src/dawn/asset/loader/JSONLoader.cpp +++ b/src/dawn/asset/loader/JSONLoader.cpp @@ -7,8 +7,11 @@ using namespace Dawn; -JSONLoader::JSONLoader(const std::string name) : - AssetLoader(name), +JSONLoader::JSONLoader( + const std::shared_ptr assetManager, + const std::string name +) : + AssetLoader(assetManager, name), loader(name), state(JSONLoaderState::INITIAL) { diff --git a/src/dawn/asset/loader/JSONLoader.hpp b/src/dawn/asset/loader/JSONLoader.hpp index 3ffcff6f..3441345f 100644 --- a/src/dawn/asset/loader/JSONLoader.hpp +++ b/src/dawn/asset/loader/JSONLoader.hpp @@ -25,7 +25,10 @@ namespace Dawn { public: json data; - JSONLoader(const std::string name); + JSONLoader( + const std::shared_ptr assetManager, + const std::string name + ); void updateSync() override; void updateAsync() override; ~JSONLoader(); diff --git a/src/dawn/asset/loader/SceneLoader.cpp b/src/dawn/asset/loader/SceneLoader.cpp new file mode 100644 index 00000000..dd861a5e --- /dev/null +++ b/src/dawn/asset/loader/SceneLoader.cpp @@ -0,0 +1,44 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "SceneLoader.hpp" + +using namespace Dawn; + +SceneLoader::SceneLoader( + const std::shared_ptr assetManager, + const std::string name +) : + AssetLoader(assetManager, name), + state(SceneLoaderState::INITIAL) +{ +} + +void SceneLoader::updateAsync() { + switch(this->state) { + case SceneLoaderState::INITIAL: + this->jsonLoader = getAssetManager()->get(this->name); + this->state = SceneLoaderState::LOADING_JSON; + break; + + case SceneLoaderState::LOADING_JSON: + break; + + default: + break; + } +} + +void SceneLoader::updateSync() { + +} + +std::shared_ptr SceneLoader::getScene() { + return scene; +} + +SceneLoader::~SceneLoader() { + +} \ No newline at end of file diff --git a/src/dawn/asset/loader/SceneLoader.hpp b/src/dawn/asset/loader/SceneLoader.hpp new file mode 100644 index 00000000..0f72cd0b --- /dev/null +++ b/src/dawn/asset/loader/SceneLoader.hpp @@ -0,0 +1,40 @@ +// 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 { + enum class SceneLoaderState { + INITIAL, + LOADING_JSON, + }; + + class SceneLoader : public AssetLoader { + protected: + SceneLoaderState state; + + std::shared_ptr jsonLoader; + std::shared_ptr scene; + + public: + SceneLoader( + const std::shared_ptr assetManager, + const std::string name + ); + void updateSync() override; + void updateAsync() override; + + /** + * Returns the Scene that was loaded, or nullptr if not loaded. + * + * @return The loaded scene. + */ + std::shared_ptr getScene(); + + ~SceneLoader(); + }; +} \ No newline at end of file diff --git a/src/dawn/asset/loader/TextureLoader.cpp b/src/dawn/asset/loader/TextureLoader.cpp index 09695c65..6f0261e7 100644 --- a/src/dawn/asset/loader/TextureLoader.cpp +++ b/src/dawn/asset/loader/TextureLoader.cpp @@ -8,8 +8,11 @@ using namespace Dawn; -TextureLoader::TextureLoader(const std::string name) : - AssetLoader(name), +TextureLoader::TextureLoader( + const std::shared_ptr assetManager, + const std::string name +) : + AssetLoader(assetManager, name), loader(name), state(TextureLoaderLoadState::INITIAL) { diff --git a/src/dawn/asset/loader/TextureLoader.hpp b/src/dawn/asset/loader/TextureLoader.hpp index caad5894..3fd0b8f4 100644 --- a/src/dawn/asset/loader/TextureLoader.hpp +++ b/src/dawn/asset/loader/TextureLoader.hpp @@ -33,13 +33,10 @@ namespace Dawn { std::shared_ptr texture; public: - /** - * Constructs a texture asset loader. You should instead use the parent - * asset managers' abstracted load method - * - * @param name File name asset to load, omitting the extension. - */ - TextureLoader(const std::string name); + TextureLoader( + const std::shared_ptr assetManager, + const std::string name + ); void updateSync() override; void updateAsync() override; @@ -50,11 +47,6 @@ namespace Dawn { * @return Texture asset. */ std::shared_ptr getTexture(); - - /** - * Dispose / Cleanup the texture asset. Will also dispose the underlying - * texture itself. - */ ~TextureLoader(); }; } diff --git a/src/dawn/asset/loader/TrueTypeLoader.cpp b/src/dawn/asset/loader/TrueTypeLoader.cpp index c07bfef0..2d6df8d5 100644 --- a/src/dawn/asset/loader/TrueTypeLoader.cpp +++ b/src/dawn/asset/loader/TrueTypeLoader.cpp @@ -8,8 +8,11 @@ using namespace Dawn; -TrueTypeLoader::TrueTypeLoader(const std::string name) : - AssetLoader(name), +TrueTypeLoader::TrueTypeLoader( + const std::shared_ptr assetManager, + const std::string name +) : + AssetLoader(assetManager, name), loader(name) { // Init the font. diff --git a/src/dawn/asset/loader/TrueTypeLoader.hpp b/src/dawn/asset/loader/TrueTypeLoader.hpp index 8735dab4..14b92b06 100644 --- a/src/dawn/asset/loader/TrueTypeLoader.hpp +++ b/src/dawn/asset/loader/TrueTypeLoader.hpp @@ -27,13 +27,10 @@ namespace Dawn { uint8_t *buffer = nullptr; public: - /** - * Constructs a TrueTypeLoader. You should instead use the parent - * asset managers' abstracted load method - * - * @param name File name asset to load, omitting the extension. - */ - TrueTypeLoader(const std::string name); + TrueTypeLoader( + const std::shared_ptr assetManager, + const std::string name + ); void updateSync() override; void updateAsync() override; @@ -48,10 +45,6 @@ namespace Dawn { const uint32_t fontSize ); - /** - * Dispose / Cleanup the truetype asset. Will also dispose the underlying - * truetype itself. - */ ~TrueTypeLoader(); }; } \ No newline at end of file diff --git a/src/dawn/dawn.hpp b/src/dawn/dawn.hpp index 799558f9..fd3703db 100644 --- a/src/dawn/dawn.hpp +++ b/src/dawn/dawn.hpp @@ -59,4 +59,5 @@ extern "C" { #include #include #include -#include \ No newline at end of file +#include +#include \ No newline at end of file diff --git a/src/dawn/game/IGame.cpp b/src/dawn/game/IGame.cpp index 69fcb253..8b4bc86e 100644 --- a/src/dawn/game/IGame.cpp +++ b/src/dawn/game/IGame.cpp @@ -7,6 +7,8 @@ #include "scene/Scene.hpp" #include "util/Flag.hpp" +#include "asset/loader/SceneLoader.hpp" + using namespace Dawn; IGame::IGame() { @@ -36,8 +38,15 @@ void IGame::init() { this->initManagers(); - auto initialScene = this->getInitialScene(); - nextFrameScene = std::make_shared(selfAsGame, initialScene); + + auto sceneLoader = this->assetManager->get("test_scene.json"); + while(!sceneLoader->loaded) { + this->assetManager->update(); + } + nextFrameScene = sceneLoader->getScene(); + + // auto initialScene = this->getInitialScene(); + // nextFrameScene = std::make_shared(selfAsGame, initialScene); } void IGame::deinit() { diff --git a/src/dawnhelloworld/scene/HelloWorldScene.cpp b/src/dawnhelloworld/scene/HelloWorldScene.cpp index b935fbd7..3f87421c 100644 --- a/src/dawnhelloworld/scene/HelloWorldScene.cpp +++ b/src/dawnhelloworld/scene/HelloWorldScene.cpp @@ -5,97 +5,113 @@ // https://opensource.org/licenses/MIT #include "scene/SceneList.hpp" +#include "component/SimpleComponent.hpp" #include "component/display/Camera.hpp" -#include "prefab/SimpleSpinningCube.hpp" -#include "display/font/TrueTypeTexture.hpp" #include "component/display/material/SimpleTexturedMaterial.hpp" -#include "display/mesh/QuadMesh.hpp" - -#include "component/ui/UICanvas.hpp" -#include "ui/elements/UIRectangle.hpp" -#include "ui/elements/UILabel.hpp" -#include "ui/UIMenu.hpp" -#include "ui/container/UIRowContainer.hpp" -#include "ui/container/UIPaddingContainer.hpp" - -#include -#include FT_FREETYPE_H +#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; -std::shared_ptr texture; - void Dawn::helloWorldScene(Scene &s) { - std::string font = "ysabeau_medium"; - texture = s.getGame()->assetManager.get(font, 24); - - while(!s.getGame()->assetManager.isLoaded(font)) { - s.getGame()->assetManager.update(); - } - auto cameraItem = s.createSceneItem(); auto camera = cameraItem->addComponent(); - cameraItem->lookAt({ 120, 0, 300 }, { 120, 0, 0 }, { 0, 1, 0 }); + cameraItem->lookAt({ 20, 20, 20 }, { 0, 0, 0 }, { 0, 1, 0 }); camera->clipFar = 99999.99f; - // auto quad = s.createSceneItem(); - // auto quadMesh = std::make_shared(); + // Ground + { + // Create the scene item. + auto groundItem = s.createSceneItem(); + groundItem->setLocalPosition(glm::vec3(0, 0, 0)); - // glm::vec2 position = { 0, 0 }; - // glm::vec2 size = texture->bufferStringToMesh( - // quadMesh, - // L"Hello World!", - // position, - // true - // ); + // 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); - // auto quadRenderer = quad->addComponent(); - // quadRenderer->mesh = quadMesh; + // Add a renderer to the scene item. + auto groundMeshRenderer = groundItem->addComponent(); + groundMeshRenderer->mesh = groundMesh; - // auto quadMaterial = quad->addComponent(); - // quadMaterial->setTexture(texture->texture); + // Add a material to the scene item. + auto groundMaterial = groundItem->addComponent(); + groundMaterial->setColor(COLOR_LIGHT_GREY); - auto uiCanvasItem = s.createSceneItem(); - auto uiCanvas = uiCanvasItem->addComponent(); + // Add collider + auto groundCollider = groundItem->addComponent(); + groundCollider->setColliderType(ColliderType::STATIC); + groundCollider->setShape(glm::vec3(15, 1, 15)); + } - auto container = std::make_shared(); - container->align = { 32, 32, UI_ALIGN_SIZE_AUTO, UI_ALIGN_SIZE_AUTO }; - container->alignX = UIAlignmentType::START; - container->alignY = UIAlignmentType::START; - uiCanvas->addElement(container); + // Box + { + // Create the scene item. + auto cubeItem = s.createSceneItem(); + cubeItem->setLocalPosition(glm::vec3(0, 10, 0)); - auto rect = std::make_shared(); - rect->color = COLOR_MAGENTA; - container->appendChild(rect); + // 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); - auto menu = std::make_shared(); - menu->setSize(1, 4); - container->appendChild(menu); + // Add a renderer to the scene item. + auto cubeMeshRenderer = cubeItem->addComponent(); + cubeMeshRenderer->mesh = cubeMesh; - auto rowContainer = std::make_shared(); - container->appendChild(rowContainer); + // Add a material to the scene item. + auto cubeMaterial = cubeItem->addComponent(); + cubeMaterial->setColor(COLOR_MAGENTA); - std::vector labels = { - L"New Game", - L"Load Game", - L"Options", - L"Exit" - }; + // Add collider + auto cubeCollider = cubeItem->addComponent(); + } - for(auto &label : labels) { - auto padding = std::make_shared(); - padding->align = { 0, 0, UI_ALIGN_SIZE_AUTO, UI_ALIGN_SIZE_AUTO }; - padding->alignX = UIAlignmentType::START; - padding->alignY = UIAlignmentType::START; - padding->padding = { 8, 8, 8, 8 }; + // Other Box + { + // Create the scene item. + auto cubeItem = s.createSceneItem(); + cubeItem->setLocalPosition(glm::vec3(0.75f, 15, 0.1f)); - auto labelElement = std::make_shared(); - labelElement->wordWrap = false; - labelElement->align = { 0, 0, UI_ALIGN_SIZE_AUTO, UI_ALIGN_SIZE_AUTO }; - labelElement->setText(label); - labelElement->setFont(texture); - - padding->appendChild(labelElement); - rowContainer->appendChild(padding); + // 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/CMakeLists.txt b/src/dawnrpg/CMakeLists.txt index be5eb845..17a91e7b 100644 --- a/src/dawnrpg/CMakeLists.txt +++ b/src/dawnrpg/CMakeLists.txt @@ -15,4 +15,5 @@ add_subdirectory(game) add_subdirectory(scene) # Assets -tool_texture(rosa FILE=rosa.png) \ No newline at end of file +tool_texture(rosa FILE=rosa.png) +tool_copy(test_scene test_scene.json) \ No newline at end of file diff --git a/src/dawnrpg/game/Game.cpp b/src/dawnrpg/game/Game.cpp index 52ff8756..1cd110b5 100644 --- a/src/dawnrpg/game/Game.cpp +++ b/src/dawnrpg/game/Game.cpp @@ -13,7 +13,7 @@ Game::Game() : IGame() { } std::function Game::getInitialScene() { - return helloWorldScene; + return rpgScene; } void Game::initManagers() {