Rendering Graphics with LibGDX
A texture is a decoded image uploaded to the GPU for rendering. Drawing a texture involves binding it and passing geometric data, typically vertex descriptions of shapes like rectangles, to OpenGL. The final rendered size and position depend on this geometry and the OpenGL viewport configuration. For games, the viewport usually matches the screen resolution, making pixel coordinates convenient for positioning.
Drawing individual rectangles for each texture instance is inefficient. The SpriteBatch class optimizes this by batching multiple draw calls that use the same texture. It collects geometry data and submits it to the GPU in groups, switching textures only when necessary.
To draw an image, place a texture file (with dimensions that are powers of two, e.g., 512x512) in the project's assets directory. LibGDX's Gdx.files module provides file handling. Use Gdx.files.internal() to access files relative to the application root or the assets folder.
public class GraphicsDemo implements ApplicationListener {
private SpriteBatch spriteBatch;
private Texture imgTexture;
@Override
public void create() {
spriteBatch = new SpriteBatch();
imgTexture = new Texture(Gdx.files.internal("sample.png"));
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
spriteBatch.draw(imgTexture, 50, 30);
spriteBatch.end();
}
// Other required methods: dispose(), pause(), resume(), resize()
}
To display a specific portion of a texture, use TextureRegion. It defines a sub-rectangle within the source texture.
public class RegionDemo implements ApplicationListener {
private SpriteBatch drawer;
private Texture sourceTex;
private TextureRegion subRegion;
@Override
public void create() {
drawer = new SpriteBatch();
sourceTex = new Texture(Gdx.files.internal("atlas.png"));
subRegion = new TextureRegion(sourceTex, 40, 100, 180, 180);
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
drawer.begin();
drawer.draw(subRegion, 5, 5);
drawer.end();
}
}
The Sprite class extends this functionality by encapsulating a texture region along with properties like position, rotation, and color tint.
public class SpriteDemo implements ApplicationListener {
private SpriteBatch renderer;
private Sprite gameSprite;
@Override
public void create() {
renderer = new SpriteBatch();
Texture tex = new Texture(Gdx.files.internal("character.png"));
gameSprite = new Sprite(tex, 90, 90, 380, 280);
gameSprite.setPosition(15, 15);
gameSprite.setRotation(12.5f);
gameSprite.setColor(0.8f, 1.0f, 0.8f, 1.0f);
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
renderer.begin();
gameSprite.draw(renderer);
renderer.end();
}
}
By default, SpriteBatch enables alpha blending for transparency. Disable blending when drawing opaque backgrounds to improve performance.
spriteBatch.disableBlending();
backgroundSprite.draw(spriteBatch);
// Draw other sprites...
spriteBatch.enableBlending();
The SpriteBatch constructor allows setting the maximum batch size. Monitor the maxSpritesInBatch and renderCalls fields to tune performance, balancing memory use and GPU calls.