Search code examples
c#xnamonogame

Why my MonoGame simple app is lagging?


I wrote my simple game for Android with MonoGame API. I touch the app was running with quite good performance but for sure I make simple FPS counter and the resoults was 54-60 fps with does not satisfied me, so i move my fps counter to the MonoGame template to see with part of my code was causing laggs and i found that even almost clear app run with the same speed of 54-60 fps. The problem was not comming from GC because there was no garbage to collect. Is there any option to avoid these lags? Here is the app:

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);
    }
}

}

Sorry for bad English, I am from Poland.


Solution

  • Set

    IsFixedTimeStep = false;
    

    in your Game1 constructor. XNA/Monogame automatically caps you at 60 FPS.