home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / REVOLVE.ZIP / REVOLVE.C < prev    next >
Text File  |  1989-05-21  |  6KB  |  179 lines

  1. /*-----------------------------------------
  2.    REVOLVE.C -- Demonstrates GPI Transforms
  3.   -----------------------------------------*/
  4.  
  5. #define INCL_WIN
  6. #define INCL_GPI
  7.  
  8. #include <os2.h>
  9. #include <math.h>
  10. #include <stddef.h>
  11.  
  12. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  13.  
  14. HAB hab ;
  15.  
  16. int main (void)
  17.      {
  18.      static CHAR szClientClass [] = "Revolve" ;
  19.      HMQ    hmq ;
  20.      HWND   hwndFrame, hwndClient ;
  21.      QMSG   qmsg ;
  22.      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU | 
  23.                                  FCF_SIZEBORDER    | FCF_MINMAX  |
  24.                                  FCF_SHELLPOSITION | FCF_TASKLIST ;
  25.  
  26.      hab = WinInitialize (0) ;
  27.      hmq = WinCreateMsgQueue (hab, 0) ;
  28.  
  29.      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW |
  30.                                                           CS_SYNCPAINT, 0) ;
  31.  
  32.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  33.                     &flFrameFlags,
  34.                     szClientClass, " GPI Transform Demo",
  35.                     0L, NULL, 0, &hwndClient) ;
  36.  
  37.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0) )
  38.           WinDispatchMsg (hab, &qmsg) ;
  39.  
  40.      WinDestroyWindow (hwndFrame) ;
  41.      WinDestroyMsgQueue (hmq) ;
  42.      WinTerminate (hab) ;
  43.  
  44.      return 0 ;
  45.      }
  46.  
  47. VOID SetModelTransform (HPS hps, FIXED fxYscale, FIXED fxAngle,
  48.                                                  FIXED fxScale)
  49.      {
  50.      MATRIXLF matlf ;
  51.  
  52.      GpiSetModelTransformMatrix (hps, 0L, NULL, TRANSFORM_REPLACE) ;
  53.      GpiQueryModelTransformMatrix (hps, 9L, &matlf) ;
  54.  
  55.      matlf.fxM22 = fxYscale ;
  56.  
  57.      GpiSetModelTransformMatrix (hps, 9L, &matlf, TRANSFORM_REPLACE) ;
  58.  
  59.      matlf.fxM11 = (FIXED) (cos (6.283*fxAngle/65536/360) * 65536) ;
  60.      matlf.fxM22 = matlf.fxM11 ;
  61.      matlf.fxM12 = (FIXED) (sin (6.283*fxAngle/65536/360) * 65536) ;
  62.      matlf.fxM21 = -matlf.fxM12 ;
  63.  
  64.      GpiSetModelTransformMatrix (hps, 9L, &matlf, TRANSFORM_ADD) ;
  65.  
  66.      matlf.fxM11 = matlf.fxM22 = fxScale ;
  67.      matlf.fxM12 = matlf.fxM21 = 0 ;
  68.  
  69.      GpiSetModelTransformMatrix (hps, 9L, &matlf, TRANSFORM_ADD) ;
  70.      }
  71.  
  72. VOID DrawFigure (HPS hps)
  73.      {
  74.      static POINTL aptlFigure [] =
  75.                {
  76.                 -20,  100,  20,  100,  20,   90,   10,   90,   10,   85,
  77.                  20,   85,  20,   75,  35,   75,   35,   70,   20,   70,
  78.                  20,   65,   5,   65,   5,   60,   20,   60,   10,   50,
  79.                  10,   40,  25,   40,  85,  100,  100,  100,  100,   85,
  80.                  25,   10,  25,  -25,  50,  -85,   75,  -85,   75, -100,
  81.                  35, -100,   0,  -15, -35, -100,  -75, -100,  -75,  -85,
  82.                 -50,  -85, -25,  -25, -25,   10,  -85,  -50, -100,  -50,
  83.                -100,  -35, -25,   40, -10,   40,  -10,   50,  -20,   60,
  84.                 -20,  100
  85.                } ;
  86.  
  87.      GpiMove (hps, aptlFigure) ;
  88.  
  89.      GpiPolyLine (hps, sizeof aptlFigure / sizeof aptlFigure [0] - 1L,
  90.                   aptlFigure + 1) ;
  91.      }
  92.  
  93. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1,
  94.                                                        MPARAM mp2) 
  95.      {
  96.      static double dScaler = 1.1, dYScaleAngle = 0.0,
  97.                    dYScaleAngleInc = 0.25 ;
  98.      static FIXED  fxAngle, fxAngleInc = 0xA0000L,
  99.                    fxYScale, fxScale = 0x2000L ;
  100.      static HPS    hps ;
  101.      HDC           hdc ;
  102.      MATRIXLF      matlf ;
  103.      POINTL        ptl ;
  104.      SIZEL         sizl ;
  105.  
  106.      switch (msg)
  107.           {
  108.           case WM_CREATE:
  109.                hdc = WinOpenWindowDC (hwnd) ;
  110.                
  111.                sizl.cx = sizl.cy = 0 ;
  112.  
  113.                hps = GpiCreatePS (hab, hdc, &sizl, PU_LOENGLISH |
  114.                                     GPIF_DEFAULT | GPIT_MICRO   |
  115.                                     GPIT_NORMAL  | GPIA_ASSOC) ;
  116.  
  117.                GpiSetMix (hps, FM_INVERT) ;
  118.  
  119.                if (!WinStartTimer (hab, hwnd, 1, 100L))
  120.                     WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  121.  
  122.                break ;
  123.           case WM_SIZE:
  124.                ptl.x = LOUSHORT (mp2) / 2 ;  // Half of window width
  125.                ptl.y = HIUSHORT (mp2) / 2 ;  // Half of window height
  126.  
  127.                GpiConvert (hps, CVTC_DEVICE, CVTC_PAGE, 1L, &ptl) ;
  128.  
  129.                GpiQueryDefaultViewMatrix (hps, 9L, &matlf) ;
  130.  
  131.                matlf.lM31 = ptl.x ;
  132.                matlf.lM32 = ptl.y ;
  133.  
  134.                GpiSetDefaultViewMatrix (hps, 9L, &matlf, 
  135.                                              TRANSFORM_REPLACE) ;
  136.                break ;
  137.  
  138.           case WM_TIMER:
  139.                SetModelTransform (hps, fxYScale, fxAngle, fxScale) ;
  140.                DrawFigure (hps) ;
  141.              
  142.                fxYScale = (FIXED) (65535 * cos (dYScaleAngle)) ;
  143.  
  144.                if ((dYScaleAngle += dYScaleAngleInc) > 6.283)
  145.                     dYScaleAngle -= 6.283 ;
  146.  
  147.                if ((fxAngle += fxAngleInc) > (FIXED) 0x16800000L)
  148.                     fxAngle -= (FIXED) 0x16800000L ;
  149.  
  150.                if ((FIXED) (fxScale *= dScaler) >= (FIXED) 0x30000L ||
  151.                          fxScale <= (FIXED) 0x2000L)
  152.                     dScaler = 1 / dScaler ;
  153.  
  154.                SetModelTransform (hps, fxYScale, fxAngle, fxScale) ;
  155.                DrawFigure (hps) ;
  156.                break ;
  157.  
  158.           case WM_PAINT:
  159.                WinBeginPaint (hwnd, hps, NULL) ;
  160.  
  161.                GpiErase (hps) ;
  162.  
  163.                SetModelTransform (hps, fxYScale, fxAngle, fxScale) ;
  164.                DrawFigure (hps) ;
  165.  
  166.                WinEndPaint (hps) ;
  167.                break ;
  168.  
  169.           case WM_DESTROY:
  170.                WinStopTimer (hab, hwnd, 1) ;
  171.                GpiDestroyPS (hps) ;
  172.                break ;
  173.  
  174.           default:
  175.                return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  176.           }
  177.      return 0 ;
  178.      }
  179.