Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 7, 2011, 8:43:37 PM (14 years ago)
Author:
dafrick
Message:

This should make the ScreenshotManager a lot more memory efficient.

Location:
code/trunk/src/modules/designtools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/modules/designtools/ScreenshotManager.cc

    r8406 r8413  
    6666        Constructor.
    6767    */
    68     ScreenshotManager::ScreenshotManager()
     68    ScreenshotManager::ScreenshotManager() : finalPicturePB_(NULL), data_(NULL)
    6969    {
    7070        RegisterRootObject(ScreenshotManager);
     
    7878    ScreenshotManager::~ScreenshotManager()
    7979    {
    80        
     80        this->cleanup();
     81    }
     82
     83    /**
     84    @brief
     85        Frees used memory.
     86    */
     87    void ScreenshotManager::cleanup(void)
     88    {
     89        if(this->finalPicturePB_ != NULL)
     90        {
     91            delete this->finalPicturePB_;
     92            this->finalPicturePB_ = NULL;
     93        }
     94        if(this->data_ != NULL)
     95        {
     96            delete this->data_;
     97            this->data_ = NULL;
     98        }
     99        if(!this->tempTexture_.isNull())
     100            this->tempTexture_.freeMethod();
    81101    }
    82102   
     
    101121        Ogre::RenderWindow* pRenderWindow = GraphicsManager::getInstance().getRenderWindow();
    102122
    103         // If the window size has changed
    104         if(this->windowWidth_ != pRenderWindow->getWidth() || this->windowHeight_ != pRenderWindow->getHeight())
    105         {
    106             // Update current window size
    107             this->windowWidth_   = pRenderWindow->getWidth();
    108             this->windowHeight_  = pRenderWindow->getHeight();
    109 
    110             // Create temporary texture
    111             this->tempTexture_ = Ogre::TextureManager::getSingleton().createManual("ScreenShotTex",
    112                 Resource::getDefaultResourceGroup(), Ogre::TEX_TYPE_2D, this->windowWidth_,
    113                 this->windowHeight_, 0, Ogre::PF_B8G8R8, Ogre::TU_RENDERTARGET);
    114 
    115             // Get the current render target of the temporary texture
    116             this->renderTarget_ = this->tempTexture_->getBuffer()->getRenderTarget();
    117 
    118             // HardwarePixelBufferSharedPtr to the buffer of the temporary texture
    119             this->buffer_ = this->tempTexture_->getBuffer();
    120 
    121             // Create PixelBox
    122             this->data_ = new uint8_t[(this->windowWidth_ * this->gridSize_) * (this->windowHeight_ * this->gridSize_) * 3];
    123             this->finalPicturePB_ = Ogre::PixelBox(this->windowWidth_ * this->gridSize_, this->windowHeight_ * this->gridSize_, 1, Ogre::PF_B8G8R8, this->data_);
    124         }
    125     }
    126 
     123        // Update current window size
     124        this->windowWidth_   = pRenderWindow->getWidth();
     125        this->windowHeight_  = pRenderWindow->getHeight();
     126
     127        // Create temporary texture
     128        this->tempTexture_ = Ogre::TextureManager::getSingleton().createManual("ScreenShotTex", Resource::getDefaultResourceGroup(), Ogre::TEX_TYPE_2D, this->windowWidth_, this->windowHeight_, 0, Ogre::PF_B8G8R8, Ogre::TU_RENDERTARGET);
     129
     130        // Get the current render target of the temporary texture
     131        this->renderTarget_ = this->tempTexture_->getBuffer()->getRenderTarget();
     132
     133        // HardwarePixelBufferSharedPtr to the buffer of the temporary texture
     134        this->buffer_ = this->tempTexture_->getBuffer();
     135
     136        // Create PixelBox
     137        this->data_ = new uint8_t[(this->windowWidth_ * this->gridSize_) * (this->windowHeight_ * this->gridSize_) * 3];
     138        this->finalPicturePB_ = new Ogre::PixelBox(this->windowWidth_ * this->gridSize_, this->windowHeight_ * this->gridSize_, 1, Ogre::PF_B8G8R8, this->data_);
     139    }
    127140
    128141    /**
     
    143156        }
    144157        else
    145         {
    146158            COUT(1) << "There needs to be an active camera to make screenshots." << endl;
    147             return;
    148         }
     159
     160        this->cleanup();
    149161    }
    150162
     
    152164    @brief
    153165        Creates a screenshot and returns it.
     166        After calling this method the ScreenshotManager should be cleaned using cleanup().
    154167    @return
    155         Returns a pointer to an Ogre::Image with the screenshot.
     168        Returns a pointer to an Ogre::Image with the screenshot. The memory must be freed, when the image is no longer needed.
    156169    */
    157170    Ogre::Image* ScreenshotManager::getScreenshot()
     
    165178    @brief
    166179        Creates a screenshot with the given camera and returns it.
     180        After calling this method the ScreenshotManager should be cleaned using cleanup().
    167181    @param camera
    168182        A pointer to the camera the screenshot should be taken with.
    169183    @return
    170         Returns a pointer to an Ogre::Image with the screenshot.
     184        Returns a pointer to an Ogre::Image with the screenshot. The memory must be freed, when the image is no longer needed.
    171185    */
    172186    Ogre::Image* ScreenshotManager::getScreenshot(Ogre::Camera* camera)
     
    198212        if(this->gridSize_ <= 1)
    199213        {
     214            //TODO: Not working.
    200215            // Simple case where the contents of the screen are taken directly
    201216            // Also used when an invalid value is passed within gridSize (zero or negative grid size)
    202217            this->renderTarget_->update(); // Render
    203218           
    204             finalImage = &finalImage->loadDynamicImage(static_cast<unsigned char*>(finalPicturePB_.data), finalPicturePB_.getWidth(), finalPicturePB_.getHeight(),Ogre::PF_B8G8R8);
     219            finalImage->loadDynamicImage(static_cast<unsigned char*>(finalPicturePB_->data), finalPicturePB_->getWidth(), finalPicturePB_->getHeight(),Ogre::PF_B8G8R8);
    205220        }
    206221        else
     
    240255                // Copy the content from the temp buffer into the final picture PixelBox
    241256                // Place the tempBuffer content at the right position
    242                 this->buffer_->blitToMemory(this->finalPicturePB_.getSubVolume(subBox));
     257                this->buffer_->blitToMemory(this->finalPicturePB_->getSubVolume(subBox));
    243258               
    244259                COUT(4) << "Created screenshot number " << nbScreenshots << " for multi grid HD screenshot." << endl;
     
    250265           
    251266            // Insert the PixelBox data into the Image Object
    252             finalImage->loadDynamicImage(static_cast<unsigned char*>(this->finalPicturePB_.data), this->finalPicturePB_.getWidth(), this->finalPicturePB_.getHeight(), 1, Ogre::PF_B8G8R8, false);
     267            finalImage->loadDynamicImage(static_cast<unsigned char*>(this->finalPicturePB_->data), this->finalPicturePB_->getWidth(), this->finalPicturePB_->getHeight(), 1, Ogre::PF_B8G8R8, false);
    253268        }
    254269       
     
    275290
    276291        this->gridSize_ = size;
    277         // New PixelBox for the changed size.
    278         this->data_ = new uint8_t[(this->windowWidth_ * this->gridSize_) * (this->windowHeight_ * this->gridSize_) * 3];
    279         this->finalPicturePB_ = Ogre::PixelBox(this->windowWidth_ * this->gridSize_, this->windowHeight_ * this->gridSize_, 1, Ogre::PF_B8G8R8, this->data_);
    280292    }
    281293
  • code/trunk/src/modules/designtools/ScreenshotManager.h

    r8232 r8413  
    5353        Class encapsulates screenshot functionality and provides a method for making multi grid (i.e. HD) screenshots.
    5454       
    55        
    5655    @author
    5756        This code comes from http://www.ogre3d.org/tikiwiki/High+resolution+screenshots which is Public Domain.
     
    8887            inline unsigned int getGridSize(void)
    8988                { return this->gridSize_; }
     89
     90            void cleanup(void); // Frees used memory.
    9091           
    9192        protected:
     
    103104            Ogre::HardwarePixelBufferSharedPtr buffer_; //!< Buffer for the temporary texture.
    104105           
    105             Ogre::PixelBox finalPicturePB_; //!< PixelBox for large screenshots, contains the screenshot for gridSize_ > 1.
     106            Ogre::PixelBox* finalPicturePB_; //!< PixelBox for large screenshots, contains the screenshot for gridSize_ > 1.
    106107            uint8_t* data_; //!< Data pointer for the PixelBox.
    107108
  • code/trunk/src/modules/designtools/SkyboxGenerator.cc

    r8351 r8413  
    315315        image->save(PathConfig::getInstance().getLogPathString()+name);
    316316        delete image;
     317        ScreenshotManager::getInstance().cleanup(); // Free memory in ScreenshotManager.
    317318    }
    318319}
Note: See TracChangeset for help on using the changeset viewer.