4.5. Real AnimationWhile motion is a form of animation, we have discussed yet how to change the form of objects that are moving. There are several ways of doing this, but they all amount in having the function that draws an object to depend on time. One example is offered by the way we provide the illusion of rolling beads in the bouncing bead game. We add a member, m_rot_ix, to the CLiveBead object that is incremented or decremented (depending on the direction of motion) at each call of the Step() method and we use its value to overlay a white circular disc on the image of the bead. // Listing A // Inside the Step() method if(m_speed.dx>0) m_rot_ix++; else m_rot_ix--; // .... // Inside the display method // draw highligh to provide illusion of rotation CPoint center = m_pos.CenterPoint(); pDC->SelectStockObject(NULL_PEN); pDC->SelectStockObject(WHITE_BRUSH); int r = m_size.cx/4; double theta = PI/30; int x = (int)(center.x + r*cos(theta*m_rot_ix)); int y = (int)(center.y + r*sin(theta*m_rot_ix)); pDC->Ellipse(x, y, x+6, y+6); This is an example of the use of vector graphics for animation. Another way is to use several bitmaps to depict an object with the choice of the bitmap depending on several parameters, such as time, direction of motion, etc. The simplest way to implement this approach is to use icons. Such icons can be loaded from resources at the start of the game and placed in an array, say IconList with a statement such as the one below. IconList[FISH_L1] = ::AfxGetApp()->LoadIcon(IDI_ICON3); We may need several icons to cover various directions of motion as well as to create the impression of animation. If we use the two icons shown below we create the impression of a fish swimming while opening and closing its mouth.
The code fragment below is from the Display() method. // Listing B // compute location of icon center x, y if(m_speed.dx > 0)icon = m_ix%2 ? IconList[FISH_R1] : IconList[FISH_R2]; else icon = m_ix%2 ? IconList[FISH_L1] : IconList[FISH_L2]; pDC->DrawIcon(x, y, icon);
|