Android Open Source - android-gear Android Game Loop






From Project

Back to project page android-gear.

License

The source code is released under:

MIT License

If you think the Android project android-gear listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.androidgear.core;
//  www. j  a v a2  s. c  o  m
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.androidgear.core.graphics.AndroidGraphics;

public class AndroidGameLoop extends SurfaceView implements
        SurfaceHolder.Callback {

    private Game game;
    private AndroidGraphics graphics;
    private AndroidGameLoopThread loopThread;
    private Bitmap bitmap;
    private Canvas canvas;
    public int expectedMilis;
    private int width;
    private int height;
    private Matrix identityMatrix;

    public AndroidGameLoop(Context context, Game game) {
        super(context);
        getHolder().addCallback(this);
        this.game = game;
        loopThread = new AndroidGameLoopThread(getHolder(), this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int arg1, int arg2,
            int arg3) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        width = getWidth();
        height = getHeight();
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        canvas = new Canvas();
        canvas.setBitmap(bitmap);
        identityMatrix = new Matrix();

        loopThread.setRunning(true);
        loopThread.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // Game Render
        game.render(graphics);

        canvas.drawBitmap(bitmap, identityMatrix, null);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        // simply copied from sample application LunarLander:
        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        loopThread.setRunning(false);
        while (retry) {
            try {
                loopThread.join();
                retry = false;
            } catch (InterruptedException e) {
            }
        }
    }

    class AndroidGameLoopThread extends Thread {
        private SurfaceHolder surfaceHolder;
        private AndroidGameLoop loop;
        private boolean run = false;
        private long lastUpdate;

        public AndroidGameLoopThread(SurfaceHolder surfaceHolder,
                AndroidGameLoop loop) {
            this.surfaceHolder = surfaceHolder;
            this.loop = loop;
        }

        public void setRunning(boolean run) {
            this.run = run;
        }

        @Override
        public void run() {
            Canvas c;

            // Create Graphics
            graphics = new AndroidGraphics();

            graphics.setCanvas(canvas);
            game.init(graphics);

            c = null;
            expectedMilis = 1000 / game.getFrameRate();

            lastUpdate = System.currentTimeMillis();

            long lastRender = 0;

            // Game loop
            do {
                long update = System.currentTimeMillis();
                long delta = update - lastUpdate;

                if (lastUpdate - lastRender >= expectedMilis) {
                    // Game Render

                    try {
                        c = surfaceHolder.lockCanvas();
                        synchronized (surfaceHolder) {
                            loop.onDraw(c);
                        }
                    } finally {
                        // do this in a finally so that if an exception is
                        // thrown
                        // during the above, we don't leave the Surface in an
                        // inconsistent state
                        if (c != null) {
                            surfaceHolder.unlockCanvasAndPost(c);
                        }
                    }

                    lastRender = System.currentTimeMillis();
                } else {
                    // Game update
                    game.update(delta);
                    Thread.yield();
                }
                lastUpdate += delta;
            } while (run);

            // Game destroy
            game.destroy();
        }
    }
}




Java Source Code List

com.androidgear.core.AbstractAndroidGearActivity.java
com.androidgear.core.AndroidGameLoop.java
com.androidgear.core.AndroidGearCommands.java
com.androidgear.core.Game.java
com.androidgear.core.GearCommands.java
com.androidgear.core.JavaGameLoop.java
com.androidgear.core.JavaGearCommands.java
com.androidgear.core.asset.AndroidAssetLoader.java
com.androidgear.core.asset.AssetLoader.java
com.androidgear.core.asset.AssetManager.java
com.androidgear.core.asset.AssetNotFoundException.java
com.androidgear.core.asset.JavaAssetLoader.java
com.androidgear.core.config.JavaConfig.java
com.androidgear.core.dialog.AndroidDialogFactory.java
com.androidgear.core.dialog.AndroidDialog.java
com.androidgear.core.dialog.DialogButtonListener.java
com.androidgear.core.dialog.DialogFactory.java
com.androidgear.core.dialog.Dialog.java
com.androidgear.core.dialog.JavaDialogFactory.java
com.androidgear.core.dialog.JavaDialog.java
com.androidgear.core.graphics.AndroidGraphics.java
com.androidgear.core.graphics.AndroidImage.java
com.androidgear.core.graphics.AndroidSpriteSheet.java
com.androidgear.core.graphics.Font.java
com.androidgear.core.graphics.Graphics.java
com.androidgear.core.graphics.Image.java
com.androidgear.core.graphics.JavaGraphics.java
com.androidgear.core.graphics.JavaImage.java
com.androidgear.core.graphics.JavaSpriteSheet.java
com.androidgear.core.graphics.SpriteSheet.java
com.androidgear.core.sound.AndroidSound.java
com.androidgear.core.sound.JavaSound.java
com.androidgear.core.sound.Sound.java
com.androidgear.core.window.JavaGameWindow.java
com.androidgear.core.window.JavaSimpleWindow.java
com.androidgear.core.window.JavaSimulatorWindow.java
com.androidgear.samples.JavaSampleGame.java
com.androidgear.samples.SampleGameActivity.java
com.androidgear.samples.core.SampleGame.java
${package}.__GameClass__Activity.java
${package}.Java__GameClass__.java
${package}.core.__GameClass__.java