5
\$\begingroup\$

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?

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Is it possible to load or create that one pixel texture without reading it from a file? \$\endgroup\$ – petervaz Feb 22 '13 at 17:09
  • \$\begingroup\$ Sure, see updated answer. \$\endgroup\$ – junkdog Feb 23 '13 at 12:26
  • \$\begingroup\$ geez, I did exatcly that, except the fill(), and was wondering why didn't work. thanks. \$\endgroup\$ – petervaz Feb 23 '13 at 20:39
  • \$\begingroup\$ Could you specify how to scale the texture? \$\endgroup\$ – Wolfrevo Kcats Aug 20 '13 at 18:55
1
\$\begingroup\$

A lot has changed, this needs an update: The ShapeRenderer got overhauled some time ago and got decently fast. The API reflects this as the disclaimer of it not being "meant to be used for performance sensitive applications" has since been removed.

The new javadoc mentions batching: ShapeRenderer (libgdx API)

Shapes are rendered in batches to increase performance.

I use the ShapeRenderer for ingame performance graphs, drawing thousands of small shapes, i can see no large impact on desktop. It will certainly trump drawing scaled tinted sprites unless you can replace tens/hundreds of shapes with a single pre-rendered sprite.

The space between two white horizontal lines represents one millisecond. performance graph

I've used it some time ago on mobile to draw some circles as placeholders for units. It had no problem drawing hundreds of circles afaik. That was around three years ago already.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.