Speed: in which MSDOS meets Windows Phone 8

Originally posted to Shawn Hargreaves Blog on MSDN, Thursday, November 1, 2012

Inspired by long standing XNA team tradition, the Windows Phone graphics team recently spent some time using our product and trying to build some apps.  One of our goals in supporting native C++ was to make it easier to port existing software and frameworks to the platform, so I thought, what is the most legacy thing I could possibly try to port?  For App Week I took a game called Speed, which I wrote 13 years ago (last century!) in C (yep, that's C, not C++) using an MSDOS library called Allegro, and ported it to Windows Phone 8.

Check it out:

t


ety


5u


rwr


If you want to play this yourself, or look at the code, you can download it here:

Speed.zip

You'll need a copy of DirectXTK to build it (unzip into a folder named DirectXTK in the same location as the Speed.sln file).  Although it runs ok in the Windows Phone emulator, you really need an actual device to play this properly.  Touch input using left, right and center of the screen, not to mention tilt sensor input, doesn't work so well via a mouse!

Porting this turned out to be pleasingly straightforward.  The OriginalGame folder contains, can you guess? Yup, that's the original C code, almost entirely unchanged since 1999.  Meanwhile, allegro.h and allegro.cpp (in the PhonePort folder) provide an emulation layer which translates the necessary subset of the old Allegro API into D3D11, XAudio2 and WinRT calls.  It uses DirectXTK SpriteFont for the text rendering, PrimitiveBatch and BasicEffect for all other graphics, XAudio2 for the music and sound effects, and WinRT ThreadPool::RunAsync to emulate Allegro timer interrupts.

A silly-but-I-think-kinda-neat thing about this game is that the music is entirely procedural.  The init_music() function (in OriginalGame\sound.c) generates a set of single cycle waveform samples using sine, square, and sawtooth waves.  Then the music_player() function is installed as a custom timer interrupt handler.  This walks through a set of tables describing the pitch and duration of each note, and calls the Allegro voice functions to trigger or stop playback as appropriate.  I didn't change this code at all, yet my emulation layer is good enough to play the music exactly as it sounded in the original.

For bonus points I added a bloom postprocess (implemented in PhonePort\Game.cpp) because, hey, this is 2012 after all :-)

Blog index   -   Back to my homepage