Android Open Source - android_game_engine Drawing Thread






From Project

Back to project page android_game_engine.

License

The source code is released under:

GNU General Public License

If you think the Android project android_game_engine 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 org.es.engine.game_mechanic;
/*from  w  w w. ja v  a2s.  c  om*/
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.util.Log;
import android.view.InputEvent;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Parent class for drawing threads
 *
 * @author Cyril Leroux
 *         Created on 22/09/13.
 */
public abstract class DrawingThread extends Thread {

    private static final String TAG = "DrawingThread";

    private final SurfaceHolder mSurfaceHolder;
    private final Resources mResources;
    private ConcurrentLinkedQueue<InputEvent> mEventQueue = new ConcurrentLinkedQueue<>();

    /** Number of frame we wish to draw per second. */
    private int mFrameRate = 20;
    /** The time a frame is suppose to stay on screen in milliseconds. */
    private int mFrameDuration = 1000 / mFrameRate;
    /** Indicate whether the thread is suppose to draw or not. */
    private boolean mRunning = true;

    public DrawingThread(SurfaceHolder surfaceHolder, Context context) {
        mSurfaceHolder = surfaceHolder;
        mResources = context.getResources();
    }

    @Override
    public void run() {

        while (mRunning) {
            long start = System.currentTimeMillis();

            update();
            draw();

            final long waitingTimeMillis = mFrameDuration - (System.currentTimeMillis() - start);
            if (waitingTimeMillis > 0) {
                try {
                    sleep(waitingTimeMillis);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    mRunning = false;
                }
            } else {
                // We are running late !
                Log.d(TAG, "Running late ! " + waitingTimeMillis);
            }
        }
    }

    public void setFrameRate(int framePerSecond) {
        mFrameRate = framePerSecond;
        mFrameDuration = 1000 / mFrameRate;
    }

    /**
     * Used to signal the thread whether it should be running or not.
     *
     * @param running true to run, false to shut down
     */
    public void setRunning(boolean running) {
        mRunning = running;
    }

    /** Callback invoked when the surface dimensions change. */
    public void setSurfaceSize(int width, int height) {
        // synchronized to make sure these all change atomically
        synchronized (mSurfaceHolder) {
            updateSurfaceSize(width, height);
        }
    }

    /**
     * Update the surface size atomically.<br />
     * Synchronized is performed by the caller ({@link #setSurfaceSize(int, int)}).
     */
    protected abstract void updateSurfaceSize(int surfaceWidth, int surfaceHeight);

    /** Check user inputs and update data. */
    protected abstract boolean update();

    /** Processes the event to update the view. */
    protected final void processEvent(InputEvent event) {

        if (event instanceof MotionEvent) {
            processEvent((MotionEvent) event);

        } else if (event instanceof KeyEvent) {
            processEvent((KeyEvent) event);
        }
    }

    /** Processes the event to update the view. */
    protected abstract void processEvent(MotionEvent event);

    /** Processes the event to update the view. */
    protected abstract void processEvent(KeyEvent event);

    /** Add a motionEvent that will be processed in {@link #update()}. */
    public boolean addInputEvent(InputEvent event) {
        return mEventQueue.add(event);
    }

    /** @return true if the event queue is not empty. */
    protected boolean hasNext() {
        return !mEventQueue.isEmpty();
    }

    /** Poll the next motionEvent to process. */
    protected InputEvent pollInputEvent() {
        return mEventQueue.poll();
    }

    /** Draw the new frame. */
    private void draw() {
        Canvas canvas = null;
        try {
            canvas = mSurfaceHolder.lockCanvas(null);
            if (canvas != null) {
                doDraw(canvas);
            }
        } finally {
            if (canvas != null) {
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

    /**
     * Draws current state of the canvas.<br />
     * Canvas null check is performed by the caller ({@link #draw()}).
     */
    protected abstract void doDraw(Canvas canvas);

    protected Resources getResources() {
        return mResources;
    }
}




Java Source Code List

com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.GameHelperUtils.java
com.google.example.games.basegameutils.GameHelper.java
com.google.example.games.basegameutils.ScreenUtils.java
org.es.engine.audio.Sound.java
org.es.engine.game_mechanic.DrawingThread.java
org.es.engine.game_mechanic.DrawingView.java
org.es.engine.graphics.animation.AnimationCallback.java
org.es.engine.graphics.animation.Animation.java
org.es.engine.graphics.animation.BitmapAnimation.java
org.es.engine.graphics.animation.SpriteSheetAnimation.java
org.es.engine.graphics.drawable.DrawableElement.java
org.es.engine.graphics.sprite.GenericSprite.java
org.es.engine.graphics.sprite.SpriteSheet.java
org.es.engine.graphics.sprite.Sprite.java
org.es.engine.graphics.utils.DrawTextUtils.java
org.es.engine.graphics.utils.DrawingParam.java
org.es.engine.hud.Button.java
org.es.engine.hud.Control.java
org.es.engine.hud.HUD.java
org.es.engine.hud.Text.java
org.es.engine.hud.ToggleButton.java
org.es.engine.toolbox.pathfinding.Node.java
org.es.engine.toolbox.pathfinding.ShortestPath.java