libgdx SpriteBatch render to texture
Solution 1:
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();
}
}
Solution 2:
For the moment, I would recommend you to use Pixmap
s. 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.