Shawn Hargreaves Blog
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:
If you want to play this yourself, or look at the code, you can download it here:
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 :-)
Looking at the screenshots, I'm pretty sure I played this back when I was a tester at THQ... Wasn't this, like, an easter egg in MotoGP or something?
Heh, good catch Ian! Yes, you're exactly right, we included an Xbox port of this same game as an easter egg in MotoGP. If you turned your bike around on the training level, there was a side door leading down a ramp into a parking garage, and in the corner of that garage was an arcade cabinet. If you rode up and bumped it, you could play Speed on Xbox (complete with a LIVE hiscore table :-)
In case people are even remotely interested, Allegro hasn't been an MSDOS (only) library for quite a while. Heck we have iOS, and Android ports for Allegro 5. It's not your father's Allegro ;D
hahaha! Shawn I remember playing your entry in that weekend contest :) Good old days :)
Just offtopic. What books or resources will you suggest for learning DirectX to 16 y.o. programmer who is quite good in C#, but not so good in C++ ?
Bob Taco made a great C++ intro for people who already know C#: geekswithblogs.net/.../c-to-c-ndash-a-somewhat-short-guide.aspx