Game Development Reference
In-Depth Information
The
drawRect()
method sets the
Paint
member's color and style attributes so that we can draw
a filled, colored rectangle. In the actual
Canvas.drawRect()
call, we have to transform the
x
,
y
,
width
, and
height
parameters of the coordinates in the top-left and bottom-right corners of the
rectangle. For the top-left corner, we simply use the
x
and
y
parameters. For the bottom-right
corner, we add the
width
and
height
to
x
and
y
and subtract 1. For example, if we render a
rectangle with an
x
and
y
of (10,10) and a
width
and
height
of 2 and 2 and we don't subtract 1,
the resulting rectangle on the screen will be 3×3 pixels in size.
public void
drawPixmap(Pixmap pixmap,
int
x,
int
y,
int
srcX,
int
srcY,
int
srcWidth,
int
srcHeight) {
srcRect.left=srcX;
srcRect.top=srcY;
srcRect.right=srcX+srcWidth - 1;
srcRect.bottom=srcY+srcHeight - 1;
dstRect.left=x;
dstRect.top=y;
dstRect.right=x+srcWidth - 1;
dstRect.bottom=y+srcHeight - 1;
canvas.drawBitmap(((AndroidPixmap) pixmap).bitmap, srcRect, dstRect,
null
);
}
The
drawPixmap()
method, which allows us to draw a portion of a
Pixmap
, sets up the source
and destination of the
Rect
members that are used in the actual drawing call. As with drawing
a rectangle, we have to translate the x and y coordinates together with the width and height to
the top-left and bottom-right corners. Again, we have to subtract 1, or else we will overshoot
by 1 pixel. Next, we perform the actual drawing via the
Canvas.drawBitmap()
method, which
will automatically do the blending if the
Pixmap
we draw has a
PixmapFormat.ARGB4444
or
a
PixmapFormat.ARGB8888
color depth. Note that we have to cast the
Pixmap
parameter to
an
AndroidPixmap
in order to fetch the
bitmap
member for drawing with the
Canvas
. That's
a bit complicated, but we can be sure that the
Pixmap
instance that is passed in will be an
AndroidPixmap
.
public void
drawPixmap(Pixmap pixmap,
int
x,
int
y) {
canvas.drawBitmap(((AndroidPixmap)pixmap).bitmap, x, y,
null
);
}
The second
drawPixmap()
method draws the complete
Pixmap
to the artificial framebuffer
at the given coordinates. Again, we must do some casting to get to the
Bitmap
member of
the
AndroidPixmap
.
public int
getWidth() {
return
frameBuffer.getWidth();
}
public int
getHeight() {
return
frameBuffer.getHeight();
}
}