earlygrey / shapedrawer

A library for libGDX that draws simple shapes using a batch.
MIT License
185 stars 32 forks source link

BatchManager: Unset textureRegion? #33

Closed TCreutzenberg closed 3 years ago

TCreutzenberg commented 3 years ago

Hey,

I did not find a possibility to unset the texture region. Is there such an option? In my game I only have one Batch used across multiple screens, so I'd also like to have only one ShapeDrawer. Not every screen uses the shapedrawer and I load/unload the texture atlases for every screen on change.

I.e. I need to unset the texture region of the BatchManager somehow for the screens that do not use it, so it does not hold a reference to an old "unloaded" texture altas. If I use BatchManager.setTextureRegion(null), there is a Nullpointer exception in this code obviously:

public TextureRegion setTextureRegion(TextureRegion region) { TextureRegion oldRegion = this.r; this.r = region; float u = 0.5f (r.getU() + r.getU2()); float v = 0.5f (r.getV() + r.getV2()); for (int i = 0; i < verts.length; i+=VERTEX_SIZE) { verts[i + SpriteBatch.U1] = u; verts[i + SpriteBatch.V1] = v; } return oldRegion; }

Do you have a solution to my problem or should I create a PR? (Do you want a PR for this at all?)

Thank you very much in advance!

TCreutzenberg commented 3 years ago

Also, it is not possible to instantiate a ShapeDrawer without textureRegion or textureRegion = null. This is related to the same problem.

earlygrey commented 3 years ago

OK that's a fair enough use case, you might also want to use different Texture Regions from different textures to optimise flushes.

You can make a PR if you like, otherwise I'll do it this week. It should also throw an exception (IllegalStateException?) when trying to use it without a Texture Region. I guess the setter would then just become

    public TextureRegion setTextureRegion(TextureRegion region) {
        TextureRegion oldRegion = this.r;
        this.r = region;
        if (r != null) {
            float u = 0.5f * (r.getU() + r.getU2());
            float v = 0.5f * (r.getV() + r.getV2());
            for (int i = 0; i < verts.length; i += VERTEX_SIZE) {
                verts[i + SpriteBatch.U1] = u;
                verts[i + SpriteBatch.V1] = v;
            }
        }
        return oldRegion;
    }

and you could add a getter and a constructor which takes only a Batch.

TCreutzenberg commented 3 years ago

@earlygrey Hey, I'll create a PR soon.

TCreutzenberg commented 3 years ago

Closed with https://github.com/earlygrey/shapedrawer/pull/34