Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
Thursday
Apr012010

How can I do Data Binding in a Windows Phone Application?

There is a very simple example of databinding in my presentation for DevDays 2010. It is used in an application called DamageCalc that receives input prices and calculates the actual “damage” to your bank account in your local currency, including any state taxes. You can find the sample application and the slide deck for this presentation here.

Thursday
Apr012010

How do I create a Frame Counter in my Program?

A frame counter will give you a feel for the graphical performance of your program. It simply tells you how many frames per second are being displayed. Creating a frame counter for a Silverlight application is easy, simply add this line to the constructor of your main page class:

Application.Current.Host.Settings.EnableFrameRateCounter = true;

The program will now display a frame counter in the top of the screen.

If you are writing XNA you will need to implement a frame counter of your own. You can use my sample here if you prefer. This creates a simple particle system, you can change the number of particles and watch what happens to the frame counter in the top left hand corner.

Note that the frame counter resets some of the rendering properties when it uses SpriteBatch to draw the text. You might find that it makes your 3D games look wrong if you add it to them. The solution is to make sure that you reset all your render properties before each draw, and not just at the start of the program.

Sunday
Apr112010

How do I get the IP address of my Windows Phone Emulator?

You don’t. If you are used to the Windows Mobile way of doing things, where the emulator has an IP address which is different from the PC it is running, then the current version of the Windows Phone emulator is different. It is best viewed as a process running on your machine, and this means that it has the same IP address as your machine.

This means that you can connect to localhost from your emulator and get back to services hosted on your PC (although if this address gets baked into any of your configuration files for your program you can look forward to fun and games when you try to deploy to a real device).

Thursday
Apr012010

How do I play a sound effect in Silverlight?

There are two ways to play a sounds in Silverlight. You can play the sound from a one media item at a time(usual for things like  background music or videos etc.) or you can use the XNA SoundEffect class.

The first step is to add the XNA libraries to your game. Open up the References tab in your project and add the Microsoft.XNA.Framework reference.

Now add a couple of using statements to your program to make the XNA classes easier to get hold of:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

The sound you want to play has to be added to your project. You don’t use the Content Manager in Silverlight though, as there isn’t one.  Instead you add the item as content. Make sure that the build action for the resource is set to Content. As with old school XNA, a SoundEffect can be wav file. I just dropped a beep file into the root level:

image

You can also see here that I’ve loaded the framework. Now I can load the SoundEffect in my game:

SoundEffect beep =
             SoundEffect.FromStream(TitleContainer.OpenStream("beep.wav"));

The static FromSteam method will fetch the effect from a stream. The TitleContainer object fills in for the Content Manager in XNA. If you want to put your sounds into a folder you can, but you must give the path (separated using the / character)

image

Sounds folder as part of the project

SoundEffect beep =
SoundEffect.FromStream(TitleContainer.OpenStream("Sounds/beep.wav"));

Now I can play my beep sound in the usual way:

beep.Play();

This is the boring form of the Play method call. You can also control the volume, pitch and left-right panning of the sound as well:

beep.Play(
    0.5f,   // half volume
    1.00f,   // one octave higher (-1 is 1 octave lower)
    -1.0f); // on the left (+1 is the right)

With the current version of the emulator there is a known issue if you try to raise the pitch more than 0.5 or so, this can cause the pitch to change randomly.

Thursday
Apr012010

How do I use the Accelerometer in the Windows Phone Emulator?

There is no native XNA support for the accelerometer in Windows Phone. It seems to have been moved into a different sensors class. I hope it doesn’t stay there. The accelerometer support in the Zune HD is fantastically easy to use and well in keeping with the other inputs.

I’ve written a tiny wrapper class that implements the accelerometer using the new sensor interface, but I really think this should really be put back into XNA, as event driven stuff really doesn’t sit well with the XNA philosophy of polling:

public class AccelerometerState
{
    public Vector3 Acceleration;

    public AccelerometerState()
    {
    }
}

public class Accelerometer
{
    static Accelerometer()
    {
        AccelerometerSensor.Default.ReadingChanged +=
            new EventHandler<AccelerometerReadingAsyncEventArgs>
                     Default_ReadingChanged);
    }

    static AccelerometerState state = new AccelerometerState();

    static void Default_ReadingChanged(object sender,
                                      AccelerometerReadingAsyncEventArgs e)
    {
        state.Acceleration.X = (float)e.Value.Value.X;
        state.Acceleration.Y = (float)e.Value.Value.Y;
        state.Acceleration.Z = (float)e.Value.Value.Z;
    }

    public static AccelerometerState GetState()
    {
        return state;
    }
}

You need to add the Microsoft.Devices.Sensors library to your project references to make this work.

I have also created an accelerometer simulator which uses an XNA gamepad to generate values which are hosted on a local web site and then consumed by a webclient in an XNA game on the phone. This works reasonably well. You can find the code for the XNA host and a game which consumes this here. Note that for this to work you must run the XNA program in Administraotr mode. You will also have to hard wire the IP address of the PC running the Windows Phone emulator into the XNA game running on the phone, so that it can get find the server.