Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pause the redraw in XNA?

Tags:

c#

throttling

xna

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?

like image 678
user1306322 Avatar asked May 15 '13 00:05

user1306322


1 Answers

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.

like image 168
chue x Avatar answered Oct 17 '22 05:10

chue x