Game Development Reference
In-Depth Information
reg = regEdge;
batch.draw(reg.getTexture(),position.x + relX, position.y +
relY, origin.x + dimension.x / 8, origin.y, dimension.x / 4,
dimension.y, scale.x, scale.y, rotation, reg.getRegionX(),
reg.getRegionY(), reg.getRegionWidth(), reg.getRegionHeight(),
true, false);
}
Before we continue, let's take a look at the signature of draw() from SpriteBatch to
clear up the more or less convoluted lines of source code that are used to draw the
texture regions:
public void draw (Texture texture, float x, float y,float originX,
float originY, float width, float height, float scaleX, float
scaleY, float rotation, int srcX, int srcY,
int srcWidth, int srcHeight, boolean flipX, boolean flipY);
This method cuts out a rectangle (defined by srcX , srcY , srcWidth , and srcHeight )
from the texture (here, our texture atlas) and draws it to a given position ( x , y ).
The origin ( originX , originY ) defines a relative position to where the rectangle is
shifted. The origin (at 0 , 0 ) denotes the bottom-left corner. The width and height
define the dimension of the image to be displayed. The scaling factor ( scaleX ,
scaleY ) defines the scale of the rectangle around the origin. The angle of rotation
defines the rotation of the rectangle around the origin. The flipping of one or both the
axes ( flipX , flipY ) means to mirror the corresponding axis of that image.
The rendering of the rock is split up into the following three drawing steps:
1.
Draw the left edge at the current position of the rock. A relative x- and y-value,
relX and relY , are also added to the position. These are used to align the left
edge to the left-hand side of the object's local y axis. The result of doing this is
that the following middle parts will now start at 0 on the x axis. This makes it
much easier to handle the positioning of rocks as each middle part represents
one pixel in a level image, while the edges are just cosmetic details.
2.
Draw all the middle parts according to the set length of the rock. The
drawing starts at 0 , which is located directly next to where the left edge ends.
Each subsequent middle part is drawn next to the last middle part. This is
achieved by adding the middle part's width to the relative position relX for
each iteration inside the loop.
3.
Finally, the mirrored left edge is drawn next to where the last middle part
ends. This mirroring is achieved by setting the flipX parameter to true .
The reg variable is used to store the currently selected texture region for each step.
 
Search WWH ::




Custom Search