I'm new in Android Game Development and after I started with libgdx ShapeRenderer and did a little more search, I became confused if I started with the right foot.
So, what I really would like to know is when should I use ShapeRenderer, Mesh + SpriteBatch, Box2D and Scene2D.
LibGDX has quite a lot of (mostly orthogonal) APIs for rendering. I'm still learning my way around many of them, but I can give an overview of the different parts.
ShapeRenderer
lets you quickly and easily put basic flat-colored polygons and lines on the screen. Its not particularly efficient (it uploads a lot of vertex data on each render). But it can be very quick to get going. Its oriented strongly to the screen (it defaults to an orthographic project on the full screen and its units are generally pixels).
The Mesh
class gives you more direct control over geometry, but you're exposed to a lot more of the details (like encoding colors as floats and storing vertex data, color data, and texture data all in the same float array, and the concept of index arrays).
The SpriteBatch
is oriented around "sprites" (generally rectangular areas with a texture in them). This hides some of the complexity (and flexibility) of texturing with meshes.
Box2D is for physics modeling, and is not like any of the other API parts you mentioned (which are mostly about rendering).
Finally, Scene2D is mostly about creating animated user-interface elements like buttons, lists, and sliders. But its powerful enough (and has enough neat and flexible support for animations and interaction events) that you can implement quite a lot of "game" in it too.
You can also use raw "OpenGL" commands (see GL20
), too.
You can totally mix-and-match these different primitives (one caveat is that you generally must "flush" and "end" whatever ShapeRenderer
/SpriteBatch
/Mesh
render before starting a different flavor). So, for example, you might use Scene2D for your "main menu" and some meta actions in your game, a SpriteBatch
to render the core of your game, and use a ShapeRenderer
to generate debug overlays (think visualizing bounding boxes, etc).
Also note that most of libGDX is geared towards 2D. Raw OpenGL
and the Mesh
APIs can also be used to build 3D objects.
Check the gdx-invaders example to see how it uses Mesh
, OpenGL, and SpriteBatch
. (I think this example pre-dates Scene2d, so there is none of that...). Its also worthwhile to look over the implementation of these classes to see what they're doing underneath the covers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With