Automatically initialize assets

This commit is contained in:
Emil Axelsson
2017-12-05 16:01:59 +01:00
parent fb675dfeab
commit d16efef3d9
4 changed files with 30 additions and 8 deletions

View File

@@ -153,7 +153,7 @@ public:
/**
* Return a a map from name to scene graph node.
*/
const std::map<std::string, SceneGraphNode*>& nodesByName() const;
const std::unordered_map<std::string, SceneGraphNode*>& nodesByName() const;
/**
* Load a scene graph node from a dictionary and return it.
@@ -185,7 +185,7 @@ private:
std::unique_ptr<Camera> _camera;
std::vector<SceneGraphNode*> _topologicallySortedNodes;
std::vector<SceneGraphNode*> _circularNodes;
std::map<std::string, SceneGraphNode*> _nodesByName;
std::unordered_map<std::string, SceneGraphNode*> _nodesByName;
bool _dirtyNodeRegistry;
SceneGraphNode _rootDummy;

View File

@@ -90,6 +90,9 @@ void Asset::removeStateChangeCallback(Asset::CallbackHandle handle) {
}
void Asset::setState(Asset::State state) {
if (_state == state) {
return;
}
_state = state;
_stateChangeCallbackMutex.lock();
std::vector<StateChangeCallback> callbacks;
@@ -169,6 +172,11 @@ bool Asset::isSynchronized() {
}
bool Asset::startSynchronizations() {
// Do not attempt to resync if this is already initialized
if (state() == State::Initialized) {
return false;
}
bool foundUnresolved = false;
// Start synchronization of all children first.
for (auto& child : childAssets()) {
@@ -176,12 +184,13 @@ bool Asset::startSynchronizations() {
foundUnresolved = true;
}
}
// Now synchronize its own synchronizations.
for (const auto& s : synchronizations()) {
if (!s->isResolved()) {
foundUnresolved = true;
s->start();
setState(State::Synchronizing);
s->start();
}
}
// If all syncs are resolved (or no syncs exist), mark as resolved.

View File

@@ -44,7 +44,7 @@ AssetManager::AssetManager(std::unique_ptr<AssetLoader> loader)
void AssetManager::initialize() {
_addAssetCallbackHandle = _assetLoader->addAssetLoadCallback(
[this] (std::shared_ptr<Asset> a) {
a->addStateChangeCallback([&a, this] (Asset::State state) {
a->addStateChangeCallback([a, this] (Asset::State state) {
assetStateChanged(*a, state);
});
}
@@ -154,9 +154,18 @@ void AssetManager::cancelSynchronization(Asset&) {
}
void AssetManager::assetStateChanged(Asset& asset, Asset::State state) {
// Todo: store this and handle the state change data in the update loop.
// to make sure this happens on the main thread.
// Check if assets should start syncing or if they should init.
if (rootAsset()->state() == Asset::State::Initialized) {
if (state == Asset::State::Loaded) {
asset.startSynchronizations();
}
if (state == Asset::State::SyncResolved) {
asset.initialize();
}
} else {
asset.deinitialize();
}
// Todo: Check if assets should start syncing or if they should init.
// flags: autoSync, autoInit ?
}

View File

@@ -124,6 +124,10 @@ Camera* Scene::camera() const {
}
void Scene::registerNode(SceneGraphNode* node) {
if (_nodesByName.count(node->name())){
throw Scene::InvalidSceneError("Node with name " + node->name() + " already exits.");
}
_topologicallySortedNodes.push_back(node);
_nodesByName[node->name()] = node;
_dirtyNodeRegistry = true;
@@ -328,7 +332,7 @@ void Scene::clear() {
_rootDummy.clearChildren();
}
const std::map<std::string, SceneGraphNode*>& Scene::nodesByName() const {
const std::unordered_map<std::string, SceneGraphNode*>& Scene::nodesByName() const {
return _nodesByName;
}