I am learning to program for the Android. I’ve made a couple of basic games, but for learning I’d like to make a car driving game. The simplest way I thought of doing this was to keep the car in the middle of the screen and scroll the background to give the illusion of movement.
As an experiment to figure out the engine, I created a 2048×1200 Bitmap which I load from a PNG. This will be the map. I then create a new bitmap of 1024×600 cropped from the map which I display on screen. I do this as getWidth() & getHeight() give these dimensions. I take a different crop each time based on movement to show on screen.
Unfortunately I find this too slow to use. The car would have to move at a snails pace if I wanted it to be smooth. I get about 18fps.
I am using James Daniello’s Simple Game Loop
I also read the following stack overflow article which says to keep the object creation out of the main loop as much as possible, which I am doing as best I can.
How to draw lots of bitmaps on screen in an Android game without slow performance
Also of use was this article by Romain Guy on keeping the Bitmap as the same format as the screen to stop it having to be transformed to be displayed on the screen, which is very costly.
Bitmap quality, banding and dithering
I’d like to figure out the best I can achieve with Canvas and Bitmaps before I move on to OpenGL. Am I at the limitations, or should I do something differently? I find I get 18fps when just scrolling the map 1px at a time for smooth scrolling.
private Bitmap track;
private Bitmap visible;
private Point position;
private int fps = 0;
private long lastFpsTime = 0;
private int fpsCount = 0;
Paint pText;
public void Draw(Canvas c){
visible = Bitmap.createBitmap(track, position.x + 512, position.y + 300, 512, 300);
position.x++;
c.drawBitmap(visible, 0,0, null);
c.drawText("FPS: " + fps, 10, 10, pText);
}
public void Init(Resources resources) {
// Load Map from track1.png
track = Bitmap.createBitmap(2048, 1200, Bitmap.Config.RGB_565);
Bitmap track1 = BitmapFactory.decodeResource(resources, R.drawable.track1);
track = track1.copy(Bitmap.Config.RGB_565, true);
position = new Point(0,0);
//Setup the Text Paint Object
pText = new Paint();
pText.setColor(Color.WHITE);
pText.setStyle(Style.FILL_AND_STROKE);
}
public void Update() {
fpsCount++;
if (System.currentTimeMillis() - lastFpsTime >= 1000){
lastFpsTime = System.currentTimeMillis();
fps = fpsCount;
fpsCount = 0;
}
}
You are creating a new bitmap on every frame you draw. It’s extremely expensive. You should use the version of drawBitmap() that takes a source and a destination rectangle instead.