Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #50 – Main Game Loop Revisited!

In the recent release of Silverlight 2 RC0 there is a new event that fires once before the rendering of each frame in your browser. This rendering event is routed to the specified event handler after animation and layout have been applied to the composition tree. In addition, if changes to the visual tree force updates to the composition tree, your event handler is also called.

So, instead of having to use the DispatchTimer or Storyboard Timer,  you can now use this new event for your MainGameLoop(). To add the event all you have to do is make a call to:

CompositionTarget.Rendering += new EventHandler(MainGameLoop);

Looking back at our Snowflake demo, the following code was used to create a Storyboard timer to run the main loop:

Storyboard _snowflakeTimer = new Storyboard();
public
Page()

{
    InitializeComponent();
 
    _snowflakeTimer.Duration = TimeSpan.FromMilliseconds(0);
    _snowflakeTimer.Completed += new EventHandler(SnowFlakeTimer);
    _snowflakeTimer.Begin();
}
 
private void SnowFlakeTimer(object sender, EventArgs e)
{
    MoveSnowFlakes();
    CreateSnowFlakes();
}

The new way to do it would be:

public Page()
{
    InitializeComponent();
 
    CompositionTarget.Rendering += new EventHandler(SnowFlakeTimer);
}
 
private void SnowFlakeTimer(object sender, EventArgs e)
{
    MoveSnowFlakes();
    CreateSnowFlakes();
}

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

In the recent release of Silverlight 2 RC0 there is a new event that fires once before the rendering

# September 29, 2008 9:11 PM

preishuber said:

what is the benfit of this method?

when is should use it? performance?

# September 30, 2008 2:42 AM

2008 September 30 - Links for today « My (almost) Daily Links said:

Pingback from  2008 September 30 - Links for today « My (almost) Daily Links

# September 30, 2008 3:52 AM

Silverlight news for September 30, 2008 said:

Pingback from  Silverlight news for September 30, 2008

# September 30, 2008 9:45 AM

Community Blogs said:

In this post: Mike Snow, Bill Reiss, Jesse Liberty, Harsh Bardhan, Tim Heuer, Matthias Shapiro, and Jeff

# October 1, 2008 9:53 AM

#.think.in said:

#think.in Weekly Info-Dose #1 (29th Nov - 3rd Oct 2008)

# October 5, 2008 6:19 PM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #57 Title: How to Dynamically Load a Silverlight Control within another Silverlight

# October 8, 2008 11:05 PM

MS Tech News » Silverlight Tips of the Day ??? Week 8 said:

Pingback from  MS Tech News » Silverlight Tips of the Day ??? Week 8

# October 27, 2008 3:26 PM

MS Tech News » Silverlight Tips of the Day ??? Week 8 said:

Pingback from  MS Tech News » Silverlight Tips of the Day ??? Week 8

# October 27, 2008 3:32 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

Main Game Loop The main game loop is the heart of your game. In this function you will execute the majority

# November 18, 2008 1:56 PM

Silverlight Tips of the Day said:

Main Game Loop The main game loop is the heart of your game. In this function you will execute the majority

# November 18, 2008 1:58 PM

Silverlight Tip of the Day #16 - StoryBoard versus DispatcherTimer for Animation and Game Loops. - Silverlight Tips of the Day - Blog by Mike Snow said:

Pingback from  Silverlight Tip of the Day #16 - StoryBoard versus DispatcherTimer for Animation and Game Loops. - Silverlight Tips of the Day - Blog by Mike Snow

# November 18, 2008 7:14 PM

Silverlight Tips of the Day said:

In Tip of the Day #5 I discussed how to use the DispatcherTimer for your main game loop. However, a better

# November 18, 2008 7:15 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

Coding, training dog, playing Aikido, harmonica and.... said:

Trong Blog viết về SilverLight của mình, MikeSnow đã so sánh các phương pháp tạo Animation: 1- Sử dụng

# March 16, 2009 12:43 PM