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