Game Development Reference
In-Depth Information
imageData.Stride, bmpProperties);
// Unlock the memory that is holding the
original bitmap object.
originalImage.UnlockBits(imageData);
// Get rid of the original bitmap object
since we no longer need it.
originalImage.Dispose();
// Return the Direct2D bitmap.
return d2dBitmap;
}
This method is a little confusing, so I've kept the comments present in this code
listing. You may have noticed that in the line with the call to the LockBits()
method, there is a pixel format parameter, but it is different from what we saw
a bit earlier in the chapter; it is Sys-
tem.Drawing.Imaging.PixelFormat.Format32bppPArgb . This is the same
format we are using, but what is that P in there for? The P is short for precalculated
alpha .Thisbasicallymeansthatthered,green,andbluechannelsareautomatically
adjusted based on the alpha value before rendering. So, if you have the red channel
at maximum and the alpha channel at 50 percent, the intensity of the red channel will
be reduced by half.
There is also straight alpha which is less efficient than precalculated alpha. The val-
ues of the red, green, and blue channels are left alone. Their intensity is adjusted
based on the value of the alpha channel during rendering. Precalculated alpha is
a bit faster since it adjusts the color channels once before any rendering happens,
whereas straight alpha has to adjust the color channels each time we render a new
frame. And lastly, there is also an ignore alpha mode. In this mode, the alpha chan-
nel is completely ignored, and thus you cannot use transparent bitmaps.
We are using the precalculated alpha mode in this case and this is important. If you
don't do this, the player character will have white in all of the transparent areas of the
robot image, which looks rather silly. We used the LockBits() method to lock the
memory holding the bitmap because if any other code on another thread accesses
that memory while we are messing with it, this can cause crashes and other odd be-
havior.
Search WWH ::




Custom Search