home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / devnews / vol3 / xform / xform.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-11  |  8.0 KB  |  330 lines

  1. //---------------------------------------------------------------------------
  2. //
  3. //  Name     : xform.c
  4. //
  5. //  Purpose  : Demonstrate simple but powerful uses of the model transform.
  6. //
  7. //
  8. //---------------------------------------------------------------------------
  9.  
  10. #define INCL_32
  11. #define INCL_GPI
  12. #define INCL_WIN
  13. #include <os2.h>
  14.  
  15. #include "xform.h"
  16.  
  17. HAB     hab;
  18. HWND    hwndFrame;
  19. PFNWP   pfnwpFrame;
  20.  
  21. #define XFORM_CLASSNAME "XFORM"
  22.  
  23. // Local function prototype declarations
  24.  
  25. MRESULT EXPENTRY ClientWndProc( HWND hwnd ,USHORT msg ,MPARAM mp1 ,MPARAM mp2 );
  26. MRESULT EXPENTRY FrameSubclassProc( HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2);
  27.  
  28. void DrawBox( HWND hwnd );
  29. void DrawTransformedBox( HWND hwnd );
  30. void DrawShearedBox( HWND hwnd );
  31. void RotateBox( HWND hwnd );
  32.  
  33. //--------------------------------------------------------------------------
  34. //
  35. //  main() procedure.
  36. //   --> Initialize PM for this process
  37. //   --> Create our message queue
  38. //   --> Create frame and client windows
  39. //   --> Show the window
  40. //   --> Enter our message dispatching loop
  41. //
  42. //
  43. // --------------------------------------------------------------------------
  44. void cdecl main(VOID)
  45. {
  46.    HMQ hmq;
  47.    HWND hwndclient;
  48.    QMSG qmsg;
  49.  
  50.    ULONG flCreateFlags =   FCF_BORDER     | FCF_SHELLPOSITION |
  51.                            FCF_TASKLIST   | FCF_TITLEBAR      | FCF_SYSMENU |
  52.                            FCF_SIZEBORDER | FCF_MINMAX        | FCF_MENU ;
  53.  
  54.    hab = WinInitialize( (USHORT)NULL );
  55.  
  56.    hmq=WinCreateMsgQueue( hab,0 );
  57.  
  58.  
  59.    WinRegisterClass( hab
  60.                    , XFORM_CLASSNAME
  61.                    , (PFNWP)ClientWndProc
  62.                    , (ULONG)CS_SIZEREDRAW
  63.                    , (USHORT)256 );
  64.  
  65.    hwndFrame = WinCreateStdWindow( HWND_DESKTOP
  66.                                  ,  0UL
  67.                                  ,  &flCreateFlags
  68.                                  ,  XFORM_CLASSNAME
  69.                                  ,  "Xform - Model transform examples"
  70.                                  ,  WS_VISIBLE
  71.                                  ,  (HMODULE)0
  72.                                  ,  ID_PIN
  73.                                  ,  &hwndclient );
  74.  
  75.  
  76.    WinShowWindow( hwndFrame, TRUE );
  77.  
  78.    while ( WinGetMsg( hab,&qmsg, (HWND)0, 0, 0 ) )
  79.    {
  80.      WinDispatchMsg( hab,&qmsg );
  81.    }
  82.  
  83.    if ( hwndFrame )
  84.    {
  85.      WinDestroyWindow(hwndFrame);
  86.      WinDestroyMsgQueue(hmq);
  87.      WinTerminate(hab);
  88.    }
  89. }
  90.  
  91. //--------------------------------------------------------------------------
  92. //
  93. //  Client window procedure.
  94. //
  95. // --------------------------------------------------------------------------
  96. MRESULT EXPENTRY ClientWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  97. {
  98.  
  99.    switch (msg)
  100.    {
  101.  
  102.      // When the left mouse button is clicked, run the sample functions.
  103.  
  104.      case WM_BUTTON1DOWN:
  105.      {
  106.        DrawBox( hwnd );
  107.        DrawTransformedBox( hwnd );
  108.        RotateBox( hwnd );
  109.        DrawShearedBox( hwnd );
  110.      }
  111.      break;
  112.  
  113.      case WM_PAINT:
  114.      {
  115.        HPS   hpsPaint ;
  116.        RECTL rectl;
  117.  
  118.        hpsPaint = WinBeginPaint( hwnd, (HPS)0, &rectl );
  119.  
  120.        WinFillRect( hpsPaint, &rectl, SYSCLR_WINDOW );
  121.  
  122.        WinEndPaint( hpsPaint );
  123.      }
  124.      break;
  125.  
  126.      default:
  127.      {
  128.        return( WinDefWindowProc( hwnd, msg, mp1, mp2 )) ;
  129.      }
  130.    }
  131. }
  132.  
  133. //--------------------------------------------------------------------------
  134. //
  135. //  DrawBox()
  136. //
  137. // --------------------------------------------------------------------------
  138.  
  139. void DrawBox( HWND hwnd )
  140. {
  141.   HPS hps;
  142.   POINTL pointl;
  143.  
  144.   // Get a cached PS for the window
  145.  
  146.   hps = WinGetPS( hwnd );
  147.  
  148.   pointl.x = pointl.y = 0;
  149.  
  150.   // Set the current position to (0,0)
  151.  
  152.   GpiSetCurrentPosition( hps, &pointl );
  153.  
  154.   pointl.x = pointl.y = 10;
  155.  
  156.   // Draw a 10 by 10 box from the current position in the
  157.   // current color.
  158.  
  159.   GpiBox( hps, DRO_OUTLINE, &pointl,0,0 );
  160.  
  161.   // Free the cached PS
  162.  
  163.   WinReleasePS( hps );
  164.  
  165.  }
  166.  
  167. //--------------------------------------------------------------------------
  168. //
  169. // DrawTransformedBox
  170. //
  171. // --------------------------------------------------------------------------
  172. void DrawTransformedBox( HWND hwnd )
  173. {
  174.   HPS hps;
  175.   POINTL pointl;
  176.   MATRIXLF m;
  177.  
  178.   // Get a cached PS for the window
  179.  
  180.   hps = WinGetPS( hwnd );
  181.  
  182.   // Query the current contents of the model transform
  183.  
  184.   GpiQueryModelTransformMatrix( hps, 9L, &m );
  185.  
  186.   m.lM31  = 100;                 // Translate the x coordinates
  187.   m.lM32  = 100;                 // Translate the y coordinates
  188.   m.fxM11 = MAKEFIXED(10,0);     // Scale up the x coordinates
  189.   m.fxM22 = MAKEFIXED(10,0);     // Scale up the y coordinates
  190.  
  191.   // Replace the model transform with our modified one
  192.  
  193.   GpiSetModelTransformMatrix( hps, 9L, &m, TRANSFORM_REPLACE );
  194.  
  195.  
  196.   // Set the current position to (0,0)
  197.  
  198.   pointl.x = pointl.y = 0;
  199.  
  200.   GpiSetCurrentPosition( hps, &pointl );
  201.  
  202.   // Draw a 10 by 10 box from the current position in the
  203.   // current color.
  204.  
  205.   pointl.x = pointl.y = 10;
  206.  
  207.   GpiBox( hps, DRO_OUTLINE, &pointl,0,0 );
  208.  
  209.   // Free the cached PS
  210.  
  211.   WinReleasePS( hps );
  212. }
  213.  
  214. //--------------------------------------------------------------------------
  215. //
  216. // RotateBox
  217. //
  218. // --------------------------------------------------------------------------
  219. void RotateBox( HWND hwnd )
  220. {
  221.   HPS hps;
  222.   POINTL pointlBox, pointlStart;
  223.   MATRIXLF m;
  224.   LONG i;
  225.  
  226.   // Get a cached PS for the window
  227.  
  228.   hps = WinGetPS( hwnd );
  229.  
  230.   // This time let's draw the boxes in blue
  231.  
  232.   GpiSetColor( hps, CLR_BLUE );
  233.  
  234.   // For this simple example, we will choose an arbitary position
  235.   // as the anchor point for each box of (400,400). This will be
  236.   // the point about which each box is rotated. A nice
  237.   // alternative, as a small enhancement, would be to make the
  238.   // start position be wherever the mouse is clicked in the
  239.   // window.
  240.  
  241.   pointlStart.x = 400;
  242.   pointlStart.y = 400;
  243.  
  244.   // Query the current contents of the model transform
  245.  
  246.   GpiQueryModelTransformMatrix( hps, 9L, &m );
  247.  
  248.   // Setup our box coordinates to be 100 by 100 from wherever
  249.   // the start position is.
  250.  
  251.   pointlBox.y = pointlStart.y + 100;
  252.   pointlBox.x = pointlStart.x + 100;
  253.  
  254.   // Draw a series of boxes, each time around the loop we'll
  255.   // rotate through an extra 10 degrees, replacing the transform
  256.   // with our newly calculated one.
  257.  
  258.   for ( i=0; i<360; i+=10 )
  259.   {
  260.     GpiRotate( hps
  261.              , &m
  262.              , TRANSFORM_REPLACE
  263.              , MAKEFIXED(i,0)
  264.              , &pointlStart );
  265.  
  266.     GpiSetModelTransformMatrix( hps, 9L, &m, TRANSFORM_REPLACE );
  267.  
  268.     GpiSetCurrentPosition( hps, &pointlStart );
  269.  
  270.     // Draw a 100 by 100 box. Note that we issue a normal box
  271.     // drawing request. Blissfully unaware that the transform
  272.     // we setup will cause our box to be rotated.
  273.  
  274.     GpiBox( hps, DRO_OUTLINE, &pointlBox,0,0 );
  275.   }
  276.  
  277.   // Free the cached PS
  278.  
  279.   WinReleasePS( hps );
  280.  
  281. }
  282.  
  283. //--------------------------------------------------------------------------
  284. //
  285. // DrawShearedBox
  286. //
  287. // --------------------------------------------------------------------------
  288. void DrawShearedBox( HWND hwnd )
  289. {
  290.   HPS hps;
  291.   POINTL pointl;
  292.   MATRIXLF m;
  293.  
  294.   // Get a cached PS for the window
  295.  
  296.   hps = WinGetPS( hwnd );
  297.  
  298.   // Query the current contents of the model transform
  299.  
  300.   GpiQueryModelTransformMatrix( hps, 9L, &m );
  301.  
  302.   m.fxM21 = MAKEFIXED(1,0); // tan(45)
  303.  // m.fxM22 = MAKEFIXED(1,0); // identity
  304.  
  305.   // Replace the model transform with our modified one
  306.  
  307.   GpiSetModelTransformMatrix( hps, 9L, &m, TRANSFORM_REPLACE );
  308.  
  309.  
  310.   // Set the current position to (0,100)
  311.  
  312.   pointl.x = 400; pointl.y = 200;
  313.  
  314.   GpiSetCurrentPosition( hps, &pointl );
  315.  
  316.   // Draw a 100 by 100 box from the current position in the
  317.   // current color.
  318.  
  319.   pointl.x = pointl.y = 100;
  320.  
  321.   GpiSetColor( hps, CLR_BLUE );
  322.   GpiSetPattern( hps, PATSYM_HALFTONE );
  323.  
  324.   GpiBox( hps, DRO_OUTLINEFILL, &pointl,0,0 );
  325.  
  326.   // Free the cached PS
  327.  
  328.   WinReleasePS( hps );
  329. }
  330.