APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.graphics.g2d

public Class SpriteCache

extends Object
implements Disposable
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.utils.Disposable
Static Imports
com.badlogic.gdx.graphics.g2d.Sprite.SPRITE_SIZE, .Sprite.VERTEX_SIZE

Draws 2D images, optimized for geometry that does not change. Sprites and/or textures are cached and given an ID, which can later be used for drawing. The size, color, and texture region for each cached image cannot be modified. This information is stored in video memory and does not have to be sent to the GPU each time it is drawn.

To cache sprites or textures, first call SpriteCache#beginCache(), then call the appropriate add method to define the images. To complete the cache, call SpriteCache#endCache() and store the returned cache ID.

To draw with SpriteCache, first call begin(), then call draw(int) with a cache ID. When SpriteCache drawing is complete, call end().

By default, SpriteCache draws using screen coordinates and uses an x-axis pointing to the right, an y-axis pointing upwards and the origin is the bottom left corner of the screen. The default transformation and projection matrices can be changed. If the screen is resized, the SpriteCache's matrices must be updated. For example:
cache.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

Note that SpriteCache does not manage blending. You will need to enable blending (Gdx.gl.glEnable(GL10.GL_BLEND);) and set the blend func as needed before or between calls to draw(int).

SpriteCache is managed. If the OpenGL context is lost and the restored, all OpenGL resources a SpriteCache uses internally are restored.

SpriteCache is a reasonably heavyweight object. Typically only one instance should be used for an entire application.

SpriteCache works with OpenGL ES 1.x and 2.0. For 2.0, it uses its own custom shader to draw.

SpriteCache must be disposed once it is no longer needed.
Author
Nathan Sweet

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private Array<SpriteCache.Cache>
private final Color
private float
private final Matrix4
private final IntArray
private SpriteCache.Cache
private ShaderProgram
private boolean
private final Mesh
private final Matrix4
public int
renderCalls

Number of render calls since the last begin().

private final ShaderProgram
private static final float[]
private final Array<Texture>
public int
totalRenderCalls

Number of rendering calls, ever.

private final Matrix4

Constructor Summary

AccessConstructor and Description
public
SpriteCache()

Creates a cache that uses indexed geometry and can contain up to 1000 images.

public
SpriteCache(int
The maximum number of images this cache can hold. The memory required to hold the images is allocated up front. Max of 8191 if indices are used.
size
,
boolean
If true, indexed geometry will be used.
useIndices
)

Creates a cache with the specified size, using a default shader if OpenGL ES 2.0 is being used.

public
SpriteCache(int
The maximum number of images this cache can hold. The memory required to hold the images is allocated up front. Max of 8191 if indices are used.
size
,
ShaderProgram shader, boolean
If true, indexed geometry will be used.
useIndices
)

Creates a cache with the specified size and OpenGL ES 2.0 shader.

Method Summary

Modifier and TypeMethod and Description
public void
add(Texture texture, float[] vertices, int offset, int length)

Adds the specified vertices to the cache.

public void
add(Texture texture, float x, float y)

Adds the specified texture to the cache.

public void
add(Texture texture, float x, float y, int srcWidth, int srcHeight, float u, float v, float u2, float v2, float color)

Adds the specified texture to the cache.

public void
add(Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight)

Adds the specified texture to the cache.

public void
add(Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)

Adds the specified texture to the cache.

public void
add(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)

Adds the specified texture to the cache.

public void
add(TextureRegion region, float x, float y)

Adds the specified region to the cache.

public void
add(TextureRegion region, float x, float y, float width, float height)

Adds the specified region to the cache.

public void
add(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)

Adds the specified region to the cache.

public void
add(Sprite sprite)

Adds the specified sprite to the cache.

public void
begin()

Prepares the OpenGL state for SpriteCache rendering.

public void
beginCache()

Starts the definition of a new cache, allowing the add and endCache() methods to be called.

public void
beginCache(int cacheID)

Starts the redefinition of an existing cache, allowing the add and endCache() methods to be called.

public void
clear()

Invalidates all cache IDs and resets the SpriteCache so new caches can be added.

pack-priv static ShaderProgram
public void
dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources held by this SpriteCache.

public void
draw(int cacheID)

Draws all the images defined for the specified cache ID.

public void
draw(int cacheID, int
The first image to render.
offset
,
int
The number of images from the first image (inclusive) to render.
length
)

Draws a subset of images defined for the specified cache ID.

public void
end()

Completes rendering for this SpriteCache.

public int
endCache()

Ends the definition of a cache, returning the cache ID to be used with draw(int).

public Color
public ShaderProgram
getCustomShader()

Returns the custom shader, or null if the default shader is being used.

public float
public Matrix4
public Matrix4
public boolean
public void
setColor(Color tint)

Sets the color used to tint images when they are added to the SpriteCache.

public void
setColor(float r, float g, float b, float a)

public void
setPackedColor(float packedColor)

Sets the color of this sprite cache, expanding the alpha from 0-254 to 0-255.

public void
public void
setShader(ShaderProgram
the ShaderProgram or null to use the default shader.
shader
)

Sets the shader to be used in a GLES 2.0 environment.

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cachesback to summary
private Array<SpriteCache.Cache> caches
colorback to summary
private final Color color
colorPackedback to summary
private float colorPacked
combinedMatrixback to summary
private final Matrix4 combinedMatrix
countsback to summary
private final IntArray counts
currentCacheback to summary
private SpriteCache.Cache currentCache
customShaderback to summary
private ShaderProgram customShader
drawingback to summary
private boolean drawing
meshback to summary
private final Mesh mesh
projectionMatrixback to summary
private final Matrix4 projectionMatrix
renderCallsback to summary
public int renderCalls

Number of render calls since the last begin().

shaderback to summary
private final ShaderProgram shader
tempVerticesback to summary
private static final float[] tempVertices
texturesback to summary
private final Array<Texture> textures
totalRenderCallsback to summary
public int totalRenderCalls

Number of rendering calls, ever. Will not be reset unless set manually.

transformMatrixback to summary
private final Matrix4 transformMatrix

Constructor Detail

SpriteCacheback to summary
public SpriteCache()

Creates a cache that uses indexed geometry and can contain up to 1000 images.

SpriteCacheback to summary
public SpriteCache(int size, boolean useIndices)

Creates a cache with the specified size, using a default shader if OpenGL ES 2.0 is being used.

Parameters
size:int

The maximum number of images this cache can hold. The memory required to hold the images is allocated up front. Max of 8191 if indices are used.

useIndices:boolean

If true, indexed geometry will be used.

SpriteCacheback to summary
public SpriteCache(int size, ShaderProgram shader, boolean useIndices)

Creates a cache with the specified size and OpenGL ES 2.0 shader.

Parameters
size:int

The maximum number of images this cache can hold. The memory required to hold the images is allocated up front. Max of 8191 if indices are used.

useIndices:boolean

If true, indexed geometry will be used.

Method Detail

addback to summary
public void add(Texture texture, float[] vertices, int offset, int length)

Adds the specified vertices to the cache. Each vertex should have 5 elements, one for each of the attributes: x, y, color, u, and v. If indexed geometry is used, each image should be specified as 4 vertices, otherwise each image should be specified as 6 vertices.

addback to summary
public void add(Texture texture, float x, float y)

Adds the specified texture to the cache.

addback to summary
public void add(Texture texture, float x, float y, int srcWidth, int srcHeight, float u, float v, float u2, float v2, float color)

Adds the specified texture to the cache.

addback to summary
public void add(Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight)

Adds the specified texture to the cache.

addback to summary
public void add(Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)

Adds the specified texture to the cache.

addback to summary
public void add(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)

Adds the specified texture to the cache.

addback to summary
public void add(TextureRegion region, float x, float y)

Adds the specified region to the cache.

addback to summary
public void add(TextureRegion region, float x, float y, float width, float height)

Adds the specified region to the cache.

addback to summary
public void add(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)

Adds the specified region to the cache.

addback to summary
public void add(Sprite sprite)

Adds the specified sprite to the cache.

beginback to summary
public void begin()

Prepares the OpenGL state for SpriteCache rendering.

beginCacheback to summary
public void beginCache()

Starts the definition of a new cache, allowing the add and endCache() methods to be called.

beginCacheback to summary
public void beginCache(int cacheID)

Starts the redefinition of an existing cache, allowing the add and endCache() methods to be called. If this is not the last cache created, it cannot have more entries added to it than when it was first created. To do that, use clear() and then begin().

clearback to summary
public void clear()

Invalidates all cache IDs and resets the SpriteCache so new caches can be added.

createDefaultShaderback to summary
pack-priv static ShaderProgram createDefaultShader()
disposeback to summary
public void dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources held by this SpriteCache.

drawback to summary
public void draw(int cacheID)

Draws all the images defined for the specified cache ID.

drawback to summary
public void draw(int cacheID, int offset, int length)

Draws a subset of images defined for the specified cache ID.

Parameters
offset:int

The first image to render.

length:int

The number of images from the first image (inclusive) to render.

endback to summary
public void end()

Completes rendering for this SpriteCache.

endCacheback to summary
public int endCache()

Ends the definition of a cache, returning the cache ID to be used with draw(int).

getColorback to summary
public Color getColor()
getCustomShaderback to summary
public ShaderProgram getCustomShader()

Returns the custom shader, or null if the default shader is being used.

getPackedColorback to summary
public float getPackedColor()
getProjectionMatrixback to summary
public Matrix4 getProjectionMatrix()
getTransformMatrixback to summary
public Matrix4 getTransformMatrix()
isDrawingback to summary
public boolean isDrawing()
setColorback to summary
public void setColor(Color tint)

Sets the color used to tint images when they are added to the SpriteCache. Default is Color#WHITE.

setColorback to summary
public void setColor(float r, float g, float b, float a)
See Also
setColor(Color)
setPackedColorback to summary
public void setPackedColor(float packedColor)

Sets the color of this sprite cache, expanding the alpha from 0-254 to 0-255.

See Also
Color#toFloatBits()
setProjectionMatrixback to summary
public void setProjectionMatrix(Matrix4 projection)
setShaderback to summary
public void setShader(ShaderProgram shader)

Sets the shader to be used in a GLES 2.0 environment. Vertex position attribute is called "a_position", the texture coordinates attribute is called called "a_texCoords", the color attribute is called "a_color". The projection matrix is uploaded via a mat4 uniform called "u_proj", the transform matrix is uploaded via a uniform called "u_trans", the combined transform and projection matrx is is uploaded via a mat4 uniform called "u_projTrans". The texture sampler is passed via a uniform called "u_texture". Call this method with a null argument to use the default shader.

Parameters
shader:ShaderProgram

the ShaderProgram or null to use the default shader.

setTransformMatrixback to summary
public void setTransformMatrix(Matrix4 transform)
com.badlogic.gdx.graphics.g2d back to summary

private Class SpriteCache.Cache

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv int[]
pack-priv final int
pack-priv int
pack-priv final int
pack-priv int
pack-priv Texture[]

Constructor Summary

AccessConstructor and Description
public
Cache(int id, int offset)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

countsback to summary
pack-priv int[] counts
idback to summary
pack-priv final int id
maxCountback to summary
pack-priv int maxCount
offsetback to summary
pack-priv final int offset
textureCountback to summary
pack-priv int textureCount
texturesback to summary
pack-priv Texture[] textures

Constructor Detail

Cacheback to summary
public Cache(int id, int offset)