2012-04-19 14:35:52 +08:00
/****************************************************************************
2012-06-12 01:43:07 +08:00
Copyright ( c ) 2010 - 2012 cocos2d - x . org
2012-04-19 14:35:52 +08:00
Copyright ( c ) 2009 Jason Booth
http : //www.cocos2d-x.org
Permission is hereby granted , free of charge , to any person obtaining a copy
of this software and associated documentation files ( the " Software " ) , to deal
in the Software without restriction , including without limitation the rights
to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
copies of the Software , and to permit persons to whom the Software is
furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# ifndef __CCRENDER_TEXTURE_H__
# define __CCRENDER_TEXTURE_H__
2012-06-19 13:50:11 +08:00
# include "base_nodes/CCNode.h"
# include "sprite_nodes/CCSprite.h"
2012-04-19 14:35:52 +08:00
# include "kazmath/mat4.h"
2013-07-25 20:38:15 +08:00
# include "platform/CCImage.h"
2012-04-19 14:35:52 +08:00
NS_CC_BEGIN
2012-06-20 18:09:11 +08:00
/**
* @ addtogroup textures
* @ {
*/
2012-04-19 14:35:52 +08:00
/**
2013-06-20 14:13:12 +08:00
@ brief RenderTexture is a generic rendering target . To render things into it ,
2012-04-19 14:35:52 +08:00
simply construct a render target , call begin on it , call visit on any cocos
2012-09-17 15:02:24 +08:00
scenes or objects to render them , and call end . For convenience , render texture
2012-04-19 14:35:52 +08:00
adds a sprite as it ' s display child with the results , so you can simply add
the render texture to your scene and treat it like any other CocosNode .
There are also functions for saving the render texture to disk in PNG or JPG format .
@ since v0 .8 .1
*/
2013-06-20 14:13:12 +08:00
class CC_DLL RenderTexture : public Node
2012-04-19 14:35:52 +08:00
{
public :
2012-06-19 13:50:11 +08:00
/** initializes a RenderTexture object with width and height in Points and a pixel format( only RGB and RGBA formats are valid ) and depthStencil format*/
2013-07-25 19:52:44 +08:00
static RenderTexture * create ( int w , int h , Texture2D : : PixelFormat eFormat , GLuint uDepthStencilFormat ) ;
2012-06-08 14:30:55 +08:00
2012-04-19 14:35:52 +08:00
/** creates a RenderTexture object with width and height in Points and a pixel format, only RGB and RGBA formats are valid */
2013-07-25 19:52:44 +08:00
static RenderTexture * create ( int w , int h , Texture2D : : PixelFormat eFormat ) ;
2012-04-19 14:35:52 +08:00
/** creates a RenderTexture object with width and height in Points, pixel format is RGBA8888 */
2013-06-20 14:13:12 +08:00
static RenderTexture * create ( int w , int h ) ;
2012-04-19 14:35:52 +08:00
2013-07-18 07:56:19 +08:00
RenderTexture ( ) ;
virtual ~ RenderTexture ( ) ;
2012-04-19 14:35:52 +08:00
/** initializes a RenderTexture object with width and height in Points and a pixel format, only RGB and RGBA formats are valid */
2013-07-25 19:52:44 +08:00
bool initWithWidthAndHeight ( int w , int h , Texture2D : : PixelFormat eFormat ) ;
2012-04-19 14:35:52 +08:00
2012-06-19 13:50:11 +08:00
/** initializes a RenderTexture object with width and height in Points and a pixel format( only RGB and RGBA formats are valid ) and depthStencil format*/
2013-07-25 19:52:44 +08:00
bool initWithWidthAndHeight ( int w , int h , Texture2D : : PixelFormat eFormat , GLuint uDepthStencilFormat ) ;
2012-06-12 01:43:07 +08:00
2012-04-19 14:35:52 +08:00
/** starts grabbing */
void begin ( ) ;
/** starts rendering to the texture while clearing the texture first.
This is more efficient then calling - clear first and then - begin */
void beginWithClear ( float r , float g , float b , float a ) ;
2012-06-19 13:50:11 +08:00
/** starts rendering to the texture while clearing the texture first.
This is more efficient then calling - clear first and then - begin */
void beginWithClear ( float r , float g , float b , float a , float depthValue ) ;
/** starts rendering to the texture while clearing the texture first.
This is more efficient then calling - clear first and then - begin */
2012-06-12 01:43:07 +08:00
void beginWithClear ( float r , float g , float b , float a , float depthValue , int stencilValue ) ;
2012-04-19 14:35:52 +08:00
/** end is key word of lua, use other name to export to lua. */
inline void endToLua ( ) { end ( ) ; } ;
/** ends grabbing*/
2012-06-14 18:37:57 +08:00
void end ( ) ;
2012-04-19 14:35:52 +08:00
/** clears the texture with a color */
void clear ( float r , float g , float b , float a ) ;
2012-06-19 13:50:11 +08:00
/** clears the texture with a specified depth value */
void clearDepth ( float depthValue ) ;
/** clears the texture with a specified stencil value */
2012-06-12 01:43:07 +08:00
void clearStencil ( int stencilValue ) ;
2013-06-20 14:13:12 +08:00
/* creates a new Image from with the texture's data.
2012-04-19 14:35:52 +08:00
Caller is responsible for releasing it by calling delete .
*/
2013-07-24 17:53:34 +08:00
2013-06-20 14:13:12 +08:00
Image * newImage ( bool flipImage = true ) ;
2013-07-24 17:53:34 +08:00
CC_DEPRECATED_ATTRIBUTE Image * newCCImage ( bool flipImage = true ) { return newImage ( flipImage ) ; } ;
2012-04-19 14:35:52 +08:00
/** saves the texture into a file using JPEG format. The file will be saved in the Documents folder.
Returns YES if the operation is successful .
*/
bool saveToFile ( const char * szFilePath ) ;
/** saves the texture into a file. The format could be JPG or PNG. The file will be saved in the Documents folder.
Returns YES if the operation is successful .
*/
2013-07-25 20:38:15 +08:00
bool saveToFile ( const char * name , Image : : Format format ) ;
2012-06-14 18:37:57 +08:00
/** Listen "come to background" message, and save render texture.
It only has effect on Android .
*/
2013-06-20 14:13:12 +08:00
void listenToBackground ( Object * obj ) ;
2012-11-14 18:05:15 +08:00
2012-11-30 07:04:07 +08:00
/** Listen "come to foreground" message and restore the frame buffer object
It only has effect on Android .
*/
2013-06-20 14:13:12 +08:00
void listenToForeground ( Object * obj ) ;
2012-11-30 07:04:07 +08:00
2012-11-14 18:05:15 +08:00
/** Valid flags: GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT. They can be OR'ed. Valid when "autoDraw is YES. */
2013-07-23 20:36:41 +08:00
inline unsigned int getClearFlags ( ) const { return _clearFlags ; } ;
inline void setClearFlags ( unsigned int clearFlags ) { _clearFlags = clearFlags ; } ;
2012-11-14 18:05:15 +08:00
/** Clear color value. Valid only when "autoDraw" is true. */
2013-07-23 20:36:41 +08:00
inline const Color4F & getClearColor ( ) const { return _clearColor ; } ;
inline void setClearColor ( const Color4F & clearColor ) { _clearColor = clearColor ; } ;
2012-11-14 18:05:15 +08:00
/** Value for clearDepth. Valid only when autoDraw is true. */
2013-07-23 20:36:41 +08:00
inline float getClearDepth ( ) const { return _clearDepth ; } ;
inline void setClearDepth ( float clearDepth ) { _clearDepth = clearDepth ; } ;
2012-11-14 18:05:15 +08:00
/** Value for clear Stencil. Valid only when autoDraw is true */
2013-07-23 20:36:41 +08:00
inline int getClearStencil ( ) const { return _clearStencil ; } ;
inline void setClearStencil ( float clearStencil ) { _clearStencil = clearStencil ; } ;
2012-11-14 18:05:15 +08:00
/** When enabled, it will render its children into the texture automatically. Disabled by default for compatiblity reasons.
Will be enabled in the future .
*/
2013-07-23 20:36:41 +08:00
inline bool isAutoDraw ( ) const { return _autoDraw ; } ;
inline void setAutoDraw ( bool isAutoDraw ) { _autoDraw = isAutoDraw ; } ;
2012-11-14 18:05:15 +08:00
2013-07-23 18:26:26 +08:00
/** Gets the Sprite being used. */
2013-07-23 20:36:41 +08:00
inline Sprite * getSprite ( ) const { return _sprite ; } ;
2013-07-23 18:26:26 +08:00
/** Sets the Sprite being used. */
2013-07-23 20:36:41 +08:00
inline void setSprite ( Sprite * sprite ) {
CC_SAFE_RETAIN ( sprite ) ;
CC_SAFE_RELEASE ( _sprite ) ;
_sprite = sprite ;
} ;
2013-07-23 18:26:26 +08:00
2013-07-18 07:56:19 +08:00
// Overrides
virtual void visit ( ) override ;
virtual void draw ( ) override ;
2012-11-14 18:05:15 +08:00
private :
void beginWithClear ( float r , float g , float b , float a , float depthValue , int stencilValue , GLbitfield flags ) ;
2012-04-19 14:35:52 +08:00
protected :
2013-06-15 14:03:30 +08:00
GLuint _FBO ;
GLuint _depthRenderBufffer ;
GLint _oldFBO ;
2013-06-20 14:13:12 +08:00
Texture2D * _texture ;
Texture2D * _textureCopy ; // a copy of _texture
Image * _UITextureImage ;
2013-07-26 04:36:19 +08:00
Texture2D : : PixelFormat _pixelFormat ;
2012-11-14 18:05:15 +08:00
// code for "auto" update
2013-06-15 14:03:30 +08:00
GLbitfield _clearFlags ;
2013-07-05 16:49:22 +08:00
Color4F _clearColor ;
2013-06-15 14:03:30 +08:00
GLclampf _clearDepth ;
GLint _clearStencil ;
bool _autoDraw ;
2013-07-18 07:56:19 +08:00
/** The Sprite being used.
The sprite , by default , will use the following blending function : GL_ONE , GL_ONE_MINUS_SRC_ALPHA .
The blending function can be changed in runtime by calling :
- [ [ renderTexture sprite ] setBlendFunc : ( BlendFunc ) { GL_ONE , GL_ONE_MINUS_SRC_ALPHA } ] ;
*/
2013-07-23 18:26:26 +08:00
Sprite * _sprite ;
2012-04-19 14:35:52 +08:00
} ;
2012-06-20 18:09:11 +08:00
// end of textures group
/// @}
2012-04-19 14:35:52 +08:00
NS_CC_END
2012-06-14 05:26:28 +08:00
# endif //__CCRENDER_TEXTURE_H__