Fixed Frame Rate?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Fixed Frame Rate?

Postby JoshuaD » Sun Dec 14, 2008 9:42 pm

I'm porting a game to the android that has a discrete game logic engine; I can't call updateGameSate(elapsed time). I need to update the game state roughly every 16.66667 milliseconds. It's ok if it's not exactly on the money each frame, I've got error correction built in, but it should be decently close.

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.
JoshuaD
Freshman
Freshman
 
Posts: 3
Joined: Sun Dec 14, 2008 8:43 pm

Top

Postby MrSnowflake » Sun Dec 14, 2008 10:11 pm

Instead of sleeping have it sleep for 1 ms (or even less (using sleep(0, x) where x is the time in nanoseconds)) in a for loop, in which it checks if the designated time is reached (16 ms).
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. while (System.currentTimeMillis() < loopTime) {
  2.  
  3.   Thread.sleep(0, 10);
  4.  
  5. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Something like that. Your thread sleeps, but hopefully never a lot too much...
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby JoshuaD » Mon Dec 15, 2008 1:12 am

Hrm. That doesn't seem to work. Any other ideas?
JoshuaD
Freshman
Freshman
 
Posts: 3
Joined: Sun Dec 14, 2008 8:43 pm

Postby MrSnowflake » Mon Dec 15, 2008 3:48 am

Leaving out the sleep all together?!

Or use the Handler.postDelayed();

btw: [mod]This is not a tutorial!!!!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby JoshuaD » Mon Dec 15, 2008 8:12 am

Hrm. Handler.postDelayed() is probably the best bet, but it turns out my updateThread is too intensive for the phone. I'll have to play with it and fix it.

Thanks for the help snowflake.
JoshuaD
Freshman
Freshman
 
Posts: 3
Joined: Sun Dec 14, 2008 8:43 pm

Postby MrSnowflake » Mon Dec 15, 2008 1:18 pm

You are very much welcome.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests