SuppressDraw in mixed XNA / Silverlight projects on Windows Phone

How to suppress drawing when mixing Silverlight and XNA on Windows Phone

If your game is paused, or there just isn’t anything happening at the moment (like in between turns in a turn-based game)  you can avoid drawing graphics to save battery.

The old way of doing this in XNA was by calling SuppressDraw() on the Game class. For the mixed XNA/Silverlight template for Windows Phone, there is no Game class available though.

The way to do it is to call the SuppressFrame() method on the GameTimer class, and you do this from your OnUpdate method.

Calling SuppressFrame() will:

Supresses all remaining FrameAction, Update and Draw events from firing this frame for all GameTimers.

 

Silverlight rendering will also be halted!

Take heed if you are drawing anything with Silverlight as this also will be halted when you call SuppressFrame(). This means any currently active story board animations will halt etc.

 

Example code calling SuppressFrame()

     /// <summary>
      /// Allows the page to run logic such as updating the world,
      /// checking for collisions, gathering input, and playing audio.
      /// </summary>
      private void OnUpdate(object sender, GameTimerEventArgs e) {
         // TODO: Add your update logic here
         HandleTouch();
         HandleEvents();
         if (NothingHasUpdated() || IsPaused) {
            GameTimer.SuppressFrame();
         }
      }

Check out some other XNA articles here

XML serialization in the Silverlight / XNA template for Windows Phone 7.1
XNA Error loading content, file not found

Related posts: