简体   繁体   English

XNA 4.0 C#极限FPS

[英]XNA 4.0 C# limit FPS

I'm trying to limit the 'frames per second' of my Monogame XNA project, but my limitFrames function is inaccurate. 我试图限制Monogame XNA项目的“每秒帧数”,但我的limitFrames函数不准确。

For example, My project is running on 60 fps without a limiter. 例如,“我的项目”以60 fps的速度运行而没有限制。 But when I'm using the limiter and setting up the frameRateLimiter variable to 30 frames per second, the project's max fps is around 27. 但是,当我使用限制器并将frameRateLimiter变量设置为每秒30帧时,该项目的最大fps约为27。

Can someone figure the solution for it? 有人可以找到解决方案吗?

Frames Limiter Code 帧限制码

private float frameRateLimiter = 30f;
// ...

protected override void Draw(GameTime gameTime)
{
    float startDrawTime = gameTime.TotalGameTime.Milliseconds;
    limitFrames(startDrawTime, gameTime);
    base.Draw(gameTime);
}

private void limitFrames(float startDrawTime, GameTime gameTime)
{
    float durationTime = startDrawTime - gameTime.TotalGameTime.Milliseconds;
    // FRAME LIMITER
    if (frameRateLimiter != 0)
    {
        if (durationTime < (1000f / frameRateLimiter))
        {
            // *THE INACCERACY IS MIGHT COMING FROM THIS LINE*
            System.Threading.Thread.Sleep((int)((1000f / frameRateLimiter) - durationTime));
        }
     }
}

Frames Per Second Shower 每秒帧数

public class FramesPerSecond
{
    // The FPS
    public float FPS;

    // Variables that help for the calculation of the FPS
    private int currentFrame;
    private float currentTime;
    private float prevTime;
    private float timeDiffrence;
    private float FrameTimeAverage;
    private float[] frames_sample;
    const int NUM_SAMPLES = 20;

    public FramesPerSecond()
    {
        this.FPS = 0;
        this.frames_sample = new float[NUM_SAMPLES];
        this.prevTime = 0;
    }

    public void Update(GameTime gameTime)
    {
        this.currentTime = (float)gameTime.TotalGameTime.TotalMilliseconds;
        this.timeDiffrence = currentTime - prevTime;
        this.frames_sample[currentFrame % NUM_SAMPLES] = timeDiffrence;
        int count;
        if (this.currentFrame < NUM_SAMPLES)
        {
            count = currentFrame;
        }
        else
        {
            count = NUM_SAMPLES;
        }
        if (this.currentFrame % NUM_SAMPLES == 0)
        {
            this.FrameTimeAverage = 0;
            for (int i = 0; i < count; i++)
            {
                this.FrameTimeAverage += this.frames_sample[i];
            }
            if (count != 0)
            {
                this.FrameTimeAverage /= count;
            }
            if (this.FrameTimeAverage > 0)
            {
                this.FPS = (1000f / this.FrameTimeAverage);
            }
            else
            {
                this.FPS = 0;
            }
        }
        this.currentFrame++;
        this.prevTime = this.currentTime;
}

You don't need to re-invent the wheel. 您无需重新发明轮子。

MonoGame and XNA already have built-in variables that handle this for you. MonoGameXNA已经具有内置变量可以为您处理此问题。

To limit the framerate to a maximum of 30fps, set IsFixedTimeStep and TargetElapsedTime to the following in your Initialize() method: 若要将帧速率限制为最大30fps,请在Initialize()方法中将IsFixedTimeStepTargetElapsedTime设置为以下值:

IsFixedTimeStep = true;  //Force the game to update at fixed time intervals
TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0f);  //Set the time interval to 1/30th of a second

The FPS of your game can be evaluated using: 您可以使用以下方式评估游戏的FPS:

//"gameTime" is of type GameTime
float fps = 1f / gameTime.ElapsedGameTime.TotalSeconds;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM