Game Development Reference
In-Depth Information
The class implements the
Graphics
interface. It contains an
AssetManager
member that we use
to load
Bitmap
instances, a
Bitmap
member that represents our artificial framebuffer, a
Canvas
member that we use to draw to the artificial framebuffer, a
Paint
member we need for drawing,
and two
Rect
members we need for implementing the
AndroidGraphics.drawPixmap()
methods.
These last three members are there so we don't have to create new instances of these classes
on every draw call. That would create a number of problems for the garbage collector.
public
AndroidGraphics(AssetManager assets, Bitmap frameBuffer) {
this
.assets=assets;
this
.frameBuffer=frameBuffer;
this
.canvas=
new
Canvas(frameBuffer);
this
.paint=
new
Paint();
}
In the constructor, we get an
AssetManager
and
Bitmap
that represent our artificial framebuffer
from the outside. We store these in the respective members and create the
Canvas
instance that
will draw the artificial framebuffer, as well as the
Paint
, which we use for some of the drawing
methods.
public
Pixmap newPixmap(String fileName, PixmapFormat format) {
Config config=
null
;
if
(format == PixmapFormat.
RGB565
)
config=Config.
RGB_565
;
else
if
(format == PixmapFormat.
ARGB4444
)
config=Config.
ARGB_4444
;
else
config=Config.
ARGB_8888
;
Options options=
new
Options();
options.inPreferredConfig=config;
InputStream in=
null
;
Bitmap bitmap=
null
;
try
{
in=assets.open(fileName);
bitmap=BitmapFactory.
decodeStream
(in);
if
(bitmap ==
null
)
throw new
RuntimeException("Couldn't load bitmap from asset '"
+ fileName+"'");
}
catch
(IOException e) {
throw new
RuntimeException("Couldn't load bitmap from asset '"
+ fileName + "'");
}
finally
{
if
(in !=
null
) {
try
{
in.close();
}
catch
(IOException e) {
}
}
}