Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Rendering Graphics with LibGDX

Tech 1

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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.