home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / games / checkers / first / ckrsetup.c < prev    next >
C/C++ Source or Header  |  1990-09-01  |  5KB  |  157 lines

  1. /*-----------------------------------------------------------
  2.    CKRSETUP.C -- SetupCntlProc for setup-board, Version 0.40
  3.                  (c) 1990, Charles Petzold
  4.   -----------------------------------------------------------*/
  5.  
  6. #define INCL_WIN
  7. #define INCL_GPI
  8. #include <os2.h>
  9. #include <stdlib.h>
  10. #include "checkers.h"
  11. #include "ckrdraw.h"
  12.  
  13. extern HAB hab ;
  14.  
  15. MRESULT EXPENTRY SetupCntlProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  16.      {
  17.      static BOARD brd ;
  18.      static HPS   hps ;
  19.      static RECTL rclOrigViewport ;
  20.      static SHORT sBottom ;
  21.      HDC          hdc ;
  22.      LONG         lScale ;
  23.      POINTL       ptlMouse ;
  24.      RECTL        rclWindow, rclViewport ;
  25.      PSETUPDLG    psetupdlg ;
  26.      SHORT        x, y, i ;
  27.      SIZEL        sizlPage ;
  28.      ULONG        ulBit ;
  29.  
  30.      switch (msg)
  31.           {
  32.           case WM_CREATE:
  33.                CkdQueryBoardDimensions (&sizlPage) ;
  34.  
  35.                hdc = WinOpenWindowDC (hwnd) ;
  36.                hps = GpiCreatePS (hab, hdc, &sizlPage,
  37.                                   PU_ARBITRARY | GPIF_DEFAULT |
  38.                                   GPIT_MICRO   | GPIA_ASSOC) ;
  39.  
  40.                GpiQueryPageViewport (hps, &rclOrigViewport) ;
  41.  
  42.                WinQueryWindowRect (hwnd, &rclWindow) ;
  43.  
  44.                if (rclWindow.xRight == 0)
  45.                     DosBeep (1000, 1000) ;
  46.  
  47.                     // Calculate scaling factor
  48.  
  49.                lScale = min (65536L * rclWindow.xRight / rclOrigViewport.xRight,
  50.                              65536L * rclWindow.yTop   / rclOrigViewport.yTop) ;
  51.  
  52.                     // Adjust page viewport of PS
  53.  
  54.                rclViewport.xLeft   = 0 ;
  55.                rclViewport.yBottom = 0 ;
  56.                rclViewport.xRight  = lScale * rclOrigViewport.xRight / 65536L ;
  57.                rclViewport.yTop    = lScale * rclOrigViewport.yTop   / 65536L ;
  58.  
  59.                rclViewport.xLeft   = (rclWindow.xRight - rclViewport.xRight) / 2 ;
  60.                rclViewport.yBottom = (rclWindow.yTop   - rclViewport.yTop)   / 2 ;
  61.                rclViewport.xRight += rclViewport.xLeft ;
  62.                rclViewport.yTop   += rclViewport.yBottom ;
  63.  
  64.                GpiSetPageViewport (hps, &rclViewport) ;
  65.  
  66.                return 0 ;
  67.  
  68.           case WM_BUTTON1DOWN:
  69.                WinSetActiveWindow (HWND_DESKTOP, hwnd) ;
  70.  
  71.           case WM_BUTTON1DBLCLK:
  72.           case WM_BUTTON2DOWN:
  73.           case WM_BUTTON2DBLCLK:
  74.                psetupdlg = WinQueryWindowPtr (hwnd, 0) ;
  75.                brd       = psetupdlg->brd ;
  76.                sBottom   = psetupdlg->sBottom ;
  77.  
  78.                                         // get mouse coords and index
  79.  
  80.                ptlMouse.x = MOUSEMSG(&msg)->x ;
  81.                ptlMouse.y = MOUSEMSG(&msg)->y ;
  82.                CkdQueryHitCoords (hps, ptlMouse, &x, &y) ;
  83.                i = CkdConvertCoordsToIndex (x, y, sBottom) ;
  84.  
  85.                if (i == -1)             // didn't hit black square
  86.                     {
  87.                     WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  88.                     return 0 ;
  89.                     }
  90.  
  91.                ulBit = 1L << i ;
  92.  
  93.                if (msg == WM_BUTTON1DOWN || msg == WM_BUTTON1DBLCLK)
  94.                     {
  95.                     if (brd.ulBlack & ulBit)
  96.                          {
  97.                          if (brd.ulKing & ulBit)
  98.                               {
  99.                               brd.ulBlack &= ~ulBit ;
  100.                               brd.ulKing  &= ~ulBit ;
  101.                               }
  102.                          else
  103.                               brd.ulKing |= ulBit ;
  104.                          }
  105.                     else
  106.                          {
  107.                          brd.ulBlack |=  ulBit ;
  108.                          brd.ulWhite &= ~ulBit ;
  109.                          brd.ulKing  &= ~ulBit ;
  110.                          }
  111.                     }
  112.                else
  113.                     {
  114.                     if (brd.ulWhite & ulBit)
  115.                          {
  116.                          if (brd.ulKing & ulBit)
  117.                               {
  118.                               brd.ulWhite &= ~ulBit ;
  119.                               brd.ulKing  &= ~ulBit ;
  120.                               }
  121.                          else
  122.                               brd.ulKing |= ulBit ;
  123.                          }
  124.                     else
  125.                          {
  126.                          brd.ulBlack &= ~ulBit ;
  127.                          brd.ulWhite |=  ulBit ;
  128.                          brd.ulKing  &= ~ulBit ;
  129.                          }
  130.                     }
  131.  
  132.                CkdErasePiece (hps, x, y) ;
  133.                CkdDrawOnePieceDirect (hps, x, y, &brd, sBottom) ;
  134.  
  135.                psetupdlg->brd = brd ;
  136.                return 0 ;
  137.  
  138.           case WM_PAINT:
  139.                WinBeginPaint (hwnd, hps, NULL) ;
  140.  
  141.                psetupdlg = WinQueryWindowPtr (hwnd, 0) ;
  142.                brd       = psetupdlg->brd ;
  143.                sBottom   = psetupdlg->sBottom ;
  144.  
  145.                CkdDrawWholeBoard (hps) ;
  146.                CkdDrawAllPiecesDirect (hps, &brd, sBottom) ;
  147.  
  148.                WinEndPaint (hps) ;
  149.                return 0 ;
  150.  
  151.           case WM_DESTROY:
  152.                GpiDestroyPS (hps) ;
  153.                return 0 ;
  154.           }
  155.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  156.      }
  157.