Instead of using animated bitmaps for the graphics for these games, this chapter focuses on standard GDI+ drawing routines. GDI+ (which stands for Graphics Device Interchange, Plus) is the subset of the .NET Framework that deals with drawing. If you’re familiar with pre-.NET drawing in any Windows programming language, you might remember the vast assortment of handles and device contexts and bitblts (oh, my!) and the potential memory problems involved if you forgot to release a handle or failed to recognize a bad return code. Fortunately, .NET has eliminated most of this headache. Like all other .NET Framework classes, the garbage collector manages the memory for all GDI+ classes automatically. This means you often don’t have to worry about cleaning up after yourself. Listing 4-1 shows some simple drawing commands that give you most of the backgrounds you need to draw the tiles used in this chapter.
Listing 4.1: Simple GDI+ Example
Private Sub DrawTest(ByVal g As Graphics) Dim b As SolidBrush Dim p As Pen Dim r As Rectangle b = New SolidBrush(Color.Blue) p = Pens.White r = New Rectangle(10, 10, 50, 50) g.FillRectangle(b, r) g.DrawRectangle(p, r) End Sub
Listing 4-1 uses several of the major GDI+ classes. The first of these is the Graphics class, which is passed into the test procedure as a parameter. Think of a Graphics object as a virtual surface onto which the drawing will be done, sort of like a digital sheet of paper.
| Note |
Old-school Windows programmers might equate a Graphics object to a device context. In fact, the Graphics class encapsulates all of the device context functionality. |
The next object is a SolidBrush, which is one of several descendants of the Brush class. You use brushes for filling areas with a single color, a color gradient, a pattern, or even a bitmap image. The SolidBrush class fills an area with a solid color. A Pen, on the other hand, draws lines. Listing 4-1 creates a white Pen. Note the difference in how Listing 4-1 creates the Brush instance and the Pen instance. With the Brush instance, a new instance was created by using the standard constructor (which takes a color as a parameter). The Pen instance, on the other hand, was created using a constant Pen object found in the .NET Framework. You can find similar Brush constants, as well; this example uses different methods of creation for illustration purposes.
The third variable used in Listing 4-1 is a Rectangle object—used to define arectangle, obviously. There are two different constructors for the Rectangle class; this one takes left, top, width, and height parameters that define the location and size of the shape.
| Note |
The second Rectangle constructor takes a Point structure and a Size structure as its two parameters. Both constructors yield similar results—the one you choose depends on what data type you have available at the time you create the rectangle. |
The last two lines of Listing 4-1 do the actual drawing. First, the declared rectangle is filled in with the blue brush. Second, it’s outlined with the white Pen. The DrawRectangle and FillRectangle methods both exist on the Graphics object, so this is where the drawing happens.
One final thing to notice about Listing 4-1 is that the Pen and Brush (or the Rectangle, for that matter) are never destroyed, disposed of, freed, released, or any other word you might use to describe removing these objects from memory. As mentioned earlier, the .NET Framework garbage collector handles all of this for you.