home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CHESS.PAK / DRAG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.9 KB  |  232 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //----------------------------------------------------------------------------
  4. #include <owl/pch.h>
  5. #include <owl/defs.h>
  6. #include <math.h>
  7. #include "wcdefs.h"
  8. #include "externs.h"
  9.  
  10. #define AnimationSpeed    2
  11.  
  12. static HDC hSaveDC;
  13. static TPoint  LastPoint;
  14. static HANDLE  hOldBmp;
  15. static HBITMAP hDragPiece, hDragMask;
  16. static HCURSOR hPrevCursor;
  17.  
  18. #undef max
  19. #undef min
  20. #define max(a, b)  (((a) > (b)) ? (a) : (b))
  21. #define min(a, b)  (((a) < (b)) ? (a) : (b))
  22.  
  23. static BOOL IsDragging = FALSE;
  24. static TPoint StartPoint;
  25. static TPoint CurPoint;
  26.  
  27. static void
  28. SaveBackground(TPoint& p)
  29. {
  30.   LastPoint = p;
  31.   HDC hDC = GetDC(hWndMain);
  32.  
  33.   HBITMAP hBitmap = PieceBmpArray[0][white]; // handle to any of the bitmaps is ok
  34.   BITMAP bitmap;
  35.   GetObject(hBitmap, sizeof(BITMAP), &bitmap);
  36.   
  37.   hSaveDC = CreateCompatibleDC(hDC);
  38.   hOldBmp = SelectObject(hSaveDC, CreateCompatibleBitmap(hDC, bitmap.bmWidth, bitmap.bmHeight));
  39.  
  40.   BitBlt(hSaveDC, 0, 0, bitmap.bmWidth, bitmap.bmHeight,
  41.     hDC, p.x, p.y, SRCCOPY);
  42.  
  43.   ReleaseDC(hWndMain, hDC);
  44. }
  45.  
  46. static void
  47. RestoreBackground()
  48. {
  49.   HDC hDC = GetDC(hWndMain);
  50.   HBITMAP hBitmap = PieceBmpArray[0][white]; // handle to any of the bitmaps is ok
  51.   BITMAP bitmap;
  52.   GetObject(hBitmap, sizeof(BITMAP), &bitmap);
  53.  
  54.   BitBlt(hDC, LastPoint.x, LastPoint.y, bitmap.bmWidth, bitmap.bmHeight,
  55.     hSaveDC, 0, 0, SRCCOPY);
  56.  
  57.   DeleteObject(SelectObject(hSaveDC, hOldBmp));
  58.   DeleteDC(hSaveDC);
  59.   ReleaseDC(hWndMain, hDC);
  60. }
  61.  
  62. static void
  63. ShowPiece(TPoint& p)
  64. {
  65.  
  66.   HDC hDC = GetDC(hWndMain);
  67.   HDC hMemoryDC = CreateCompatibleDC(hDC);
  68.   BITMAP bitmap;
  69.   GetObject(hDragPiece, sizeof(BITMAP), &bitmap);
  70.  
  71.   HANDLE hOldBmp = SelectObject(hMemoryDC, hDragMask);
  72.   BitBlt(hDC, p.x, p.y, bitmap.bmWidth, bitmap.bmHeight,
  73.     hMemoryDC, 0, 0, SRCAND);
  74.  
  75.   SelectObject(hMemoryDC, hDragPiece);
  76.   BitBlt(hDC, p.x, p.y, bitmap.bmWidth, bitmap.bmHeight,
  77.     hMemoryDC, 0, 0, SRCINVERT);
  78.  
  79.   SelectObject(hMemoryDC, hOldBmp);
  80.   DeleteDC(hMemoryDC);
  81.   ReleaseDC(hWndMain, hDC);
  82. }
  83.  
  84. static void
  85. UpdateScreen(TPoint& p)
  86. {
  87.   BITMAP bitmap;
  88.   GetObject(hDragPiece, sizeof(BITMAP), &bitmap);
  89.  
  90.   TRect rect1(p, TSize(bitmap.bmWidth, bitmap.bmHeight));
  91.   TRect rect2(LastPoint, TSize(bitmap.bmWidth, bitmap.bmHeight));
  92.   
  93.   if (!rect1.Touches(rect2)) {
  94.     RestoreBackground();
  95.     SaveBackground(p);
  96.     ShowPiece(p);
  97.     return;
  98.   }
  99.  
  100.   HDC hDC = GetDC(hWndMain);
  101.  
  102.   HDC hNewSaveDC = CreateCompatibleDC(hDC);
  103.   HANDLE hNewOldBmp = SelectObject(hNewSaveDC,
  104.       CreateCompatibleBitmap(hDC, bitmap.bmWidth, bitmap.bmHeight));
  105.  
  106.   TRect rect = rect1 | rect2;
  107.   HDC hUpdateDC = CreateCompatibleDC(hDC);
  108.   HANDLE hUOldBmp = SelectObject(hUpdateDC,
  109.             CreateCompatibleBitmap(hDC, rect.Width(), rect.Height()));
  110.  
  111.   // get new section
  112.   BitBlt(hUpdateDC, 0, 0, rect.Width(), rect.Height(), hDC, rect.left, rect.top, SRCCOPY);
  113.  
  114.   // restore old section
  115.   BitBlt(hUpdateDC, abs(rect.left-rect2.left), abs(rect.top-rect2.top),
  116.     bitmap.bmWidth, bitmap.bmHeight, hSaveDC, 0, 0, SRCCOPY);
  117.  
  118.   // save new background
  119.   BitBlt(hNewSaveDC, 0, 0, bitmap.bmWidth, bitmap.bmHeight,
  120.     hUpdateDC, abs(rect.left-rect1.left), abs(rect.top-rect1.top), SRCCOPY);
  121.  
  122.   // blit the mask
  123.   DeleteObject(SelectObject(hSaveDC, hDragMask));
  124.  
  125.   BitBlt(hUpdateDC, abs(rect.left-rect1.left), abs(rect.top-rect1.top),
  126.     bitmap.bmWidth, bitmap.bmHeight, hSaveDC, 0, 0, SRCAND);
  127.  
  128.   // blit the piece
  129.   SelectObject(hSaveDC, hDragPiece);
  130.   BitBlt(hUpdateDC, abs(rect.left-rect1.left), abs(rect.top-rect1.top),
  131.     bitmap.bmWidth, bitmap.bmHeight, hSaveDC, 0, 0, SRCINVERT);
  132.  
  133.   // put it on the screen
  134.   BitBlt(hDC, rect.left, rect.top, rect.Width(), rect.Height(), hUpdateDC, 0, 0, SRCCOPY);
  135.  
  136.   SelectObject(hSaveDC, hOldBmp); // don't delete current bmp (hDragPiece)
  137.   DeleteDC(hSaveDC);
  138.   hSaveDC = hNewSaveDC;
  139.   hOldBmp = hNewOldBmp;
  140.   LastPoint = p;
  141.   DeleteObject(SelectObject(hUpdateDC, hUOldBmp));
  142.   DeleteDC(hUpdateDC);
  143.   ReleaseDC(hWndMain, hDC);
  144. }
  145.  
  146. void
  147. DragStart(SQUARETYPE square, TPoint& point)
  148. {
  149.   point.x -= 18;
  150.   point.y -= 18;
  151.  
  152.   StartPoint = GetSquareXY(square);
  153.  
  154.   hDragPiece = PieceBmpArray[Board[square].piece-1][Board[square].color];
  155.   hDragMask = MaskArray[Board[square].piece-1];
  156.  
  157.   hPrevCursor = SetCursor(0);
  158.  
  159.   SetClassWindowCursor(hWndMain, 0);
  160.   SetCapture(hWndMain);
  161.   ClearSquare(square);
  162.   SaveBackground(point); 
  163.   ShowPiece(point);  
  164.   IsDragging = TRUE;
  165. }
  166.  
  167. void FAR PASCAL
  168. ReturnHome(int x, int y, LPSTR)
  169. {
  170.   static int Count = AnimationSpeed;
  171.   if (Count) {
  172.     Count--;
  173.     return;
  174.   }
  175.   Count = AnimationSpeed;
  176.   UpdateScreen(TPoint(x,y));
  177. }
  178.  
  179. void
  180. DragEnd(BOOL ValidMove)
  181. {
  182.   if (!IsDragging)
  183.     return;
  184.   if (ValidMove)
  185.     RestoreBackground();
  186.   else {
  187.     LineDDA(CurPoint.x, CurPoint.y, StartPoint.x, StartPoint.y,
  188.           LINEDDAPROC(ReturnHome), 0);
  189.     RestoreBackground();
  190.   }
  191.   SetCursor(hPrevCursor);
  192.   SetClassWindowCursor(hWndMain, hPrevCursor);
  193.   ReleaseCapture();
  194.   IsDragging = FALSE;
  195. }
  196.  
  197. void
  198. Drag(TPoint& p)
  199. {
  200.   p.x -= 18;
  201.   p.y -= 18;
  202.   CurPoint = p;
  203.   UpdateScreen(p);
  204. }
  205.  
  206. void
  207. SlidePiece(SQUARETYPE end, SQUARETYPE start)
  208. {
  209.   TPoint p1 = GetSquareXY(start);
  210.   TPoint p2;
  211.  
  212.   if (IsDragging)
  213.     MessageBox(0, "SlidePiece called before DragEnd", "WChess", MB_OK);
  214.  
  215.   hDragPiece = PieceBmpArray[Board[start].piece-1][Board[start].color];
  216.   hDragMask = MaskArray[Board[start].piece-1];
  217.  
  218.   hPrevCursor = SetCursor(0);
  219.   SetClassWindowCursor(hWndMain, 0);
  220.   ClearSquare(start);
  221.   SaveBackground(p1);
  222.   ShowPiece(p1);
  223.   
  224.   p2 = GetSquareXY(end);
  225.   LineDDA(p1.x, p1.y, p2.x, p2.y, LINEDDAPROC(ReturnHome), 0);
  226.   UpdateScreen(p2);
  227.   DeleteObject(SelectObject(hSaveDC, hOldBmp));
  228.   DeleteDC(hSaveDC);
  229.   SetCursor(hPrevCursor);
  230.   SetClassWindowCursor(hWndMain, hPrevCursor);
  231. }
  232.