home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sm099e.zip / example / run.c < prev    next >
C/C++ Source or Header  |  1997-01-26  |  3KB  |  122 lines

  1. #include "example.h"
  2.  
  3. #pragma argsused
  4. void Run ( BALLS *pBalls )
  5. /* Main loop of the program. */
  6. {
  7.    #if defined (OS2) && defined (GUI)
  8.    while (WinGetMsg (hAB, &QMsg, 0, 0, 0))
  9.    {
  10.       WinDispatchMsg (hAB, &QMsg);
  11.    }
  12.    #else
  13.    while (!kbhit ())
  14.    {
  15.       RunBalls (pBalls);
  16.       Pause (DELAY);
  17.    }
  18.    getch ();   /* Remove that termination key from keyboard buffer */
  19.    #endif
  20. }
  21.  
  22. void RunBalls ( BALLS *pBalls )
  23. /* Let every running ball take a new step. Heading to their current direction,
  24.    and moving in their current speed. Change direction of each ball so that
  25.    no balls run outside visible screen area. */
  26. {
  27.    int iC1;
  28.  
  29.    #if defined (OS2) && defined (GUI)
  30.    if (WinQueryFocus (HWND_DESKTOP) != hWndClient)
  31.    {
  32.       /* Don't hogg the single input queue of OS/2 when user work in some
  33.          other window than this stupid one. */
  34.       return;     
  35.    }
  36.    #endif
  37.  
  38.    for (iC1 = 0;
  39.         iC1 < pBalls->iNr;
  40.         RunBall (&pBalls->Ball[iC1++]));
  41. }
  42.  
  43. void RunBall ( BALL *pBall )
  44. /* Kernel function of RunBalls(). */
  45. {
  46.    PaintBall (pBall, BLACK);         /* Hide the ball from old position */
  47.  
  48.    MoveHorizontally (pBall);
  49.    MoveVertically (pBall);
  50.  
  51.    PaintBall (pBall, pBall->iColor); /* Show the ball at new position */
  52. }
  53.  
  54. void MoveHorizontally ( BALL *pBall )
  55. /* Move specified ball left/right.
  56.    Change speed and direction when the  ball reach any edge of screen. */
  57. {
  58.    pBall->iXPos += pBall->iHSpeed;
  59.  
  60.    if (pBall->iXPos > iScreenW)
  61.    {
  62.       pBall->iHSpeed = -NEWSPEED (MAXHSPEED);
  63.       pBall->iXPos = iScreenW;
  64.       NEWCOLOR (pBall->iColor);
  65.    }
  66.    else
  67.    if (pBall->iXPos <= 0)
  68.    {
  69.       pBall->iXPos = 1;
  70.       pBall->iHSpeed = NEWSPEED (MAXHSPEED);
  71.    }
  72. }
  73.  
  74. void MoveVertically ( BALL *pBall )
  75. /* Move specified ball up/down.
  76.    Change speed and direction when the  ball reach any edge of screen. */
  77. {
  78.    pBall->iYPos += pBall->iVSpeed;
  79.  
  80.    if (pBall->iYPos > iScreenH)
  81.    {
  82.       pBall->iYPos = iScreenH;
  83.       pBall->iVSpeed = -NEWSPEED (MAXVSPEED);
  84.    }
  85.    else
  86.    if (pBall->iYPos <= 0)
  87.    {
  88.       pBall->iYPos = 1;
  89.       pBall->iVSpeed = NEWSPEED (MAXVSPEED);
  90.    }
  91. }
  92.  
  93. void PaintBall ( BALL *pBall, int iColor )
  94. /* Paint specified ball at its current position, using specified color */
  95. {
  96.    #ifdef GUI
  97.    #ifdef OS2
  98.    POINTL pt;
  99.    pt.x = pBall->iXPos;
  100.    pt.y = pBall->iYPos;
  101.    GpiMove (hPS, &pt);
  102.    GpiSetColor (hPS, iColor);
  103.    GpiFullArc (hPS, DRO_OUTLINE, MAKEFIXED (1, 0));
  104.    #else
  105.    setcolor (iColor);
  106.    circle (pBall->iXPos, pBall->iYPos, 5);
  107.    #endif
  108.    #else
  109.    /* Prevent text screen from being vertically scrolled if the ball is
  110.       positioned at bottom right corner of screen (text modes only) */
  111.    if ((pBall->iXPos == iScreenW) &&
  112.        (pBall->iYPos == iScreenH))
  113.    {
  114.       return;
  115.    }
  116.  
  117.    textcolor (iColor);
  118.    gotoxy (pBall->iXPos, pBall->iYPos);
  119.    putch ('*');
  120.    #endif
  121. }
  122.