24.4 Graphics in the Pop Framework

The data about the state of the game and its critters lives inside the CGame object in the CPopDoc . The individual CPopView is responsible for getting graphic onscreen representations of the sprite objects associated with the game's critters.

We allow the different views to use different kinds of graphics and to look at the world from different viewpoints. To this end, there are two key members of the CPopView class.

 cGraphics *_pgraphics;  cCritterViewer *_pviewpointcritter; 

Before continuing, let's redraw our Pop Framework UML class diagram another time (see Figure 24.6). In redrawing a UML diagram, you might think of the classes as little pieces of wood connected by strings, with the strings being the inheritance and composition lines. In redrawing the diagram, you can flip things around in order to bring out different features.

Figure 24.6. The Pop Framework classes


The CPopView::OnDraw(CDC* pDC)

The code of the CPopView::OnDraw is expressed using the cGraphics class with no reference to the details of any particular kind of graphics implementation. In the case of cGraphicsMFC , behind the scenes the child class uses a scratch-pad cMemoryDC to prepare the image by clearing it, and drawing the background and the critters on it. And then, still if our graphics is cGraphicsMFC , we use a rapid BitBlt -based copy operation to copy the bitmap to the screen.

If our graphics is cGraphicsOpenGL , the draw method activates an OpenGL context, writes the background and critters to an invisible 'graphics page,' and then ' flips ' the page to make it visible.

The actual code goes like this. By the way, a CDC is an MFC class used to hold the device context of an onscreen window (or a printer).

 void CPopView::OnDraw(CDC* pDC)  {      if (pDC->IsPrinting())          return; //Don't try to deal with printing case.  //Wake up the graphics.      _pgraphics->activate();  //Tell the cGraphics to get rid of any extra unused image resources.      _pgraphics->garbageCollect();  //Graphically show the status of the game.      if (pgame()->gameover()) //Dim the lights          _pgraphics->setClearColor(CPopView::GAMEOVEREDGECOLOR);      else //turn the lights on          _pgraphics->setClearColor(CPopView::GAMEACTIVEEDGECOLOR);  //Clear the graphics background.      CRect targetrect;      pDC->GetClipBox(&targetrect);      _pgraphics->clear(targetrect);  //Install the projection and view matrices.      _pviewpointcritter->loadProjectionMatrix(); /* Initializes the          PROJECTION matrix or, in the case of cGraphicsMFC,          initializes the cRealPixelConverter. */      pviewpointcritter->loadViewMatrix(); /* Initializes the MODELVIEW          matrix */  //Draw the world, by default as a background and a foreground  //rectangle.      pgame()->drawWorld(_pgraphics, _drawflags);  //Draw the critters.      pgame()->drawCritters(_pgraphics, _drawflags);  //Send the graphics to your video display. cGraphicsMFC needs to draw  //foreground again in here.      _pgraphics->display(this, pDC);  } 

Finally let's make a sequence diagram for the drawing process. This is shown in Figure 24.7.

Figure 24.7. Sequence diagram for the Pop draw


Software Engineering and Computer Games
Software Engineering and Computer Games
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net