Escape Characters in XNA fonts

DSCF2130.jpg

I’m making a sister application for the PlayMusic application. The PlayMusic program doesn’t do much, it just starts playing a random playlist when you run it. Not a lot of functionality, but it does mean you can start music playing by issuing a spoken command. I’ve had nearly a thousand downloads up to now, so a few people must find it useful.

The PlayMeMore application will do something similar. When you run it the program will find the album that the currently playing song is part of, and then play that album. I want this because frequently on random play I hear a track that I’ve not heard for a while, and fancy hearing the album it came from. I thought it would be nice to be able to say “Start PlayMeMore” and have it do this for you.

I’ve written the program in XNA because I like XNA and I might add a visualisation or two later. The program displays the name of the album when it starts it. By a wonderful quick of fate the first album it tried to play was Bête Noire by Bryan Ferry (darned good album too). And the program crashed. Any idea why? Took me a few minutes to work it out. It has to do with SpriteFonts.

When you use a font in XNA it builds a raster with the character designs on it. The font requirements are given in an little XML file that specifies the TrueType font to be used for the character designs. This font also specifies which character codes are rendered. And the default character codes don’t include the character ê. The fix is simple enough, open the file and make the end value a bit bigger.

    <CharacterRegions>
      <CharacterRegion>
        <Start>&#32;</Start>
        <End>&#255;</End>
      </CharacterRegion>
    </CharacterRegions>

Normally the end value is 126, but this does not include accented characters like ê. Making the value 255 seems to fix the problem. And I’ve put a try-catch around my DrawString calls in case I hit any music info that has any weird characters in it. Like the artist formally known as Prince….