为什么我的 MonoGame 简单应用程序滞后?
Why my MonoGame simple app is lagging?
我用 MonoGame API 为 Android 编写了我的简单游戏。
我触摸应用程序 运行 相当不错的性能,但可以肯定的是,我制作了简单的 FPS 计数器,结果是 54-60 fps,我并不满意,所以我将我的 fps 计数器移动到 MonoGame 模板以查看部分我的代码导致了延迟,我发现即使是几乎清晰的应用程序 运行 也具有 54-60 fps 的相同速度。问题不是来自 GC,因为没有要收集的垃圾。有没有办法避免这些滞后?这是应用程序:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace perf_test
{
public class FRAPS
{
float fps_from;
int fps;
public int fps_toshow;
public float in_Secs;
public void add_frame()
{
fps_from += in_Secs;
if (fps_from > 1)
{
fps_toshow = fps;
fps = 0;
fps_from = 0;
}
fps++;
}
}
public class numbers
{
Texture2D[] texture;
int num1, num2;
Vector2 pos1, pos2, origin;
public int number;
public void load(ContentManager content)
{
texture = new Texture2D[10];
for (int i = 0; i < 10; i++)
{
texture[i] = content.Load<Texture2D>(("" + i));
}
num1 = 0;
num2 = 0;
number = 22;
pos1 = new Vector2(250, 50);
pos2 = new Vector2(270, 50);
origin = new Vector2(10, 10);
}
public void update()
{
num1 = 0;
num2 = 0;
int nuu = number;
if (nuu > 10)
{
for (int i = 0; i < 10; i++)
{
if (number >= (i * 10))
{
num1 = i;
}
else break;
}
}
nuu -= num1 * 10;
for (int i = 0; i < 10; i++)
{
if (nuu >= i)
{
num2 = i;
}
else break;
}
}
public void draw()
{
if (number > 9) Game1.spriteBatch.Draw(texture[num1], pos1, origin: origin);
Game1.spriteBatch.Draw(texture[num2], pos2, origin: origin);
}
}
public class Game1 : Game
{
GraphicsDeviceManager graphics;
public static SpriteBatch spriteBatch;
FRAPS fraps;
numbers Numb;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
fraps = new FRAPS();
Numb = new numbers();
graphics.IsFullScreen = true;
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 480;
graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Numb.load(Content);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
fraps.in_Secs = (float)gameTime.ElapsedGameTime.TotalSeconds;
fraps.add_frame();
Numb.number = fraps.fps_toshow;
Numb.update();
Numb.draw();
spriteBatch.End();
base.Draw(gameTime);
}
}
}
抱歉英语不好,我来自波兰。
设置
IsFixedTimeStep = false;
在您的 Game1 构造函数中。 XNA/Monogame 自动将您限制在 60 FPS。
我用 MonoGame API 为 Android 编写了我的简单游戏。 我触摸应用程序 运行 相当不错的性能,但可以肯定的是,我制作了简单的 FPS 计数器,结果是 54-60 fps,我并不满意,所以我将我的 fps 计数器移动到 MonoGame 模板以查看部分我的代码导致了延迟,我发现即使是几乎清晰的应用程序 运行 也具有 54-60 fps 的相同速度。问题不是来自 GC,因为没有要收集的垃圾。有没有办法避免这些滞后?这是应用程序:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace perf_test
{
public class FRAPS
{
float fps_from;
int fps;
public int fps_toshow;
public float in_Secs;
public void add_frame()
{
fps_from += in_Secs;
if (fps_from > 1)
{
fps_toshow = fps;
fps = 0;
fps_from = 0;
}
fps++;
}
}
public class numbers
{
Texture2D[] texture;
int num1, num2;
Vector2 pos1, pos2, origin;
public int number;
public void load(ContentManager content)
{
texture = new Texture2D[10];
for (int i = 0; i < 10; i++)
{
texture[i] = content.Load<Texture2D>(("" + i));
}
num1 = 0;
num2 = 0;
number = 22;
pos1 = new Vector2(250, 50);
pos2 = new Vector2(270, 50);
origin = new Vector2(10, 10);
}
public void update()
{
num1 = 0;
num2 = 0;
int nuu = number;
if (nuu > 10)
{
for (int i = 0; i < 10; i++)
{
if (number >= (i * 10))
{
num1 = i;
}
else break;
}
}
nuu -= num1 * 10;
for (int i = 0; i < 10; i++)
{
if (nuu >= i)
{
num2 = i;
}
else break;
}
}
public void draw()
{
if (number > 9) Game1.spriteBatch.Draw(texture[num1], pos1, origin: origin);
Game1.spriteBatch.Draw(texture[num2], pos2, origin: origin);
}
}
public class Game1 : Game
{
GraphicsDeviceManager graphics;
public static SpriteBatch spriteBatch;
FRAPS fraps;
numbers Numb;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
fraps = new FRAPS();
Numb = new numbers();
graphics.IsFullScreen = true;
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 480;
graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Numb.load(Content);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
fraps.in_Secs = (float)gameTime.ElapsedGameTime.TotalSeconds;
fraps.add_frame();
Numb.number = fraps.fps_toshow;
Numb.update();
Numb.draw();
spriteBatch.End();
base.Draw(gameTime);
}
}
}
抱歉英语不好,我来自波兰。
设置
IsFixedTimeStep = false;
在您的 Game1 构造函数中。 XNA/Monogame 自动将您限制在 60 FPS。