Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #101 – Silverlight 3 Beta 1 Feature Summary

Unless you have had your head under a rock you probably have heard Silverlight 3 Beta 1 has been released! If you haven’t already, please install it from http://silverlight.net/getstarted/silverlight3/default.aspx

This release comes packed with some of the greatest features to date. If you are a game developer, you will be happy to hear the following features below are now included in Silverlight 3. I will be writing detailed blogs on a number of these to help you apply them to your application.

Image Opened

In Silverlight 2 it was difficult to accurately determine when a image was completely opened. This case was often needed to determine the actual dimensions of the image. The problem occurred because the DownloadProgress is fired at 100% before the image is fully decoded. The solve this, a new event called ImageOpened is available. Now you can be guaranteed to get the right size for the image. Example:

private void LoadImage(string fileName)
{
    Image img = new Image();
    Uri uri = new Uri(fileName, UriKind.Relative);
    img.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
    img.ImageOpened += new EventHandler<RoutedEventArgs>(Image_ImageOpened);
}
 
void Image_ImageOpened(object sender, RoutedEventArgs e)
{
    Image img = (Image)sender;
    BitmapImage bi = (BitmapImage)img.Source;
    double width = bi.PixelWidth;
    double height = bi.PixelWidth != _radius)
}

XAP Compression

Optimization were made to drastically reduce the size of the XAP file. You can expect the XAP file size to now be on par (if not better) than Windows zipping. Games often come packed with a lot of content so obviously the smaller your XAP the faster your customers will be able to download your game.

Perspective 3D

Perspective 3D transforms can now be applied to Silverlight controls via the PlaneProjection property. This allows you to rotate a 2D control in a 3D plane.

image

Example code that shows how to do this:

<Image Source="Man.png">
    <Image.Projection>
        <PlaneProjection RotationX="45"></PlaneProjection>
    </Image.Projection>
</Image>

Pixel Shaders 

Pixel shaders allow you to apply effects to controls such as images. There are two built in effects available by default: Drop Shadow and Blur. In addition, you can write and apply your own custom effects.

The following images show the result of applying pixel shader effects to them.image

Example code to accomplish this through a built in shader:

<Image Source="dwarf.jpg">
    <Image.Effect>
        <BlurEffect></BlurEffect>
    </Image.Effect>
</Image>

Binding 

You can now bind controls to properties of each other. For example, the Text of a TextBlock can bind to the Text of a TextBox. Or, a TextBox Text can bind to the Value of Slider. Example:

<StackPanel>
    <TextBox x:Name="UserText"></TextBox>            
    <TextBlock Text="{Binding Text, ElementName=UserText}"></TextBlock>
</StackPanel>

Whatever someone types in the TextBox is automatically reflected in the TextBlock.

 

Bitmap API

Bitmap APIs are now available through a class called WriteableBitmap. This method takes as parameters the surface width and height and the pixel format.

Example code:

WriteableBitmap wb = new WriteableBitmap(0, 0, PixelFormats.Bgr32);
wb.Render(img, new ScaleTransform());
wb.Lock();
// walk through WriteableBitmap[]
wb.Invalidate();
wb.Unlock();

 

Hardware Acceleration

Silverlight now makes use of the GPU (Graphics Processing Unit) for rendering which frees up CPU time for other tasks. The GPU typically sits on a video card. It is a dedicated graphics processing device that is used for a variety of tasks such as calculating floating point operations.

Animated Text Performance Improvements

A property called TextRenderingMode now allows you specify RenderForAnimation. This turns off optimizations that could directly affect text animation such as scaling, rotating, etc. The result is a smother, more peformant animation of text. Example on how to set this property:

RenderOptions.SetTextRenderingMode(MyTextbox, TextRenderingMode.RenderForAnimation);

Network Monitoring API

There are now API’s that allow you to monitor the status of the network. To see if a network is available you can call:

bool isConnected = NetworkInterface.GetIsNetworkAvailable();

You can also hook up an event that will fire when the network status changes:

NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);

To access these methods you will need to reference the namespace System.Net.NetworkInformation;

SaveFileDialog

Finally we are able to save files in Silverlight! Calling this method will invoke a dialog that will allow you to specify a file to save to. The method returns a pointer to a Stream that you can than write to. Example code:

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "map files (*.xml)|*.xml|All files (*.*)|*.*";
sfd.ShowDialog();
System.IO.Stream stream = sfd.OpenFile();
 
// Save your data here
 
stream.Flush();
stream.Close();

Make certain to flush and close the stream or the file will not be created.

CaretBrush

The CaretBrush is useful because you can now change the appearance of the caret. This was a problem if, for example, you wanted to have a background color of a textbox set to back. The cursor was also black so it would not appear.  Example on how to set it:

<TextBox Foreground="White" Background="Black" Width="200" Text="Hello">
    <TextBox.CaretBrush>
        <SolidColorBrush Color="White"></SolidColorBrush>
    </TextBox.CaretBrush>
</TextBox>

Resulting Screenshot (notice the white caret):

image

Local Connection

This feature allows two separate Silverlight applications to communicate with each other on the client side without the need to roundtrip to the server.

Implementing this is fairly straight forward. Start by creating two separate Silverlight applications. To one, add this code that will listen for a message:

LocalMessageReceiver receiver = new LocalMessageReceiver("R2D2");
receiver.MessageReceived += new EventHandler<MessageReceivedEventArgs>(receiver_MessageReceived);
receiver.Listen();

You will need to reference System.Windows.Messaging to make these calls. The constructor for LocalMessageReceiver takes any unique identifier.

To the other application add this code that will send a message to the first Silverlight application:

LocalMessageSender sender = new LocalMessageSender("R2D2");
sender.SendCompleted += new EventHandler<SendCompletedEventArgs>(sender_SendCompleted);
sender.SendAsync("Hello World!");

 

Navigation

Navigation is now supported in Silverlight. This allows you to do things like hook up navigation in your Silverlight application to the Back and Forward buttons of your browser. A new template called “Silverlight Navigation Application” is available that demonstrates how to do this.

Essentially to accomplish this you will need to put your main page in a <navigation:Frame>. You add history by this call:

this.Frame.Navigate(new Uri(currentPage, UriKind.Relative));

Then, when the back and forward buttons of your browse are clicked this frame object will intercept them and display the correct page.

System Colors

You can now get access to a users settings for their System Colors. These are available through System.Windows.SystemColors.*. Example: System.Windows.SystemColors.ActiveBorderColor. This will allow ou present a contrast that matches  a user's settings.

Other

  1. Out of Browser Experience – Allows you to run your Silverlight application as a desktop application.
  2. Assembly Caching – Enables non-core runtime extensions to be cached.
  3. ManagedResourceDictionary – Manage resources files in separate files.
  4. New controls – A number of new controls have been added.
  5. H.264 / AAC Media Playback – Newly supported Media format.
  6. Animation Easing Effects – Grants more visual effects including smoother transitions.
  7. Media Logging – Allows you to collect data about a customer and their meda experience. The data is posted back to one or many servers for analysis.

Thanks,

--Mike Snow

Comments

Microsoft Weblogs said:

Unless you have had your head under a rock you probably have heard Silverlight 3 Beta 1 has been released

# March 20, 2009 5:46 PM

Silverlight 3 Beta 1 Feature Summary | DavideZordan.net said:

Pingback from  Silverlight 3 Beta 1 Feature Summary | DavideZordan.net

# March 24, 2009 9:55 AM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# March 27, 2009 12:39 AM

Visual Web Developer Team Blog said:

Most Recent Posts: Silverlight Tip of the Day #106 - Setting Default Browser from within VS Silverlight

# April 6, 2009 6:13 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

# April 6, 2009 6:20 PM

NewsPeeps said:

Thank you for submitting this cool story - Trackback from NewsPeeps

# August 8, 2009 6:36 PM