Starting scene loader.
This commit is contained in:
@@ -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
|
||||
@@ -32,6 +41,13 @@ namespace Dawn {
|
||||
* false.
|
||||
*/
|
||||
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();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user