home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / CHESS.ZIP / DRAG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  7.0 KB  |  282 lines

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