home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / S12143.ZIP / REVOLVE.C < prev    next >
C/C++ Source or Header  |  1988-07-31  |  6KB  |  166 lines

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