ShapeRenderer
In the Saxion Game App, we use a ShapeRenderer from LibGDX to draw shapes on the screen. We currently support shapes like rectangles, circles, lines, polygons, and ellipses. The ShapeRenderer helps us draw them quickly and with the right colors and transparency.
Why we need the ShapeRenderer
The ShapeRenderer talks to the computer’s graphics hardware (the GPU) to draw shapes. Drawing each shape one by one can be slow, especially if there are many shapes. The ShapeRenderer groups drawing commands together to make it faster.
How it works
Before we start calling drawing methods, we have to let the ShapeRenderer know that we want to start. Rendering shapes consists of three phases.
1. Start the ShapeRenderer
GameApp.startShapeRenderingFilled()– for solid shapesGameApp.startShapeRenderingOutlined()– for shape outlines
This will create an vertex buffer in memory where we can draw on.
2. Draw the shapes
Call shape rendering methods like: GameApp.drawRect(), GameApp.drawCircle(), GameApp.drawLine(), etc.
This will draw all of you shapes in the vertex buffer.
3. End drawing
Call GameApp.endShapeRendering(). This tells the computer, “I’m done drawing for now,” so all the shapes are sent to the graphics card at once.
Why start and end are important
You must call a start method before drawing and end after drawing:
- If you forget start, the computer doesn’t know where to draw to.
- If you forget end, the shapes will not appear, because the vertex buffer is never sent to the GPU.
Think of it like painting a picture: you open your canvas (start), paint all your shapes, then close the canvas (end) to finish.