0
votes

I am trying to add my own custom draw() function to the SpriteBatch class in libGDX, but the SpriteBatch class is compiled in a jar file.

I had attempted to make my own class that extends the SpriteBatch class, but I need to access the private variables that can only be accessed in the class itself...

I would assume that I need to download the libGDX source, modify what I need to, and recompile, but i'm having a bit of trouble following this tutorial and this question, in addition to installing Ant on my mac following this method (Mac early-2011).

How should I go about modifying and applying new source code?

2
Wanting to extend/override SpriteBatch is a good indication that you might be trying to solve the wrong problem. What is the actual issue you're trying to solve?Xoppa
@Xoppa I need to add a draw function that can allow to input four different colors for each vertex. Is this possible with the SpriteBatch class? Or will I have to use a different way of accomplishing this entirely? I think you can accomplish this with shaders, but I have followed several tutorials on shaders and can't seem wrap my head around doing it. Edit: I forgot to mention this link, in which he 'adds a method to SpriteBatch'.satvikb
Sure, you dont have to extend spritebatch for that. It has methods that allow you to specify the vertices. And even more convenient, the sprite class provides access to the vertices so you can change whatever you like.Xoppa

2 Answers

0
votes

Everything contained in Stage.draw () can be retrieved with public getters that are included in the Stage class.

From Stage:

public void draw () {
    Camera camera = viewport.getCamera();
    camera.update();

    if (!root.isVisible()) return;

    Batch batch = this.batch;
    if (batch != null) {
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        root.draw(batch, 1);
        batch.end();
    }

    if (debug) drawDebug();
}

Could be written as:

public void draw () {
    Camera camera = stage.getCamera();
    Group root = stage.getRoot();
    Batch batch = stage.getBatch();
    camera.update();

    if (!root.isVisible()) return;

    if (batch != null) {
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        root.draw(batch, 1);
        batch.end();
    }

    if (root.getDebug()) super.drawDebug();
}

I'm shooting from the hip here, I can't test this code currently. If all goes well you should be able to edit that draw method to meet your needs.

0
votes

In the end, I just simply copied the whole SpriteBatch.java source from here, and made my own SprBatch.java (shouldn't be the same name as SpriteBatch, or you can get a error when running) class with the copied code along with modifications, and used that class instead of SpriteBatch for all my draw() calls.

And because I am adding a function that overrides draw() in Batch, and SpriteBatch extends Batch for the draw methods, I needed to also create a copied Batch class (SBatch in my case). And make SprBatch extend SBatch instead.

Works fine with my copied SpriteBatch and Batch modified code.

Note: When you add a function to SprBatch that needs a @Overidde (the draw() for example), be sure add the function constructor to your copied Batch class also.

Finally, how it looks like in the project (Eclipse):

Project Explorer

And to use it, use it just like SpriteBatch...

Import

Declare