# CCTexture Class Reference

Inherits from | NSObject
Declared in | CCTexture.h

## Overview

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!

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

## Tasks

### Initializing a CCTexture Object

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

### Creating a CCTexture Object

- `+ textureWithFile:`
- `+ none`

### Accessing The Texture Attributes

- `pixelFormat` property
- `pixelWidth` property
- `pixelHeight` property
- `contentSizeInPixels` property
- `premultipliedAlpha` property
- `antialiased` property
- `contentScale` property
- `– contentSize`
- `– createSpriteFrame`

### Image Methods

- `– initWithCGImage:contentScale:`

### PixelFormat Methods

- `+ setDefaultAlphaPixelFormat:`
- `+ defaultAlphaPixelFormat`
- `– bitsPerPixelForFormat`
- `– stringForFormat`
- `+ bitsPerPixelForFormat:`

## Properties

### antialiased

True if antialised.

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

#### 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. Loading behavior is changed by [CCFileUtils set*ContentScaleFactor:]. The value can be changed manually if you want to force a certain content scale.

`@property (nonatomic, readwrite) CGFloat contentScale`

#### Declared In

`CCTexture.h`

### contentSizeInPixels

Returns content size of the texture in pixels.

`@property (nonatomic, readonly, nonatomic) CGSize contentSizeInPixels`

#### Declared In

`CCTexture.h`

### pixelFormat

Pixel format of the texture.

`@property (nonatomic, readonly) CCTexturePixelFormat pixelFormat`

#### Declared In

`CCTexture.h`

### pixelHeight

Height in pixels.

`@property (nonatomic, readonly) NSUInteger pixelHeight`

#### Declared In

`CCTexture.h`

### pixelWidth

Width in pixels.

`@property (nonatomic, readonly) NSUInteger pixelWidth`

#### Declared In

`CCTexture.h`

### premultipliedAlpha

Whether or not the texture has their Alpha premultiplied.

`@property (nonatomic, readonly, getter=hasPremultipliedAlpha) BOOL premultipliedAlpha`

#### Declared In

`CCTexture.h`

## Class Methods

### 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`

### defaultAlphaPixelFormat

Returns the alpha pixel format.

`+ (CCTexturePixelFormat)defaultAlphaPixelFormat`

#### Return Value

The pixel format.

#### Declared In

`CCTexture.h`

### none

A placeholder value for a blank sizeless texture.

`+ (instancetype)none`

#### Declared In

`CCTexture.h`

### 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`

### textureWithFile:

Creates and returns a new texture, based on the specified file path value. If the texture has already been loaded, and resides in cache, the previously created texture is returned.

`+ (instancetype)textureWithFile:(NSString *)file`

#### Parameters

file

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

#### Return Value

The CCTexture object.

#### Declared In

`CCTexture.h`

## Instance Methods

### 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`

### contentSize

Returns the content size of the texture in points.

`- (CGSize)contentSize`

#### Declared In

`CCTexture.h`

### createSpriteFrame

Creates a sprite frame from the texture.

`- (CCSpriteFrame *)createSpriteFrame`

#### Return Value

A new sprite frame.

#### Declared In

`CCTexture.h`

### 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`

### 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`

### stringForFormat

Returns the pixel format in a NSString.

`- (NSString *)stringForFormat`

#### Declared In

`CCTexture.h`