home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / ball.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-19  |  4.2 KB  |  143 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11. //
  12. // CBall
  13. //
  14. // Provides the ball that bounces.
  15.  
  16. #include <streams.h>
  17.  
  18. #include "ball.h"
  19.  
  20.  
  21. //
  22. // CBall::Constructor
  23. //
  24. // The default arguments provide a reasonable image and ball size.
  25. CBall::CBall(int iImageWidth, int iImageHeight, int iBallSize)
  26.     : m_iImageWidth(iImageWidth)
  27.     , m_iImageHeight(iImageHeight)
  28.     , m_iBallSize(iBallSize)
  29.     , m_iAvailableWidth(iImageWidth - iBallSize)    // precompute these here for code clarity
  30.     , m_iAvailableHeight(iImageHeight - iBallSize)
  31.     , m_x(0)        // start the ball in the bottom left...
  32.     , m_y(0)
  33.     , m_xDir(RIGHT)    // ... and travelling to the top right
  34.     , m_yDir(UP) {
  35.  
  36.     // Check we have some (arbitrary) space to bounce in.
  37.     ASSERT(iImageWidth > 2*iBallSize);
  38.     ASSERT(iImageHeight > 2*iBallSize);
  39.  
  40.     // random position for showing off a video mixer
  41.     m_iRandX = rand();
  42.     m_iRandY = rand();
  43.  
  44. }
  45.  
  46.  
  47. //
  48. // PlotBall
  49. //
  50. // assumes the image buffer is arranged row 1,row 2,row 3,...,row n in memory and
  51. // that the data is contiguous.
  52. void CBall::PlotBall(BYTE pFrame[], BYTE BallPixel[], int iPixelSize) {
  53.  
  54.     ASSERT(m_x >= 0);            // the ball is in the frame, allowing for its width
  55.     ASSERT(m_x <= m_iAvailableWidth);    //
  56.     ASSERT(m_y >= 0);            //
  57.     ASSERT(m_y <= m_iAvailableHeight);    //
  58.  
  59.     BYTE *pBack =   pFrame;        // The current byte of interest in the frame
  60.  
  61.     // Plot the ball into the correct location
  62.     BYTE *pBall =   pFrame                     // Set pBall to the first pixel
  63.               + ( m_y * m_iImageWidth * iPixelSize)     // of the ball to plot
  64.               + m_x * iPixelSize;
  65.  
  66.     for (int row = 0; row < m_iBallSize; row++) {
  67.  
  68.         for (int col = 0; col < m_iBallSize; col++) {
  69.  
  70.             for (int i = 0; i < iPixelSize; i++) {    // For each byte in the pixel,
  71.                                  // fill its value from BallPixel[]
  72.                 if (WithinCircle(col, row)) {
  73.                     *pBall = BallPixel[i];
  74.                 }
  75.                 pBall++;
  76.         }
  77.     }
  78.  
  79.     pBall += m_iAvailableWidth * iPixelSize;
  80.     }
  81. }
  82.  
  83.  
  84. //
  85. // BallPosition
  86. //
  87. // Return the 1-dimensional position of the ball at time t millisecs
  88. // millisecs runs out after about a month
  89. int CBall::BallPosition( int iPixelTime  // millisecs per pixel
  90.                        , int iLength     // distance between the bounce points
  91.                        , int time        // time in millisecs
  92.                        , int iOffset     // for a bit of randomness
  93.                        ) {
  94.     // Calculate the position of an unconstrained ball (no walls)
  95.     // then fold it back and forth to calculate the actual position
  96.  
  97.     int x = time / iPixelTime;
  98.     x += iOffset;
  99.     x %= 2*iLength;
  100. //    x += iLength/2;    // adjust for ball starting mid frame
  101.  
  102.     if (x>iLength) {    // check it is still in bounds
  103.         x = 2*iLength - x;
  104.     }
  105.  
  106.     return x;
  107. }
  108.  
  109.  
  110.  
  111. //
  112. // MoveBall
  113. //
  114. // Set (m_x, m_y) to the new position of the ball.  move diagonally
  115. // with speed m_v in each of x and y directions.
  116. // Guarantees to keep the ball in valid areas of the frame.
  117. // When it hits an edge the ball bounces in the traditional manner!.
  118. // The boundaries are (0..m_iAvailableWidth, 0..m_iAvailableHeight)
  119. void CBall::MoveBall(CRefTime rt) {
  120.  
  121.     m_x = BallPosition( 10, m_iAvailableWidth, rt.Millisecs(), m_iRandX );
  122.     m_y = BallPosition( 10, m_iAvailableHeight, rt.Millisecs(), m_iRandY );
  123. }
  124.  
  125.  
  126. //
  127. // WithinCircle
  128. //
  129. // return TRUE if (x,y) is within a circle radius S/2, centre (S/2, S/2)
  130. //                                         where S is m_iBallSize
  131. // else return FALSE
  132. inline BOOL CBall::WithinCircle(int x, int y) {
  133.  
  134.     unsigned int r = m_iBallSize / 2;
  135.  
  136.     if ( (x-r)*(x-r) + (y-r)*(y-r)  < r*r) {
  137.         return TRUE;
  138.     }
  139.     else {
  140.         return FALSE;
  141.     }
  142. }
  143.