Browse Source

Asynchronous loading for ParticleEffect2D.
Correctly store material dependency when async loading ParticleEffect.

Lasse Öörni 11 years ago
parent
commit
5450b1d111

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

@@ -146,7 +146,7 @@ bool ParticleEffect::BeginLoad(Deserializer& source)
         loadMaterialName_ = 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 async loading, can not GetResource() the material. But can do a background request for it
         if (GetAsyncLoadState() == ASYNC_LOADING)
         if (GetAsyncLoadState() == ASYNC_LOADING)
-            GetSubsystem<ResourceCache>()->BackgroundLoadResource<Material>(loadMaterialName_);
+            GetSubsystem<ResourceCache>()->BackgroundLoadResource<Material>(loadMaterialName_, true, this);
     }
     }
 
 
     if (rootElem.HasChild("numparticles"))
     if (rootElem.HasChild("numparticles"))

+ 22 - 7
Source/Engine/Urho2D/ParticleEffect2D.cpp

@@ -107,6 +107,8 @@ void ParticleEffect2D::RegisterObject(Context* context)
 
 
 bool ParticleEffect2D::BeginLoad(Deserializer& source)
 bool ParticleEffect2D::BeginLoad(Deserializer& source)
 {
 {
+    loadSpriteName_.Clear();
+
     XMLFile xmlFile(context_);
     XMLFile xmlFile(context_);
     if (!xmlFile.Load(source))
     if (!xmlFile.Load(source))
         return false;
         return false;
@@ -116,13 +118,10 @@ bool ParticleEffect2D::BeginLoad(Deserializer& source)
         return false;
         return false;
 
 
     String texture = rootElem.GetChild("texture").GetAttribute("name");
     String texture = rootElem.GetChild("texture").GetAttribute("name");
-    ResourceCache* cache = GetSubsystem<ResourceCache>();
-    sprite_= cache->GetResource<Sprite2D>(GetParentPath(GetName()) + texture);
-    if (!sprite_)
-    {
-        LOGERROR("Could not load sprite " + GetParentPath(GetName()) + texture);
-        return false;
-    }
+    loadSpriteName_ = GetParentPath(GetName()) + texture;
+    // If async loading, request the sprite beforehand
+    if (GetAsyncLoadState() == ASYNC_LOADING)
+        GetSubsystem<ResourceCache>()->BackgroundLoadResource<Sprite2D>(loadSpriteName_, true, this);
 
 
     sourcePositionVariance_ = ReadVector2(rootElem, "sourcePositionVariance");
     sourcePositionVariance_ = ReadVector2(rootElem, "sourcePositionVariance");
 
 
@@ -198,6 +197,22 @@ bool ParticleEffect2D::BeginLoad(Deserializer& source)
     return true;
     return true;
 }
 }
 
 
+bool ParticleEffect2D::EndLoad()
+{
+    // Apply the sprite now
+    if (!loadSpriteName_.Empty())
+    {
+        ResourceCache* cache = GetSubsystem<ResourceCache>();
+        sprite_ = cache->GetResource<Sprite2D>(loadSpriteName_);
+        if (!sprite_)
+            LOGERROR("Could not load sprite " + loadSpriteName_ + " for particle effect");
+
+        loadSpriteName_.Clear();
+    }
+    
+    return true;
+}
+
 bool ParticleEffect2D::Save(Serializer& dest) const
 bool ParticleEffect2D::Save(Serializer& dest) const
 {
 {
     if (!sprite_)
     if (!sprite_)

+ 7 - 3
Source/Engine/Urho2D/ParticleEffect2D.h

@@ -53,6 +53,8 @@ public:
 
 
     /// Load resource from stream. May be called from a worker thread. Return true if successful.
     /// Load resource from stream. May be called from a worker thread. Return true if successful.
     virtual bool BeginLoad(Deserializer& source);
     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.
     /// Save resource. Return true if successful.
     virtual bool Save(Serializer& dest) const;
     virtual bool Save(Serializer& dest) const;
 
 
@@ -208,11 +210,11 @@ private:
     /// Read Vector2.
     /// Read Vector2.
     Vector2 ReadVector2(const XMLElement& element, const String& name) const;
     Vector2 ReadVector2(const XMLElement& element, const String& name) const;
     /// Write integer.
     /// Write integer.
-	void WriteInt(XMLElement& element, const String& name, int value) const;
+    void WriteInt(XMLElement& element, const String& name, int value) const;
     /// Write float.
     /// Write float.
-	void WriteFloat(XMLElement& element, const String& name, float value) const;
+    void WriteFloat(XMLElement& element, const String& name, float value) const;
     /// Write Color.
     /// Write Color.
-	void WriteColor(XMLElement& element, const String& name, const Color& color) const;
+    void WriteColor(XMLElement& element, const String& name, const Color& color) const;
     /// Write Vector2.
     /// Write Vector2.
     void WriteVector2(XMLElement& element, const String& name, const Vector2& value) const;
     void WriteVector2(XMLElement& element, const String& name, const Vector2& value) const;
 
 
@@ -286,6 +288,8 @@ private:
     float rotationEnd_;
     float rotationEnd_;
     /// Rotation end variance.
     /// Rotation end variance.
     float rotationEndVariance_;
     float rotationEndVariance_;
+    /// Sprite name acquired during BeginLoad().
+    String loadSpriteName_;
 };
 };
 
 
 }
 }