37
votes

Is it possible to render to texture using SpriteBatch in libGdx (Java engine for Android/Desktop)? If so, how do it?

Basically I want to render everything to 320 x 240 region of 512 x 256 texture and than scale region to fit screen (in landscape mode). This way I want to eliminate artifacts which happen when I scale alpha blended textures independently. If there is any other way to remove such artifacts, please point them out :)

And is there any online documentation for libGdx?

2
I am interested in such a feature. After casting a glance at the 'SpriteBatch' code, though, I don't reckon that it would be trivial to implement such a feature -and that it would involve recompiling the library. I added some functions to 'Pixmap' (on the native side) in the past, but this seems more encumbering.huff

2 Answers

67
votes

This snippet was given to me on the LibGDX forum and it works flawlessly.

private float m_fboScaler = 1.5f;
private boolean m_fboEnabled = true;
private FrameBuffer m_fbo = null;
private TextureRegion m_fboRegion = null;

public void render(SpriteBatch spriteBatch)
{
    int width = Gdx.graphics.getWidth();
    int height = Gdx.graphics.getHeight();

    if(m_fboEnabled)      // enable or disable the supersampling
    {                  
        if(m_fbo == null)
        {
            // m_fboScaler increase or decrease the antialiasing quality

            m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false);
            m_fboRegion = new TextureRegion(m_fbo.getColorBufferTexture());
            m_fboRegion.flip(false, true);
        }

        m_fbo.begin();
    }

    // this is the main render function
    my_render_impl();

    if(m_fbo != null)
    {
        m_fbo.end();

        spriteBatch.begin();         
        spriteBatch.draw(m_fboRegion, 0, 0, width, height);               
        spriteBatch.end();
    }   
}
3
votes

For the moment, I would recommend you to use Pixmaps. You see, there are not many functions written for them, but apparently you can write a Pixmap (with alpha channel) to another one (pm1.drawPixmap(pm2, srcx, srcy, w, h, ...)), providing that you don't want to scale it (you may scale the composition later, but the proportion the pictures used in the composition won't be resized... unless you write a resizing function).

If you want to do more complex stuff (like writing a string using a BitmapFont into a Texture for later manipulation, or writing it into a Pixmap and then uploading it to the video memory), then... then tell me if you succeed (as I want to go into that direction to optimize). I think that the best would be to add the needed functions to libgdx...

Anyway, don't take anything I wrote here as an undeniable truth- if I get further, I will update.

The documentation I found is somewhat limited -I'm sure you've gone through it already. There's information in the badlogic's blog and in the googlecode project page -and also there's a relatively good book written by Mario, "Beginning Android Games". Also, there are a few (very basic) videos. And don't forget that you can consult source and the beautiful examples...

Also, you can always ask in the forum: http://badlogicgames.com/forum/

UPDATE: Your answer using the FrameBuffer and TextureRegion is undeniably much better. I leave this one just because it mentions the documentation.