Ezra Schwartz
Ezra Schwartz

Reputation: 23

Why is my XNA Game slow on another computer?

I'm trying to get an XNA game (4.0, Visual Studio 2015, .NET 4.0 Framework) I wrote on one computer to work on my laptop but the performance drops down to 2 in some parts and then goes back to 60 in others (by that I mean at certain different GameStates that I enumerated). To be clear, the computer I wrote it on works perfectly and stays at a constant 60 fps, yet the other computer lags terribly.

The memory/cpu usage is less than 25% of both of them so I don't think it has to do with the code, nevertheless here is what's being called during the Update method part that runs just a blank screen:

private void switchGameMode()
    {
        switch (state)
        {
            case GameState.preTitleScreen:
                if (playing != paganBackground)
                {
                    MediaPlayer.Stop();
                    playing = paganBackground;
                }
                MediaPlayer.Stop();
                surprisePreTitleScreen();
                break;

The rest is the end of a switch statement. The method is:

private void surprisePreTitleScreen()
    {
        if ((oldpad1.Buttons.Start == ButtonState.Released && pad1.Buttons.Start == ButtonState.Pressed) || (oldkeys.IsKeyUp(Keys.Enter) && keys.IsKeyDown(Keys.Enter)))
        {
            jeopardyTitle.Play();
            state = GameState.titleScreen;
        }
    }

And here is what is being drawn in the draw method:

protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Wheat);

        spriteBatch.Begin();

        switch(state)
        {
            case GameState.preTitleScreen:

                break;

After finishing the method and switch statement of course.

Does anyone have a suggestion as to what's wrong?

Upvotes: 0

Views: 192

Answers (1)

Monset
Monset

Reputation: 657

Due to not enough code presented, I must presume what is in question.
As far as I know, switch statemenst shouldnt slow your game too much. What could be the problem is that you are updating on every "Update" without the real time consideration. What I mean by that:
Lets say you have a player. In update method, you update his poition (Vector2) by 1 on the right. So, it will look something like this:


    protected override void Update (GameTime gameTime) 
    {
        palyer1.Position.X += 1;
    }

Now, that means that every time your processor gets to update your game, it will add 1 to the X of the position of your player. So, what if you have a slow processor? The 1 in the formula will be lets say 5, and that will look good "on your PC". But, you take the game to another PC, and it moves too fast, because the other PC has more powerful processor and it gets to update your game more often. Also, the fps will drop or increase if the background processess start and end often. This is what I presume that is in question.
The solution to this problem is simple. You call something that tells you how much real time passed from the last update. Your code will look like this now, and your player will move to the right the same amount on every PC.


    protected override void Update (GameTime gameTime) 
    {
        elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
        palyer1.Position.X += elapsedTime * ASD;
    }

Where ASD presents how many pixels to move per second
This way, every processor will multiply the time elapsed with the amount of movement you want to create, and will create the exact amount that you want, on every processor.
As for the sound, it will pretty much always be laggy on slow computers.

Upvotes: 1

Related Questions