// Silverlight Speed Tutorial C# source-code
// Written by John Bower, Deep Space Objects
// =========================================
using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace SilverlightSpeed
{
public partial class Page : Canvas
{
Image imgSprite = new Image(); // Create new Image
TextBlock txtFPS = new TextBlock(); // Create new TextBlock
Canvas cParent; // Create cParent
Storyboard sbLoop = new Storyboard();
Int32 intX, intY;
Boolean movingLeft, movingUp;
Int32 FrameRate, LastTime;
// Page_Loaded - Called when the canvas is loaded
// ================================================
public void Page_Loaded(object o, EventArgs e)
{
// Required to initialize variables
InitializeComponent();
// set rootCanvas to the canvas that called this function
cParent = (Canvas)FindName("parentCanvas");
// Create Image
attachImage();
// Create FPS TextBlock
attachFPSText();
// Create Storyboard
attachStoryboard();
}
// Image attachment - Adds an Image object ot the canvas
// =====================================================
private void attachImage()
{
// Set up the new Image object
imgSprite.Source = new Uri("images/car.jpg", UriKind.Relative);
// Add the Image to the Parent Canvas
cParent.Children.Add(imgSprite);
}
// FPS attachment - Adds FPS textblock to the canvas
// =================================================
private void attachFPSText()
{
// add textblock to canvas
cParent.Children.Add(txtFPS);
}
// Storyboard attachment - Adds the storyboard to the Canvas
// =========================================================
private void attachStoryboard()
{
// set up the new Storyboard
sbLoop.SetValue(Storyboard.NameProperty, "loop");
// Add the Storyboard to the Canvas
cParent.Resources.Add(sbLoop);
sbLoop.Completed += new EventHandler(loop_Completed);
// Start playing the Storyboard loop
sbLoop.Begin();
}
// Loop function - Handles storyboard.completed event
// ==================================================
private void loop_Completed(Object sender, EventArgs e)
{
// Update frame rate
updateFPS();
// Y-Axis logic
if (movingUp)
{
intY -= 1; // update Y
if (intY < 0) // bounce
{
movingUp = !movingUp;
}
}
else
{
intY += 1; // update Y
if (intY > (cParent.Height - imgSprite.Height))
{ // bounce
movingUp = !movingUp;
}
}
// X-Axis logic
if (movingLeft)
{
intX -= 2; // update X
if (intX < 0)
{ // bounce
movingLeft = !movingLeft;
}
}
else
{
intX += 2; // update X
if (intX > (cParent.Width - imgSprite.Width))
{ // bounce
movingLeft = !movingLeft;
}
}
// Update the Image object's position
imgSprite.SetValue(Canvas.LeftProperty, intX);
imgSprite.SetValue(Canvas.TopProperty, intY);
// Restart the Storyboard
sbLoop.Begin();
}
// Frames per second - Updates FPS variables
// =========================================
private void updateFPS()
{
if (LastTime > DateTime.Now.Millisecond)
{
txtFPS.Text = "FPS: " + FrameRate;
FrameRate = 0;
LastTime = DateTime.Now.Millisecond;
}
else
{
LastTime = DateTime.Now.Millisecond;
FrameRate += 1;
}
}
}
}