Procházet zdrojové kódy

Asynchronous loading for ParticleEffect.

Lasse Öörni před 11 roky
rodič
revize
677ab20986

+ 20 - 1
Source/Engine/Graphics/ParticleEffect.cpp

@@ -92,6 +92,8 @@ void ParticleEffect::RegisterObject(Context* context)
 
 bool ParticleEffect::BeginLoad(Deserializer& source)
 {
+    loadMaterialName_.Clear();
+
     XMLFile file(context_);
     if (!file.Load(source))
     {
@@ -140,7 +142,12 @@ bool ParticleEffect::BeginLoad(Deserializer& source)
     textureFrames_.Clear();
 
     if (rootElem.HasChild("material"))
-        SetMaterial(GetSubsystem<ResourceCache>()->GetResource<Material>(rootElem.GetChild("material").GetAttribute("name")));
+    {
+        loadMaterialName_ = rootElem.GetChild("material").GetAttribute("name");
+        // If async loading, can not GetResource() the material. But can do a background request for it
+        if (GetAsyncLoadState() == ASYNC_LOADING)
+            GetSubsystem<ResourceCache>()->BackgroundLoadResource<Material>(loadMaterialName_);
+    }
 
     if (rootElem.HasChild("numparticles"))
         SetNumParticles(rootElem.GetChild("numparticles").GetInt("value"));
@@ -273,6 +280,18 @@ bool ParticleEffect::BeginLoad(Deserializer& source)
     return true;
 }
 
+bool ParticleEffect::EndLoad()
+{
+    // Apply the material now
+    if (!loadMaterialName_.Empty())
+    {
+        SetMaterial(GetSubsystem<ResourceCache>()->GetResource<Material>(loadMaterialName_));
+        loadMaterialName_.Clear();
+    }
+
+    return true;
+}
+
 bool ParticleEffect::Save(Serializer& dest) const
 {
     XMLFile file(context_);

+ 4 - 0
Source/Engine/Graphics/ParticleEffect.h

@@ -113,6 +113,8 @@ public:
 
     /// Load resource from stream. May be called from a worker thread. Return true if successful.
     virtual bool BeginLoad(Deserializer& source);
+    /// Finish resource loading. Always called from the main thread. Return true if successful.
+    virtual bool EndLoad();
     /// Save resource. Return true if successful.
     virtual bool Save(Serializer& dest) const;
 
@@ -337,6 +339,8 @@ private:
     Vector<ColorFrame> colorFrames_;
     /// Texture animation frames.
     Vector<TextureFrame> textureFrames_;
+    /// Material name acquired during BeginLoad().
+    String loadMaterialName_;
 };
 
 }

+ 1 - 1
Source/Engine/Resource/JSONFile.cpp

@@ -73,7 +73,7 @@ bool JSONFile::BeginLoad(Deserializer& source)
 
     if (document_->Parse<0>(buffer).HasParseError())
     {
-        LOGERROR("Could not parse JOSO data from " + source.GetName());
+        LOGERROR("Could not parse JSON data from " + source.GetName());
         return false;
     }