LibGDX - Best practice for rendering?

(sorry if this isn’t the right board to put this on)

Hey all,
I’m in the process of porting the work I’ve done on a game engine over to LibGDX - previously, the rendering was handled by Java2D but I ended up reaching its limits pretty quickly in terms of what I’m trying to achieve. I really only have experience with Java2D (and I am incredibly new to LibGDX) and I’m used to being able to pass around a Graphics2D object between classes so that they may handle their own rendering. Is a similar thing considered the right thing to do in LibGDX?

I mean, say when I were using Java2D, I had a Graphics2D object that I grabbed from a Canvas, and I wanted to use it to render a few things that were handled by different classes - I’d just pass that object to those classes by reference and let them handle their own drawing.

My question is, is such a practice appropriate in LibGDX? Passing around a SpriteBatch object, for example?
And in this case, what would be the best case if I needed to render shapes and text as well as sprites in one of these classes?

Thank you all.

If I understood your question then I would say that you could make a render function for each class where you want to render something and pass it a SpriteBatch then in the main class you could render them all.

Main class:


@Override
public void render(float delta){
    batch.begin();
        SomeClass.render(spriteBatch);
        AnotherClass.render(spriteBatch);
    batch.end();
}

SomeClass:


public void render(SpriteBatch batch){
    randomSprite.draw(batch);
}

etc.

Yeah, that’s what I meant. My question though was more whether it was an acceptable practice to do so. Thank you.

I would say that it’s 100% acceptable or should I say that I haven’t figured out a better way yet. I have been using this method on many projects I’ve been in.