2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 10:59:01 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (C) 2008 Apple Inc. All Rights Reserved.
|
2010-08-02 10:58:00 +08:00
|
|
|
|
|
|
|
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 __CCTEXTURE2D_H__
|
|
|
|
#define __CCTEXTURE2D_H__
|
|
|
|
|
|
|
|
#include <string>
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCObject.h"
|
|
|
|
#include "CCGeometry.h"
|
2010-08-16 15:02:17 +08:00
|
|
|
#include "ccTypes.h"
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2010-11-12 17:26:01 +08:00
|
|
|
namespace cocos2d {
|
2011-03-07 17:11:57 +08:00
|
|
|
class CCImage;
|
2010-07-26 17:32:23 +08:00
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
//CONSTANTS:
|
|
|
|
|
|
|
|
/** @typedef CCTexture2DPixelFormat
|
|
|
|
Possible texture pixel formats
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
kCCTexture2DPixelFormat_Automatic = 0,
|
|
|
|
//! 32-bit texture: RGBA8888
|
|
|
|
kCCTexture2DPixelFormat_RGBA8888,
|
2010-09-26 10:09:05 +08:00
|
|
|
//! 24-bit texture: RGBA888
|
|
|
|
kCCTexture2DPixelFormat_RGB888,
|
2010-12-30 17:30:11 +08:00
|
|
|
//! 16-bit texture without Alpha channel
|
2010-07-15 18:15:00 +08:00
|
|
|
kCCTexture2DPixelFormat_RGB565,
|
|
|
|
//! 8-bit textures used as masks
|
|
|
|
kCCTexture2DPixelFormat_A8,
|
2011-07-19 15:14:59 +08:00
|
|
|
//! 8-bit intensity texture
|
2011-07-05 10:47:25 +08:00
|
|
|
kCCTexture2DPixelFormat_I8,
|
2011-07-19 15:14:59 +08:00
|
|
|
//! 16-bit textures used as masks
|
2011-07-05 10:47:25 +08:00
|
|
|
kCCTexture2DPixelFormat_AI88,
|
2010-07-15 18:15:00 +08:00
|
|
|
//! 16-bit textures: RGBA4444
|
|
|
|
kCCTexture2DPixelFormat_RGBA4444,
|
|
|
|
//! 16-bit textures: RGB5A1
|
|
|
|
kCCTexture2DPixelFormat_RGB5A1,
|
2011-07-19 15:14:59 +08:00
|
|
|
//! 4-bit PVRTC-compressed texture: PVRTC4
|
|
|
|
kCCTexture2DPixelFormat_PVRTC4,
|
|
|
|
//! 2-bit PVRTC-compressed texture: PVRTC2
|
2011-07-05 10:47:25 +08:00
|
|
|
kCCTexture2DPixelFormat_PVRTC2,
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
//! Default texture format: RGBA8888
|
|
|
|
kCCTexture2DPixelFormat_Default = kCCTexture2DPixelFormat_RGBA8888,
|
|
|
|
|
|
|
|
// backward compatibility stuff
|
|
|
|
kTexture2DPixelFormat_Automatic = kCCTexture2DPixelFormat_Automatic,
|
|
|
|
kTexture2DPixelFormat_RGBA8888 = kCCTexture2DPixelFormat_RGBA8888,
|
2010-09-26 10:09:05 +08:00
|
|
|
kTexture2DPixelFormat_RGB888 = kCCTexture2DPixelFormat_RGB888,
|
2010-07-15 18:15:00 +08:00
|
|
|
kTexture2DPixelFormat_RGB565 = kCCTexture2DPixelFormat_RGB565,
|
|
|
|
kTexture2DPixelFormat_A8 = kCCTexture2DPixelFormat_A8,
|
|
|
|
kTexture2DPixelFormat_RGBA4444 = kCCTexture2DPixelFormat_RGBA4444,
|
|
|
|
kTexture2DPixelFormat_RGB5A1 = kCCTexture2DPixelFormat_RGB5A1,
|
|
|
|
kTexture2DPixelFormat_Default = kCCTexture2DPixelFormat_Default
|
|
|
|
|
|
|
|
} CCTexture2DPixelFormat;
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
class CCGLProgram;
|
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
/**
|
|
|
|
Extension to set the Min / Mag filter
|
|
|
|
*/
|
|
|
|
typedef struct _ccTexParams {
|
|
|
|
GLuint minFilter;
|
|
|
|
GLuint magFilter;
|
|
|
|
GLuint wrapS;
|
|
|
|
GLuint wrapT;
|
|
|
|
} ccTexParams;
|
|
|
|
|
|
|
|
//CLASS INTERFACES:
|
|
|
|
|
2010-09-29 09:44:57 +08:00
|
|
|
/** @brief CCTexture2D class.
|
2010-07-15 18:15:00 +08:00
|
|
|
* 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!
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCTexture2D : public CCObject
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCTexture2D();
|
2010-08-06 16:05:19 +08:00
|
|
|
virtual ~CCTexture2D();
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2010-08-06 16:05:19 +08:00
|
|
|
char * description(void);
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
/** These functions are needed to create mutable textures */
|
|
|
|
void releaseData(void *data);
|
|
|
|
void* keepData(void *data, unsigned int length);
|
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
/** Intializes with a texture2d with data */
|
2011-08-17 21:19:57 +08:00
|
|
|
bool initWithData(const void* data, CCTexture2DPixelFormat pixelFormat, unsigned int pixelsWide, unsigned int pixelsHigh, const CCSize& contentSize);
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
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 */
|
2011-08-17 21:19:57 +08:00
|
|
|
void drawAtPoint(const CCPoint& point);
|
2010-07-15 18:15:00 +08:00
|
|
|
/** draws a texture inside a rect */
|
2011-08-17 21:19:57 +08:00
|
|
|
void drawInRect(const CCRect& rect);
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
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 */
|
2012-04-08 14:16:29 +08:00
|
|
|
|
|
|
|
bool initWithImage(CCImage * uiImage);
|
|
|
|
|
|
|
|
bool initWithImage(CCImage *uiImage, ccResolutionType resolution);
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Extensions to make it easy to create a CCTexture2D object from a string of text.
|
|
|
|
Note that the generated textures are of type A8 - use the blending mode (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).
|
|
|
|
*/
|
|
|
|
/** Initializes a texture from a string with dimensions, alignment, font name and font size */
|
2011-08-17 21:19:57 +08:00
|
|
|
bool initWithString(const char *text, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize);
|
2010-07-15 18:15:00 +08:00
|
|
|
/** Initializes a texture from a string with font name and font size */
|
2010-09-04 12:02:52 +08:00
|
|
|
bool initWithString(const char *text, const char *fontName, float fontSize);
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
#ifdef CC_SUPPORT_PVRTC
|
2010-07-15 18:15:00 +08:00
|
|
|
/**
|
|
|
|
Extensions to make it easy to create a CCTexture2D object from a PVRTC file
|
|
|
|
Note that the generated textures don't have their alpha premultiplied - use the blending mode (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).
|
|
|
|
*/
|
|
|
|
/** Initializes a texture from a PVRTC buffer */
|
2011-07-19 15:14:59 +08:00
|
|
|
bool initWithPVRTCData(const void *data, int level, int bpp, bool hasAlpha, int length, CCTexture2DPixelFormat pixelFormat);
|
|
|
|
#endif // CC_SUPPORT_PVRTC
|
|
|
|
|
|
|
|
/** Initializes a texture from a PVR file */
|
|
|
|
bool initWithPVRFile(const char* file);
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
/** 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}.
|
|
|
|
@since v0.8
|
|
|
|
*/
|
|
|
|
void setTexParameters(ccTexParams* texParams);
|
|
|
|
|
|
|
|
/** sets antialias texture parameters:
|
|
|
|
- GL_TEXTURE_MIN_FILTER = GL_LINEAR
|
|
|
|
- GL_TEXTURE_MAG_FILTER = GL_LINEAR
|
|
|
|
|
|
|
|
@since v0.8
|
|
|
|
*/
|
|
|
|
void setAntiAliasTexParameters();
|
|
|
|
|
|
|
|
/** sets alias texture parameters:
|
|
|
|
- GL_TEXTURE_MIN_FILTER = GL_NEAREST
|
|
|
|
- GL_TEXTURE_MAG_FILTER = GL_NEAREST
|
|
|
|
|
|
|
|
@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();
|
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
/** returns the bits-per-pixel of the in-memory OpenGL texture
|
|
|
|
@since v1.0
|
2011-07-05 10:47:25 +08:00
|
|
|
*/
|
2011-12-20 14:59:15 +08:00
|
|
|
unsigned int bitsPerPixelForFormat();
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2011-12-20 14:59:15 +08:00
|
|
|
/** sets the default pixel format for UIImagescontains alpha channel.
|
2010-07-15 18:15:00 +08:00
|
|
|
If the UIImage contains alpha channel, then the options are:
|
|
|
|
- generate 32-bit textures: kCCTexture2DPixelFormat_RGBA8888 (default one)
|
2010-09-28 12:14:12 +08:00
|
|
|
- generate 24-bit textures: kCCTexture2DPixelFormat_RGB888
|
2010-07-15 18:15:00 +08:00
|
|
|
- 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)
|
2010-09-28 12:14:12 +08:00
|
|
|
- If the image is an RGB (without Alpha) then an RGB565 or RGB888 texture will be used (16-bit texture)
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
@since v0.8
|
|
|
|
*/
|
|
|
|
static void setDefaultAlphaPixelFormat(CCTexture2DPixelFormat format);
|
|
|
|
|
|
|
|
/** returns the alpha pixel format
|
|
|
|
@since v0.8
|
|
|
|
*/
|
|
|
|
static CCTexture2DPixelFormat defaultAlphaPixelFormat();
|
|
|
|
|
2011-12-20 14:59:15 +08:00
|
|
|
/** 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);
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
/** content size */
|
|
|
|
const CCSize& getContentSizeInPixels();
|
2010-07-15 18:15:00 +08:00
|
|
|
private:
|
2011-03-07 17:11:57 +08:00
|
|
|
bool initPremultipliedATextureWithImage(CCImage * image, unsigned int pixelsWide, unsigned int pixelsHigh);
|
2011-07-19 15:14:59 +08:00
|
|
|
|
|
|
|
// By default PVR images are treated as if they don't have the alpha channel premultiplied
|
|
|
|
bool m_bPVRHaveAlphaPremultiplied;
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
/** pixel format of the texture */
|
|
|
|
CC_PROPERTY_READONLY(CCTexture2DPixelFormat, m_ePixelFormat, PixelFormat)
|
|
|
|
/** width in pixels */
|
|
|
|
CC_PROPERTY_READONLY(unsigned int, m_uPixelsWide, PixelsWide)
|
|
|
|
/** hight 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 */
|
2012-04-08 22:37:58 +08:00
|
|
|
CC_PROPERTY_READONLY(CCSize, m_tContentSize, ContentSize)
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
/** whether or not the texture has their Alpha premultiplied */
|
|
|
|
CC_PROPERTY_READONLY(bool, m_bHasPremultipliedAlpha, HasPremultipliedAlpha);
|
|
|
|
|
2012-04-08 14:16:29 +08:00
|
|
|
/** shader program used by drawAtPoint and drawInRect */
|
|
|
|
CC_PROPERTY(CCGLProgram*, m_pShaderProgram, ShaderProgram);
|
|
|
|
|
2012-04-08 14:26:50 +08:00
|
|
|
|
2012-04-08 14:16:29 +08:00
|
|
|
/** Returns the resolution type of the texture.
|
|
|
|
Is it a RetinaDisplay texture, an iPad texture or an standard texture ?
|
|
|
|
Only valid on iOS. Not valid on OS X.
|
|
|
|
|
|
|
|
Should be a readonly property. It is readwrite as a hack.
|
|
|
|
|
|
|
|
@since v1.1
|
|
|
|
*/
|
|
|
|
CC_SYNTHESIZE(ccResolutionType, m_eResolutionType, ResolutionType);
|
2010-07-15 18:15:00 +08:00
|
|
|
};
|
2010-11-12 17:26:01 +08:00
|
|
|
}//namespace cocos2d
|
2010-08-02 10:58:00 +08:00
|
|
|
|
|
|
|
#endif //__CCTEXTURE2D_H__
|
|
|
|
|