Browse Source

Add allocation-less version of Texture2D::GetImage.

Eugene Kozlov 8 years ago
parent
commit
7b036b57af
2 changed files with 15 additions and 12 deletions
  1. 13 12
      Source/Urho3D/Graphics/Texture2D.cpp
  2. 2 0
      Source/Urho3D/Graphics/Texture2D.h

+ 13 - 12
Source/Urho3D/Graphics/Texture2D.cpp

@@ -134,7 +134,7 @@ bool Texture2D::SetSize(int width, int height, unsigned format, TextureUsage usa
     renderSurface_.Reset();
     renderSurface_.Reset();
 
 
     usage_ = usage;
     usage_ = usage;
-    
+
     if (usage >= TEXTURE_RENDERTARGET)
     if (usage >= TEXTURE_RENDERTARGET)
     {
     {
         renderSurface_ = new RenderSurface(this);
         renderSurface_ = new RenderSurface(this);
@@ -160,24 +160,25 @@ bool Texture2D::SetSize(int width, int height, unsigned format, TextureUsage usa
     return Create();
     return Create();
 }
 }
 
 
-SharedPtr<Image> Texture2D::GetImage() const
+bool Texture2D::GetImage(Image& image) const
 {
 {
     if (format_ != Graphics::GetRGBAFormat() && format_ != Graphics::GetRGBFormat())
     if (format_ != Graphics::GetRGBAFormat() && format_ != Graphics::GetRGBFormat())
     {
     {
         URHO3D_LOGERROR("Unsupported texture format, can not convert to Image");
         URHO3D_LOGERROR("Unsupported texture format, can not convert to Image");
-        return SharedPtr<Image>();
+        return false;
     }
     }
 
 
-    Image* rawImage = new Image(context_);
-    if (format_ == Graphics::GetRGBAFormat())
-        rawImage->SetSize(width_, height_, 4);
-    else if (format_ == Graphics::GetRGBFormat())
-        rawImage->SetSize(width_, height_, 3);
-    else
-        assert(0);
+    image.SetSize(width_, height_, GetComponents());
+    GetData(0, image.GetData());
+    return true;
+}
 
 
-    GetData(0, rawImage->GetData());
-    return SharedPtr<Image>(rawImage);
+SharedPtr<Image> Texture2D::GetImage() const
+{
+    auto rawImage = MakeShared<Image>(context_);
+    if (!GetImage(*rawImage))
+        return nullptr;
+    return rawImage;
 }
 }
 
 
 void Texture2D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
 void Texture2D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)

+ 2 - 0
Source/Urho3D/Graphics/Texture2D.h

@@ -69,6 +69,8 @@ public:
     /// Get data from a mip level. The destination buffer must be big enough. Return true if successful.
     /// Get data from a mip level. The destination buffer must be big enough. Return true if successful.
     bool GetData(unsigned level, void* dest) const;
     bool GetData(unsigned level, void* dest) const;
     /// Get image data from zero mip level. Only RGB and RGBA textures are supported.
     /// Get image data from zero mip level. Only RGB and RGBA textures are supported.
+    bool GetImage(Image& image) const;
+    /// Get image data from zero mip level. Only RGB and RGBA textures are supported.
     SharedPtr<Image> GetImage() const;
     SharedPtr<Image> GetImage() const;
 
 
     /// Return render surface.
     /// Return render surface.