Cocos2d-x  v3.10
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups
Texture2D Class Reference

Texture2D class. More...

Inherits Ref.

Inherited by TextureCube.

Public Types

enum  PixelFormat {
  AUTO, BGRA8888, RGBA8888, RGB888,
  RGB565, A8, I8, AI88,
  RGBA4444, RGB5A1, PVRTC4, PVRTC4A,
  PVRTC2, PVRTC2A, ETC, S3TC_DXT1,
  S3TC_DXT3, S3TC_DXT5, ATC_RGB, ATC_EXPLICIT_ALPHA,
  ATC_INTERPOLATED_ALPHA, DEFAULT = AUTO
}
 Possible texture pixel formats. More...
 

Public Member Functions

virtual std::string getDescription () const
 Get texutre name, dimensions and coordinates message by a string. More...
 
void releaseGLTexture ()
 Release only the gl texture.
 
bool initWithData (const void *data, ssize_t dataLen, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, const Size &contentSize)
 Initializes a Texture2D object with data. More...
 
bool initWithMipmaps (MipmapInfo *mipmaps, int mipmapsNum, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh)
 Initializes with mipmaps. More...
 
bool updateWithData (const void *data, int offsetX, int offsetY, int width, int height)
 Update with texture data. More...
 
void drawAtPoint (const Vec2 &point)
 Drawing extensions to make it easy to draw basic quads using a Texture2D object. More...
 
var drawAtPoint ( var point)
 Drawing extensions to make it easy to draw basic quads using a Texture2D object. More...
 
local drawAtPoint ( local point)
 Drawing extensions to make it easy to draw basic quads using a Texture2D object. More...
 
void drawInRect (const Rect &rect)
 Draws a texture inside a rect.
 
bool initWithImage (Image *image)
 Extensions to make it easy to create a Texture2D object from an image file. More...
 
bool initWithImage (Image *image, PixelFormat format)
 Initializes a texture from a UIImage object. More...
 
bool initWithString (const char *text, const std::string &fontName, float fontSize, const Size &dimensions=Size(0, 0), TextHAlignment hAlignment=TextHAlignment::CENTER, TextVAlignment vAlignment=TextVAlignment::TOP)
 Initializes a texture from a string with dimensions, alignment, font name and font size. More...
 
local initWithString ( local text, local fontName, local fontSize, local 0, local CENTER, local TOP)
 Initializes a texture from a string with dimensions, alignment, font name and font size. More...
 
bool initWithString (const char *text, const FontDefinition &textDefinition)
 Initializes a texture from a string using a text definition. More...
 
var initWithString ( var text, var textDefinition)
 Initializes a texture from a string using a text definition. More...
 
local initWithString ( local text, local textDefinition)
 Initializes a texture from a string using a text definition. More...
 
void setTexParameters (const TexParams &texParams)
 Sets the min filter, mag filter, wrap s and wrap t texture parameters. More...
 
void setAntiAliasTexParameters ()
 Sets antialias texture parameters: More...
 
var setAntiAliasTexParameters ()
 Sets antialias texture parameters: More...
 
local setAntiAliasTexParameters ()
 Sets antialias texture parameters: More...
 
void setAliasTexParameters ()
 Sets alias texture parameters: More...
 
var setAliasTexParameters ()
 Sets alias texture parameters: More...
 
local setAliasTexParameters ()
 Sets alias texture parameters: More...
 
void generateMipmap ()
 Generates mipmap images for the texture. More...
 
var generateMipmap ()
 Generates mipmap images for the texture. More...
 
local generateMipmap ()
 Generates mipmap images for the texture. More...
 
const char * getStringForFormat () const
 Returns the pixel format by string.
 
var getStringForFormat ()
 Returns the pixel format by string.
 
local getStringForFormat ()
 Returns the pixel format by string.
 
unsigned int getBitsPerPixelForFormat () const
 Returns the bits-per-pixel of the in-memory OpenGL texture.
 
unsigned int getBitsPerPixelForFormat (Texture2D::PixelFormat format) const
 Helper functions that returns bits per pixels for a given format.
 
const SizegetContentSizeInPixels ()
 Get content size in pixels.
 
local getContentSizeInPixels ()
 Get content size in pixels.
 
bool hasPremultipliedAlpha () const
 Whether or not the texture has their Alpha premultiplied.
 
var hasPremultipliedAlpha ()
 Whether or not the texture has their Alpha premultiplied.
 
local hasPremultipliedAlpha ()
 Whether or not the texture has their Alpha premultiplied.
 
bool hasMipmaps () const
 Whether or not the texture has mip maps.
 
var hasMipmaps ()
 Whether or not the texture has mip maps.
 
local hasMipmaps ()
 Whether or not the texture has mip maps.
 
Texture2D::PixelFormat getPixelFormat () const
 Gets the pixel format of the texture.
 
int getPixelsWide () const
 Gets the width of the texture in pixels.
 
var getPixelsWide ()
 Gets the width of the texture in pixels.
 
local getPixelsWide ()
 Gets the width of the texture in pixels.
 
int getPixelsHigh () const
 Gets the height of the texture in pixels.
 
var getPixelsHigh ()
 Gets the height of the texture in pixels.
 
local getPixelsHigh ()
 Gets the height of the texture in pixels.
 
GLuint getName () const
 Gets the texture name.
 
var getName ()
 Gets the texture name.
 
local getName ()
 Gets the texture name.
 
GLfloat getMaxS () const
 Gets max S.
 
var getMaxS ()
 Gets max S.
 
local getMaxS ()
 Gets max S.
 
void setMaxS (GLfloat maxS)
 Sets max S.
 
var setMaxS ( var maxS)
 Sets max S.
 
local setMaxS ( local maxS)
 Sets max S.
 
GLfloat getMaxT () const
 Gets max T.
 
var getMaxT ()
 Gets max T.
 
local getMaxT ()
 Gets max T.
 
void setMaxT (GLfloat maxT)
 Sets max T.
 
var setMaxT ( var maxT)
 Sets max T.
 
local setMaxT ( local maxT)
 Sets max T.
 
Size getContentSize () const
 Get the texture content size.
 
var getContentSize ()
 Get the texture content size.
 
local getContentSize ()
 Get the texture content size.
 
void setGLProgram (GLProgram *program)
 Set a shader program to the texture. More...
 
var setGLProgram ( var program)
 Set a shader program to the texture. More...
 
local setGLProgram ( local program)
 Set a shader program to the texture. More...
 
GLProgramgetGLProgram () const
 Get a shader program from the texture for drawAtPoint & drawInRect.
 
std::string getPath () const
 Return texture file name.
 
local getPath ()
 Return texture file name.
 
- Public Member Functions inherited from Ref
void retain ()
 Retains the ownership. More...
 
local retain ()
 Retains the ownership. More...
 
void release ()
 Releases the ownership immediately. More...
 
Refautorelease ()
 Releases the ownership sometime soon automatically. More...
 
unsigned int getReferenceCount () const
 Returns the Ref's current reference count. More...
 
local getReferenceCount ()
 Returns the Ref's current reference count. More...
 
virtual ~Ref ()
 Destructor.
 

Static Public Member Functions

static void setDefaultAlphaPixelFormat (Texture2D::PixelFormat format)
 sets the default pixel format for Image contains alpha channel. More...
 
local setDefaultAlphaPixelFormat ( local format)
 sets the default pixel format for Image contains alpha channel. More...
 
static Texture2D::PixelFormat getDefaultAlphaPixelFormat ()
 Returns the default alpha pixel format.
 
static void PVRImagesHavePremultipliedAlpha (bool haveAlphaPremultiplied)
 Treats (or not) PVR files as if they have alpha premultiplied. More...
 
local PVRImagesHavePremultipliedAlpha ( local haveAlphaPremultiplied)
 Treats (or not) PVR files as if they have alpha premultiplied. More...
 
static const PixelFormatInfoMap & getPixelFormatInfoMap ()
 Get pixel info map, the key-value pairs is PixelFormat and PixelFormatInfo.
 
var getPixelFormatInfoMap ()
 Get pixel info map, the key-value pairs is PixelFormat and PixelFormatInfo.
 
local getPixelFormatInfoMap ()
 Get pixel info map, the key-value pairs is PixelFormat and PixelFormatInfo.
 

Additional Inherited Members

- Public Attributes inherited from Ref
unsigned int _ID
 object id, ScriptSupport need public _ID
 
local _ID
 object id, ScriptSupport need public _ID
 
int _luaID
 Lua reference id.
 
local _luaID
 Lua reference id.
 
void * _scriptObject
 scriptObject, support for swift
 
local _scriptObject
 scriptObject, support for swift
 
bool _rooted
 When true, it means that the object was already rooted.
 
local _rooted
 When true, it means that the object was already rooted.
 
bool _scriptOwned
 The life of the object is scrolled by the scripting engine. More...
 
local _scriptOwned
 The life of the object is scrolled by the scripting engine. More...
 

Detailed Description

Texture2D class.

This class allows to easily create OpenGL 2D textures from images, text or raw data. The created Texture2D object will always have power-of-two dimensions. Depending on how you create the Texture2D 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!

Member Function Documentation

static void
setDefaultAlphaPixelFormat
( Texture2D::PixelFormat  format)
static

sets the default pixel format for Image contains alpha channel.

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

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.

var setDefaultAlphaPixelFormat ( var  format)
static

sets the default pixel format for Image contains alpha channel.

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

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.

local setDefaultAlphaPixelFormat ( local  format)
static

sets the default pixel format for Image contains alpha channel.

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

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.

static void
PVRImagesHavePremultipliedAlpha
( bool  haveAlphaPremultiplied)
static

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.

deprecated, please use Image::setPVRImagesHavePremultipliedAlpha() instead.

var PVRImagesHavePremultipliedAlpha ( var  haveAlphaPremultiplied)
static

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.

deprecated, please use Image::setPVRImagesHavePremultipliedAlpha() instead.

local
PVRImagesHavePremultipliedAlpha
( local  haveAlphaPremultiplied)
static

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.

deprecated, please use Image::setPVRImagesHavePremultipliedAlpha() instead.

virtual std::string getDescription ( ) const
virtual

Get texutre name, dimensions and coordinates message by a string.

The string include texture name, dimensions and coordinates message.

bool initWithData ( const void *  data,
ssize_t  dataLen,
Texture2D::PixelFormat  pixelFormat,
int  pixelsWide,
int  pixelsHigh,
const Size contentSize 
)

Initializes a Texture2D object with data.

Specifies a pointer to the image data in memory. The image data length. The image pixelFormat. The image width. The image height. The image content size. If the initialization is success, return true; else return false.

bool initWithMipmaps ( MipmapInfo *  mipmaps,
int  mipmapsNum,
Texture2D::PixelFormat  pixelFormat,
int  pixelsWide,
int  pixelsHigh 
)

Initializes with mipmaps.

Specifies a pointer to the mipmaps image data in memory. The mipmaps number. The image pixelFormat. The image width. The image height. If the initialization is success, return true; else return false.

var initWithMipmaps ( var  mipmaps,
var  mipmapsNum,
var  pixelFormat,
var  pixelsWide,
var  pixelsHigh 
)

Initializes with mipmaps.

Specifies a pointer to the mipmaps image data in memory. The mipmaps number. The image pixelFormat. The image width. The image height. If the initialization is success, return true; else return false.

local initWithMipmaps ( local  mipmaps,
local  mipmapsNum,
local  pixelFormat,
local  pixelsWide,
local  pixelsHigh 
)

Initializes with mipmaps.

Specifies a pointer to the mipmaps image data in memory. The mipmaps number. The image pixelFormat. The image width. The image height. If the initialization is success, return true; else return false.

bool updateWithData ( const void *  data,
int  offsetX,
int  offsetY,
int  width,
int  height 
)

Update with texture data.

Specifies a pointer to the image data in memory. Specifies a texel offset in the x direction within the texture array. Specifies a texel offset in the y direction within the texture array. Specifies the width of the texture subimage. Specifies the height of the texture subimage. If the update is success, return true; else return false.

var updateWithData ( var  data,
var  offsetX,
var  offsetY,
var  width,
var  height 
)

Update with texture data.

Specifies a pointer to the image data in memory. Specifies a texel offset in the x direction within the texture array. Specifies a texel offset in the y direction within the texture array. Specifies the width of the texture subimage. Specifies the height of the texture subimage. If the update is success, return true; else return false.

local updateWithData ( local  data,
local  offsetX,
local  offsetY,
local  width,
local  height 
)

Update with texture data.

Specifies a pointer to the image data in memory. Specifies a texel offset in the x direction within the texture array. Specifies a texel offset in the y direction within the texture array. Specifies the width of the texture subimage. Specifies the height of the texture subimage. If the update is success, return true; else return false.

void drawAtPoint ( const Vec2 point)

Drawing extensions to make it easy to draw basic quads using a Texture2D 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.

var drawAtPoint ( var  point)

Drawing extensions to make it easy to draw basic quads using a Texture2D 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.

local drawAtPoint ( local  point)

Drawing extensions to make it easy to draw basic quads using a Texture2D 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.

bool initWithImage ( Image *  image)

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

Initializes a texture from a Image object.

We will use the format you specified with setDefaultAlphaPixelFormat to convert the image for texture. NOTE: It will not convert the pvr image file. An Image object. If the initialization is success, return true; else return false.

var initWithImage ( var  image)

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

Initializes a texture from a Image object.

We will use the format you specified with setDefaultAlphaPixelFormat to convert the image for texture. NOTE: It will not convert the pvr image file. An Image object. If the initialization is success, return true; else return false.

local initWithImage ( local  image)

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

Initializes a texture from a Image object.

We will use the format you specified with setDefaultAlphaPixelFormat to convert the image for texture. NOTE: It will not convert the pvr image file. An Image object. If the initialization is success, return true; else return false.

bool initWithImage ( Image *  image,
PixelFormat  format 
)

Initializes a texture from a UIImage object.

We will use the format you passed to the function to convert the image format to the texture format. If you pass PixelFormat::Automatic, we will auto detect the image render type and use that type for texture to render. An Image object. Texture pixel formats. If the initialization is success, return true; else return false.

var initWithImage ( var  image,
var  format 
)

Initializes a texture from a UIImage object.

We will use the format you passed to the function to convert the image format to the texture format. If you pass PixelFormat::Automatic, we will auto detect the image render type and use that type for texture to render. An Image object. Texture pixel formats. If the initialization is success, return true; else return false.

local initWithImage ( local  image,
local  format 
)

Initializes a texture from a UIImage object.

We will use the format you passed to the function to convert the image format to the texture format. If you pass PixelFormat::Automatic, we will auto detect the image render type and use that type for texture to render. An Image object. Texture pixel formats. If the initialization is success, return true; else return false.

bool initWithString ( const char *  text,
const std::string &  fontName,
float  fontSize,
const Size dimensions = Size(0, 0),
TextHAlignment  hAlignment = TextHAlignment::CENTER,
TextVAlignment  vAlignment = TextVAlignment::TOP 
)

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

A null terminated string. The font name. The font size. The font dimension. The font horizontal text alignment type. The font vertical text alignment type. If the initialization is success, return true; else return false.

var initWithString ( var  text,
var  fontName,
var  fontSize,
var  dimensions = Size(0, 0),
var  hAlignment = TextHAlignment::CENTER,
var  vAlignment = TextVAlignment::TOP 
)

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

A null terminated string. The font name. The font size. The font dimension. The font horizontal text alignment type. The font vertical text alignment type. If the initialization is success, return true; else return false.

local initWithString ( local  text,
local  fontName,
local  fontSize,
local  dimensions = Size(0, 0),
local  hAlignment = TextHAlignment::CENTER,
local  vAlignment = TextVAlignment::TOP 
)

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

A null terminated string. The font name. The font size. The font dimension. The font horizontal text alignment type. The font vertical text alignment type. If the initialization is success, return true; else return false.

bool initWithString ( const char *  text,
const FontDefinition textDefinition 
)

Initializes a texture from a string using a text definition.

A null terminated string. A FontDefinition object contains font attributes. If the initialization is success, return true; else return false.

var initWithString ( var  text,
var  textDefinition 
)

Initializes a texture from a string using a text definition.

A null terminated string. A FontDefinition object contains font attributes. If the initialization is success, return true; else return false.

local initWithString ( local  text,
local  textDefinition 
)

Initializes a texture from a string using a text definition.

A null terminated string. A FontDefinition object contains font attributes. If the initialization is success, return true; else return false.

void setTexParameters ( const TexParams texParams)

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}.

Calling this method could allocate additional texture memory.

When this function bound into js or lua,the input parameter will be changed
In js: var setTexParameters(var arg1, var arg2, var arg3, var arg4)
In lua: local setTexParameters(local arg1, local arg2, local arg3, local arg4)
var setTexParameters ( var  texParams)

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}.

Calling this method could allocate additional texture memory.

When this function bound into js or lua,the input parameter will be changed
In js: var setTexParameters(var arg1, var arg2, var arg3, var arg4)
In lua: local setTexParameters(local arg1, local arg2, local arg3, local arg4)
local setTexParameters ( local  texParams)

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}.

Calling this method could allocate additional texture memory.

When this function bound into js or lua,the input parameter will be changed
In js: var setTexParameters(var arg1, var arg2, var arg3, var arg4)
In lua: local setTexParameters(local arg1, local arg2, local arg3, local arg4)
void setAntiAliasTexParameters ( )

Sets antialias texture parameters:

  • GL_TEXTURE_MIN_FILTER = GL_LINEAR
  • GL_TEXTURE_MAG_FILTER = GL_LINEAR

    Calling this method could allocate additional texture memory.

var setAntiAliasTexParameters ( )

Sets antialias texture parameters:

  • GL_TEXTURE_MIN_FILTER = GL_LINEAR
  • GL_TEXTURE_MAG_FILTER = GL_LINEAR

    Calling this method could allocate additional texture memory.

local setAntiAliasTexParameters ( )

Sets antialias texture parameters:

  • GL_TEXTURE_MIN_FILTER = GL_LINEAR
  • GL_TEXTURE_MAG_FILTER = GL_LINEAR

    Calling this method could allocate additional texture memory.

void setAliasTexParameters ( )

Sets alias texture parameters:

  • GL_TEXTURE_MIN_FILTER = GL_NEAREST
  • GL_TEXTURE_MAG_FILTER = GL_NEAREST

    Calling this method could allocate additional texture memory.

var setAliasTexParameters ( )

Sets alias texture parameters:

  • GL_TEXTURE_MIN_FILTER = GL_NEAREST
  • GL_TEXTURE_MAG_FILTER = GL_NEAREST

    Calling this method could allocate additional texture memory.

local setAliasTexParameters ( )

Sets alias texture parameters:

  • GL_TEXTURE_MIN_FILTER = GL_NEAREST
  • GL_TEXTURE_MAG_FILTER = GL_NEAREST

    Calling this method could allocate additional texture memory.

void generateMipmap ( )

Generates mipmap images for the texture.

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

var generateMipmap ( )

Generates mipmap images for the texture.

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

local generateMipmap ( )

Generates mipmap images for the texture.

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

void setGLProgram ( GLProgram program)

Set a shader program to the texture.

It's used by drawAtPoint and drawInRect

var setGLProgram ( var  program)

Set a shader program to the texture.

It's used by drawAtPoint and drawInRect

local setGLProgram ( local  program)

Set a shader program to the texture.

It's used by drawAtPoint and drawInRect


The documentation for this class was generated from the following file: