Search code examples
javaandroidloopsframe-rate

Android Game Loop: How to reach an average of 60 FPS?


I have seen the game Airplane Madness rendering the screen at a constant average of 60 FPS on my HTC Wildfire S. I've been trying to imitate the results on the same smartphone, but I just can't understand why my program only tops 15 FPS on average?

How I can view the frame rate of Airplane Madness, you may ask? There's an option to toggle FPS counter in the Settings. I set mine to ENABLED.

Here's my code. This is my game loop. There are some interesting results from this:

    public void run() {
        // Average FPS for Android is 15 on Test Phone is 13 through 15.
        // Max FPS is 20.
        // Occasional bursts of FPS60 may occur.
        long now, lastTime = System.nanoTime();
        double process = 0.0;
        final double NSperTick = 1000000000.0 / 60.0;
        while (running) {
            now = System.nanoTime();
            process += (now - lastTime) / NSperTick;
            lastTime = now;
            boolean ticked = false;
            while (process >= 1) {
                process -= 1;
                tick();
                ticked = true;
            }
            if (ticked) {
                render();
            }
            swap();
            try {
                Thread.sleep(2);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

Can anyone tell me what I'm doing wrong, what needs to be improved, etc., so that I can get an average of 60 FPS? This game loop, when running in Java (Not Android), it runs practically fine, so I'm not sure what logic parts are affecting the frame rate in my smartphone.

Thanks in advance.


EDIT: Looks like I need to make all functions known to Man.

private void render() {
    synchronized (holder) {
        if (holder.getSurface().isValid()) {
            Canvas c = holder.lockCanvas();
            c.drawRGB(0x44, 0x55, 0xff);
            ball.render(c);
            holder.unlockCanvasAndPost(c);
        }
    }
}

private void swap() {
}

private void tick() {
    float[] values = {meter.X, meter.Y, meter.Z};
    ball.tick(values);
    handler.tick(ball);
}

And here's how I check my game's average FPS:

public void run() {
    // Average FPS for Android is 15 on Test Phone is 13 through 15.
    // Max FPS is 20.
    // Occasional bursts of FPS60 may occur.
    long now, lastTime = System.nanoTime();
    int frames = 0;
    double process = 0.0;
    long frameTime = System.currentTimeMillis();
    final double NSperTick = 1000000000.0 / 60.0;
    while (running) {
        now = System.nanoTime();
        process += (now - lastTime) / NSperTick;
        lastTime = now;
        boolean ticked = false;
        while (process >= 1) {
            process -= 1;
            tick();
            ticked = true;
        }
        if (ticked) {
            //render();
            frames++;
        }
        swap();
        try {
            Thread.sleep(2);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (frameTime <= System.currentTimeMillis() - 1000)
        {
            Log.d("FrameRate", Integer.toString(frames));
            frameTime += 1000;
            frames = 0;
        }
    }
}

And here are the results from DDMS:

03-12 14:29:21.008: D/FrameRate(3533): 14
03-12 14:29:22.061: D/FrameRate(3533): 15
03-12 14:29:23.048: D/FrameRate(3533): 14
03-12 14:29:24.033: D/FrameRate(3533): 14
03-12 14:29:25.018: D/FrameRate(3533): 14
03-12 14:29:26.000: D/FrameRate(3533): 14
03-12 14:29:27.056: D/FrameRate(3533): 15
03-12 14:29:28.047: D/FrameRate(3533): 14
03-12 14:29:29.026: D/FrameRate(3533): 14
03-12 14:29:29.995: D/FrameRate(3533): 14
03-12 14:29:31.037: D/FrameRate(3533): 15
03-12 14:29:32.015: D/FrameRate(3533): 14

EDIT 2: There's more info:

I have commented out this code in the render() function:

synchronized (holder)

But still, I can only achieve 14 to 15 FPS. Is this a limitation I can't break through?


Solution

  • I don't understand why your synchronizing in your rendering loop. This is bad form as it may lock up your rendering thread, causing the app to appear to freeze if it can't acquire the lock immediately for any reason. It also takes time to get acquire the lock.

    If you've got your data organized in such a way that you need to get a lock in your rendering thread, you need to refactor your design.

    Never synchronize in the UI thread!