Game Development Reference
In-Depth Information
Let's calculate a simple example for a target resolution of 320×480 pixels and a device with a
resolution of 480×800 pixels. If we touch the middle of the screen, we receive an event with the
coordinates (240,400). Using the two preceding formulas, we arrive at the following equations,
which are exactly in the middle of our target coordinate system:
transformed touch x=240 * (320 / 480)=160
transformed touch y=400 * (480 / 800)=240
Let's do another one, assuming a real resolution of 240×320, again touching the middle of the
screen, at (120,160):
transformed touch x=120 * (320 / 240)=160
transformed touch y=160 * (480 / 320)=240
This works in both directions. If we multiply the real touch event coordinates by the target factor
divided by the real factor, we don't have to worry about transforming our actual game code. All
the touch coordinates will be expressed in our fixed-target coordinate system.
With that issue out of our way, we can implement the last few classes of our game framework.
AndroidPixmap: Pixels for the People
According to the design of our
Pixmap
interface from Chapter 3, there's not much to implement.
Listing 5-13 presents the code.
Listing 5-13. AndroidPixmap.java, a Pixmap Implementation Wrapping a Bitmap
package
com.badlogic.androidgames.framework.impl;
import
android.graphics.Bitmap;
import
com.badlogic.androidgames.framework.Graphics.PixmapFormat;
import
com.badlogic.androidgames.framework.Pixmap;
public class
AndroidPixmap
implements
Pixmap {
Bitmap bitmap;
PixmapFormat format;
public
AndroidPixmap(Bitmap bitmap, PixmapFormat format) {
this
.bitmap=bitmap;
this
.format=format;
}
public int
getWidth() {
return
bitmap.getWidth();
}
public int
getHeight() {
return
bitmap.getHeight();
}