|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.badlogic.gdx.graphics.g2d.SpriteCache
public class SpriteCache
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 beginCache()
, then call the
appropriate add method to define the images. To complete the cache, call 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.
Field Summary | |
---|---|
int |
renderCalls
Number of render calls since the last begin() . |
int |
totalRenderCalls
Number of rendering calls, ever. |
Constructor Summary | |
---|---|
SpriteCache()
Creates a cache that uses indexed geometry and can contain up to 1000 images. |
|
SpriteCache(int size,
boolean useIndices)
Creates a cache with the specified size, using a default shader if OpenGL ES 2.0 is being used. |
|
SpriteCache(int size,
ShaderProgram shader,
boolean useIndices)
Creates a cache with the specified size and OpenGL ES 2.0 shader. |
Method Summary | |
---|---|
void |
add(Sprite sprite)
Adds the specified sprite to the cache. |
void |
add(Texture texture,
float[] vertices,
int offset,
int length)
Adds the specified vertices to the cache. |
void |
add(Texture texture,
float x,
float y)
Adds the specified texture to the cache. |
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. |
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. |
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. |
void |
add(Texture texture,
float x,
float y,
int srcX,
int srcY,
int srcWidth,
int srcHeight)
Adds the specified texture to the cache. |
void |
add(TextureRegion region,
float x,
float y)
Adds the specified region to the cache. |
void |
add(TextureRegion region,
float x,
float y,
float width,
float height)
Adds the specified region to the cache. |
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. |
void |
begin()
Prepares the OpenGL state for SpriteCache rendering. |
void |
beginCache()
Starts the definition of a new cache, allowing the add and endCache() methods to be called. |
void |
beginCache(int cacheID)
Starts the redefinition of an existing cache, allowing the add and endCache() methods to be called. |
void |
clear()
Invalidates all cache IDs and resets the SpriteCache so new caches can be added. |
void |
dispose()
Releases all resources held by this SpriteCache. |
void |
draw(int cacheID)
Draws all the images defined for the specified cache ID. |
void |
draw(int cacheID,
int offset,
int length)
Draws a subset of images defined for the specified cache ID. |
void |
end()
Completes rendering for this SpriteCache.f |
int |
endCache()
Ends the definition of a cache, returning the cache ID to be used with draw(int) . |
Color |
getColor()
|
Matrix4 |
getProjectionMatrix()
|
Matrix4 |
getTransformMatrix()
|
void |
setColor(Color tint)
Sets the color used to tint images when they are added to the SpriteCache. |
void |
setColor(float color)
|
void |
setColor(float r,
float g,
float b,
float a)
|
void |
setProjectionMatrix(Matrix4 projection)
|
void |
setShader(ShaderProgram shader)
Sets the shader to be used in a GLES 2.0 environment. |
void |
setTransformMatrix(Matrix4 transform)
|
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public int renderCalls
begin()
.
public int totalRenderCalls
Constructor Detail |
---|
public SpriteCache()
public SpriteCache(int size, boolean useIndices)
size
- The maximum number of images this cache can hold. The memory required to hold the images is allocated up front.
Max of 5460 if indices are used.useIndices
- If true, indexed geometry will be used.public SpriteCache(int size, ShaderProgram shader, boolean useIndices)
size
- The maximum number of images this cache can hold. The memory required to hold the images is allocated up front.
Max of 5460 if indices are used.useIndices
- If true, indexed geometry will be used.Method Detail |
---|
public void setColor(Color tint)
Color.WHITE
.
public void setColor(float r, float g, float b, float a)
setColor(Color)
public void setColor(float color)
setColor(Color)
,
Color.toFloatBits()
public Color getColor()
public void beginCache()
endCache()
methods to be called.
public void beginCache(int cacheID)
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()
.
public int endCache()
draw(int)
.
public void clear()
public void add(Texture texture, float[] vertices, int offset, int length)
public void add(Texture texture, float x, float y)
public void add(Texture texture, float x, float y, int srcWidth, int srcHeight, float u, float v, float u2, float v2, float color)
public void add(Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight)
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)
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)
public void add(TextureRegion region, float x, float y)
public void add(TextureRegion region, float x, float y, float width, float height)
public void add(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
public void add(Sprite sprite)
public void begin()
public void end()
public void draw(int cacheID)
public void draw(int cacheID, int offset, int length)
offset
- The first image to render.length
- The number of images from the first image (inclusive) to render.public void dispose()
dispose
in interface Disposable
public Matrix4 getProjectionMatrix()
public void setProjectionMatrix(Matrix4 projection)
public Matrix4 getTransformMatrix()
public void setTransformMatrix(Matrix4 transform)
public void setShader(ShaderProgram shader)
shader
- the ShaderProgram
or null to use the default shader.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |