Chapter 8: Using DirectX
Overview
You may not have Noticed, but the drawing speed in some of the previous games was a bit slow—okay, maybe more than a bit slow. The slowdown was most noticeable in the NineTiles game from Chapter 3, “Understanding Object-Oriented Programming from the Start.” In fact, I was originally going to add an opening animation sequence that showed all the tiles flipping over simultaneously, but this turned out to be too slow.If you do some Google research on speed issues, you’ll find that the bitmap rendering in the Graphics Device Interchange, Plus (GDI+) classes isn’t quite ready for prime time. There are reports of unnecessary palette and color transformations going on behind the scenes when using the GDI+ classes for drawing. Trying to correct the problem by changing the color depth of the source bitmaps does nothing to speed up the drawing to any great degree.Fortunately, for most of the games discussed to this point, blazing-fast bitmap rendering speed isn’t necessary. The games should run at an acceptable speed. If you continue on the game-development track and create bigger and more complicated games, this speed will most likely become a barrier at some point, though. You have three ways to get around the graphics speed trap:
Stop writing games until Microsoft addresses some of the GDI issues.
Drop back down to the Win32 application programming interface (API) for graphics drawing (bitblt, stretchblt, and so on).
Move over to DirectX drawing.
Obviously, the first option is no fun at all (plus, the book would have to end right here!). The second option is possible, but don’t you get a slight feeling of failure when you have to stop using a cool new language and return to old habits? Plus, using the bitblt function could get tricky with all the device handles and such.The third option sounds like the best choice by default. DirectX is a huge set of multimedia functionality built into the Windows operating system. It has gotten both bigger and better with each new release, and the latest release, DirectX 9, is no exception. DirectX 9 includes a managed class interface for the .NET developer. In other words, drawing using the DirectX classes is scarcely more difficult than drawing using the GDI+ classes. Thus, you’ll break the speed barrier.