Nov 15

Full source code: http://www.bluerosegames.com/BlitWriteableBitmapSample.zip

This post is partially inspired by René Schulte’s series on extending WriteableBitmap. You can see his fantastic posts here:

http://kodierer.blogspot.com/2009/11/drawing-shapes-silverlight.html

The other inspiration for this comes from efforts that I have been making with SilverSprite, an open source library to compile and run 2D XNA games in Silverlight. When rendering a lot of bitmap sprites to the screen using traditional Silverlight techniques of adding Images to the visual tree works fine for tens or even a couple of hundred of sprites, but add more than this and have them all moving around and this technique starts falling short.

There are some other issues as well, such as drawing with a tint or drawing with anything other than alpha blending. For cases such as these, I wanted to be able to offer an alternative and settled upon rendering (“blitting”) images myself to a WriteableBitmap. This functionality will make it into SilverSprite soon, but in the meantime you can take advantage of it now.

Following in René’s footsteps, I have implemented these methods as extension methods on WriteableBitmap. There are some overloads to simplify the call a bit, but they all end up calling this method:

public static void Blit(this WriteableBitmap bmp, Rect destRect, WriteableBitmap source, Rect sourceRect, Color color, BlendMode blendMode) 

Most of these parameters should be clear as to what they do. The part of the source bitmap that is specified is copied to the destination rectangle of the target bitmap. The image is scaled to fill the destination rectangle if the source and destination rectangle differ in size. The two parameters that need a bit more explanation are the color and blend mode.

The color, if not Colors.White, will tint the source image. You can specify a partially transparent color to draw with and the image will be drawn partially transparent.

The blend mode can be AlphaBlend, Additive, Subtractive, or None. AlphaBlend is what you’re used to with Silverlight, but it can be too limiting in some cases. Additive blending can be very useful. Instead of alpha blending the colors, the source and destination red, green, and blue colors are added together to get the resulting color.  Let’s consider 3 fully opaque circles, one each of red, green, and blue. With alpha blending, we get something like this:

 

alphaBlend

Whichever one is drawn last ends up on top. If the circles were partially transparent, the values would be alpha blended. For additive, we get something like this:

additive

The center is white because the red, green, and blue components are added together to make white. for subtractive we can start with a white background and draw:

subtractive

This is exactly what you would get if you inverted the colors in the additive example. This makes sense because we’re subtracting color values instead of adding them, so we’re doing an inverse operation.

Finally, with blend mode None, The source pixels are copied as is to the target, and so the transparent areas of the source image are drawn overwriting the values that were there:

none

 

One place where additive blending is useful is with particles effects. Particle effects look more natural and impressive when using additive blending. Here is an example of using additive blending with particles. The particle emitter follows the mouse. The source code for this sample is included at the top of this post.

Sep 04

Ok so this isn’t a game development related post, but gamers and game developers are usually more interested in their hardware than the casual user so I hope this isn’t too far off base.

 

You’ve probably heard already that Windows 7 has an XP mode to run old software. What you may not know is that you need to have “Hardware Virtualization” on your system in order to use it. I’m going to focus on Intel here with their VT technology because honestly that’s where I’ve done my research and I don’t know much about the AMD version of hardware virtualization.

 

NOTE: Running Virtual PC on Windows 7 also requires Hardware Virtualization.

 

My research into this started because my current laptop, less that a year old, doesn’t have this capability. Along with being required for XP mode, you can also get better performance out of virtual machines if your machine supports VT. You may be surprised to hear that the top 2 bestselling laptops on Amazon right now don’t support it. The T6500 and the P7350 Core 2 Duo processors are a couple of the most popular right now and don’t have VT support.

 

So how do you know if your system supports VT? Well on an existing computer, you can run a tool to check. You may also have to go into your BIOS and enable it.

http://www.microsoft.com/windows/virtual-pc/support/configure-bios.aspx

 

Now what if you’re looking to buy a new system (let’s say hypothetically because your current system doesn’t support VT). If you’re fortunate to find the model you’re interested in at a brick and mortar store, you can check for yourself. If you’re buying online it gets a bit trickier.

 

I haven’t seen a listing yet for a computer online where the specs say if the system supports VT. I assume this will be coming once people start complaining about not being able to run XP mode on their brand new system. It’s easier to tell if the system doesn’t support VT, it’s harder to tell if it does.

 

First of all, you can look up your processor on Intel’s site and see if it supports it. They have a list of some of the more common processors here:

http://www.intel.com/products/processor/core2duo/mobile/specifications.htm

If your processor is not in the list, you can search for it in the search box at the top of that page.

 

Now here’s where it gets a bit tricky. Even if your processor is in the list doesn’t necessarily mean you can run VT. There also needs to be a BIOS setting to turn it on, and in some systems (specifically some Lenovo and Sony models) the BIOS doesn’t give you an option to turn on VT. So then you need to either find in the forums where someone confirms that the particular system can do VT, or you can contact the manufacturer.

 

Definitely when you order a new system and receive it, if VT is important to you, make sure you can enable it right away so that you can return it if necessary.

 

Hopefully the PC manufacturers will start to add entries to their specs concerning VT or whether the PC supports XP mode (which is the same thing, but might be easier for the user to grasp). Until then, do your homework and good luck.

Jul 27

This blog is about Silverlight game development, but if you’re writing managed code for your Silverlight game, why not explore some of the other options available to you as well to get maximum exposure for your game and hopefully make some money as well?

 

XNA Game Studio and Xbox Live Independent Games

XNA Game Studio allows you to create games for Windows, Xbox, and Zune. You write your games in C# and you can write 2D or 3D games (2D only on Zune right now). XNA doesn’t have a vector graphics library built in, so if you want to make your games more portable, stick to image based graphics.

 

Xbox Live Independent Games is a way for you to sell your game on Xbox. Anyone can do it, you just need to pay $99 a year for a premium subscription.

 

As a bit of a shameless plug, I currently have a game on Xbox called Dr. Popper, it’s a simple bubble breaker game but it’s fun even the kids enjoy it. You can download a free trial here (or buy it for a dollar):

 

http://marketplace.xbox.com/en-US/games/media/66acd000-77fe-1000-9115-d80258550147/

 

You make 70% of every sale and now games can be sold in more countries including USA, Canada, UK, Singapore, Germany, Italy, Spain, France, Japan, and Sweden. There are restrictions on the countries that can submit games as well right now, so please view the details before shelling out the $99. With this subscription you also get to playtest and peer review other people’s games for the Independent Games channel.

 

There are some great game development resources available on http://creators.xna.com and even if you don’t end up creating a game in XNA, it can be helpful to get an understanding of how they structure their game class. It may give you some ideas for how to structure things in Silverlight, a lot of my game tutorials came from implementing what they do in a Silverlight way.

 

You can now write 2D games for both XNA and Silverlight using my free SilverSprite library at http://silversprite.codeplex.com. To use it, you would have to write your game in the XNA style and then recompile for Siverlight using the SilverSprite library.

 

Windows Mobile Marketplace

Windows Mobile has a new marketplace coming up for 6.x phones. Think iPhone app store for Windows Mobile and it’s probably pretty close. This is just getting started so instead of jumping into a saturated market like iPhone, why don’t you become an early adopter here?

 

http://developer.windowsmobile.com/Marketplace.aspx

 

Silver Arcade

For Silverlight games, you can get more exposure for your game and hopefully make some money at http://silverarcade.com as the volume of users on the site increases. I’m one of the founders of Silver Arcade, and my partners on the site are extremely talented and we’ll be providing helpful libraries for Silverlight game developers soon.

Jul 15

The WriteableBitmap is one of the most interesting new features in Silverlight 3, especially for game development. I’m looking forward to the first pixel perfect collision game using this technology, one of my favorites was the classic Lemmings game. 

 

The Pixels array contains one integer for every pixel, and you can find the correct array element by indexing row * width + col like this:

 

WriteableBitmap bm = new WriteableBitmap(width, height);
bm.Pixels[row * bm.PixelWidth + col] = value;

 

Personally I would have liked to have seen the Pixels array be made up of unsigned integers, since all bits are used and it would have made using hex values easier. The bytes in the 4 byte integer value correspond to alpha, red, green, and blue respectively. So pretty straightforward, a SetPixel method should look like this:

 

void SetPixel(WriteableBitmap bm, int row, int col, byte alpha,
    byte r, byte g, byte b)

{
    int idx = row * bm.PixelWidth + col;
    bm.Pixels[idx] = (alpha << 24) | (r << 16) | (g << 8) | b;
}

Unfortunately there is a problem with this. This would be fine for ARGB32 format, but what the Pixels array uses is Premultiplied ARGB32. This means that if you specify an alpha value other than 255 (fully opaque), the other 3 values (red, green, and blue) need to be scaled based on the alpha value. A fixed version could look something like this:

 

void SetPixel(WriteableBitmap bm, int row, int col, byte alpha,
    byte r, byte g, byte b)
{
    int idx = row * bm.PixelWidth + col;
    byte r1 = (byte)(r * (alpha / 255d));
    byte g1 = (byte)(g * (alpha / 255d));
    byte b1 = (byte)(b * (alpha / 255d));
    bm.Pixels[idx] = (alpha << 24) | (r1 << 16) | (g1 << 8) | b1;
}

I assume that this format is more easily consumable for display. I spent a few hours figuring this out so hopefully you won’t have to struggle with it.

Jul 15

Pardon the pun, but Behaviors in Silverlight 3 have made me rethink game development in Silverlight and if you’re not looking at Behaviors you should be. I’ll be updating my old game tutorial posts building an asteroids game to use behaviors and I’ll provide full source so that you’ll be able to easily use these Behaviors in your own games.

 

I recommend you look at this video that convinced me this is the way to go:

 

http://www.microsoft.com/video/en/us/details/f8a1c6a2-0fc4-474e-8e7f-c66fc7e7952a

 

As a consequence of this, I’m going to leave my old blog for posterity at http://www.bluerosegames.com/silverlight-games-101 and everything here will be new material.

Jun 07

I’ve been contemplating a move to WordPress for a while for my blog, I’ve used Community Server and BlogEngine.net previously, and both of those have left me feeling lacking. I have been barraged with comment spam lately, and I hope that WordPress will make it easier to avoid this.

I also wanted to move my Silverlight game development blog under the Silver Arcade umbrella, as a service that Silver Arcade can offer to Silverlight game developers. I’m really excited about our work on Silver Arcade and we’re getting ready for a major upgrade, so this is part of that as well. I hope since I switched to Feedburner a while back that most of my subscribers will easily migrate over to the new site.

I’m currently going through all of the old content and bringing it up to date, the migration didn’t go well so there are a few issues right now that I’ll fix over the next few days.

After that is done, I will be adding some great new content here, I already have a few things in the works and I need to finish the asteroids game…

preload preload preload