home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / freethrd / server / ball.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  28.2 KB  |  949 lines

  1. /*+==========================================================================
  2.   File:      BALL.CPP
  3.  
  4.   Summary:   Implementation file for the COBall COM Object Class (for
  5.              aggregatable COBall COM Objects). This module provides a free
  6.              threaded virtual ball object. The ball has internal
  7.              algorithms that determine its position within a bounded two
  8.              dimensional area. No display or other GUI behavior is done in
  9.              this ball.  It is a mathematical entity. Clients of this ball
  10.              can command it to reset, move, and reveal its current
  11.              position, size, and color. These last are used by a client
  12.              that displays images of this ball. The color in particular is
  13.              an internal property maintained by the ball that indicates
  14.              the thread of execution that last moved this ball.
  15.  
  16.              COBall offers a main standard IUnknown interface (basic COM
  17.              object features) and the custom IBall interface (Moving Ball
  18.              related features).  This multiple interface COM Object Class
  19.              is achieved via the technique of nested classes.  The
  20.              implementation of the IBall interface is nested inside the
  21.              COBall Class.
  22.  
  23.              This file also implements some internal C++ classes (CXForm
  24.              and CBallThread) that provide internal support for the custom
  25.              IBall interface.
  26.  
  27.              For a comprehensive tutorial code tour of this module's
  28.              contents and offerings see the accompanying FRESERVE.TXT
  29.              file. For more specific technical details on the internal
  30.              workings see the comments dispersed throughout the module's
  31.              source code.
  32.  
  33.   Classes:   CXForm, CBallThread, COBall.
  34.  
  35.   Functions: none.
  36.  
  37.   Origin:    4-5-96: atrent - Editor-inheritance from CAR.CPP in
  38.              the DLLSERVE OLE Tutorial Code Sample. Also borrows from
  39.              the GDIDEMO sample in the Win32 samples of the Win32 SDK.
  40.  
  41. ----------------------------------------------------------------------------
  42.   This file is part of the Microsoft OLE Tutorial Code Samples.
  43.  
  44.   Copyright (C) Microsoft Corporation, 1996.  All rights reserved.
  45.  
  46.   This source code is intended only as a supplement to Microsoft
  47.   Development Tools and/or on-line documentation.  See these other
  48.   materials for detailed information regarding Microsoft code samples.
  49.  
  50.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  51.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  52.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  53.   PARTICULAR PURPOSE.
  54. ==========================================================================+*/
  55.  
  56.  
  57. /*---------------------------------------------------------------------------
  58.   We include WINDOWS.H for all Win32 applications.
  59.   We include OLE2.H because we will be making calls to the OLE Libraries.
  60.   We include APPUTIL.H because we will be building this application using
  61.     the convenient Virtual Window and Dialog classes and other
  62.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  63.   We include IBALL.H and BALLGUID.H for the common Ball-related Interface
  64.     class, GUID, and CLSID specifications.
  65.   We include SERVER.H because it has internal class declarations and
  66.     resource ID definitions specific for this DLL.
  67.   We include BALL.H because it has the class COBall declarations.
  68. ---------------------------------------------------------------------------*/
  69. #include "preserve.h"
  70. #include "ball.h"
  71.  
  72.  
  73. /*---------------------------------------------------------------------------
  74.   COBall's implementation of its internal utility class CXForm.
  75. ---------------------------------------------------------------------------*/
  76.  
  77.  
  78. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  79.   Method:   CXForm::Clear
  80.  
  81.   Summary:  Clears and initializes the transformation matrix.
  82.  
  83.   Args:     void
  84.  
  85.   Modifies: ...
  86.  
  87.   Returns:  void
  88. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  89. void CXForm::Clear(void)
  90. {
  91.   int Row,Col;
  92.  
  93.   for(Row=0; Row < 3; Row++)
  94.     for(Col=0; Col < 3; Col++)
  95.       if(Row == Col)
  96.         XForm[Row][Col] = 1;
  97.       else
  98.         XForm[Row][Col] = 0;
  99.  
  100.   return;
  101. }
  102.  
  103.  
  104. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  105.   Method:   CXForm::Scale
  106.  
  107.   Summary:  Method to allow setting the transformation to multiply
  108.             by a scale factor.
  109.  
  110.   Args:     int xScale
  111.               x Scale factor.
  112.             int yScale
  113.               y Scale factor.
  114.  
  115.   Modifies: ...
  116.  
  117.   Returns:  void
  118. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  119. void CXForm::Scale(int xScale, int yScale)
  120. {
  121.   int idx;
  122.  
  123.   for(idx=0; idx < 3; idx++)
  124.   {
  125.     XForm[idx][0] = XForm[idx][0] * xScale;
  126.     XForm[idx][1] = XForm[idx][1] * yScale;
  127.   }
  128.  
  129.   return;
  130. }
  131.  
  132.  
  133. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  134.   Method:   CXForm::Trans
  135.  
  136.   Summary:  Perform the transform uing the internal matrix.
  137.  
  138.   Args:     int xTrans
  139.               x coordinate.
  140.             int yTrans
  141.               y coordinate.
  142.  
  143.   Modifies: ...
  144.  
  145.   Returns:  void
  146. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  147. void CXForm::Trans(int xTrans, int yTrans)
  148. {
  149.   XForm[2][0] = XForm[2][0] + xTrans;
  150.   XForm[2][1] = XForm[2][1] + yTrans;
  151.  
  152.   return;
  153. }
  154.  
  155.  
  156. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  157.   Method:   CXForm::Point
  158.  
  159.   Summary:  Transform a point.
  160.  
  161.   Args:     POINT* pPoint
  162.               Pointer to the point.
  163.  
  164.   Modifies: ...
  165.  
  166.   Returns:  void
  167. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  168. void CXForm::Point(POINT* pPoint)
  169. {
  170.   int x,y;
  171.  
  172.   x = (XForm[0][0] * pPoint->x) + (XForm[1][0] * pPoint->y) + XForm[2][0];
  173.   y = (XForm[0][1] * pPoint->x) + (XForm[1][1] * pPoint->y) + XForm[2][1];
  174.  
  175.   pPoint->x = x;
  176.   pPoint->y = y;
  177.  
  178.   return;
  179. }
  180.  
  181.  
  182. /*---------------------------------------------------------------------------
  183.   COBall's implementation of its main COM object class including
  184.   Constructor, Destructor, QueryInterface, AddRef, and Release.
  185. ---------------------------------------------------------------------------*/
  186.  
  187. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  188.   Method:   COBall::COBall
  189.  
  190.   Summary:  COBall Constructor. Note the member initializer:
  191.             "m_ImpIBall(this, pUnkOuter)" which is used to pass the 'this'
  192.             and pUnkOuter pointers of this constructor function to the
  193.             constructor in the instantiation of the implementation of the
  194.             CImpIBall interface (which is nested inside this present
  195.             COBall Object Class).
  196.  
  197.   Args:     IUnknown* pUnkOuter,
  198.               Pointer to the the outer Unknown.  NULL means this COM Object
  199.               is not being Aggregated.  Non NULL means it is being created
  200.               on behalf of an outside COM object that is reusing it via
  201.               aggregation.
  202.             CServer* pServer)
  203.               Pointer to the server's control object.
  204.  
  205.   Modifies: m_cRefs, m_pUnkOuter, m_pServer.
  206.  
  207.   Returns:  void
  208. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  209. COBall::COBall(
  210.   IUnknown* pUnkOuter,
  211.   CServer* pServer) :
  212.   m_ImpIBall(this, pUnkOuter)
  213. {
  214.   // Zero the COM object's reference count.
  215.   m_cRefs = 0;
  216.  
  217.   // No AddRef necessary if non-NULL, as we're nested.
  218.   m_pUnkOuter = pUnkOuter;
  219.  
  220.   // Assign the pointer to the server control object.
  221.   m_pServer = pServer;
  222.  
  223.   return;
  224. }
  225.  
  226.  
  227. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  228.   Method:   COBall::~COBall
  229.  
  230.   Summary:  COBall Destructor.
  231.  
  232.   Args:     void
  233.  
  234.   Modifies: .
  235.  
  236.   Returns:  void
  237. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  238. COBall::~COBall(void)
  239. {
  240.   return;
  241. }
  242.  
  243.  
  244. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  245.   Method:   COBall::QueryInterface
  246.  
  247.   Summary:  QueryInterface of the COBall non-delegating
  248.             IUnknown implementation.
  249.  
  250.   Args:     REFIID riid,
  251.               [in] GUID of the Interface being requested.
  252.             PPVOID ppv)
  253.               [out] Address of the caller's pointer variable that will
  254.               receive the requested interface pointer.
  255.  
  256.   Modifies: .
  257.  
  258.   Returns:  HRESULT
  259. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  260. STDMETHODIMP COBall::QueryInterface(
  261.                REFIID riid,
  262.                PPVOID ppv)
  263. {
  264.   HRESULT hr = E_NOINTERFACE;
  265.  
  266.   if (OwnThis())
  267.   {
  268.     *ppv = NULL;
  269.  
  270.     if (IID_IUnknown == riid)
  271.       *ppv = this;
  272.     else if (IID_IBall == riid)
  273.       *ppv = &m_ImpIBall;
  274.  
  275.     if (NULL != *ppv)
  276.     {
  277.       // We've handed out a pointer to the interface so obey the COM rules
  278.       // and AddRef the reference count.
  279.       ((LPUNKNOWN)*ppv)->AddRef();
  280.       hr = NOERROR;
  281.     }
  282.  
  283.     UnOwnThis();
  284.   }
  285.  
  286.   return (hr);
  287. }
  288.  
  289.  
  290. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  291.   Method:   COBall::AddRef
  292.  
  293.   Summary:  AddRef of the COBall non-delegating IUnknown implementation.
  294.  
  295.   Args:     void
  296.  
  297.   Modifies: m_cRefs.
  298.  
  299.   Returns:  ULONG
  300.               New value of m_cRefs (COM object's reference count).
  301. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  302. STDMETHODIMP_(ULONG) COBall::AddRef(void)
  303. {
  304.   ULONG cRefs;
  305.  
  306.   if (OwnThis())
  307.   {
  308.     cRefs = ++m_cRefs;
  309.  
  310.     UnOwnThis();
  311.   }
  312.  
  313.   return cRefs;
  314. }
  315.  
  316.  
  317. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  318.   Method:   COBall::Release
  319.  
  320.   Summary:  Release of the COBall non-delegating IUnknown implementation.
  321.  
  322.   Args:     void
  323.  
  324.   Modifies: m_cRefs.
  325.  
  326.   Returns:  ULONG
  327.               New value of m_cRefs (COM object's reference count).
  328. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  329. STDMETHODIMP_(ULONG) COBall::Release(void)
  330. {
  331.   ULONG cRefs;
  332.  
  333.   if (OwnThis())
  334.   {
  335.     cRefs = --m_cRefs;
  336.  
  337.     if (0 == cRefs)
  338.     {
  339.       // We've reached a zero reference count for this COM object.
  340.       // So we tell the server housing to decrement its global object
  341.       // count so that the server will be unloaded if appropriate.
  342.       if (NULL != m_pServer)
  343.         m_pServer->ObjectsDown();
  344.  
  345.       // We artificially bump the main ref count to prevent reentrancy
  346.       // via the main object destructor.  Not really needed in this
  347.       // COBall but a good practice because we are aggregatable and
  348.       // may at some point in the future add something entertaining like
  349.       // some Releases to the COBall destructor. We relinquish thread
  350.       // ownership of this object before deleting it--a good practice.
  351.       m_cRefs++;
  352.       UnOwnThis();
  353.       delete this;
  354.     }
  355.     else
  356.       UnOwnThis();
  357.   }
  358.  
  359.   return cRefs;
  360. }
  361.  
  362.  
  363. /*---------------------------------------------------------------------------
  364.   COBall's nested implementation of the IBall interface including
  365.   Constructor, Destructor, QueryInterface, AddRef, Release, Reset, Move,
  366.   and GetBall. This interface implementation also has internal methods
  367.   that are not particulary intended for outside clients: GetDimensions,
  368.   SetDimensions, GetDirection, SetDirection, GetPosition, SetPostion,
  369.   CheckBounce, and FindThread. The IBall interface only provides client
  370.   access to the IUnknown methods and the Reset, Move, and GetBall methods.
  371. ---------------------------------------------------------------------------*/
  372.  
  373. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  374.   Method:   COBall::CImpIBall::CImpIBall
  375.  
  376.   Summary:  Constructor for the CImpIBall interface instantiation.
  377.  
  378.   Args:     COBall* pBackObj,
  379.               Back pointer to the parent outer object.
  380.             IUnknown* pUnkOuter
  381.               Pointer to the outer Unknown.  For delegation.
  382.  
  383.   Modifies: m_pBackObj, m_pUnkOuter.
  384.  
  385.   Returns:  void
  386. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  387. COBall::CImpIBall::CImpIBall(
  388.   COBall* pBackObj,
  389.   IUnknown* pUnkOuter)
  390. {
  391.   size_t i;
  392.   BYTE r=128, g=128, b=128;
  393.  
  394.   // Init the Back Object Pointer to point to the parent object.
  395.   m_pBackObj = pBackObj;
  396.  
  397.   // Init the CImpIBall interface's delegating Unknown pointer.  We use
  398.   // the Back Object pointer for IUnknown delegation here if we are not
  399.   // being aggregated.  If we are being aggregated we use the supplied
  400.   // pUnkOuter for IUnknown delegation.  In either case the pointer
  401.   // assignment requires no AddRef because the CImpIBall lifetime is
  402.   // quaranteed by the lifetime of the parent object in which
  403.   // CImpIBall is nested.
  404.   if (NULL == pUnkOuter)
  405.     m_pUnkOuter = pBackObj;
  406.   else
  407.     m_pUnkOuter = pUnkOuter;
  408.  
  409.   // Now initialize the Ball application entity data.
  410.   m_bAlive       = TRUE;
  411.   m_xDirection   = 0;
  412.   m_yDirection   = 0;
  413.   m_bNewPosition = FALSE;
  414.   m_xPosition    = 0;
  415.   m_yPosition    = 0;
  416.   m_nWidth       = 30;
  417.   m_nHeight      = 30;
  418.   m_xSkew        = BALL_MOVE_SKEW;
  419.   m_ySkew        = BALL_MOVE_SKEW;
  420.   m_crColor      = RGB(0,0,0);
  421.  
  422.   // Clear point transformation array.
  423.   m_XForm.Clear();
  424.  
  425.   // Init BallThread array--init colors and clear thread Ids.
  426.   // The BallThreads are the threads that contend to move and/or
  427.   // paint the ball object.
  428.   for (i = 0; i < MAX_BALLTHREADS; i++)
  429.     m_aBallThreads[i].Id = 0;
  430.   m_aBallThreads[0].Color = RGB(0  ,  0,255);  // Blue
  431.   m_aBallThreads[1].Color = RGB(255,  0,  0);  // Red
  432.   m_aBallThreads[2].Color = RGB(0  ,255,  0);  // Green
  433.   m_aBallThreads[3].Color = RGB(0  ,  0,  0);  // Black
  434.   m_aBallThreads[4].Color = RGB(255,  0,255);  // Purple
  435.   m_aBallThreads[5].Color = RGB(0  ,255,255);  // Aqua
  436.   m_aBallThreads[6].Color = RGB(255,255,  0);  // Brown
  437.   if (MAX_BALLTHREADS > 8)
  438.     for (i=7; i<MAX_BALLTHREADS; i++)
  439.     {
  440.       // Fill the remainder with some random colors.
  441.       m_aBallThreads[i].Color = RGB(r,g,b);
  442.       r = (BYTE) lRandom() % 255;
  443.       g = (BYTE) lRandom() % 255;
  444.       b = (BYTE) lRandom() % 255;
  445.     }
  446.  
  447.   return;
  448. }
  449.  
  450.  
  451. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  452.   Method:   COBall::CImpIBall::~CImpIBall
  453.  
  454.   Summary:  Destructor for the CImpIBall interface instantiation.
  455.  
  456.   Args:     void
  457.  
  458.   Modifies: .
  459.  
  460.   Returns:  void
  461. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  462. COBall::CImpIBall::~CImpIBall(void)
  463. {
  464.   return;
  465. }
  466.  
  467.  
  468. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  469.   Method:   COBall::CImpIBall::GetDimensions
  470.  
  471.   Summary:  Internal utility method to get the ball x,y dimensions.
  472.  
  473.   Args:     POINT* pDimension
  474.               Pointer to the point that will contain the dimensions.
  475.  
  476.   Modifies: *pDimension.
  477.  
  478.   Returns:  void
  479. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  480. void COBall::CImpIBall::GetDimensions(POINT* pDimension)
  481. {
  482.   pDimension->x = m_nWidth;
  483.   pDimension->y = m_nHeight;
  484.  
  485.   return;
  486. }
  487.  
  488.  
  489. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  490.   Method:   COBall::CImpIBall::SetDimensions
  491.  
  492.   Summary:  Internal utility method to set the ball x,y dimensions.
  493.  
  494.   Args:     int nWidth
  495.               New Ball width.
  496.             int nHeight
  497.               New Ball Height.
  498.  
  499.   Modifies: .
  500.  
  501.   Returns:  void
  502. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  503. void COBall::CImpIBall::SetDimensions(int nWidth, int nHeight)
  504. {
  505.   m_nWidth  = nWidth;
  506.   m_nHeight = nHeight;
  507.  
  508.   return;
  509. }
  510.  
  511.  
  512. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  513.   Method:   COBall::CImpIBall::GetDirection
  514.  
  515.   Summary:  Internal utility method to get the ball direction.
  516.  
  517.   Args:     POINT* pDirection
  518.               Pointer to the Point that will contain the x,y direction
  519.               data.
  520.  
  521.   Modifies: ...
  522.  
  523.   Returns:  void
  524. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  525. void COBall::CImpIBall::GetDirection(POINT* pDirection)
  526. {
  527.   pDirection->x = m_xDirection;
  528.   pDirection->y = m_yDirection;
  529.  
  530.   return;
  531. }
  532.  
  533.  
  534. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  535.   Method:   COBall::CImpIBall::SetDirection
  536.  
  537.   Summary:  Internal utility method to set the ball direction.
  538.  
  539.   Args:     int xDirection
  540.               x coordinate of the new direction.
  541.             int yDirection
  542.               y coordinate of the new direction.
  543.  
  544.   Modifies: ...
  545.  
  546.   Returns:  void
  547. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  548. void COBall::CImpIBall::SetDirection(int xDirection, int yDirection)
  549. {
  550.   m_xDirection = xDirection;
  551.   m_yDirection = yDirection;
  552.  
  553.   return;
  554. }
  555.  
  556.  
  557. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  558.   Method:   COBall::CImpIBall::GetPosition
  559.  
  560.   Summary:  Internal utility method to get current the ball position.
  561.  
  562.   Args:     POINT* pPosition
  563.               Pointer to the Point that is the position.
  564.  
  565.   Modifies: *pPostion.
  566.  
  567.   Returns:  void
  568. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  569. void COBall::CImpIBall::GetPosition(POINT* pPosition)
  570. {
  571.   POINT Org;
  572.  
  573.   Org.x = 0;
  574.   Org.y = 0;
  575.   m_XForm.Point(&Org);
  576.  
  577.   pPosition->x = Org.x;
  578.   pPosition->y = Org.y;
  579.  
  580.   return;
  581. }
  582.  
  583.  
  584. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  585.   Method:   COBall::CImpIBall::SetPosition
  586.  
  587.   Summary:  Internal utility method to set current the ball position.
  588.  
  589.   Args:     int x
  590.               x-coordinate of new position.
  591.             int y
  592.               y-coordinate of new position.
  593.  
  594.   Modifies: ...
  595.  
  596.   Returns:  void
  597. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  598. void COBall::CImpIBall::SetPosition(int x, int y)
  599. {
  600.   m_bNewPosition = TRUE;
  601.   m_xPosition    = x;
  602.   m_yPosition    = y;
  603.  
  604.   return;
  605. }
  606.  
  607.  
  608. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  609.   Method:   COBall::CImpIBall::CheckBounce
  610.  
  611.   Summary:  Internal utility method to check the current ball position,
  612.             dimension, and direction data and determine if the ball has
  613.             hit the internal WinRect bounding rectangle. If it has then
  614.             the ball data is recalculated to achieve a "bounce" effect
  615.             for the ball as it moves.
  616.  
  617.   Args:     void
  618.  
  619.   Modifies: ...
  620.  
  621.   Returns:  void
  622. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  623. void COBall::CImpIBall::CheckBounce(void)
  624. {
  625.   POINT Pos, Dir, Dim;
  626.   int   xNewPos, yNewPos, xNewDir, yNewDir;
  627.  
  628.   GetPosition(&Pos);
  629.   GetDirection(&Dir);
  630.   GetDimensions(&Dim);
  631.  
  632.   // Check each edge of the client rectangle.  If the ball goes past the
  633.   // boundries, reset its position and direction to give it a "bounce"
  634.   // effect the next time it is displayed.
  635.   xNewDir = Dir.x;
  636.   yNewDir = Dir.y;
  637.   xNewPos = Pos.x + Dir.x;
  638.   yNewPos = Pos.y + Dir.y;
  639.  
  640.   if(xNewPos < m_WinRect.left)
  641.   {
  642.     xNewDir = ((lRandom() % m_xSkew) + m_xSkew);
  643.     SetPosition(m_WinRect.left, Pos.y);
  644.   }
  645.   if((xNewPos + Dim.x) > m_WinRect.right)
  646.   {
  647.     xNewDir = -(((int)lRandom() % m_xSkew) + m_xSkew);
  648.     SetPosition(m_WinRect.right - Dim.x, Pos.y);
  649.   }
  650.   if(yNewPos < m_WinRect.top)
  651.   {
  652.     yNewDir = ((lRandom() % m_ySkew) + m_ySkew);
  653.     SetPosition(Pos.x, m_WinRect.top);
  654.   }
  655.   if((yNewPos + Dim.y) > m_WinRect.bottom)
  656.   {
  657.     yNewDir = -(((int)lRandom() % m_ySkew) + m_ySkew);
  658.     SetPosition(Pos.x, m_WinRect.bottom - Dim.y);
  659.   }
  660.  
  661.   SetDirection(xNewDir, yNewDir);
  662.  
  663.   return;
  664. }
  665.  
  666.  
  667. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  668.   Method:   COBall::CImpIBall::FindThread
  669.  
  670.   Summary:  Internal utility method to Find the thread that is now
  671.             executing this code. If the executing thread is not already in
  672.             the Thread array remember the new Thread's Id and add it to the
  673.             array. This in effect assigns the thread a color that can be
  674.             used for tutorial display of which thread is executing on the
  675.             ball object.
  676.  
  677.   Args:     void
  678.  
  679.   Modifies: ...
  680.  
  681.   Returns:  void
  682. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  683. void COBall::CImpIBall::FindThread(void)
  684. {
  685.   BOOL bFound = FALSE;
  686.   DWORD dwTId = GetCurrentThreadId();
  687.   size_t i = 0;
  688.  
  689.   while(!bFound && i<MAX_BALLTHREADS)
  690.   {
  691.     if (m_aBallThreads[i].Id == 0)
  692.     {
  693.       // Found empty slot. This simple array logic allows no empty holes.
  694.       m_aBallThreads[i].Id = dwTId;
  695.       bFound = TRUE;
  696.     }
  697.     else
  698.     {
  699.       if (m_aBallThreads[i].Id == dwTId)
  700.       {
  701.         // Found previous visiting thread--use its assigned color.
  702.         m_crColor = m_aBallThreads[i].Color;
  703.         bFound = TRUE;
  704.       }
  705.       else
  706.       {
  707.         i++;
  708.         if (i >= MAX_BALLTHREADS)
  709.         {
  710.           // Thread array is full--use a gray color for all other
  711.           // excess visiting threads.
  712.           m_crColor = RGB(127,127,127);
  713.           bFound = TRUE;
  714.         }
  715.       }
  716.     }
  717.   }
  718.  
  719.   return;
  720. }
  721.  
  722.  
  723. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  724.   Method:   COBall::CImpIBall::QueryInterface
  725.  
  726.   Summary:  The QueryInterface IUnknown member of this IBall interface
  727.             implementation that delegates to m_pUnkOuter, whatever it is.
  728.  
  729.   Args:     REFIID riid,
  730.               [in] GUID of the Interface being requested.
  731.             PPVOID ppv)
  732.               [out] Address of the caller's pointer variable that will
  733.               receive the requested interface pointer.
  734.  
  735.   Modifies: .
  736.  
  737.   Returns:  HRESULT
  738.               Returned by the delegated outer QueryInterface call.
  739. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  740. STDMETHODIMP COBall::CImpIBall::QueryInterface(
  741.                REFIID riid,
  742.                PPVOID ppv)
  743. {
  744.   // Delegate this call to the outer object's QueryInterface.
  745.   return m_pUnkOuter->QueryInterface(riid, ppv);
  746. }
  747.  
  748.  
  749. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  750.   Method:   COBall::CImpIBall::AddRef
  751.  
  752.   Summary:  The AddRef IUnknown member of this IBall interface
  753.             implementation that delegates to m_pUnkOuter, whatever it is.
  754.  
  755.   Args:     void
  756.  
  757.   Modifies: .
  758.  
  759.   Returns:  ULONG
  760.               Returned by the delegated outer AddRef call.
  761. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  762. STDMETHODIMP_(ULONG) COBall::CImpIBall::AddRef(void)
  763. {
  764.   // Delegate this call to the outer object's AddRef.
  765.   return m_pUnkOuter->AddRef();
  766. }
  767.  
  768.  
  769. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  770.   Method:   COBall::CImpIBall::Release
  771.  
  772.   Summary:  The Release IUnknown member of this IBall interface
  773.             implementation that delegates to m_pUnkOuter, whatever it is.
  774.  
  775.   Args:     void
  776.  
  777.   Modifies: .
  778.  
  779.   Returns:  ULONG
  780.               Returned by the delegated outer Release call.
  781. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  782. STDMETHODIMP_(ULONG) COBall::CImpIBall::Release(void)
  783. {
  784.   // Delegate this call to the outer object's Release.
  785.   return m_pUnkOuter->Release();
  786. }
  787.  
  788.  
  789. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  790.   Method:   COBall::CImpIBall::Reset
  791.  
  792.   Summary:  The Reset member method of the IBall interface implementation.
  793.             Called by outside clients of a COBall object to reset the
  794.             virtual ball. It is restored to the upper left corner.
  795.  
  796.   Args:     RECT* pNewRect,
  797.               Pointer to a RECT structure. Tells the COBall the bounding
  798.               rectangle within which the ball can move.
  799.             short nBallSize,
  800.               The size of the ball in pixels. nBallSize == Width == Height
  801.               meaning that a circle is assumed.
  802.  
  803.   Modifies: ...
  804.  
  805.   Returns:  void
  806. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  807. STDMETHODIMP COBall::CImpIBall::Reset(
  808.                RECT* pNewRect,
  809.                short nBallSize)
  810. {
  811.   HRESULT hr = E_FAIL;
  812.   int nDim, xDirection, yDirection;
  813.  
  814.   if (OwnThis())
  815.   {
  816.     // Find the thread who is executing this and remember its color.
  817.     FindThread();
  818.  
  819.     m_xSkew = m_ySkew = BALL_MOVE_SKEW;
  820.     m_WinRect.left = pNewRect->left;
  821.     m_WinRect.top = pNewRect->top;
  822.     m_WinRect.right = pNewRect->right;
  823.     m_WinRect.bottom = pNewRect->bottom;
  824.     nDim = nBallSize ? nBallSize : max(5, m_WinRect.right / 13);
  825.     SetDimensions(nDim, nDim);
  826.     SetPosition(0, 0);
  827.     xDirection = ((lRandom() % m_xSkew) + m_xSkew);
  828.     yDirection = ((lRandom() % m_ySkew) + m_ySkew);
  829.     SetDirection(xDirection, yDirection);
  830.  
  831.     hr = NOERROR;
  832.  
  833.     UnOwnThis();
  834.   }
  835.  
  836.   return hr;
  837. }
  838.  
  839.  
  840. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  841.   Method:   COBall::CImpIBall::Move
  842.  
  843.   Summary:  The Move member method of this IBall interface implementation.
  844.             Called by outside clients of a COBall object to advance the
  845.             "motion" of this COBall virtual ball entity.
  846.  
  847.   Args:     BOOL bAlive
  848.               TRUE means stay alive; FALSE means don't move but die.
  849.  
  850.   Modifies: m_bAlive.
  851.  
  852.   Returns:  BOOL
  853.               TRUE means the move was done and the ball is still alive.
  854.               FALSE means the move was not done and the ball has been
  855.               killed.
  856. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  857. STDMETHODIMP COBall::CImpIBall::Move(BOOL bAlive, BOOL* bRet)
  858. {
  859.   *bRet = FALSE;
  860.  
  861.   if (OwnThis())
  862.   {
  863.     if (bAlive)
  864.     {
  865.       // Find thread that is now executing this code. Remember its Id and
  866.       // assign it a color. If this thread previously visited here then
  867.       // use its remembered values. In any case, set a color value in
  868.       // m_crColor of its existing or newly assigned color.
  869.       FindThread();
  870.  
  871.       // Ask the Ball if it has hit any of the edges of the current window
  872.       // rectangle. If so, it will recalculate its position and direction to
  873.       // achieve a "bounce" effect in its motion the next time it is painted.
  874.       CheckBounce();
  875.  
  876.       // Calculate and set new ball position.
  877.       if(m_bNewPosition)
  878.       {
  879.         m_bNewPosition = FALSE;
  880.         m_XForm.Clear();
  881.         m_XForm.Trans(m_xPosition, m_yPosition);
  882.       }
  883.       else
  884.         m_XForm.Trans(m_xDirection, m_yDirection);
  885.     }
  886.     else
  887.       m_bAlive = FALSE;
  888.  
  889.     *bRet = m_bAlive;
  890.  
  891.     UnOwnThis();
  892.   }
  893.  
  894.   return S_OK;
  895. }
  896.  
  897.  
  898. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  899.   Method:   COBall::CImpIBall::GetBall
  900.  
  901.   Summary:  The GetBall member method of this IBall interface
  902.             implementation. Called by outside clients of a COBall object
  903.             to get the necessary data on the moving ball to enable GUI
  904.             display of an actual image of this virtual ball. This COBall
  905.             is a data entity only that is kept alive by client threads
  906.             that call Move. A GUI client can independently call GetBall
  907.             to allow it to display some visual representation of the Ball.
  908.  
  909.   Args:     POINT* pOrg,
  910.               Pointer to a point that will contain the current origin
  911.               position of the ball.
  912.             POINT* pExt,
  913.               Pointer to a point that will contain the current extent
  914.               size of the ball.
  915.             COLORREF* pcrColor)
  916.               Pointer to a COLORREF that will contain the current color
  917.               of the ball. This color is determined by the last thread
  918.               that was executing in the ball before this call is made.
  919.  
  920.   Modifies: ...
  921.  
  922.   Returns:  void
  923. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  924. STDMETHODIMP COBall::CImpIBall::get_Ball(
  925.        POINT* pOrg,
  926.        POINT* pExt,
  927.        COLORREF* pcrColor)
  928. {
  929.   HRESULT hr = E_FAIL;
  930.  
  931.   if (OwnThis())
  932.   {
  933.     *pcrColor = m_crColor;
  934.  
  935.     pOrg->x = 0;
  936.     pOrg->y = 0;
  937.     m_XForm.Point(pOrg);
  938.     pExt->x = m_nWidth;
  939.     pExt->y = m_nHeight;
  940.     m_XForm.Point(pExt);
  941.  
  942.     hr = NOERROR;
  943.  
  944.     UnOwnThis();
  945.   }
  946.  
  947.   return hr;
  948. }
  949.