package org.even23hator.projektproz;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;

/**
 * Created by hator on 23.04.16.
 */
public class GameThread extends Thread {
    private volatile boolean running;
    private GameView gameView;

    public GameThread(GameView gameView) {
        super();
        this.gameView = gameView;
        this.running = true;
    }

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

    @Override
    public void run() {
        long lastFrameTime = System.currentTimeMillis();
        while(running) {
            final long dt = lastFrameTime - System.currentTimeMillis();
            lastFrameTime = System.currentTimeMillis();

            update(dt);

            gameView.post(new Runnable() {
                public void run() {
                    draw(dt);
                }
            });
        }
    }

    private void update(long dt) {

    }


    private void draw(long dt) {
        SurfaceHolder surfaceHolder = gameView.getHolder();

        synchronized (surfaceHolder) {
            if(!surfaceHolder.getSurface().isValid()) {
                return;
            }

            Canvas canvas = surfaceHolder.lockCanvas(null);

            // background
            canvas.drawColor(Color.argb(255, 15, 128, 255));

            Paint paint = new Paint();
            paint.setColor(Color.argb(255, 0, 0, 0));
            paint.setTextSize(45);

            canvas.drawText("delta " + dt + "ms", 20, 40, paint);

            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
}

@@ -125,16 +125,72 @@
 .Paint;%0A
+import android.os.SystemClock;%0Aimport android.util.Log;%0A
 import a
@@ -308,70 +308,283 @@
    p
-rivate volatile boolean running;%0A    private GameView gameView
+ublic static final int TARGET_FPS = 60;%0A    public static final int TIMESTEP_NS = 1000000000 / TARGET_FPS;%0A%0A    private volatile boolean running;%0A    private GameView gameView;%0A    private long elapsedTime;%0A    private double fps;%0A    private long frames;%0A    private long dt
 ;%0A%0A 
@@ -852,51 +852,175 @@
 ong 
-lastFrameTime = System.currentTimeMillis();
+currentTime;%0A        long startTime = SystemClock.elapsedRealtimeNanos();%0A        long lastFrameTime = startTime;%0A        dt = 0;%0A        fps = 0;%0A        frames = 0;%0A
 %0A   
@@ -1029,16 +1029,17 @@
    while
+ 
 (running
@@ -1058,68 +1058,248 @@
     
-final long dt = lastFrameTime - System.currentTimeMillis()
+currentTime = SystemClock.elapsedRealtimeNanos();%0A            elapsedTime = currentTime - lastFrameTime;%0A            lastFrameTime = currentTime;%0A%0A            ++frames;%0A            fps = (double)(frames) / (currentTime - startTime) * 1e9
 ;%0A
+%0A
     
@@ -1310,77 +1310,516 @@
     
-lastFrameTime = System.currentTimeMillis();%0A%0A            update(dt);%0A
+// NOTE(hator): for game mechanics update we use fixed timestep for simulation stability%0A            //      we accumulate time in dt and use it up for updates%0A            dt += elapsedTime;%0A            Log.d(%22GameThread%22, %22elapsed %22 + elapsedTime/1e6f + %22, delta %22 + dt/1e6f);%0A            while(dt %3E= TIMESTEP_NS) %7B%0A                dt -= TIMESTEP_NS;%0A                update(TIMESTEP_NS);%0A            %7D%0A%0A            // NOTE(hator): drawing needs to be done by the UI thread so we post draw method to our view
 %0A   
@@ -1919,18 +1919,16 @@
    draw(
-dt
 );%0A     
@@ -1955,16 +1955,337 @@
      %7D);
+%0A%0A            // NOTE(hator): if we render faster then target fps then we can sleep for a while%0A            //      so we don't block the cpu (improves battery life etc.)%0A            long sleepTime = (TIMESTEP_NS - dt) / 1000000;%0A            if(sleepTime %3E 0) %7B%0A                SystemClock.sleep(sleepTime);%0A            %7D
 %0A       
@@ -2360,23 +2360,16 @@
 id draw(
-long dt
 ) %7B%0A    
@@ -2878,16 +2878,26 @@
 a %22 + dt
+/1000000.f
  + %22ms%22,
@@ -2912,16 +2912,75 @@
  paint);
+%0A            canvas.drawText(%22FPS %22 + fps, 20, 100, paint);
 %0A%0A      

Make game loop use fixed timestep, add fps cap