6.4 Updating the views

The UpdateAllViews( CView* pSender, int lHint, CObject* pHint) method generates calls to the CPopView::OnUpdate( CView* pSender, int lHint, CObject* pHint) method for each open view, passing on the same arguments.

A minimal version of the OnUpdate method could look like this.

void CPopView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
    Invalidate(); /* Enqueue a message asking Windows to call OnDraw 
        for this view. */ 

Invalidate produces a call to OnDraw, which is where the critters get drawn to the active window.

But what about the three arguments to UpdateAllViews and OnUpdate ? It's possible for a view to initiate a call to UpdateAllViews with a line like this: GetDocument()->UpdateAllViews. This trick provides a path by which one view can contact the others. In some programs this mechanism is heavily used, and in programs like this you may want the other views to know which view initiated the UpdateAllViews call. This is what the psender argument is for. In the Pop Framework we don't make any use of the psender.

The second argument to the OnUpdate method is an integer hint field which lets the document feed in an integer to tell the view that some special action is called for here. In the normal run of things we just want OnUpdate to call Invalidate so the view will draw itself to the screen, but if you're in the process of starting up a new game you might need to do something additional, and in this case you'd have the call put in a hint integer to remind you. In the Pop Framework, the lHint will usually be 0, but in the case where we're starting up a new game, for instance, we use the PopDoc::VIEWHINT_STARTGAME hint value of 2.

Although stepDoc never produces an UpdateAllViews call with an lHint other than 0, there are other spots in the program where UpdateAllViews can be called. For instance the CPopDoc::setGameClass(..) method has a call to UpdateAllViews(NULL, CPopDoc::VIEWHINT_STARTGAME, 0).

The third argument to the OnUpdate method is a pointer hint, that is, it's a pointer to whatever kind of structure of additional information you want to pass to the view. In the Pop Framework, we pass the Real dt on to the view so that the view can appropriately move a viewer object that it owns, but we have to wrap it up inside a cTimeHint object with a single Real field so we can pass it.

Here are the steps taken by the CPopView::OnUpdate( CView* pSender, int lHint, CObject* pHint).

  • If lHint is CPopDoc::VIEWHINT_LOADINGARCHIVE, you're saving or loading a game, and you've placed a pointer to a CArchive file object inside the pHint field. Get the active CArchive from pHint and read or write the view parameters from or into the archive.

  • If lHint is CPopDoc::VIEWHINT_STARTGAME, you're initializing a new game. In this case, use the CGame methods initializeView and initializeCritterViewer to prepare this view, and then return.

  • If the lHint is the default value 0, we get the dt timestep out of the pHint.

  • Use the dt to move and update the pviewpointcritter by the timestep dt.

  • If this view is the active view, update the status bar, and pass any mouse or keyboard actions to the pviewpointcritter.

  • Invalidate the view to force a call to OnDraw.

For further information, the commented source code for CPopView::OnUpdate can be consulted in popview.cpp.

The OnDraw method carries out these steps.

  • Wake up the graphics.

  • 'Garbage collect' any unused image resources.

  • Graphically show the status of the game by adjusting the color of the window margins around the game.

  • Clear the graphics background.

  • Install the projection and view matrices.

  • Draw the world, by default as a background and a foreground rectangle.

  • Draw the critters.

  • Send the graphics to your video display by a BitBlt or a page-flip.

More information about the action of CPopView::OnDraw can be found in Chapter 24: Two- and Three-Dimensional Graphics, or by looking at the source code for this method in popview.cpp.

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