I've been playing around for a while, and I can't seem to get a solution that works. I'm going to have the rendering code on a completely different thread, and my gameLogic code is not CPU-intensive, so this code should be able to run with hardly any impact on the resources of the phone.
I can't figure out how to do this the right way on the android though. Here's the actual code from my game that I'm porting.
- Code: Select all
private void gameLoop() throws InterruptedException {
int sleepTimeMilli, sleepLengthNano;
long startSleepTime = 0, endSleepTime = 0, difference = 0;
double frameLength = 16.666667;
while(true) {
if( frameStepMode ) stepToNextFrame = false;
sleepLengthNano = (int)((frameLength * 1000000) - difference);
if( sleepLengthNano < 0 ) {
sleepLengthNano = 0;
}
startSleepTime = System.nanoTime();
timeStep();
Thread.sleep(sleepLengthNano / 1000000,sleepLengthNano % 1000000 );
endSleepTime = System.nanoTime();
long elapsedTime = endSleepTime - startSleepTime;
Log.e("GameLogic", "Actual Frame Length: " + elapsedTime / 1000000f );
if( blockState != ALL_CLEAR
&& blockState != GAME_OVER && blockState != NEW_GAME && !paused) {
gameClock += elapsedTime;
}
difference = sleepLengthNano - elapsedTime;
}
}
When I run this, I'm getting frame lengths of about 200ms.
I don't believe my gameLogic code is that intensive: it's a tetris-like game. On my desktop the gameLogic code execution time is negligable. Am I working with the android wrong here?
Any help would be greatly appreciated.


