CCTexture Class Reference

Inherits from NSObject
Declared in CCTexture.h

Overview

Represents a texture, an in-memory representation of an image in a compatible format the graphics processor can process.

Allows to create OpenGL textures from image files, text (font rendering) and raw data.

Note: Be aware that the content of the generated texture will be upside-down! This is an OpenGL oddity.

Creating a Texture

– initWithData:pixelFormat:pixelsWide:pixelsHigh:contentSizeInPixels:contentScale:

Initializes and returns a texture object using the specified data, pixelFormat, width, height, sizeInPixels and contentScale values.

- (id)initWithData:(const void *)data pixelFormat:(CCTexturePixelFormat)pixelFormat pixelsWide:(NSUInteger)width pixelsHigh:(NSUInteger)height contentSizeInPixels:(CGSize)sizeInPixels contentScale:(CGFloat)contentScale

Parameters

data

Pointer to a buffer containing the raw data.

pixelFormat

Pixelformat of the data

width

Width if the texture

height

Height of the texture

sizeInPixels

Size of resulting texture.

contentScale

Content scale.

Return Value

An initialized CCTexture Object.

Declared In

CCTexture.h

+ textureWithFile:

Creates and returns a new texture, based on the specified image file path.

+ (instancetype)textureWithFile:(NSString *)file

Parameters

file

File path to load (should not include any suffixes).

Return Value

The CCTexture object.

Discussion

If the texture has already been loaded, and resides in the internal cache, the previously created texture is returned from the cache. While this is fast, it still has an overhead compared to manually caching textures in an ivar or property.

Declared In

CCTexture.h

+ none

A placeholder value for a blank sizeless texture.

+ (instancetype)none

Return Value

An empty texture.

Declared In

CCTexture.h

Creating a Sprite Frame

– createSpriteFrame

Creates a sprite frame from the texture.

- (CCSpriteFrame *)createSpriteFrame

Return Value

A new sprite frame.

See Also

Declared In

CCTexture.h

Texture Format and Size

  pixelFormat

Pixel format of the texture.

@property (nonatomic, readonly) CCTexturePixelFormat pixelFormat

Declared In

CCTexture.h

  pixelWidth

Width in pixels.

@property (nonatomic, readonly) NSUInteger pixelWidth

Declared In

CCTexture.h

  pixelHeight

Height in pixels.

@property (nonatomic, readonly) NSUInteger pixelHeight

Declared In

CCTexture.h

– contentSize

Returns the content size of the texture in points.

- (CGSize)contentSize

Declared In

CCTexture.h

  contentSizeInPixels

Returns content size of the texture in pixels.

@property (nonatomic, readonly, nonatomic) CGSize contentSizeInPixels

Declared In

CCTexture.h

  contentScale

Returns the contentScale of the texture. In general “HD” textures return a contentScale of 2.0, while non-HD textures return 1.0.

@property (nonatomic, readwrite) CGFloat contentScale

Discussion

Loading behavior is changed by [CCFileUtils set*ContentScaleFactor:]. The value can be changed manually if you want to force a certain content scale.

Declared In

CCTexture.h

Texture Settings

  premultipliedAlpha

Whether or not the texture has their Alpha premultiplied.

@property (nonatomic, readwrite) BOOL premultipliedAlpha

Declared In

CCTexture.h

  antialiased

True if antialised.

@property (nonatomic, assign, getter=isAntialiased) BOOL antialiased

Declared In

CCTexture.h

CGImage Methods

– initWithCGImage:contentScale:

Initializes and returns a texture from a CGImage object.

- (id)initWithCGImage:(CGImageRef)cgImage contentScale:(CGFloat)contentScale

Parameters

cgImage

CGImage to use for texture

contentScale

Content scale.

Return Value

An initialized CCTexture object.

Declared In

CCTexture.h

PixelFormat Methods

+ setDefaultAlphaPixelFormat:

Sets the default pixel format for CGImages that contains alpha channel.

+ (void)setDefaultAlphaPixelFormat:(CCTexturePixelFormat)format

Parameters

format

Format to use with texture.

Discussion

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.

Declared In

CCTexture.h

+ defaultAlphaPixelFormat

Returns the alpha pixel format.

+ (CCTexturePixelFormat)defaultAlphaPixelFormat

Return Value

The pixel format.

Declared In

CCTexture.h

– bitsPerPixelForFormat

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

- (NSUInteger)bitsPerPixelForFormat

Return Value

Number of bits per pixel.

Declared In

CCTexture.h

– stringForFormat

Returns the pixel format in a NSString.

- (NSString *)stringForFormat

Declared In

CCTexture.h

+ bitsPerPixelForFormat:

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

+ (NSUInteger)bitsPerPixelForFormat:(CCTexturePixelFormat)format

Parameters

format

Format to query for pixelsize.

Return Value

Number of bits for pixelformat.

Declared In

CCTexture.h