Starting scene loader.

This commit is contained in:
2024-12-01 13:27:36 -06:00
parent bcbc8796da
commit 4dccd7d969
18 changed files with 251 additions and 136 deletions

0
assets/test_scene.json Normal file
View File

View File

@ -8,10 +8,23 @@
using namespace Dawn; using namespace Dawn;
AssetLoader::AssetLoader(const std::string name) : name(name) { AssetLoader::AssetLoader(
const std::shared_ptr<AssetManager> assetManager,
const std::string name
) :
assetManager(assetManager),
name(name)
{
assertNotNull(assetManager, "AssetManager cannot be null");
assertTrue(name.size() > 0, "Name cannot be empty"); assertTrue(name.size() > 0, "Name cannot be empty");
} }
std::shared_ptr<AssetManager> AssetLoader::getAssetManager() {
auto am = this->assetManager.lock();
assertNotNull(am, "AssetManager is null");
return am;
}
AssetLoader::~AssetLoader() { AssetLoader::~AssetLoader() {
this->loaded = false; this->loaded = false;
} }

View File

@ -7,17 +7,26 @@
#include "dawn.hpp" #include "dawn.hpp"
namespace Dawn { namespace Dawn {
class AssetManager;
class AssetLoader { class AssetLoader {
private:
std::weak_ptr<AssetManager> assetManager;
public: public:
const std::string name; const std::string name;
bool_t loaded = false; bool_t loaded = false;
/** /**
* Create an abstract Asset object. * Create an abstract Asset object.
* *
* @param name Name of the asset. * @param name Name of the asset.
*/ */
AssetLoader(const std::string name); AssetLoader(
const std::shared_ptr<AssetManager> assetManager,
const std::string name
);
/** /**
* Virtual function that will be called by the asset manager on a * Virtual function that will be called by the asset manager on a
@ -32,6 +41,13 @@ namespace Dawn {
* false. * false.
*/ */
virtual void updateAsync() = 0; virtual void updateAsync() = 0;
/**
* Returns the asset manager.
*
* @return The asset manager.
*/
std::shared_ptr<AssetManager> getAssetManager();
/** /**
* Dispose the asset item. * Dispose the asset item.

View File

@ -8,7 +8,7 @@
#include "asset/AssetLoader.hpp" #include "asset/AssetLoader.hpp"
namespace Dawn { namespace Dawn {
class AssetManager final { class AssetManager final : public std::enable_shared_from_this<AssetManager> {
private: private:
std::vector<std::shared_ptr<AssetLoader>> pendingAssetLoaders; std::vector<std::shared_ptr<AssetLoader>> pendingAssetLoaders;
std::vector<std::shared_ptr<AssetLoader>> finishedAssetLoaders; std::vector<std::shared_ptr<AssetLoader>> finishedAssetLoaders;
@ -88,39 +88,16 @@ namespace Dawn {
auto existing = this->getExisting<T>(filename); auto existing = this->getExisting<T>(filename);
if(existing) return existing; if(existing) return existing;
std::shared_ptr<T> loader = std::make_shared<T>(filename); std::shared_ptr<T> loader = std::make_shared<T>(
shared_from_this(),
filename
);
pendingAssetLoaders.push_back( pendingAssetLoaders.push_back(
std::static_pointer_cast<AssetLoader>(loader) std::static_pointer_cast<AssetLoader>(loader)
); );
return 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<TrueTypeTexture> get(
// const std::string filename,
// const uint32_t fontSize
// ) {
// auto existing = this->getExisting<TrueTypeLoader>(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<TrueTypeLoader> loader = std::make_shared<TrueTypeLoader>(
// filename
// );
// pendingAssetLoaders.push_back(std::static_pointer_cast<AssetLoader>(loader));
// return loader->getTexture(fontSize);
// }
/** /**
* Dispose the asset manager, and all attached assets. * Dispose the asset manager, and all attached assets.
*/ */

View File

@ -9,4 +9,5 @@ target_sources(${DAWN_TARGET_NAME}
TextureLoader.cpp TextureLoader.cpp
JSONLoader.cpp JSONLoader.cpp
TrueTypeLoader.cpp TrueTypeLoader.cpp
SceneLoader.cpp
) )

View File

@ -7,8 +7,11 @@
using namespace Dawn; using namespace Dawn;
JSONLoader::JSONLoader(const std::string name) : JSONLoader::JSONLoader(
AssetLoader(name), const std::shared_ptr<AssetManager> assetManager,
const std::string name
) :
AssetLoader(assetManager, name),
loader(name), loader(name),
state(JSONLoaderState::INITIAL) state(JSONLoaderState::INITIAL)
{ {

View File

@ -25,7 +25,10 @@ namespace Dawn {
public: public:
json data; json data;
JSONLoader(const std::string name); JSONLoader(
const std::shared_ptr<AssetManager> assetManager,
const std::string name
);
void updateSync() override; void updateSync() override;
void updateAsync() override; void updateAsync() override;
~JSONLoader(); ~JSONLoader();

View File

@ -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> assetManager,
const std::string name
) :
AssetLoader(assetManager, name),
state(SceneLoaderState::INITIAL)
{
}
void SceneLoader::updateAsync() {
switch(this->state) {
case SceneLoaderState::INITIAL:
this->jsonLoader = getAssetManager()->get<JSONLoader>(this->name);
this->state = SceneLoaderState::LOADING_JSON;
break;
case SceneLoaderState::LOADING_JSON:
break;
default:
break;
}
}
void SceneLoader::updateSync() {
}
std::shared_ptr<Scene> SceneLoader::getScene() {
return scene;
}
SceneLoader::~SceneLoader() {
}

View File

@ -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> jsonLoader;
std::shared_ptr<Scene> scene;
public:
SceneLoader(
const std::shared_ptr<AssetManager> 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<Scene> getScene();
~SceneLoader();
};
}

View File

@ -8,8 +8,11 @@
using namespace Dawn; using namespace Dawn;
TextureLoader::TextureLoader(const std::string name) : TextureLoader::TextureLoader(
AssetLoader(name), const std::shared_ptr<AssetManager> assetManager,
const std::string name
) :
AssetLoader(assetManager, name),
loader(name), loader(name),
state(TextureLoaderLoadState::INITIAL) state(TextureLoaderLoadState::INITIAL)
{ {

View File

@ -33,13 +33,10 @@ namespace Dawn {
std::shared_ptr<Texture> texture; std::shared_ptr<Texture> texture;
public: public:
/** TextureLoader(
* Constructs a texture asset loader. You should instead use the parent const std::shared_ptr<AssetManager> assetManager,
* asset managers' abstracted load method const std::string name
* );
* @param name File name asset to load, omitting the extension.
*/
TextureLoader(const std::string name);
void updateSync() override; void updateSync() override;
void updateAsync() override; void updateAsync() override;
@ -50,11 +47,6 @@ namespace Dawn {
* @return Texture asset. * @return Texture asset.
*/ */
std::shared_ptr<Texture> getTexture(); std::shared_ptr<Texture> getTexture();
/**
* Dispose / Cleanup the texture asset. Will also dispose the underlying
* texture itself.
*/
~TextureLoader(); ~TextureLoader();
}; };
} }

View File

@ -8,8 +8,11 @@
using namespace Dawn; using namespace Dawn;
TrueTypeLoader::TrueTypeLoader(const std::string name) : TrueTypeLoader::TrueTypeLoader(
AssetLoader(name), const std::shared_ptr<AssetManager> assetManager,
const std::string name
) :
AssetLoader(assetManager, name),
loader(name) loader(name)
{ {
// Init the font. // Init the font.

View File

@ -27,13 +27,10 @@ namespace Dawn {
uint8_t *buffer = nullptr; uint8_t *buffer = nullptr;
public: public:
/** TrueTypeLoader(
* Constructs a TrueTypeLoader. You should instead use the parent const std::shared_ptr<AssetManager> assetManager,
* asset managers' abstracted load method const std::string name
* );
* @param name File name asset to load, omitting the extension.
*/
TrueTypeLoader(const std::string name);
void updateSync() override; void updateSync() override;
void updateAsync() override; void updateAsync() override;
@ -48,10 +45,6 @@ namespace Dawn {
const uint32_t fontSize const uint32_t fontSize
); );
/**
* Dispose / Cleanup the truetype asset. Will also dispose the underlying
* truetype itself.
*/
~TrueTypeLoader(); ~TrueTypeLoader();
}; };
} }

View File

@ -59,4 +59,5 @@ extern "C" {
#include <Jolt/Physics/Collision/Shape/BoxShape.h> #include <Jolt/Physics/Collision/Shape/BoxShape.h>
#include <Jolt/Physics/Collision/Shape/SphereShape.h> #include <Jolt/Physics/Collision/Shape/SphereShape.h>
#include <Jolt/Physics/Body/BodyCreationSettings.h> #include <Jolt/Physics/Body/BodyCreationSettings.h>
#include <Jolt/Physics/Body/BodyActivationListener.h> #include <Jolt/Physics/Body/BodyActivationListener.h>
#include <Jolt/Physics/Character/Character.h>

View File

@ -7,6 +7,8 @@
#include "scene/Scene.hpp" #include "scene/Scene.hpp"
#include "util/Flag.hpp" #include "util/Flag.hpp"
#include "asset/loader/SceneLoader.hpp"
using namespace Dawn; using namespace Dawn;
IGame::IGame() { IGame::IGame() {
@ -36,8 +38,15 @@ void IGame::init() {
this->initManagers(); this->initManagers();
auto initialScene = this->getInitialScene();
nextFrameScene = std::make_shared<Scene>(selfAsGame, initialScene); auto sceneLoader = this->assetManager->get<SceneLoader>("test_scene.json");
while(!sceneLoader->loaded) {
this->assetManager->update();
}
nextFrameScene = sceneLoader->getScene();
// auto initialScene = this->getInitialScene();
// nextFrameScene = std::make_shared<Scene>(selfAsGame, initialScene);
} }
void IGame::deinit() { void IGame::deinit() {

View File

@ -5,97 +5,113 @@
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#include "scene/SceneList.hpp" #include "scene/SceneList.hpp"
#include "component/SimpleComponent.hpp"
#include "component/display/Camera.hpp" #include "component/display/Camera.hpp"
#include "prefab/SimpleSpinningCube.hpp"
#include "display/font/TrueTypeTexture.hpp"
#include "component/display/material/SimpleTexturedMaterial.hpp" #include "component/display/material/SimpleTexturedMaterial.hpp"
#include "display/mesh/QuadMesh.hpp" #include "component/display/MeshRenderer.hpp"
#include "display/mesh/CubeMesh.hpp"
#include "component/ui/UICanvas.hpp" #include "display/mesh/SphereMesh.hpp"
#include "ui/elements/UIRectangle.hpp" #include "component/physics/CubeCollider.hpp"
#include "ui/elements/UILabel.hpp" #include "component/physics/SphereCollider.hpp"
#include "ui/UIMenu.hpp"
#include "ui/container/UIRowContainer.hpp"
#include "ui/container/UIPaddingContainer.hpp"
#include <ft2build.h>
#include FT_FREETYPE_H
using namespace Dawn; using namespace Dawn;
std::shared_ptr<TrueTypeTexture> texture;
void Dawn::helloWorldScene(Scene &s) { void Dawn::helloWorldScene(Scene &s) {
std::string font = "ysabeau_medium";
texture = s.getGame()->assetManager.get<TrueTypeTexture>(font, 24);
while(!s.getGame()->assetManager.isLoaded(font)) {
s.getGame()->assetManager.update();
}
auto cameraItem = s.createSceneItem(); auto cameraItem = s.createSceneItem();
auto camera = cameraItem->addComponent<Camera>(); auto camera = cameraItem->addComponent<Camera>();
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; camera->clipFar = 99999.99f;
// auto quad = s.createSceneItem(); // Ground
// auto quadMesh = std::make_shared<Mesh>(); {
// Create the scene item.
auto groundItem = s.createSceneItem();
groundItem->setLocalPosition(glm::vec3(0, 0, 0));
// glm::vec2 position = { 0, 0 }; // Create a simple cube mesh.
// glm::vec2 size = texture->bufferStringToMesh( auto groundMesh = std::make_shared<Mesh>();
// quadMesh, groundMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
// L"Hello World!", CubeMesh::buffer(groundMesh, glm::vec3(-15, -1, -15), glm::vec3(30, 2, 30), 0, 0);
// position,
// true
// );
// auto quadRenderer = quad->addComponent<MeshRenderer>(); // Add a renderer to the scene item.
// quadRenderer->mesh = quadMesh; auto groundMeshRenderer = groundItem->addComponent<MeshRenderer>();
groundMeshRenderer->mesh = groundMesh;
// auto quadMaterial = quad->addComponent<SimpleTexturedMaterial>(); // Add a material to the scene item.
// quadMaterial->setTexture(texture->texture); auto groundMaterial = groundItem->addComponent<SimpleTexturedMaterial>();
groundMaterial->setColor(COLOR_LIGHT_GREY);
auto uiCanvasItem = s.createSceneItem(); // Add collider
auto uiCanvas = uiCanvasItem->addComponent<UICanvas>(); auto groundCollider = groundItem->addComponent<CubeCollider>();
groundCollider->setColliderType(ColliderType::STATIC);
groundCollider->setShape(glm::vec3(15, 1, 15));
}
auto container = std::make_shared<UIContainer>(); // Box
container->align = { 32, 32, UI_ALIGN_SIZE_AUTO, UI_ALIGN_SIZE_AUTO }; {
container->alignX = UIAlignmentType::START; // Create the scene item.
container->alignY = UIAlignmentType::START; auto cubeItem = s.createSceneItem();
uiCanvas->addElement(container); cubeItem->setLocalPosition(glm::vec3(0, 10, 0));
auto rect = std::make_shared<UIRectangle>(); // Create a simple cube mesh.
rect->color = COLOR_MAGENTA; auto cubeMesh = std::make_shared<Mesh>();
container->appendChild(rect); 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<UIMenu>(); // Add a renderer to the scene item.
menu->setSize(1, 4); auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
container->appendChild(menu); cubeMeshRenderer->mesh = cubeMesh;
auto rowContainer = std::make_shared<UIRowContainer>(); // Add a material to the scene item.
container->appendChild(rowContainer); auto cubeMaterial = cubeItem->addComponent<SimpleTexturedMaterial>();
cubeMaterial->setColor(COLOR_MAGENTA);
std::vector<std::wstring> labels = { // Add collider
L"New Game", auto cubeCollider = cubeItem->addComponent<CubeCollider>();
L"Load Game", }
L"Options",
L"Exit"
};
for(auto &label : labels) { // Other Box
auto padding = std::make_shared<UIPaddingContainer>(); {
padding->align = { 0, 0, UI_ALIGN_SIZE_AUTO, UI_ALIGN_SIZE_AUTO }; // Create the scene item.
padding->alignX = UIAlignmentType::START; auto cubeItem = s.createSceneItem();
padding->alignY = UIAlignmentType::START; cubeItem->setLocalPosition(glm::vec3(0.75f, 15, 0.1f));
padding->padding = { 8, 8, 8, 8 };
auto labelElement = std::make_shared<UILabel>(); // Create a simple cube mesh.
labelElement->wordWrap = false; auto cubeMesh = std::make_shared<Mesh>();
labelElement->align = { 0, 0, UI_ALIGN_SIZE_AUTO, UI_ALIGN_SIZE_AUTO }; cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
labelElement->setText(label); CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0);
labelElement->setFont(texture);
// Add a renderer to the scene item.
padding->appendChild(labelElement); auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
rowContainer->appendChild(padding); cubeMeshRenderer->mesh = cubeMesh;
// Add a material to the scene item.
auto cubeMaterial = cubeItem->addComponent<SimpleTexturedMaterial>();
cubeMaterial->setColor(COLOR_MAGENTA);
// Add collider
auto cubeCollider = cubeItem->addComponent<CubeCollider>();
}
// 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<Mesh>();
SphereMesh::create(sphereMesh, 1.0f);
// Add a renderer to the scene item.
auto sphereMeshRenderer = sphereItem->addComponent<MeshRenderer>();
sphereMeshRenderer->mesh = sphereMesh;
// Add a material to the scene item.
auto sphereMaterial = sphereItem->addComponent<SimpleTexturedMaterial>();
sphereMaterial->setColor(COLOR_CYAN);
// Add collider
auto sphereCollider = sphereItem->addComponent<SphereCollider>();
} }
} }

View File

@ -15,4 +15,5 @@ add_subdirectory(game)
add_subdirectory(scene) add_subdirectory(scene)
# Assets # Assets
tool_texture(rosa FILE=rosa.png) tool_texture(rosa FILE=rosa.png)
tool_copy(test_scene test_scene.json)

View File

@ -13,7 +13,7 @@ Game::Game() : IGame() {
} }
std::function<void(Scene&)> Game::getInitialScene() { std::function<void(Scene&)> Game::getInitialScene() {
return helloWorldScene; return rpgScene;
} }
void Game::initManagers() { void Game::initManagers() {