Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #5: Timers and the Main Game Loop

Main Game Loop

The main game loop is the heart of your game. In this function you will execute the majority of your game related tasks including:

  • Game AI
  • Animations - Updating objects and their positions.
  • Etc...

In this tip I will be demonstrating how to setup the main game loop using the DispatcherTimer. Alternate methods you should look into for your main game loop include:

  1. StoryboardTimer – See Tip of the Day #16
  2. CompositionTarget.Rendering -  See Tip of the Day #50.

My preferred method is using the CompositionTarget.Rendering event as the event is fired before the rendering of each frame.

Couple notes about DispatcherTimer:

  1. You will need to add a using statement: using System.Windows.Threading;
  2. If you set the timer interval to TimeSpan.Zero it will put the rate to be in sync with your monitors refresh rate.

Now, let’s take a look at the code. In the demo below we are simply displaying the number of frames per second our browser is rendering.

Page.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
 
namespace SilverlightApplication6
{
    public partial class Page : UserControl
    {
        private DispatcherTimer _gameLoopTimer;
        private int _fps = 0; 
        private DateTime _lastFPS = DateTime.Now;
 
        public Page()
        {
            _gameLoopTimer = new DispatcherTimer();
            _gameLoopTimer.Interval = TimeSpan.Zero;
            _gameLoopTimer.Tick += new EventHandler(MainGameLoop); 
            _gameLoopTimer.Start();
 
            InitializeComponent();
        }
        void MainGameLoop(object sender, EventArgs e)
        {
            _fps++; 
            if ((DateTime.Now - _lastFPS).Seconds >= 1) 
            { 
                FPS.Text = _fps.ToString() + " FPS"; 
                _fps = 0; 
                _lastFPS = DateTime.Now; 
            }
        }
    }
}

Page.xaml:

<UserControl x:Class="SilverlightApplication6.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock x:Name="FPS">Current FPS</TextBlock>
    </Grid>
</UserControl>

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Dew Drop - April 2, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - April 2, 2008 | Alvin Ashcraft's Morning Dew

# April 2, 2008 9:45 AM

14 Silverlight Tips | DavideZordan.net said:

Pingback from  14 Silverlight Tips | DavideZordan.net

# July 2, 2008 4:30 AM

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:24 AM

jin_u as blog » Blog Archive » ??????????????? ??? ?????? said:

Pingback from  jin_u as blog  &raquo; Blog Archive   &raquo; ??????????????? ??? ??????

# January 14, 2009 7:49 PM

79 Plymouth Duster Slant Six, 1970 Plymouth Duster Parts said:

Pingback from  79 Plymouth Duster Slant Six, 1970 Plymouth Duster Parts

# May 20, 2010 2:29 PM

Aveo Ep William Wilson, Kia Rio5 Chevrolet Aveo Small Hatchbacks said:

Pingback from  Aveo Ep William Wilson, Kia Rio5 Chevrolet Aveo Small Hatchbacks

# May 20, 2010 3:31 PM

R320 Discount Prices, R500 Auction R320 Cdi Used Mercedes Benz said:

Pingback from  R320 Discount Prices, R500 Auction R320 Cdi Used Mercedes Benz

# May 20, 2010 9:19 PM

Wolf 209 Magnum Break Action, W200 Auto Used Pickup Parts Magnum Mini said:

Pingback from  Wolf 209 Magnum Break Action, W200 Auto Used Pickup Parts Magnum Mini

# May 21, 2010 4:18 AM

Xl 7 Honda Crv 2007 Suzuki, 1997 Honda Crv Air Conditioning said:

Pingback from  Xl 7 Honda Crv 2007 Suzuki, 1997 Honda Crv Air Conditioning

# May 21, 2010 4:46 AM

Gmc V1500 Used Yukon Xl, Wholesale Silverado 2500 Gmc V1500 Suburban said:

Pingback from  Gmc V1500 Used Yukon Xl, Wholesale Silverado 2500 Gmc V1500 Suburban

# May 21, 2010 7:53 AM

1965 Malibu Parts, 1998 Malibu Problems said:

Pingback from  1965 Malibu Parts, 1998 Malibu Problems

# May 21, 2010 4:37 PM

Auto D400 Part, Chevy Part Dodge D400 said:

Pingback from  Auto D400 Part, Chevy Part Dodge D400

# May 21, 2010 9:19 PM

Chevy Chevette Discount Diesel, Chevy Chevette Forum said:

Pingback from  Chevy Chevette Discount Diesel, Chevy Chevette Forum

# May 21, 2010 11:09 PM

Cl55 Amg Part Vehicle 2002 Mercedes Benz, Part Used Mercedes Benz C55 Cl55 Amg said:

Pingback from  Cl55 Amg Part Vehicle 2002 Mercedes Benz, Part Used Mercedes Benz C55 Cl55 Amg

# May 21, 2010 11:16 PM

L300 2 Tail Lights, L300 2 Parts Filter Headlight Assembly said:

Pingback from  L300 2 Tail Lights, L300 2 Parts Filter Headlight Assembly

# May 22, 2010 11:04 AM

2006 Super Duty Headlight F250 F450, 2002 Crf450r Graphics Kit said:

Pingback from  2006 Super Duty Headlight F250 F450, 2002 Crf450r Graphics Kit

# May 22, 2010 12:57 PM

Radiator Flush B3000 Mazda B2500, Order Cornwall Pb250 said:

Pingback from  Radiator Flush B3000 Mazda B2500, Order Cornwall Pb250

# May 22, 2010 1:40 PM

1963 1964 Buick Riviera For Sale, Cadillac Gran Sport Discount 1966 Buick Riviera - 155.luna-atra.net said:

Pingback from  1963 1964 Buick Riviera For Sale, Cadillac Gran Sport Discount 1966 Buick Riviera - 155.luna-atra.net

# May 23, 2010 5:47 AM

F450 Front Bumper, Honda Crf450r Graphic Kit Crf 450x - 408.tijuanareader.com said:

Pingback from  F450 Front Bumper, Honda Crf450r Graphic Kit Crf 450x - 408.tijuanareader.com

# May 23, 2010 6:07 AM

1968 Gran Torino For Sale Elite, Ford Torino Catalog Restoration - 310.1fh.org said:

Pingback from  1968 Gran Torino For Sale Elite, Ford Torino Catalog Restoration - 310.1fh.org

# May 23, 2010 7:50 AM

D150 Used Truck Parts Accessories, E 150 Discount Replacement Tail Light Assembly Body - 345.cmanager.org said:

Pingback from  D150 Used Truck Parts Accessories, E 150 Discount Replacement Tail Light Assembly Body - 345.cmanager.org

# May 23, 2010 9:46 AM

2011 Volkswagen Passat Wagon, 1988 Volkswagen Vanagon - 86.rkwrh.com said:

Pingback from  2011 Volkswagen Passat Wagon, 1988 Volkswagen Vanagon - 86.rkwrh.com

# May 24, 2010 1:12 PM

1500 Radiator Core Chevrolet Silverado Avalanche 2500, Buy Chevrolet R2500 Suburban S10 Blazer Ball Joints - 296.computeronlinebingo.com said:

Pingback from  1500 Radiator Core Chevrolet Silverado Avalanche 2500, Buy Chevrolet R2500 Suburban S10 Blazer Ball Joints - 296.computeronlinebingo.com

# May 24, 2010 5:55 PM

760i Dvd, 760i Buy - 408.ja3ra.com said:

Pingback from  760i Dvd, 760i Buy - 408.ja3ra.com

# May 25, 2010 9:34 PM

LetsBlogAbout.NET said:

Silverlight Tips of the Day

# October 15, 2010 11:05 AM

kurumsal site tasarimi said:

For information, thank you very much, Mike. Worked for me too.

# July 3, 2011 1:13 PM