Question

I made an XNA image viewer, but it always redraws the scene, even if it's not changing, and it's making my netbook burn like hell, so I'd like it to pause drawing when nothing's changing.

Reducing framerate to 1 is one way to keep it cool, but it results in laggy output.

How do I prevent the redraw while there is no input?


This problem was solved, but another problem was found — the game consumes a lot of CPU when its window is in focus, but when it's not, it only takes about 1% of the CPU. See this question for details on how to solve this other problem:

How to reduce XNA game CPU usage while nothing worth computing is happening?

Was it helpful?

Solution

You can use the Game.SupressDraw method for this purpose. From the remarks at the link:

Call this method during Update to prevent any calls to Draw until after the next call to Update. This method can be used on small devices to conserve battery life if the display does not change as a result of Update. For example, if the screen is static with no background animations, the player input can be examined during Update to determine whether the player is performing any action. If no input is detected, this method allows the game to skip drawing until the next update.

As an example, the following code will only call the Draw function once. When I tested this I noticed that the CPU usage was high (70%) without the SuppressDraw call. When using SupressDraw, the CPU usage dropped dramatically (to less than 15%). Your numbers may vary.

public class Game1 : Microsoft.Xna.Framework.Game
{
    ...
    private bool _drawn = false;
    protected override void Update(GameTime gameTime)
    {
        if (_drawn)
            SuppressDraw();
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        /* draw stuff here */
        spriteBatch.End();

        _drawn = true;
    }
    ...
}

Note that SupressDraw only suppresses one Draw call. To prevent more calls to Draw, you have to continually call SupressDraw in Update. Hopefully that makes sense.

OTHER TIPS

As the accepted answer says, you can use SuppressDraw to avoid drawing an unchanged frame. Your Update-method will run regardless, however, which is why it still uses more CPU than you expect. To avoid using as much CPU, you need to somehow avoid updating things you know haven't changed since the last frame.

I assume you would know this the same way as you know when to suppress draw, so what you need to do is rearrange your Update-method like this:

private void Update(GameTime gameTime)
{
    var frameHasChanges = DetectChanges(); //This method you need to figure out yourself
    if (!frameHasChanges)
    {
        SuppressDraw();
        base.Update(gameTime);
        return;
    }

    //Do the rest of normal Update

}

As a final note; I have never had to use this in any game I have ever made. Which I feel suggests you may have some other, underlying problem with performance. I have, however, used many techniques to avoid rendering text and so on each frame (causes boxing/unboxing). Ask about this in comment if you want to know more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top