Game Development Reference
In-Depth Information
if
(bitmap.getConfig() == Config.
RGB_565
)
format=PixmapFormat.
RGB565
;
else if
(bitmap.getConfig() == Config.
ARGB_4444
)
format=PixmapFormat.
ARGB4444
;
else
format=PixmapFormat.
ARGB8888
;
return new
AndroidPixmap(bitmap, format);
}
The
newPixmap()
method tries to load a
Bitmap
from an asset file, using the specified
PixmapFormat
. We start by translating the
PixmapFormat
into one of the constants of the Android
Config
class used in Chapter 4. Next, we create a new
Options
instance and set our preferred
color format. Then, we try to load the
Bitmap
from the asset via the
BitmapFactory
, and throw a
RuntimeException
if something goes wrong. Otherwise, we check what format the
BitmapFactory
used to load the
Bitmap
and translate it into a
PixmapFormat
enumeration value. Remember
that the
BitmapFactory
might decide to ignore our desired color format, so we have to check to
determine what it used to decode the image. Finally, we construct a new
AndroidBitmap
instance
based on the
Bitmap
we loaded, as well as its
PixmapFormat
, and return it to the caller.
public void
clear(
int
color) {
canvas.drawRGB((color & 0xff0000) >>16, (color & 0xff00) >>8,
(color & 0xff));
}
The
clear()
method extracts the red, green, and blue components of the specified 32-bit ARGB
color parameter and calls the
Canvas.drawRGB()
method, which clears our artificial framebuffer
with that color. This method ignores any alpha value of the specified color, so we don't have to
extract it.
public void
drawPixel(int x,
int
y,
int
color) {
paint.setColor(color);
canvas.drawPoint(x, y, paint);
}
The
drawPixel()
method draws a pixel of our artificial framebuffer via the
Canvas.drawPoint()
method. First, we set the color of our
Paint
member variable and pass it to the drawing method
in addition to the x and y coordinates of the pixel.
public void
drawLine(
int
x,
int
y,
int
x2,
int
y2,
int
color) {
paint.setColor(color);
canvas.drawLine(x, y, x2, y2, paint);
}
The
drawLine()
method draws the given line of the artificial framebuffer, using the
Paint
member
to specify the color when calling the
Canvas.drawLine()
method.
public void
drawRect(
int
x,
int
y,
int
width,
int
height,
int
color) {
paint.setColor(color);
paint.setStyle(Style.
FILL
);
canvas.drawRect(x, y, x+width - 1, y+width - 1, paint);
}