Game Development Reference
In-Depth Information
public void render () {
renderTestObjects();
}
private void renderTestObjects() {
batch.setProjectionMatrix(camera.combined);
batch.begin();
for(Sprite sprite : worldController.testSprites) {
sprite.draw(batch);
}
batch.end();
}
public void resize (int width, int height) {
camera.viewportWidth = (Constants.VIEWPORT_HEIGHT / height) *
width;
camera.update();
}
@Override
public void dispose () {
batch.dispose();
}
The first action in WorldRenderer is to store the reference to WorldController
when it is instantiated. This is necessary for the renderer to access the game objects
that are managed by the controller. In init() , a new SpriteBatch object is created,
which will be used for all our rendering tasks. Before we can start to render objects,
we need to create a camera and define its viewport properly. The camera's viewport
defines the size of the captured game world it is looking at. It works basically the
same as a real camera. Obviously, when looking through a camera, you cannot see
anything else except the area it is currently pointed at. For example, if you want
to see what is to the left of it, you will have to move your camera to the left, which
holds true for both real and virtual cameras. We are using the width and height
defined in Constants to set the viewport.
In the event of a resized displaying area, resize() will be called by LibGDX. This
is our chance to adapt to the new display dimensions and redefine how the game
world should be rendered in this case. The code we added in resize() calculates
the aspect ratio between our desired visible world height and the currently available
display height. The answer is then multiplied with the available display width to
find the new viewport width for the camera. The resulting effect of this calculation is
that the world's visible height will always be kept to its full extent, while the world's
width will scale according to the calculated aspect ratio. It is very important to not
forget to call camera.update() whenever changes are made to the camera to let
them take effect.
 
Search WWH ::




Custom Search