Monday, June 21, 2010

WP7: Primitive Cool

Imagine this: you've got a cool new mobile phone coming to market, and you want to compete on the world stage against the iPhones and Androids of the day. What do you need?

Slick multi-touch screen, check. Good MP3 experience, check. Um, telephone stuff I guess, check. Mmm... what about a gaming experience? Well, turns out it's Microsoft's turn to come out with a new phone, and since they've got this Xbox product line, they've got a bit of entertainment reputation to uphold. So, the new toy better have a heavy 3D processor or six built in. Check.

Picking up with WP7 (that's "windows phone 7" for those who aren't yet in the know) development kit, the first confrontation I got was from the language: everything's written in "C#". Which I do not know. This is kind of embarrassing, since I (a) used to work for Microsoft, the company that eventually came up with C#; and since I (b) am used to knowing all about any programming language I need. Whoops.

Fortunately, picking up C# was really just a day or two of effort (thanks in part to a C# pocket reference, which really summarized the language quite nicely). What proved harder was teaching the stupid phone to draw a line.

No shit. There literally is no graphics primitive for drawing a freaking line on the freaking screen. So when the first trick I wanted to do was take an image and kind of draw it slanted in a 3D-like-way, I had to do some serious experimentation to even get the ball rolling.

Which is the point of the post, dear reader. I submit to you now two blobs of C# source code:
  • PrimitiveRect.cs - a class that lets you easily draw lines and filled rectangles on the WP7 device; and
  • TexturedQuad.cs - a more complex class that lets you take an offscreen bitmap and stretch it onto any four-pixel-defined-rectangle on the screen.

Since I'm a mobile phone veteran, both of these classes are strong on the ability to precalculate early in order to keep the actual render cycle CPU-light. And since I'm a WP7 and C# newbie they probably look a little weird. But they're working great for me, so maybe they'll be useful to you too.

No comments:

Post a Comment