Android Open Source - AdoreLib Adore Loop






From Project

Back to project page AdoreLib.

License

The source code is released under:

MIT License

If you think the Android project AdoreLib 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.ylinval.adore.adorelib;
/*w  w  w .ja  v  a2s . c  o m*/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;

import java.util.logging.Logger;

/**
 * Created by jdourlens on 5/9/14.
 */
public class AdoreLoop extends Thread {

    private SurfaceHolder mSurfaceHolder;
    private Handler mHandler;
    private Context mContext;
    private Paint mLinePaint;
    private Paint blackPaint;
    private AdoreFragment fragment;

    //for consistent rendering
    private long sleepTime;
    //amount of time to sleep for (in milliseconds)
    private long delay=70;

    //state of game (Running or Paused).
    int state = 1;
    public final static int RUNNING = 1;
    public final static int PAUSED = 2;

    public AdoreLoop(SurfaceHolder surfaceHolder, Context context, Handler handler,
                       AdoreFragment fragment) {

        //data about the screen
        mSurfaceHolder = surfaceHolder;
        this.mHandler = handler;
        this.mContext = context;

        //standard game painter. Used to draw on the canvas
        mLinePaint = new Paint();
        mLinePaint.setARGB(255, 0, 255, 0);
        //black painter below to clear the screen before the game is rendered
        blackPaint = new Paint();
        blackPaint.setARGB(255, 236, 240, 241);
        //mLinePaint.setAntiAlias(true);
        Log.i("Adore", "Adore Loop initialized");
        this.fragment = fragment;
    }


    //This is the most important part of the code. It is invoked when the call to start() is
    //made from the SurfaceView class. It loops continuously until the game is finished or
    //the application is suspended.
    @Override
    public void run() {

 long previous;
        //UPDATE
        previous = System.currentTimeMillis();
        while (state==RUNNING) {
            //time before update
            long beforeTime = System.currentTimeMillis();
            //This is where we update the game engine
            this.fragment.update((int)(beforeTime - previous));
            previous = beforeTime;
            //DRAW
            Canvas c = null;
            try {
                //lock canvas so nothing else can use it
                c = mSurfaceHolder.lockCanvas(null);
                                synchronized (mSurfaceHolder) {
                    if(c != null) {
                        //clear the screen with the black painter.
                        c.drawRect(0, 0, c.getWidth(), c.getHeight(), blackPaint);
                        //This is where we draw the game engine.
                        this.fragment.draw(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) {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }



            //SLEEP
            //Sleep time. Time required to sleep to keep game consistent
            //This starts with the specified delay time (in milliseconds) then subtracts from that the
            //actual time it took to update and render the game. This allows our game to render smoothly.
            this.sleepTime = delay-((System.nanoTime()-beforeTime)/1000000L);
            try {
                if(sleepTime>0 && sleepTime < 1000){
                    this.sleep(sleepTime);
                }
            } catch (InterruptedException ex) {
                Log.i("Exception sleep", ex.getMessage());
            }
        }
    }
}




Java Source Code List

com.ylinval.adore.adorelib.AdoreActivity.java
com.ylinval.adore.adorelib.AdoreFragment.java
com.ylinval.adore.adorelib.AdoreLoop.java
com.ylinval.adore.adorelib.AdoreView.java
com.ylinval.adore.adorelib.AdoreGraphics.Circle.java
com.ylinval.adore.adorelib.AdoreGraphics.Image.java
com.ylinval.adore.adorelib.AdoreGraphics.Line.java
com.ylinval.adore.adorelib.AdoreGraphics.Point.java
com.ylinval.adore.adorelib.AdoreGraphics.Rectangle.java
com.ylinval.adore.adorelib.AdoreGraphics.RoundRectangle.java
com.ylinval.adore.adorelib.AdoreGraphics.RoundSquare.java
com.ylinval.adore.adorelib.AdoreGraphics.Square.java
com.ylinval.adore.adorelib.AdoreGraphics.TextOutlined.java
com.ylinval.adore.adorelib.AdoreGraphics.Text.java
com.ylinval.adore.adorelib.AndroidViews.VerticalTextView.java
com.ylinval.adore.adorelib.Libs.Atlas.Circ.java
com.ylinval.adore.adorelib.Libs.Atlas.Pos.java
com.ylinval.adore.adorelib.Libs.Atlas.Rect.java
com.ylinval.adore.adorelib.Libs.State.StateManager.java
com.ylinval.adore.adorelib.Libs.State.State.java
com.ylinval.adore.adorelib.Libs.Tween.Tween.java
com.ylinval.adore.adorelib.Mobile.Accelerometer.java
com.ylinval.adore.adorelib.Sound.Music.java
com.ylinval.adore.adorelib.Sound.SoundEffect.java