Monday, June 22, 2009

Silverlight Nebula

Today I cracked the problem of how to dynamically generate a bitmap that represents the background of the game map for Solar Vengeance Silverlight. Silverlight does not provide out of the box any class or method that allows you to simply create an offscreen bitmap and render its pixels using drawing methods. In Silverlight 3, this will be addressed by the inclusion of the WriteableBitmap class, but I wanted to move forward in Silverlight 2 and wait until 3 is released before switching to that version for my development.

I call the BuildScenario method of a Scenario-derived class (in this case Classic).

Classic creates some random Nebula in the SVGame object.

I create a PngGenerator of 100x100 pixels in size, representing the full number of cells in the map grid.

For each cell that contains a Nebula, I set the pixel to a bluish color, otherwise set it to black.

I make the image the source for an Image object and add this to the Canvas of the game board, sizing it to the full dimensions of the board (3,500 x 3,500) and setting its Stretch property to Stretch.

I was expecting to see sharp edges in the blown up image, but was surprised and happy to see the blended edges that Silverlight produced. Another hurlde overcome (this time thanks to Ian) in my quest to port Solar Vengeance to Silverlight.