Axmol Engine 2.4.0-258ceca
Loading...
Searching...
No Matches
Texture2D Class Reference

Texture2D class. More...

#include <Texture2D.h>

Inherits Object.

Public Types

using TexParams = backend::SamplerDescriptor
 Extension to set the Min / Mag filter.
 

Public Member Functions

 Texture2D ()
 @js ctor
 
virtual ~Texture2D ()
 @js NA @lua NA
 
bool initWithData (const void *data, ssize_t dataLen, backend::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, bool preMultipliedAlpha=false)
 Initializes with a texture2d with data.
 
bool initWithData (const void *data, ssize_t dataLen, backend::PixelFormat pixelFormat, backend::PixelFormat renderFormat, int pixelsWide, int pixelsHigh, bool preMultipliedAlpha=false)
 Initializes with a texture2d with data.
 
bool initWithMipmaps (MipmapInfo *mipmaps, int mipmapsNum, backend::PixelFormat pixelFormat, backend::PixelFormat renderFormat, int pixelsWide, int pixelsHigh, bool preMultipliedAlpha=false)
 Initializes with mipmaps.
 
bool updateWithImage (Image *image, backend::PixelFormat format, int index=0)
 Update with image.
 
bool updateWithSubData (void *data, int offsetX, int offsetY, int width, int height, int index=0)
 Update with texture data.
 
void drawAtPoint (const Vec2 &point, float globalZOrder)
 Drawing extensions to make it easy to draw basic quads using a Texture2D object.
 
void drawInRect (const Rect &rect, float globalZOrder)
 Draws a texture inside a rect.
 
bool initWithImage (Image *image)
 Extensions to make it easy to create a Texture2D object from an image file.
 
bool initWithImage (Image *image, backend::PixelFormat format)
 Initializes a texture from a UIImage object.
 
bool initWithString (std::string_view text, std::string_view fontName, float fontSize, const Vec2 &dimensions=Vec2(0, 0), TextHAlignment hAlignment=TextHAlignment::CENTER, TextVAlignment vAlignment=TextVAlignment::TOP, bool enableWrap=true, int overflow=0)
 Initializes a texture from a string with dimensions, alignment, font name and font size.
 
bool initWithString (std::string_view text, const FontDefinition &textDefinition)
 Initializes a texture from a string using a text definition.
 
bool updateTextureDescriptor (const backend::TextureDescriptor &descriptor, bool preMultipliedAlpha=false)
 !Used for render buffer, such depth stencil attachment
 
void generateMipmap ()
 Generates mipmap images for the texture.
 
void setAntiAliasTexParameters ()
 Sets antialias texture parameters:
 
void setAliasTexParameters ()
 Sets alias texture parameters:
 
const char * getStringForFormat () const
 Returns the pixel format.
 
unsigned int getBitsPerPixelForFormat () const
 Returns the bits-per-pixel of the in-memory OpenGL texture.
 
unsigned int getBitsPerPixelForFormat (backend::PixelFormat format) const
 Helper functions that returns bits per pixels for a given format.
 
const Vec2getContentSizeInPixels ()
 Get content size.
 
bool hasPremultipliedAlpha () const
 Whether or not the texture has their Alpha premultiplied.
 
bool hasMipmaps () const
 Whether or not the texture has mip maps.
 
backend::PixelFormat getPixelFormat () const
 Gets the pixel format of the texture.
 
int getPixelsWide () const
 Gets the width of the texture in pixels.
 
int getPixelsHigh () const
 Gets the height of the texture in pixels.
 
float getMaxS () const
 Gets max S.
 
void setMaxS (float maxS)
 Sets max S.
 
float getMaxT () const
 Gets max T.
 
void setMaxT (float maxT)
 Sets max T.
 
Vec2 getContentSize () const
 Get the texture content size.
 
- Public Member Functions inherited from Object
void retain ()
 Retains the ownership.
 
void release ()
 Releases the ownership immediately.
 
Objectautorelease ()
 Releases the ownership sometime soon automatically.
 
unsigned int getReferenceCount () const
 Returns the Object's current reference count.
 
virtual ~Object ()
 Destructor.
 

Static Public Member Functions

static void setDefaultAlphaPixelFormat (backend::PixelFormat format)
 sets the default pixel format for UIImagescontains alpha channel.
 
static backend::PixelFormat getDefaultAlphaPixelFormat ()
 Returns the alpha pixel format.
 

Additional Inherited Members

- Public Attributes inherited from Object
unsigned int _ID
 object id, ScriptSupport need public _ID
 
int _luaID
 Lua reference id.
 

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

◆ setDefaultAlphaPixelFormat()

static void setDefaultAlphaPixelFormat ( backend::PixelFormat format)
static

sets the default pixel format for UIImagescontains alpha channel.

Parameters
formatIf the UIImage contains alpha channel, then the options are:
  • generate 32-bit textures: backend::PixelFormat::RGBA8 (default one)
  • generate 24-bit textures: backend::PixelFormat::RGB8
  • generate 16-bit textures: backend::PixelFormat::RGBA4
  • generate 16-bit textures: backend::PixelFormat::RGB5A1
  • generate 16-bit textures: backend::PixelFormat::RGB565
  • generate 8-bit textures: backend::PixelFormat::R8 (only use it if you use just 1 color)
  • generate 16-bit textures: backend::PixelFormat::RG8 (only use it if you use just 2 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

◆ getDefaultAlphaPixelFormat()

static backend::PixelFormat getDefaultAlphaPixelFormat ( )
static

Returns the alpha pixel format.

Since
v0.8

◆ initWithData() [1/2]

bool initWithData ( const void * data,
ssize_t dataLen,
backend::PixelFormat pixelFormat,
int pixelsWide,
int pixelsHigh,
bool preMultipliedAlpha = false )
inline

Initializes with a texture2d with data.

Parameters
dataSpecifies a pointer to the image data in memory.
dataLenThe image data length.
pixelFormatThe image pixelFormat.
pixelsWideThe image width.
pixelsHighThe image height.
contentSizeThe image content size.
preMultipliedAlphaThe texture has premultiplied alpha @js NA @lua NA

◆ initWithData() [2/2]

bool initWithData ( const void * data,
ssize_t dataLen,
backend::PixelFormat pixelFormat,
backend::PixelFormat renderFormat,
int pixelsWide,
int pixelsHigh,
bool preMultipliedAlpha = false )

Initializes with a texture2d with data.

Parameters
dataSpecifies a pointer to the image data in memory.
dataLenThe image data length.
pixelFormatThe image pixelFormat.
renderFormatThe format converted to.
pixelsWideThe image width.
pixelsHighThe image height.
contentSizeThe image content size.
preMultipliedAlphaThe texture has premultiplied alpha @js NA @lua NA

◆ initWithMipmaps()

bool initWithMipmaps ( MipmapInfo * mipmaps,
int mipmapsNum,
backend::PixelFormat pixelFormat,
backend::PixelFormat renderFormat,
int pixelsWide,
int pixelsHigh,
bool preMultipliedAlpha = false )

Initializes with mipmaps.

Parameters
mipmapsSpecifies a pointer to the image data in memory.
mipmapsNumThe mipmaps number.
pixelFormatThe image pixelFormat.
pixelsWideThe image width.
pixelsHighThe image height.
preMultipliedAlphaThe texture has premultiplied alpha

◆ updateWithImage()

bool updateWithImage ( Image * image,
backend::PixelFormat format,
int index = 0 )

Update with image.

Parameters
dataSpecifies a pointer to the image data in memory.
offsetXSpecifies a texel offset in the x direction within the texture array.
offsetYSpecifies a texel offset in the y direction within the texture array.
widthSpecifies the width of the texture subimage.
heightSpecifies the height of the texture subimage.

◆ updateWithSubData()

bool updateWithSubData ( void * data,
int offsetX,
int offsetY,
int width,
int height,
int index = 0 )

Update with texture data.

Parameters
dataSpecifies a pointer to the image data in memory.
offsetXSpecifies a texel offset in the x direction within the texture array.
offsetYSpecifies a texel offset in the y direction within the texture array.
widthSpecifies the width of the texture subimage.
heightSpecifies the height of the texture subimage.

◆ drawAtPoint()

void drawAtPoint ( const Vec2 & point,
float globalZOrder )

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.

◆ initWithImage() [1/2]

bool initWithImage ( Image * image)

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

Initializes a texture from a UIImage 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.

Parameters
imageAn UIImage object.

◆ initWithImage() [2/2]

bool initWithImage ( Image * image,
backend::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::NONE, we will auto detect the image render type and use that type for texture to render.

Parameters
imageAn UIImage object.
formatTexture pixel formats.

◆ initWithString() [1/2]

bool initWithString ( std::string_view text,
std::string_view fontName,
float fontSize,
const Vec2 & dimensions = Vec2(0, 0),
TextHAlignment hAlignment = TextHAlignment::CENTER,
TextVAlignment vAlignment = TextVAlignment::TOP,
bool enableWrap = true,
int overflow = 0 )

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

Parameters
textA null terminated string.
fontNameThe font name.
fontSizeThe font size.
dimensionsThe font dimension.
hAlignmentThe font horizontal text alignment type.
vAlignmentThe font vertical text alignment type.
enableWrapWhether enable text wrap or not.
overflowWhether shrink font size when content larger than the dimensions.

◆ initWithString() [2/2]

bool initWithString ( std::string_view text,
const FontDefinition & textDefinition )

Initializes a texture from a string using a text definition.

Parameters
textA null terminated string.
textDefinitionA FontDefinition object contains font attributes.

◆ generateMipmap()

void generateMipmap ( )

Generates mipmap images for the texture.

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

Since
v0.99.0

◆ setAntiAliasTexParameters()

void setAntiAliasTexParameters ( )

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

◆ setAliasTexParameters()

void setAliasTexParameters ( )

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

◆ getStringForFormat()

const char * getStringForFormat ( ) const

Returns the pixel format.

Since
v2.0

◆ getBitsPerPixelForFormat() [1/2]

unsigned int getBitsPerPixelForFormat ( ) const

Returns the bits-per-pixel of the in-memory OpenGL texture.

Since
v1.0

◆ getBitsPerPixelForFormat() [2/2]

unsigned int getBitsPerPixelForFormat ( backend::PixelFormat format) const

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

Since
v2.0

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