void calculateNextFrame(){
  if (mPrevFrameWhenNsec == 0) {
    mPrevFrameWhenNsec=System.nanoTime();
    mRecentTimeDeltaNext=-1;
    return;
  }
  long nowNsec=System.nanoTime();
  double curDeltaSec=(nowNsec - mPrevFrameWhenNsec) / NANOS_PER_SECOND;
  if (curDeltaSec > MAX_FRAME_DELTA_SEC) {
    Log.d(TAG,"delta time was " + curDeltaSec + ", capping at "+ MAX_FRAME_DELTA_SEC);
    curDeltaSec=MAX_FRAME_DELTA_SEC;
  }
  double deltaSec;
  if (FRAME_RATE_SMOOTHING) {
    if (mRecentTimeDeltaNext < 0) {
      for (int i=0; i < RECENT_TIME_DELTA_COUNT; i++) {
        mRecentTimeDelta[i]=curDeltaSec;
      }
      mRecentTimeDeltaNext=0;
    }
    mRecentTimeDelta[mRecentTimeDeltaNext]=curDeltaSec;
    mRecentTimeDeltaNext=(mRecentTimeDeltaNext + 1) % RECENT_TIME_DELTA_COUNT;
    deltaSec=0.0f;
    for (int i=0; i < RECENT_TIME_DELTA_COUNT; i++) {
      deltaSec+=mRecentTimeDelta[i];
    }
    deltaSec/=RECENT_TIME_DELTA_COUNT;
  }
 else {
    deltaSec=curDeltaSec;
  }
  boolean advanceFrame=true;
  if (mPauseDuration > 0.0f) {
    advanceFrame=false;
    if (mPauseDuration > deltaSec) {
      mPauseDuration-=deltaSec;
      if (mGamePlayState == GAME_PLAYING) {
        float[] colors=mPaddle.getColor();
        if (colors[0] == 0.0f) {
          mPaddle.setColor(1.0f,0.0f,1.0f);
        }
 else         if (colors[1] == 0.0f) {
          mPaddle.setColor(1.0f,1.0f,0.0f);
        }
 else {
          mPaddle.setColor(0.0f,1.0f,1.0f);
        }
      }
    }
 else {
      mPauseDuration=0.0f;
      mPaddle.setColor(1.0f,1.0f,1.0f);
    }
  }
switch (mGamePlayState) {
case GAME_INITIALIZING:
    mGamePlayState=GAME_READY;
  break;
case GAME_READY:
mGameStatusMessageNum=TextResources.READY;
if (advanceFrame) {
mGamePlayState=GAME_PLAYING;
mGameStatusMessageNum=TextResources.NO_MESSAGE;
setPauseTime(0.5f);
advanceFrame=false;
}
break;
case GAME_WON:
mGameStatusMessageNum=TextResources.WINNER;
mIsAnimating=false;
advanceFrame=false;
break;
case GAME_LOST:
mGameStatusMessageNum=TextResources.GAME_OVER;
mIsAnimating=false;
advanceFrame=false;
break;
case GAME_PLAYING:
break;
default :
Log.e(TAG,"GLITCH: bad state " + mGamePlayState);
break;
}
if (advanceFrame) {
int event=moveBall(deltaSec);
switch (event) {
case EVENT_LAST_BRICK:
mGamePlayState=GAME_WON;
SoundResources.play(SoundResources.PADDLE_HIT);
SoundResources.play(SoundResources.WALL_HIT);
SoundResources.play(SoundResources.BALL_LOST);
break;
case EVENT_BALL_LOST:
if (--mLivesRemaining == 0) {
mGamePlayState=GAME_LOST;
}
 else {
mGamePlayState=GAME_READY;
mGameStatusMessageNum=TextResources.READY;
setPauseTime(1.5f);
resetBall();
}
break;
case EVENT_NONE:
break;
default :
throw new RuntimeException("bad game event: " + event);
}
}
mPrevFrameWhenNsec=nowNsec;
}

void calculateNextFrame ( ) { if ( mPrevFrameWhenNsec == 0 ) { mPrevFrameWhenNsec = System . nanoTime ( ) ; mRecentTimeDeltaNext = - 1 ; return ; } long nowNsec = System . nanoTime ( ) ; double curDeltaSec = ( nowNsec - mPrevFrameWhenNsec ) / NANOS_PER_SECOND ; if ( curDeltaSec > MAX_FRAME_DELTA_SEC ) { Log . d ( TAG , "delta time was " + curDeltaSec + ", capping at " + MAX_FRAME_DELTA_SEC ) ; curDeltaSec = MAX_FRAME_DELTA_SEC ; } double deltaSec ; if ( FRAME_RATE_SMOOTHING ) { if ( mRecentTimeDeltaNext < 0 ) { for ( int i = 0 ; i < RECENT_TIME_DELTA_COUNT ; i ++ ) { mRecentTimeDelta [ i ] = curDeltaSec ; } mRecentTimeDeltaNext = 0 ; } mRecentTimeDelta [ mRecentTimeDeltaNext ] = curDeltaSec ; mRecentTimeDeltaNext = ( mRecentTimeDeltaNext + 1 ) % RECENT_TIME_DELTA_COUNT ; deltaSec = 0.0f ; for ( int i = 0 ; i < RECENT_TIME_DELTA_COUNT ; i ++ ) { deltaSec += mRecentTimeDelta [ i ] ; } deltaSec /= RECENT_TIME_DELTA_COUNT ; } else { deltaSec = curDeltaSec ; } boolean advanceFrame = true ; if ( mPauseDuration > 0.0f ) { advanceFrame = false ; if ( mPauseDuration > deltaSec ) { mPauseDuration -= deltaSec ; if ( mGamePlayState == GAME_PLAYING ) { float [ ] colors = mPaddle . getColor ( ) ; if ( colors [ 0 ] == 0.0f ) { mPaddle . setColor ( 1.0f , 0.0f , 1.0f ) ; } else if ( colors [ 1 ] == 0.0f ) { mPaddle . setColor ( 1.0f , 1.0f , 0.0f ) ; } else { mPaddle . setColor ( 0.0f , 1.0f , 1.0f ) ; } } } else { mPauseDuration = 0.0f ; mPaddle . setColor ( 1.0f , 1.0f , 1.0f ) ; } } switch ( mGamePlayState ) { case GAME_INITIALIZING : mGamePlayState = GAME_READY ; break ; case GAME_READY : mGameStatusMessageNum = TextResources . READY ; if ( advanceFrame ) { mGamePlayState = GAME_PLAYING ; mGameStatusMessageNum = TextResources . NO_MESSAGE ; setPauseTime ( 0.5f ) ; advanceFrame = false ; } break ; case GAME_WON : mGameStatusMessageNum = TextResources . WINNER ; mIsAnimating = false ; advanceFrame = false ; break ; case GAME_LOST : mGameStatusMessageNum = TextResources . GAME_OVER ; mIsAnimating = false ; advanceFrame = false ; break ; case GAME_PLAYING : break ; default : Log . e ( TAG , "GLITCH: bad state " + mGamePlayState ) ; break ; } if ( advanceFrame ) { int event = moveBall ( deltaSec ) ; switch ( event ) { case EVENT_LAST_BRICK : mGamePlayState = GAME_WON ; SoundResources . play ( SoundResources . PADDLE_HIT ) ; SoundResources . play ( SoundResources . WALL_HIT ) ; SoundResources . play ( SoundResources . BALL_LOST ) ; break ; case EVENT_BALL_LOST : if ( -- mLivesRemaining == 0 ) { mGamePlayState = GAME_LOST ; } else { mGamePlayState = GAME_READY ; mGameStatusMessageNum = TextResources . READY ; setPauseTime ( 1.5f ) ; resetBall ( ) ; } break ; case EVENT_NONE : break ; default : throw new RuntimeException ( "bad game event: " + event ) ; } } mPrevFrameWhenNsec = nowNsec ; }
updates all game state for the next frame . this primarily consists of moving the ball and checking for collisions .
