[Orxonox-commit 3733] r8413 - code/trunk/src/modules/designtools

dafrick at orxonox.net dafrick at orxonox.net
Sat May 7 20:43:38 CEST 2011


Author: dafrick
Date: 2011-05-07 20:43:37 +0200 (Sat, 07 May 2011)
New Revision: 8413

Modified:
   code/trunk/src/modules/designtools/ScreenshotManager.cc
   code/trunk/src/modules/designtools/ScreenshotManager.h
   code/trunk/src/modules/designtools/SkyboxGenerator.cc
Log:
This should make the ScreenshotManager a lot more memory efficient.


Modified: code/trunk/src/modules/designtools/ScreenshotManager.cc
===================================================================
--- code/trunk/src/modules/designtools/ScreenshotManager.cc	2011-05-06 22:06:56 UTC (rev 8412)
+++ code/trunk/src/modules/designtools/ScreenshotManager.cc	2011-05-07 18:43:37 UTC (rev 8413)
@@ -65,7 +65,7 @@
     @brief
         Constructor. 
     */
-    ScreenshotManager::ScreenshotManager()
+    ScreenshotManager::ScreenshotManager() : finalPicturePB_(NULL), data_(NULL)
     {
         RegisterRootObject(ScreenshotManager);
         
@@ -77,8 +77,28 @@
 
     ScreenshotManager::~ScreenshotManager()
     {
-        
+        this->cleanup();
     }
+
+    /**
+    @brief
+        Frees used memory.
+    */
+    void ScreenshotManager::cleanup(void)
+    {
+        if(this->finalPicturePB_ != NULL)
+        {
+            delete this->finalPicturePB_;
+            this->finalPicturePB_ = NULL;
+        }
+        if(this->data_ != NULL)
+        {
+            delete this->data_;
+            this->data_ = NULL;
+        }
+        if(!this->tempTexture_.isNull())
+            this->tempTexture_.freeMethod();
+    }
     
     /**
     @brief
@@ -100,31 +120,24 @@
     {
         Ogre::RenderWindow* pRenderWindow = GraphicsManager::getInstance().getRenderWindow();
 
-        // If the window size has changed
-        if(this->windowWidth_ != pRenderWindow->getWidth() || this->windowHeight_ != pRenderWindow->getHeight())
-        {
-            // Update current window size
-            this->windowWidth_   = pRenderWindow->getWidth();
-            this->windowHeight_  = pRenderWindow->getHeight();
+        // Update current window size
+        this->windowWidth_   = pRenderWindow->getWidth();
+        this->windowHeight_  = pRenderWindow->getHeight();
 
-            // Create temporary texture
-            this->tempTexture_ = Ogre::TextureManager::getSingleton().createManual("ScreenShotTex",
-                Resource::getDefaultResourceGroup(), Ogre::TEX_TYPE_2D, this->windowWidth_,
-                this->windowHeight_, 0, Ogre::PF_B8G8R8, Ogre::TU_RENDERTARGET);
+        // Create temporary texture
+        this->tempTexture_ = Ogre::TextureManager::getSingleton().createManual("ScreenShotTex", Resource::getDefaultResourceGroup(), Ogre::TEX_TYPE_2D, this->windowWidth_, this->windowHeight_, 0, Ogre::PF_B8G8R8, Ogre::TU_RENDERTARGET);
 
-            // Get the current render target of the temporary texture
-            this->renderTarget_ = this->tempTexture_->getBuffer()->getRenderTarget();
+        // Get the current render target of the temporary texture
+        this->renderTarget_ = this->tempTexture_->getBuffer()->getRenderTarget();
 
-            // HardwarePixelBufferSharedPtr to the buffer of the temporary texture
-            this->buffer_ = this->tempTexture_->getBuffer();
+        // HardwarePixelBufferSharedPtr to the buffer of the temporary texture
+        this->buffer_ = this->tempTexture_->getBuffer();
 
-            // Create PixelBox
-            this->data_ = new uint8_t[(this->windowWidth_ * this->gridSize_) * (this->windowHeight_ * this->gridSize_) * 3];
-            this->finalPicturePB_ = Ogre::PixelBox(this->windowWidth_ * this->gridSize_, this->windowHeight_ * this->gridSize_, 1, Ogre::PF_B8G8R8, this->data_);
-        }
+        // Create PixelBox
+        this->data_ = new uint8_t[(this->windowWidth_ * this->gridSize_) * (this->windowHeight_ * this->gridSize_) * 3];
+        this->finalPicturePB_ = new Ogre::PixelBox(this->windowWidth_ * this->gridSize_, this->windowHeight_ * this->gridSize_, 1, Ogre::PF_B8G8R8, this->data_);
     }
 
-
     /**
     @brief
         Make a screenshot.
@@ -142,17 +155,17 @@
             COUT(3) << "Finished taking " << this->gridSize_*this->windowWidth_ << "x" << this->gridSize_*this->windowHeight_ << " pixel HD screenshot. Storing in log/." << endl;
         }
         else
-        {
             COUT(1) << "There needs to be an active camera to make screenshots." << endl;
-            return;
-        }
+
+        this->cleanup();
     }
 
     /**
     @brief
         Creates a screenshot and returns it.
+        After calling this method the ScreenshotManager should be cleaned using cleanup().
     @return
-        Returns a pointer to an Ogre::Image with the screenshot.
+        Returns a pointer to an Ogre::Image with the screenshot. The memory must be freed, when the image is no longer needed.
     */
     Ogre::Image* ScreenshotManager::getScreenshot()
     {
@@ -164,10 +177,11 @@
     /**
     @brief
         Creates a screenshot with the given camera and returns it.
+        After calling this method the ScreenshotManager should be cleaned using cleanup().
     @param camera
         A pointer to the camera the screenshot should be taken with.
     @return
-        Returns a pointer to an Ogre::Image with the screenshot.
+        Returns a pointer to an Ogre::Image with the screenshot. The memory must be freed, when the image is no longer needed.
     */
     Ogre::Image* ScreenshotManager::getScreenshot(Ogre::Camera* camera)
     {
@@ -197,11 +211,12 @@
         
         if(this->gridSize_ <= 1)
         {
+            //TODO: Not working.
             // Simple case where the contents of the screen are taken directly
             // Also used when an invalid value is passed within gridSize (zero or negative grid size)
             this->renderTarget_->update(); // Render
             
-            finalImage = &finalImage->loadDynamicImage(static_cast<unsigned char*>(finalPicturePB_.data), finalPicturePB_.getWidth(), finalPicturePB_.getHeight(),Ogre::PF_B8G8R8);
+            finalImage->loadDynamicImage(static_cast<unsigned char*>(finalPicturePB_->data), finalPicturePB_->getWidth(), finalPicturePB_->getHeight(),Ogre::PF_B8G8R8);
         }
         else
         {
@@ -239,7 +254,7 @@
                 Ogre::Box subBox = Ogre::Box(x* this->windowWidth_,y * this->windowHeight_,x * this->windowWidth_ + this->windowWidth_, y * this->windowHeight_ + this->windowHeight_);
                 // Copy the content from the temp buffer into the final picture PixelBox
                 // Place the tempBuffer content at the right position
-                this->buffer_->blitToMemory(this->finalPicturePB_.getSubVolume(subBox));
+                this->buffer_->blitToMemory(this->finalPicturePB_->getSubVolume(subBox));
                 
                 COUT(4) << "Created screenshot number " << nbScreenshots << " for multi grid HD screenshot." << endl;
                 
@@ -249,7 +264,7 @@
             camera->resetFrustumExtents();
             
             // Insert the PixelBox data into the Image Object
-            finalImage->loadDynamicImage(static_cast<unsigned char*>(this->finalPicturePB_.data), this->finalPicturePB_.getWidth(), this->finalPicturePB_.getHeight(), 1, Ogre::PF_B8G8R8, false);
+            finalImage->loadDynamicImage(static_cast<unsigned char*>(this->finalPicturePB_->data), this->finalPicturePB_->getWidth(), this->finalPicturePB_->getHeight(), 1, Ogre::PF_B8G8R8, false);
         }
         
         // Do we have to re-enable our overlays?
@@ -274,9 +289,6 @@
             return;
 
         this->gridSize_ = size;
-        // New PixelBox for the changed size.
-        this->data_ = new uint8_t[(this->windowWidth_ * this->gridSize_) * (this->windowHeight_ * this->gridSize_) * 3];
-        this->finalPicturePB_ = Ogre::PixelBox(this->windowWidth_ * this->gridSize_, this->windowHeight_ * this->gridSize_, 1, Ogre::PF_B8G8R8, this->data_);
     }
 
 }

Modified: code/trunk/src/modules/designtools/ScreenshotManager.h
===================================================================
--- code/trunk/src/modules/designtools/ScreenshotManager.h	2011-05-06 22:06:56 UTC (rev 8412)
+++ code/trunk/src/modules/designtools/ScreenshotManager.h	2011-05-07 18:43:37 UTC (rev 8413)
@@ -52,7 +52,6 @@
     @brief
         Class encapsulates screenshot functionality and provides a method for making multi grid (i.e. HD) screenshots.
         
-        
     @author
         This code comes from http://www.ogre3d.org/tikiwiki/High+resolution+screenshots which is Public Domain.
     @author
@@ -87,6 +86,8 @@
             */
             inline unsigned int getGridSize(void)
                 { return this->gridSize_; }
+
+            void cleanup(void); // Frees used memory.
             
         protected:
             void update(void); // Update internal parameters.
@@ -102,7 +103,7 @@
             Ogre::RenderTexture* renderTarget_; //!< Render target for the temporary texture.
             Ogre::HardwarePixelBufferSharedPtr buffer_; //!< Buffer for the temporary texture.
             
-            Ogre::PixelBox finalPicturePB_; //!< PixelBox for large screenshots, contains the screenshot for gridSize_ > 1.
+            Ogre::PixelBox* finalPicturePB_; //!< PixelBox for large screenshots, contains the screenshot for gridSize_ > 1.
             uint8_t* data_; //!< Data pointer for the PixelBox.
 
     };

Modified: code/trunk/src/modules/designtools/SkyboxGenerator.cc
===================================================================
--- code/trunk/src/modules/designtools/SkyboxGenerator.cc	2011-05-06 22:06:56 UTC (rev 8412)
+++ code/trunk/src/modules/designtools/SkyboxGenerator.cc	2011-05-07 18:43:37 UTC (rev 8413)
@@ -314,5 +314,6 @@
         image->resize(this->size_, this->size_);
         image->save(PathConfig::getInstance().getLogPathString()+name);
         delete image;
+        ScreenshotManager::getInstance().cleanup(); // Free memory in ScreenshotManager.
     }
 }




More information about the Orxonox-commit mailing list