Starting scene loader.
This commit is contained in:
0
assets/test_scene.json
Normal file
0
assets/test_scene.json
Normal file
@ -8,10 +8,23 @@
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
std::shared_ptr<AssetManager> AssetLoader::getAssetManager() {
|
||||
auto am = this->assetManager.lock();
|
||||
assertNotNull(am, "AssetManager is null");
|
||||
return am;
|
||||
}
|
||||
|
||||
AssetLoader::~AssetLoader() {
|
||||
this->loaded = false;
|
||||
}
|
@ -7,17 +7,26 @@
|
||||
#include "dawn.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class AssetManager;
|
||||
|
||||
class AssetLoader {
|
||||
private:
|
||||
std::weak_ptr<AssetManager> 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> assetManager,
|
||||
const std::string name
|
||||
);
|
||||
|
||||
/**
|
||||
* Virtual function that will be called by the asset manager on a
|
||||
@ -33,6 +42,13 @@ namespace Dawn {
|
||||
*/
|
||||
virtual void updateAsync() = 0;
|
||||
|
||||
/**
|
||||
* Returns the asset manager.
|
||||
*
|
||||
* @return The asset manager.
|
||||
*/
|
||||
std::shared_ptr<AssetManager> getAssetManager();
|
||||
|
||||
/**
|
||||
* Dispose the asset item.
|
||||
*/
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include "asset/AssetLoader.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class AssetManager final {
|
||||
class AssetManager final : public std::enable_shared_from_this<AssetManager> {
|
||||
private:
|
||||
std::vector<std::shared_ptr<AssetLoader>> pendingAssetLoaders;
|
||||
std::vector<std::shared_ptr<AssetLoader>> finishedAssetLoaders;
|
||||
@ -88,39 +88,16 @@ namespace Dawn {
|
||||
auto existing = this->getExisting<T>(filename);
|
||||
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(
|
||||
std::static_pointer_cast<AssetLoader>(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.
|
||||
*/
|
||||
|
@ -9,4 +9,5 @@ target_sources(${DAWN_TARGET_NAME}
|
||||
TextureLoader.cpp
|
||||
JSONLoader.cpp
|
||||
TrueTypeLoader.cpp
|
||||
SceneLoader.cpp
|
||||
)
|
@ -7,8 +7,11 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
JSONLoader::JSONLoader(const std::string name) :
|
||||
AssetLoader(name),
|
||||
JSONLoader::JSONLoader(
|
||||
const std::shared_ptr<AssetManager> assetManager,
|
||||
const std::string name
|
||||
) :
|
||||
AssetLoader(assetManager, name),
|
||||
loader(name),
|
||||
state(JSONLoaderState::INITIAL)
|
||||
{
|
||||
|
@ -25,7 +25,10 @@ namespace Dawn {
|
||||
public:
|
||||
json data;
|
||||
|
||||
JSONLoader(const std::string name);
|
||||
JSONLoader(
|
||||
const std::shared_ptr<AssetManager> assetManager,
|
||||
const std::string name
|
||||
);
|
||||
void updateSync() override;
|
||||
void updateAsync() override;
|
||||
~JSONLoader();
|
||||
|
44
src/dawn/asset/loader/SceneLoader.cpp
Normal file
44
src/dawn/asset/loader/SceneLoader.cpp
Normal 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() {
|
||||
|
||||
}
|
40
src/dawn/asset/loader/SceneLoader.hpp
Normal file
40
src/dawn/asset/loader/SceneLoader.hpp
Normal 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();
|
||||
};
|
||||
}
|
@ -8,8 +8,11 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
TextureLoader::TextureLoader(const std::string name) :
|
||||
AssetLoader(name),
|
||||
TextureLoader::TextureLoader(
|
||||
const std::shared_ptr<AssetManager> assetManager,
|
||||
const std::string name
|
||||
) :
|
||||
AssetLoader(assetManager, name),
|
||||
loader(name),
|
||||
state(TextureLoaderLoadState::INITIAL)
|
||||
{
|
||||
|
@ -33,13 +33,10 @@ namespace Dawn {
|
||||
std::shared_ptr<Texture> 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> assetManager,
|
||||
const std::string name
|
||||
);
|
||||
|
||||
void updateSync() override;
|
||||
void updateAsync() override;
|
||||
@ -50,11 +47,6 @@ namespace Dawn {
|
||||
* @return Texture asset.
|
||||
*/
|
||||
std::shared_ptr<Texture> getTexture();
|
||||
|
||||
/**
|
||||
* Dispose / Cleanup the texture asset. Will also dispose the underlying
|
||||
* texture itself.
|
||||
*/
|
||||
~TextureLoader();
|
||||
};
|
||||
}
|
||||
|
@ -8,8 +8,11 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
TrueTypeLoader::TrueTypeLoader(const std::string name) :
|
||||
AssetLoader(name),
|
||||
TrueTypeLoader::TrueTypeLoader(
|
||||
const std::shared_ptr<AssetManager> assetManager,
|
||||
const std::string name
|
||||
) :
|
||||
AssetLoader(assetManager, name),
|
||||
loader(name)
|
||||
{
|
||||
// Init the font.
|
||||
|
@ -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> 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();
|
||||
};
|
||||
}
|
@ -60,3 +60,4 @@ extern "C" {
|
||||
#include <Jolt/Physics/Collision/Shape/SphereShape.h>
|
||||
#include <Jolt/Physics/Body/BodyCreationSettings.h>
|
||||
#include <Jolt/Physics/Body/BodyActivationListener.h>
|
||||
#include <Jolt/Physics/Character/Character.h>
|
@ -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<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() {
|
||||
|
@ -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 <ft2build.h>
|
||||
#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<TrueTypeTexture> texture;
|
||||
|
||||
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 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;
|
||||
|
||||
// auto quad = s.createSceneItem();
|
||||
// auto quadMesh = std::make_shared<Mesh>();
|
||||
// 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<Mesh>();
|
||||
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<MeshRenderer>();
|
||||
// quadRenderer->mesh = quadMesh;
|
||||
// Add a renderer to the scene item.
|
||||
auto groundMeshRenderer = groundItem->addComponent<MeshRenderer>();
|
||||
groundMeshRenderer->mesh = groundMesh;
|
||||
|
||||
// auto quadMaterial = quad->addComponent<SimpleTexturedMaterial>();
|
||||
// quadMaterial->setTexture(texture->texture);
|
||||
// Add a material to the scene item.
|
||||
auto groundMaterial = groundItem->addComponent<SimpleTexturedMaterial>();
|
||||
groundMaterial->setColor(COLOR_LIGHT_GREY);
|
||||
|
||||
auto uiCanvasItem = s.createSceneItem();
|
||||
auto uiCanvas = uiCanvasItem->addComponent<UICanvas>();
|
||||
// Add collider
|
||||
auto groundCollider = groundItem->addComponent<CubeCollider>();
|
||||
groundCollider->setColliderType(ColliderType::STATIC);
|
||||
groundCollider->setShape(glm::vec3(15, 1, 15));
|
||||
}
|
||||
|
||||
auto container = std::make_shared<UIContainer>();
|
||||
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<UIRectangle>();
|
||||
rect->color = COLOR_MAGENTA;
|
||||
container->appendChild(rect);
|
||||
// Create a simple cube mesh.
|
||||
auto cubeMesh = std::make_shared<Mesh>();
|
||||
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>();
|
||||
menu->setSize(1, 4);
|
||||
container->appendChild(menu);
|
||||
// Add a renderer to the scene item.
|
||||
auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
|
||||
cubeMeshRenderer->mesh = cubeMesh;
|
||||
|
||||
auto rowContainer = std::make_shared<UIRowContainer>();
|
||||
container->appendChild(rowContainer);
|
||||
// Add a material to the scene item.
|
||||
auto cubeMaterial = cubeItem->addComponent<SimpleTexturedMaterial>();
|
||||
cubeMaterial->setColor(COLOR_MAGENTA);
|
||||
|
||||
std::vector<std::wstring> labels = {
|
||||
L"New Game",
|
||||
L"Load Game",
|
||||
L"Options",
|
||||
L"Exit"
|
||||
};
|
||||
// Add collider
|
||||
auto cubeCollider = cubeItem->addComponent<CubeCollider>();
|
||||
}
|
||||
|
||||
for(auto &label : labels) {
|
||||
auto padding = std::make_shared<UIPaddingContainer>();
|
||||
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<UILabel>();
|
||||
labelElement->wordWrap = false;
|
||||
labelElement->align = { 0, 0, UI_ALIGN_SIZE_AUTO, UI_ALIGN_SIZE_AUTO };
|
||||
labelElement->setText(label);
|
||||
labelElement->setFont(texture);
|
||||
// Create a simple cube mesh.
|
||||
auto cubeMesh = std::make_shared<Mesh>();
|
||||
cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
|
||||
CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0);
|
||||
|
||||
padding->appendChild(labelElement);
|
||||
rowContainer->appendChild(padding);
|
||||
// Add a renderer to the scene item.
|
||||
auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
|
||||
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>();
|
||||
}
|
||||
}
|
@ -16,3 +16,4 @@ add_subdirectory(scene)
|
||||
|
||||
# Assets
|
||||
tool_texture(rosa FILE=rosa.png)
|
||||
tool_copy(test_scene test_scene.json)
|
@ -13,7 +13,7 @@ Game::Game() : IGame() {
|
||||
}
|
||||
|
||||
std::function<void(Scene&)> Game::getInitialScene() {
|
||||
return helloWorldScene;
|
||||
return rpgScene;
|
||||
}
|
||||
|
||||
void Game::initManagers() {
|
||||
|
Reference in New Issue
Block a user