Book Image

3D Game Development with Microsoft Silverlight 3: Beginner's Guide

By : Gaston C. Hillar
Book Image

3D Game Development with Microsoft Silverlight 3: Beginner's Guide

By: Gaston C. Hillar

Overview of this book

Microsoft Silverlight is a programmable web browser plug-in that enables the animation, vector graphics, and audio-video playback features that characterize Rich Internet Applications. Silverlight is a great (and growing) RIA platform and games are the next level to exploit in it. But it doesn't offer 3D capabilities out of the box and integrating a 3D engine can involve lot of complex mathematics and matrix algebra. This book will help C# developers to get their fingers on the pulse of 3D in Silverlight. This book uses Balder, an open source 3D engine offering 3D capabilities for Silverlight 3. It leaves out boring matrix algebra and complex 3D mathematics. By the end of the book you will have explored the entire engine, and will be able to design and program your own 3D games with ease! The book begins by introducing you to the fundamental concepts of 2D games and then drives you into the 3D world, using easy-to-follow, step-by-step examples. The book employs amazing graphics and impressive performance, and increasingly adds more features to a 3D game giving you a rich interactive experience. By following the practical examples in this book, you will learn the important concepts, from the creation of the initial models, up to the addition of physics and artificial intelligence. The book helps you to provide realistic behaviors for 3D characters by enveloping models with different textures, using lights to create effects, animating multiple 3D characters using a physics engine (Farseer Physics Engine), and simulating real-life physics. Videos, music, and sounds associated with specific events offer the final touches to the 3D game development learning experience.
Table of Contents (21 chapters)
3D Game Development with Microsoft Silverlight 3
Credits
About the Author
Acknowledgement
About the Reviewer
Preface
Pop quiz—Answers

Time for action—building and running the XBAP WPF applications


Now, you want to see the ghost moving on the screen as an XBAP WPF application while the mouse pointer changes its position. In order to do this, we must add some code to show the ghost and to move it. We will add both XAML and C# code as follows:

  1. 1. Stay in the SilverlightMonsterXBAP project.

  2. 2. Open the XAML code for Page1.xaml (double-click on it in the Solution Explorer) and replace the existing code with the following:

    <Page x:Class="SilverlightMonsterXBAP.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml
    /presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page1"
    Cursor="None">
    <Canvas Width="1366" Height="768" MouseMove="Ghost_MouseMove"
    xmlns:SilverlightMonsterXBAP=
    "clr-namespace:SilverlightMonsterXBAP">
    <Canvas x:Name="cnvMovementTest" Width="1366" Height="768"
    Background="Bisque">
    <SilverlightMonsterXBAP:Ghost Canvas.Left="10"
    Canvas.Top="10" x:Name="ghost"/>
    </Canvas>
    </Canvas>
    </Page>
    
  3. 3. You will see the ghost appearing in the upper-left corner of the page in the designer.

  4. 4. Expand Page1.xaml in the Solution Explorer and open Page1.xaml.cs (double-click on it). We need to add an event handler to move the ghost on the screen to the position to which the mouse has moved.

  5. 5. Add the same lines of code previously explained for the Silverlight version in the public partial class Page1 : Page to program the event handler for the MouseMove event.

  6. 6. Build and run the solution. The default web browser will appear showing a bisque background and the ghost will move following the mouse pointer. This time, it is running an XBAP WPF application, as shown in the following picture:

What just happened?

The ghost appeared in the web browser and moved through the bisque rectangle as if it were a mouse pointer. The application looked very similar to its Silverlight version. However, in this case, you have access to most of the WPF features and controls. You created and ran an XBAP WPF application in the web browser.

Note

XBAP WPF applications require more time to start than Silverlight applications. Therefore, we should not confuse Silverlight with XBAP WPF. They are two different technologies. However, we can transform a Silverlight application to an XBAP WPF application by making small changes to the source code, as shown in this example.

First, we changed the XAML code for Page1.xaml. We made some changes to do the following:

  • Hide the mouse pointer (the cursor) using the following code:

    <Page x:Class="SilverlightMonsterXBAP.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page1"
    Cursor="None">
    
  • Replace the Grid definition with a Canvas and indicate a MouseMove event handler:

    MouseMove="Ghost_MouseMove"
    
  • Reference the SilverlightMonsterXBAP namespace to allow access to the definitions specified in it:

    xmlns:SilverlightMonsterXBAP="clr-namespace:SilverlightMonsterXBAP">
    

Next, we added a Canvas named cnvMovementTest with a resolution of 1366X768 pixels (width and height), a bisque background, and showed an instance of Ghost (SilverlightMonster:Ghost) named ghost located on the point (10, 10) by taking into account both the Canvas and its upper-left corner:

<Canvas x:Name="cnvMovementTest" Width="1366" Height="768" Background="Bisque">
<SilverlightMonsterXBAP:Ghost Canvas.Left="10" Canvas.Top="10"
x:Name="ghost"/>
</Canvas>

Once the ghost is shown using XAML code, we had to program the code for the event handler that triggered when the mouse moved on the main page's UserControl. We followed the same procedure previously explained in the Silverlight version of this application.

Have a go hero—creating a prototype for the remake

You receive an email from the game contest. They require each participant to send a Silverlight prototype of the first scene of the game without movement—a still snapshot.

Using the scalable vector graphics in an XAML format prepared for the Invader game, create a new Silverlight application that reproduces the first screen of the game with the aliens organized in five rows and eleven columns, the four tents, and the ship.

You can do it creating one UserControl for each vector graphics as we did for the ghost. Now, you can organize a nice Silverlight prototype by defining a Canvas for each alien and tent, and then for the ship using 2D coordinates.

Pop quiz—preparing the digital content

  1. 1. When scaling raster digital assets:

    • a. Pixelation becomes easily visible.

    • b. Pixelation is no problem.

    • c. We must previously perform a conversion to XAML to avoid pixelation.

  2. 2. When scaling vector graphics:

    • a. We must previously perform a conversion to WMF to avoid pixelation.

    • b. Pixelation becomes easily visible.

    • c. Pixelation is no problem.

  3. 3. XamlPad is an utility whose main goal is:

    • a. To preview XAML content.

    • b. To convert WMF and EMF formats to XAML.

    • c. To test a Silverlight 3 application.

  4. 4. Silverlight 3 works great with raster digital assets in:

    • a. The TGA (Targa) format.

    • b. The PNG (Portable Network Graphics) format.

    • c. The XJT (Compressed XJT GIMP) format.

  5. 5. Silverlight 3 works great with vector graphics in:

    • a. The WMF (Windows Meta-File) format.

    • b. The CDR (CorelDRaw) format.

    • c. The XAML (eXtensible Application Markup Language) format.

  6. 6. Silverlight is:

    • a. A subset of WPF.

    • b. An extension to WPF.

    • c. A plugin to add more controls to the XBAP WPF applications.

  7. 7. XBAP WPF applications are:

    • a. The same as Silverlight applications (with just a different name).

    • b. .Net WPF applications that can run in a sandbox or inside a web browser with some security restrictions.

    • c. .Net WPF applications that can run on a desktop or in a web browser with no security restrictions.