Game Development Reference
In-Depth Information
Implementing the WorldRenderer class
The following listing shows the first implementation of WorldRenderer :
package com.packtpub.libgdx.canyonbunny.game;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Disposable;
import com.packtpub.libgdx.canyonbunny.util.Constants;
public class WorldRenderer implements Disposable {
private OrthographicCamera camera;
private SpriteBatch batch;
private WorldController worldController;
public WorldRenderer (WorldController worldController) { }
private void init () { }
public void render () { }
public void resize (int width, int height) { }
@Override public void dispose () { }
}
This class also has an internal init() method for its initialization. Furthermore, it
contains a render() method that will contain the logic to define in which order the
game objects are drawn over others. Whenever the screen size is changed, including
the event at the start of the program, resize() will spring into action and initiate the
required steps to accommodate the new situation.
The rendering is accomplished using an orthographic camera that is suitable for
two-dimensional projections. Fortunately, LibGDX comes with a ready-to-use
OrthographicCamera class to simplify our 2D rendering tasks. The SpriteBatch
class is the actual workhorse that draws all our objects with respect to the
camera's current settings (for example, position, zoom, and so on) to the screen.
As SpriteBatch implements LibGDX's Disposable interface, it is advisable to
always call its dispose() method to free the allocated memory when it is no longer
needed. We will do this in WorldRenderer by also implementing the Disposable
interface. This allows us to easily cascade the disposal process when dispose()
in CanyonBunnyMain is called by LibGDX. In this case, we will simply call the
WorldRenderer class' dispose() method, which in turn will call the SpriteBatch
class' dispose() method.
Notice that this class requires a reference to an instance of WorldController in its
constructor so that it will be accessible later on to render all the game world objects
that are managed by the controller.
 
Search WWH ::




Custom Search