Question

The libGDX documentation explains about binding textures to a SpriteBatch...

If [SpriteBatch] is given a texture different than the last texture, then it binds the last texture, submits the collected geometry to be drawn, and begins collecting geometry for the new texture.

Changing textures every few rectangles that are drawn prevents SpriteBatch from batching much geometry. Also, binding a texture is a somewhat expensive operation. For these reasons, it is common to store many smaller images in a larger image and then draw regions of the larger image to both maximize geometry batching and avoid texture changes.

I've arranged all of my game sprite images into a single texture for this reason. However, I now wish to place a large background image in my game to fill the entire screen.

My options seem to be either...

  1. put the background in the same texture I'm using for the smaller game sprites, or...

  2. use a separate texture for the background.

The problem with #1 is that this seems very impractical if I want to use different background images for different levels (either my image gets very very large, or I have to have a copy of my game sprites in multiple images for each different background)

But if I do #2, then do I'll have to bind the SpriteBatch to 2 different textures before each submission to the GPU, which which the documentation describes as "rather expensive".

I have no idea what the best approach is. Do I really need to worry about binding to multiple textures that much? Or is there a better approach I haven't considered?

Any advice sought, thanks!

Was it helpful?

Solution

In case of already large textures, which are not going to be used very often, you should try to avoid packing them in the same texture (atlas) like all the other small and often used textures.

Why is this? Because many devices will force you to use power of two (POT) sized textures only, and in case of big textures which you want to put together, this might result in a pretty big file with lots of wasted space.

Furthermore if you have 10 levels, each with a different background and you would put all backgrounds in a single texture, then you have no chance of sending just a single background to your GPU. In every level you would have to send the huge texture file with all backgrounds to the GPU, in order to display just one of them. Thus it would waste not only space, but also GPU memory, which is even worse.

For those reasons and because you usually draw your background just once, you can neglect the overhead of the single texture switch. It is not that expensive and if you do it just once per frame (in case of the backgrounds) it won't make a difference.

So in total: seperate the background textures from the rest and make them individual files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top