其他分享
首页 > 其他分享> > c – 我正确计算我的FPS吗?

c – 我正确计算我的FPS吗?

作者:互联网

所以我想知道我是否正确计算我的FPS:

Uint32 delayFrom(float startTime, float endTime){
    return(endTime - startTime );
}


int main(){
    int numFrames = 0;
    Uint32 startTime = SDL_GetTicks();
    while(!done){
        frameTime = 0;
        float fps = ( numFrames/(float)(SDL_GetTicks() - startTime) )*1000;
        cout << fps << endl;
        SDL_Delay(delayFrom(frameTime, 1/60));
        ++numFrames;
    }
}

解决方法:

int main() {
  int numFrames = 0;
  Uint32 startTime = SDL_GetTicks();
  while (!done) {
    ++numFrames;
    Uint32 elapsedMS = SDL_GetTicks() - startTime; // Time since start of loop
    if (elapsedMS) { // Skip this the first frame
      double elapsedSeconds = elapsedMS / 1000.0; // Convert to seconds
      double fps = numFrames / elapsedSeconds; // FPS is Frames / Seconds
      cout << fps << endl; 
    }
    SDL_Delay(1.0/60.0); // Use floating point division, not integer
  }
}

标签:frame-rate,c,sdl
来源: https://codeday.me/bug/20190827/1736375.html