Saturday, March 31, 2007

Growing Yards and Glowing WPF

We have a new house in a new subdivision. New houses are generally surrounded by lots of dirt, plus lots of other new houses that look suspiciously similar to yours (except the colors they picked always look better). Now, a reasonable person would—under these circumstances—simply pick up the phone and call Joe’s Landscaping to come install a nice green apron around the house. You might even end up with a tree or two by accident, and some industrial bushes in a sea of woodchips against your house. Unfortunately, I’m not reasonable. I just had to go make a landscaping “design” where nothing is square (despite the fact that the whole thing is on graph paper). These “designs” require a multitude of trees, bushes, and various other plants with unspeakable Latin names, an irrigation system second only to that running under Central Park, and lots of digging in the kind of soil that would get a pottery class excited.

Making games with Windows Presentation Foundation is pretty unreasonable, too. But so far it doesn’t seem like it is going to be quite the can of worms that my landscaping “design” has turned out to be. Overall, I am impressed with WPF. I like how super-easy it is to rotate, scale, animate, and perform groovy special effects (like the “Glow” in this week’s demo that applies to any visual). It is really fun to animate the special effects. You can animate pretty much anything you want.

If any of you are just learning WPF, don’t give up! There is a very short, steep learning curve at first, but you will soon hit that “ah ha!” moment and everything is downhill from there.

A couple of snags I ran into, though: First, when laying out the planets I have to make sure nothing overlaps. To do that, I have to know what the final size of each planet is going to be after any transformations. However, WPF doesn’t tell you what the final size will be until everything has rendered the first time. But of course, I can’t render something before I’ve figured out where it should go on the screen! So I ended writing some extra event handlers and such to keep track of what the planet’s size should be regardless of whether a given visual has been rendered or loaded yet. This had the nice side-effect of giving us a good framework for high-performance collision detection.

The second WPF strangeness is how events and properties work. They are not really built in to C#, but use a lot of reflection and external “attaching” functions that just feel a bit tacked-on. This is hidden from you in XAML, but in C# where our game has to do some fancy stuff, I think the implementation could have been a bit cleaner (even without extending the C# grammar).

Otherwise, things are going fine and I only wish I had more spare time. Unfortunately, I have a lot of digging, planting, and who-knows-what-else to do in my yard. So we’ll just have to eat this elephant one juicy bite at a time.

So what are you waiting for? Head on over to my freshly-baked Scorched Earths Google Code project where you can get the “Glow” demo from the downloads section, and peruse the source code from the comfort of your home office (you aren’t reading this from work are you?!).

No comments: