25.1 The Windows sandwich

Windows graphics code typically has the form of a 'sandwich.' The top slice of bread is some preparation work, the filling is when you draw your graphics, and the bottom slice is cleanup work.

Assume that these variables get values set somewhere in your code.

COLORREF bubblecolor; 
int intcenterx, intcentery, intradius; 

Here's an example of Windows sandwich code that draws a circle of bubblecolor with pixel intradius around the pixel (intcenterx, intcentery). The code uses two local CBrush variables, where a Windows CBrush is an object that Windows uses for filling in the insides of shapes. Note that one variable is a CBrush object, while the other is a CBrush* pointer.

CBrush cbrush, *pbrush_old; 
  • The preparatory 'top slice' of the sandwich looks like this.

    pbrush_old = pDC->SelectObject(&cbrush); 
  • The yummy sandwich filling might be as follows.

    pDC->Ellipse(intcenterx ? intradius, intcentery ? intradius, 
        intcenterx + intradius, intcentery + intradius); 
  • And the cleanup 'bottom slice' is this.

    pDC->SelectObject(pbrush_old); //Need to unselect the brush before 
    deleting cbrush.DeleteObject(); //Delete the brush. 

Whenever we need to write Windows sandwich code, we try and arrange the code so that we undo things in the reverse order to that we do them in.

When you first see Windows sandwich code, it's a little hard to believe how much trouble it is. But it's very versatile. And even though it looks like a lot of steps, it happens very fast.

At this point we need to say some more about Windows graphics tools, and how they relate to the MFC class used to hold the device context of an onscreen window (or a printer): CDC.

    Part I: Software Engineering and Computer Games
    Part II: Software Engineering and Computer Games Reference