Game Development Reference
In-Depth Information
As you can see, there is a lot of empty space in this screenshot. In order to be able to
use the filled part of this screenshot only, a new instance of TextureRegion is created.
It references the previously loaded texture that contains the full image and has the
additional information to cut all the pixels starting from ( 0, 0 ) to ( 512, 275 ). These
two points describe a rectangle starting at the top-left corner of the image with a width
and height of 512 by 275 pixels. Finally, a sprite is created using the information of the
previously created texture region. The sprite's size is set to 90 percent of its original
size. The sprite's origin is set to half of its width and height to move the origin to its
center. Eventually, the position is set to the negative half of the sprite's width and
height so that the sprite moves to the center of the scene.
LibGDX uses a coordinate system that has its origin ( 0 , 0 ) at the
bottom-left corner. This means that the positive x axis points to
the right-hand side, while the positive y axis points upwards.
The render() method
The render() method contains the commands to render a scene on screen,
as shown here:
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
}
The first two lines call the low-level OpenGL methods to set the clear color to a solid
white, and then execute the clear screen command.
Next, the projection matrix of the sprite batch is set to the camera's combined
projection and view matrix. You do not have to understand what this means in detail
at the moment. It basically just means that every following drawing command will
behave according to the rules of an orthographic projection, or simply put, drawing
will be done in 2D space using the position and bounds of the given camera.
The begin() and end() methods will always have to appear in pairs and
should not be nested or there will be errors. The actual drawing of the sprite is
accomplished by calling the draw() method of the sprite to draw and pass the
instance of the sprite batch.
 
Search WWH ::




Custom Search