2

XNA 4.0 を少し試して、チュートリアルに従い、非常に基本的なもの (三角形やいくつかの線など ;-)) を作成しています。これを行っているときに、すべてのアプリケーションが (Fraps を使用して) 50 ~ 51 fps を超えて実行されないことに気付きました。低速のコンピューターまたはグラフィック カード (Ati HD4870) で重いプログラムを実行しているわけではありません。XNA と関係があるはずです (ゲームはここで問題なく実行されます)。

現在、XNA について読んだすべての記事では、デフォルトの更新頻度は 1 秒あたり 60 回であると書かれており、それを実現したいと考えています。

  • フルスクリーンでもウィンドウ表示でも同じ
  • SynchronizeWithVerticalRetrace を false または true に設定した場合: 同じ
  • Visual Studio なしでプログラムを実行すると、41 fps しか得られません。
  • TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 10);fpsを使用して更新頻度をオーバーライドすると、大幅に上がります。10 は 10 ミリ秒を意味しますが、100 ではなく 83 fps しか得られません。1 ミリ秒では 850 fps になります。したがって、得られる fps と得られるはずの fps の偏差はかなり一貫しています。タイミングに問題があるように見えますか?

ここで何が問題なのかを知っている人や、安定した 60 fps を得るための提案がある人はいますか?

ありがとう!

4

2 に答える 2

4

FRAPS が正確な結果を出していない可能性はありませんか? 独自のフレームレート カウンターをゲームに追加して、その結果を確認しましたか? Shawn Hargreaves は、彼のブログでコード化したメソッドを持っています。これは、新しい空白の XNA ゲーム プロジェクトに簡単に追加できるはずです。

http://blogs.msdn.com/b/shawnhar/archive/2007/06/08/displaying-the-framerate.aspx

自分で計算した場合、同じ FPS が表示されますか、それとも別の方法で報告されますか?

于 2011-01-12T17:14:03.487 に答える
1

XNA 4 で次のコードをまとめたところ、60 fps がロックされています。システムで試してみて (適切なスプライト フォントを追加するだけで済みます)、60 fps も得られるかどうかを確認してください。その場合は、問題のコードに調整を加えて、同じ結果が得られるかどうかを確認してください。

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _60fps
{
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    SpriteFont OutputFont;
    float Fps = 0f;
    private const int NumberSamples = 50; //Update fps timer based on this number of samples
    int[] Samples = new int[NumberSamples];
    int CurrentSample = 0;
    int TicksAggregate = 0;
    int SecondSinceStart = 0;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        base.Initialize();
        graphics.SynchronizeWithVerticalRetrace = false;
        int DesiredFrameRate = 60;
        TargetElapsedTime = new TimeSpan(TimeSpan.TicksPerSecond / DesiredFrameRate);
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        OutputFont = Content.Load<SpriteFont>("MessageFont");
    }

    protected override void UnloadContent()
    {/* Nothing to do */}

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
            this.Exit();

        base.Update(gameTime);
    }

    private float Sum(int[] Samples)
    {
        float RetVal = 0f;
        for (int i = 0; i < Samples.Length; i++)
        {
            RetVal += (float)Samples[i];
        }
        return RetVal;
    }

    private Color ClearColor = Color.FromNonPremultiplied(20, 20, 40, 255);
    protected override void Draw(GameTime gameTime)
    {
        Samples[CurrentSample++] = (int)gameTime.ElapsedGameTime.Ticks;
        TicksAggregate += (int)gameTime.ElapsedGameTime.Ticks;
        if (TicksAggregate > TimeSpan.TicksPerSecond)
        {
            TicksAggregate -= (int)TimeSpan.TicksPerSecond;
            SecondSinceStart += 1;
        }
        if (CurrentSample == NumberSamples) //We are past the end of the array since the array is 0-based and NumberSamples is 1-based
        {
            float AverageFrameTime = Sum(Samples) / NumberSamples;
            Fps = TimeSpan.TicksPerSecond / AverageFrameTime;
            CurrentSample = 0;
        }

        GraphicsDevice.Clear(ClearColor);
        spriteBatch.Begin();
        if (Fps > 0)
        {
            spriteBatch.DrawString(OutputFont, string.Format("Current FPS: {0}\r\nTime since startup: {1}", Fps.ToString("000"), TimeSpan.FromSeconds(SecondSinceStart).ToString()), new Vector2(10,10), Color.White);
        }
        spriteBatch.End();
        base.Draw(gameTime);
    }
}
}
于 2011-10-25T18:37:42.837 に答える