Browse Source

Exposed Texture2D::Load() to Lua. Added Detach() function to SharedPtr which will safely detach the pointer without destroying the object. To only be used in scripting integration. Fixed loading of texture with compressed data first and then uncompressed.

Lasse Öörni 12 years ago
parent
commit
5bc98f68d3

+ 12 - 0
Source/Engine/Container/Ptr.h

@@ -104,6 +104,18 @@ public:
     /// Reset to null and release the object reference.
     void Reset() { ReleaseRef(); }
     
+    /// Detach without destroying the object even if the refcount goes zero. To be used for scripting language interoperation.
+    void Detach()
+    {
+        if (ptr_)
+        {
+            RefCount* refCount = RefCountPtr();
+            ++refCount->refs_; // 2 refs
+            Reset(); // 1 ref
+            --refCount->refs_; // 0 refs
+        }
+    }
+    
     /// Perform a static cast from a shared pointer of another type.
     template <class U> void StaticCast(const SharedPtr<U>& rhs)
     {

+ 1 - 0
Source/Engine/Graphics/Direct3D9/D3D9Texture2D.cpp

@@ -339,6 +339,7 @@ bool Texture2D::Load(SharedPtr<Image> image, bool useAlpha)
             break;
         }
         
+        SetNumLevels(0); // Determine number of levels after creation
         SetSize(levelWidth, levelHeight, format);
         
         for (unsigned i = 0; i < levels_; ++i)

+ 1 - 0
Source/Engine/Graphics/Direct3D9/D3D9Texture3D.cpp

@@ -391,6 +391,7 @@ bool Texture3D::Load(SharedPtr<Image> image, bool useAlpha)
             break;
         }
         
+        SetNumLevels(0); // Determine number of levels after creation
         SetSize(levelWidth, levelHeight, levelDepth, format);
         
         for (unsigned i = 0; i < levels_; ++i)

+ 3 - 0
Source/Engine/Graphics/Direct3D9/D3D9TextureCube.cpp

@@ -408,7 +408,10 @@ bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
         
         // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
         if (!face)
+        {
+            SetNumLevels(0); // Determine number of levels after creation
             SetSize(levelWidth, format);
+        }
         else
         {
             if (!object_)

+ 1 - 0
Source/Engine/Graphics/OpenGL/OGLTexture2D.cpp

@@ -289,6 +289,7 @@ bool Texture2D::Load(SharedPtr<Image> image, bool useAlpha)
             break;
         }
         
+        SetNumLevels(0); // Determine number of levels after creation
         SetSize(levelWidth, levelHeight, format);
         if (!object_)
             return false;

+ 1 - 0
Source/Engine/Graphics/OpenGL/OGLTexture3D.cpp

@@ -335,6 +335,7 @@ bool Texture3D::Load(SharedPtr<Image> image, bool useAlpha)
             break;
         }
         
+        SetNumLevels(0); // Determine number of levels after creation
         SetSize(levelWidth, levelHeight, levelDepth, format);
         if (!object_)
             return false;

+ 3 - 0
Source/Engine/Graphics/OpenGL/OGLTextureCube.cpp

@@ -368,7 +368,10 @@ bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
         
         // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
         if (!face)
+        {
+            SetNumLevels(0); // Determine number of levels after creation
             SetSize(levelWidth, format);
+        }
         else
         {
             if (!object_)

+ 13 - 2
Source/Engine/LuaScript/pkgs/Graphics/Texture2D.pkg

@@ -6,15 +6,26 @@ class Texture2D : public Texture
 {
     Texture2D();
     ~Texture2D();
-    
+
     bool SetSize(int width, int height, unsigned format, TextureUsage usage = TEXTURE_STATIC);
-    
+    tolua_outside bool Texture2DLoad @ Load(Image* image, bool useAlpha = false);
+
     RenderSurface* GetRenderSurface() const;
     
     tolua_readonly tolua_property__get_set RenderSurface* renderSurface;
 };
 
 ${
+static bool Texture2DLoad(Texture2D* texture, Image* image, bool useAlpha)
+{
+    SharedPtr<Image> imagePtr(image);
+    bool ret = texture->Load(imagePtr, useAlpha);
+    // Need to safely detach the object from the shared pointer so that the Lua script can manually
+    // delete the object once done
+    imagePtr.Detach();
+    return ret;
+}
+
 #define TOLUA_DISABLE_tolua_GraphicsLuaAPI_Texture2D_new00
 static int tolua_GraphicsLuaAPI_Texture2D_new00(lua_State* tolua_S)
 {

+ 2 - 5
Source/Engine/LuaScript/pkgs/Resource/ResourceCache.pkg

@@ -53,12 +53,9 @@ static File* ResourceCacheGetFile(ResourceCache* cache, const String& fileName)
     else
     {
         // Need to safely detach the object from the shared pointer so that the Lua script can manually
-        // delete the object once done. The refcount needs to be set zero or else the destructor will assert
+        // delete the object once done. The refcount needs to be zero or else the destructor will assert
         File* fileRaw = file.Get();
-        fileRaw->AddRef();
-        file.Reset();
-        RefCount* refCount = fileRaw->RefCountPtr();
-        refCount->refs_ = 0;
+        file.Detach();
         return fileRaw;
     }
 }