Wednesday, November 25, 2015

libgdx - ShapeRenderer efficiency on games


I want to draw a rectangle box to use as a text background for a visual novel library I'm writing, I wanted some flexibility of color and transparency and the ShapeRenderer class seems to fit the need but the doc brings this disclaimer:



This class is not meant to be used for performance sensitive applications but more oriented towards debugging.


If I use it as background the most action on screen would probably be the text scrolling over it. Can someone more experient on the engine tell me if it is viable to use it under those premises, or it is really for debug only?



Answer



It is probably fine for a desktop-only game if you don't draw too many shapes, but it can produce FPS stutter on some mobile GPUs and, iirc, doesn't cache any of the shapes between frames.


A better approach might be to allocate a Sprite with a small white texture that you upscale to the appropriate size. Set tint and alpha with Sprite#setColor


update:


It is possible to manually create a texture by using a pixmap. It is probably easier grabbing the background texture from a texture atlas, if you're using it - allowing libgdx to automatically manage the texture for you. Otherwise:


public static Texture createBgTexture()
{
Pixmap pixmap = new Pixmap(1, 1, Format.RGBA4444); // or RGBA8888

pixmap.setColor(Color.WHITE);
pixmap.fill();
Texture texture = new Texture(pixmap); // must be manually disposed
pixmap.dispose();

return texture;
}

You can scale the texture by using Image class, and then add it to your Stage, or you can get its Drawable using Image#getDrawable to draw in the SpriteBatch.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...