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.


Fortunately I was not the only one who was having this problem, and I came upon Ian Griffith's weblog posting here (http://www.interact-sw.co.uk/iangblog/2008/11/07/silverlight-writeable-bitmap) where he describes his PngGenerator class. This class suited my needs perfectly, and it looks like I will be able to easily swap in the WriteableBitmap when Silverlight 3 is released.


Now, here's the process flow of my nice new Nebula image in Solar Vengeance Silverlight,



  • 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.


See the latest demo build here:


C:\Silicon Commander\NET3.5\SV.Silverlight\SCG.SolarVengeance\Bin\Debug\TestPage.html



No comments:

Post a Comment