Hello! Now I have (1: the UI-loop, there my SurfaceView is placed), (2: a second thread, there the draw function from the UI-loop is called AND the update calculations from my Engine) and (3: the engine, there all the calculations stuff are).
Now I wonder how the best and smoothest way to do the SurfaceView independent from the actual frame rate. How shall I do so it will compensate if there is a low frame rate?
I think my current solution isn't enough, see code below.
Thread-class
//Constructor stuff....
int static delay = 0; /* My delay in milliseconds, depends most on
which sort of game I'm trying to make */
@Override
public void run() {
while (state==RUNNING) {
long beforeTime = System.nanoTime();
engine.updateSprites(); //Update calculations from my engine
//Rita
Canvas c = null;
try {
c = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
view.myDraw(c); //My draw function
}
} finally {
if (c != null) {
surfaceHolder.unlockCanvasAndPost(c);
}
}
sleepTime = delay-((System.nanoTime()-beforeTime)/1000000); /* 1000000 because
of the nanoTime gives us the current timestamp in nanoseconds */
try {
if(sleepTime>0){
Thread.sleep(sleepTime);
}
} catch (InterruptedException ex) {
}
}
Is this code enough to ensure the independent from low frame rate? If not, where is my way to go?
Thanks in advance!