CCTexture2D

#ifndef __CCTEXTURE2D_H__

#define __CCTEXTURE2D_H__


#include <string>

#include "cocoa/CCObject.h"

#include "cocoa/CCGeometry.h"

#include "ccTypes.h"

#ifdef EMSCRIPTEN

#include "base_nodes/CCGLBufferedNode.h"

#endif // EMSCRIPTEN


NS_CC_BEGIN


class CCImage;


/**

 * @addtogroup textures

 * @{

 */


//CONSTANTS:


/** @typedef CCTexture2DPixelFormat

Possible texture pixel formats

*/

typedef enum {


    //! 32-bit texture: RGBA8888

    kCCTexture2DPixelFormat_RGBA8888,

    //! 24-bit texture: RGBA888

    kCCTexture2DPixelFormat_RGB888,

    //! 16-bit texture without Alpha channel

    kCCTexture2DPixelFormat_RGB565,

    //! 8-bit textures used as masks

    kCCTexture2DPixelFormat_A8,

    //! 8-bit intensity texture

    kCCTexture2DPixelFormat_I8,

    //! 16-bit textures used as masks

    kCCTexture2DPixelFormat_AI88,

    //! 16-bit textures: RGBA4444

    kCCTexture2DPixelFormat_RGBA4444,

    //! 16-bit textures: RGB5A1

    kCCTexture2DPixelFormat_RGB5A1,    

    //! 4-bit PVRTC-compressed texture: PVRTC4

    kCCTexture2DPixelFormat_PVRTC4,

    //! 2-bit PVRTC-compressed texture: PVRTC2

    kCCTexture2DPixelFormat_PVRTC2,



    //! Default texture format: RGBA8888

    kCCTexture2DPixelFormat_Default = kCCTexture2DPixelFormat_RGBA8888,


    // backward compatibility stuff

    kTexture2DPixelFormat_RGBA8888 = kCCTexture2DPixelFormat_RGBA8888,

    kTexture2DPixelFormat_RGB888 = kCCTexture2DPixelFormat_RGB888,

    kTexture2DPixelFormat_RGB565 = kCCTexture2DPixelFormat_RGB565,

    kTexture2DPixelFormat_A8 = kCCTexture2DPixelFormat_A8,

    kTexture2DPixelFormat_RGBA4444 = kCCTexture2DPixelFormat_RGBA4444,

    kTexture2DPixelFormat_RGB5A1 = kCCTexture2DPixelFormat_RGB5A1,

    kTexture2DPixelFormat_Default = kCCTexture2DPixelFormat_Default


} CCTexture2DPixelFormat;


class CCGLProgram;


/**

Extension to set the Min / Mag filter

*/

typedef struct _ccTexParams {

    GLuint    minFilter;

    GLuint    magFilter;

    GLuint    wrapS;

    GLuint    wrapT;

} ccTexParams;


//CLASS INTERFACES:


/** @brief CCTexture2D class.

* This class allows to easily create OpenGL 2D textures from images, text or raw data.

* The created CCTexture2D object will always have power-of-two dimensions. 

* Depending on how you create the CCTexture2D object, the actual image area of the texture might be smaller than the texture dimensions i.e. "contentSize" != (pixelsWide, pixelsHigh) and (maxS, maxT) != (1.0, 1.0).

* Be aware that the content of the generated textures will be upside-down!

*/

class CC_DLL CCTexture2D : public CCObject

#ifdef EMSCRIPTEN

, public CCGLBufferedNode

#endif // EMSCRIPTEN

{

public:

    CCTexture2D();

    virtual ~CCTexture2D();


    const char* description(void);


    /** These functions are needed to create mutable textures */

    void releaseData(void *data);

    void* keepData(void *data, unsigned int length);


    /** Initializes with a texture2d with data */

    bool initWithData(const void* data, CCTexture2DPixelFormat pixelFormat, unsigned int pixelsWide, unsigned int pixelsHigh, const CCSize& contentSize);


    /**

    Drawing extensions to make it easy to draw basic quads using a CCTexture2D object.

    These functions require GL_TEXTURE_2D and both GL_VERTEX_ARRAY and GL_TEXTURE_COORD_ARRAY client states to be enabled.

    */

    /** draws a texture at a given point */

    void drawAtPoint(const CCPoint& point);

    /** draws a texture inside a rect */

    void drawInRect(const CCRect& rect);


    /**

    Extensions to make it easy to create a CCTexture2D object from an image file.

    Note that RGBA type textures will have their alpha premultiplied - use the blending mode (GL_ONE, GL_ONE_MINUS_SRC_ALPHA).

    */

    /** Initializes a texture from a UIImage object */


    bool initWithImage(CCImage * uiImage);


    /** Initializes a texture from a string with dimensions, alignment, font name and font size */

    bool initWithString(const char *text,  const char *fontName, float fontSize, const CCSize& dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment);

    /** Initializes a texture from a string with font name and font size */

    bool initWithString(const char *text, const char *fontName, float fontSize);

    /** Initializes a texture from a string using a text definition*/

    bool initWithString(const char *text, ccFontDefinition *textDefinition);

    

    /** Initializes a texture from a PVR file */

    bool initWithPVRFile(const char* file);

    

    /** Initializes a texture from a ETC file */

    bool initWithETCFile(const char* file);


    /** sets the min filter, mag filter, wrap s and wrap t texture parameters.

    If the texture size is NPOT (non power of 2), then in can only use GL_CLAMP_TO_EDGE in GL_TEXTURE_WRAP_{S,T}.


    @warning Calling this method could allocate additional texture memory.


    @since v0.8

    */

    void setTexParameters(ccTexParams* texParams);


    /** sets antialias texture parameters:

    - GL_TEXTURE_MIN_FILTER = GL_LINEAR

    - GL_TEXTURE_MAG_FILTER = GL_LINEAR


    @warning Calling this method could allocate additional texture memory.


    @since v0.8

    */

    void setAntiAliasTexParameters();


    /** sets alias texture parameters:

    - GL_TEXTURE_MIN_FILTER = GL_NEAREST

    - GL_TEXTURE_MAG_FILTER = GL_NEAREST


    @warning Calling this method could allocate additional texture memory.


    @since v0.8

    */

    void setAliasTexParameters();



    /** Generates mipmap images for the texture.

    It only works if the texture size is POT (power of 2).

    @since v0.99.0

    */

    void generateMipmap();


    /** returns the pixel format.

     @since v2.0

     */

    const char* stringForFormat();


    /** returns the bits-per-pixel of the in-memory OpenGL texture

    @since v1.0

    */

    unsigned int bitsPerPixelForFormat();  


    /** Helper functions that returns bits per pixels for a given format.

     @since v2.0

     */

    unsigned int bitsPerPixelForFormat(CCTexture2DPixelFormat format);


    /** sets the default pixel format for UIImagescontains alpha channel.

    If the UIImage contains alpha channel, then the options are:

    - generate 32-bit textures: kCCTexture2DPixelFormat_RGBA8888 (default one)

    - generate 24-bit textures: kCCTexture2DPixelFormat_RGB888

    - generate 16-bit textures: kCCTexture2DPixelFormat_RGBA4444

    - generate 16-bit textures: kCCTexture2DPixelFormat_RGB5A1

    - generate 16-bit textures: kCCTexture2DPixelFormat_RGB565

    - generate 8-bit textures: kCCTexture2DPixelFormat_A8 (only use it if you use just 1 color)


    How does it work ?

    - If the image is an RGBA (with Alpha) then the default pixel format will be used (it can be a 8-bit, 16-bit or 32-bit texture)

    - If the image is an RGB (without Alpha) then: If the default pixel format is RGBA8888 then a RGBA8888 (32-bit) will be used. Otherwise a RGB565 (16-bit texture) will be used.


    This parameter is not valid for PVR / PVR.CCZ images.


    @since v0.8

    */

    static void setDefaultAlphaPixelFormat(CCTexture2DPixelFormat format);


    /** returns the alpha pixel format

    @since v0.8

    */

    static CCTexture2DPixelFormat defaultAlphaPixelFormat();


    /** treats (or not) PVR files as if they have alpha premultiplied.

     Since it is impossible to know at runtime if the PVR images have the alpha channel premultiplied, it is

     possible load them as if they have (or not) the alpha channel premultiplied.

     

     By default it is disabled.

     

     @since v0.99.5

     */

    static void PVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied);


    /** content size */

    const CCSize& getContentSizeInPixels();

    

    bool hasPremultipliedAlpha();

    bool hasMipmaps();

private:

    bool initPremultipliedATextureWithImage(CCImage * image, unsigned int pixelsWide, unsigned int pixelsHigh);

    

    // By default PVR images are treated as if they don't have the alpha channel premultiplied

    bool m_bPVRHaveAlphaPremultiplied;


    /** pixel format of the texture */

    CC_PROPERTY_READONLY(CCTexture2DPixelFormat, m_ePixelFormat, PixelFormat)

    /** width in pixels */

    CC_PROPERTY_READONLY(unsigned int, m_uPixelsWide, PixelsWide)

    /** height in pixels */

    CC_PROPERTY_READONLY(unsigned int, m_uPixelsHigh, PixelsHigh)


    /** texture name */

    CC_PROPERTY_READONLY(GLuint, m_uName, Name)


    /** texture max S */

    CC_PROPERTY(GLfloat, m_fMaxS, MaxS)

    /** texture max T */

    CC_PROPERTY(GLfloat, m_fMaxT, MaxT)

    /** content size */

    CC_PROPERTY_READONLY(CCSize, m_tContentSize, ContentSize)


    /** whether or not the texture has their Alpha premultiplied */

    bool m_bHasPremultipliedAlpha;


    bool m_bHasMipmaps;


    /** shader program used by drawAtPoint and drawInRect */

    CC_PROPERTY(CCGLProgram*, m_pShaderProgram, ShaderProgram);

};


// end of textures group

/// @}


NS_CC_END


#endif //__CCTEXTURE2D_H__


转载于:https://www.cnblogs.com/sssssnian/p/3748617.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值