home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / WKFRAME / MAHJONGG / TILE.C < prev   
Text File  |  1993-02-28  |  8KB  |  182 lines

  1. /*┌──────────────────────────────────────────────────────────────────────────┐*/
  2. /*│                                                                          │*/
  3. /*│ PROGRAM NAME: MAHJONGG                                                   │*/
  4. /*│ -------------                                                            │*/
  5. /*│  A PM version of a Chinese game similar to 'concentration' but without   │*/
  6. /*│  the background puzzle.                                                  │*/
  7. /*│                                                                          │*/
  8. /*│ COPYRIGHT:                                                               │*/
  9. /*│ ----------                                                               │*/
  10. /*│  Copyright (C) International Business Machines Corp., 1991,1992,1993.    │*/
  11. /*│                                                                          │*/
  12. /*│ DISCLAIMER OF WARRANTIES:                                                │*/
  13. /*│ -------------------------                                                │*/
  14. /*│  The following [enclosed] code is sample code created by IBM Corporation.│*/
  15. /*│  This sample code is not part of any standard IBM product and is provided│*/
  16. /*│  to you solely for the purpose of assisting you in the development of    │*/
  17. /*│  your applications.  The code is provided "AS IS", without warranty of   │*/
  18. /*│  any kind.  IBM shall not be liable for any damages arising out of your  │*/
  19. /*│  use of the sample code, even if they have been advised of the           │*/
  20. /*│  possibility of such damages.                                            │*/
  21. /*│                                                                          │*/
  22. /*│ For details on what this program does etc., please see the MAHJONGG.C    │*/
  23. /*│ file.                                                                    │*/
  24. /*│                                                                          │*/
  25. /*└──────────────────────────────────────────────────────────────────────────┘*/
  26.  
  27. /*┌──────────────────────────────────────────────────────────────────────────┐*/
  28. /*│ TILE.C                                                                   │*/
  29. /*│                                                                          │*/
  30. /*│ Generic Tile Object source file                                          │*/
  31. /*└──────────────────────────────────────────────────────────────────────────┘*/
  32.  
  33. /*┌──────────────────────────────────────────────────────────────────────────┐*/
  34. /*│ Includes                                                                 │*/
  35. /*└──────────────────────────────────────────────────────────────────────────┘*/
  36. #include "mahjongg.h"
  37.  
  38. PFNWP   WCTextProc;
  39. FNWP    NbrSubClass;
  40.  
  41. /*┌──────────────────────────────────────────────────────────────────────────┐*/
  42. /*│ is a tile selectable ?                                                   │*/
  43. /*└──────────────────────────────────────────────────────────────────────────┘*/
  44. BOOL IsTileSelectable(INT id)
  45. {
  46.   if (Tiles[id].bRemoved) return(FALSE);
  47.  
  48.   /* No tile above the current tile ? */
  49.   if (Neighbors[Tiles[id].iPos][2] != NO_TILE) {
  50.     /* there might be a tile above */
  51.     if (!Tiles[Shuffled[Neighbors[Tiles[id].iPos][2]]].bRemoved) {
  52.       /* there is a tile above */
  53.       return(FALSE);
  54.     } /* endif */
  55.   } /* endif */
  56.  
  57.   /* is the place to the right or left free ? */
  58.   if (Neighbors[Tiles[id].iPos][0] == NO_TILE) {
  59.     return(TRUE);
  60.   } else {
  61.     if (Tiles[Shuffled[Neighbors[Tiles[id].iPos][0]]].bRemoved) {
  62.       return(TRUE);
  63.     } /* endif */
  64.   } /* endif */
  65.   if (Neighbors[Tiles[id].iPos][1] == NO_TILE) {
  66.     return(TRUE);
  67.   } else {
  68.     if (Tiles[Shuffled[Neighbors[Tiles[id].iPos][1]]].bRemoved) {
  69.       return(TRUE);
  70.     } /* endif */
  71.   } /* endif */
  72.   return(FALSE);
  73. }
  74.  
  75. /*┌──────────────────────────────────────────────────────────────────────────┐*/
  76. /*│ Default Tile Window Procedure                                            │*/
  77. /*└──────────────────────────────────────────────────────────────────────────┘*/
  78. MRESULT TileWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  79.  
  80. {
  81.   switch (msg) {
  82.     case WM_ERASEBACKGROUND:
  83.       return((MRESULT)FALSE);
  84.       break;
  85.     case WM_PAINT:
  86.       { RECTL    rectl;
  87.         HPS      hps;
  88.         INT      id;
  89.         HBITMAP  hbm;
  90.  
  91.         hps = WinBeginPaint(hwnd, NULLH, &rectl);
  92.         WinQueryWindowRect(hwnd, &rectl);
  93.         id = WinQueryWindowUShort(hwnd, QWS_ID);
  94.         hbm = GpiLoadBitmap(hps, NULLH, Tiles[id].iTxt+IDB_TILE_C_1, 0L, 0L);
  95.         WinDrawBitmap(hps, hbm, NULL, (PPOINTL)&rectl, 0L, 0L,
  96.                       DBM_NORMAL | DBM_STRETCH);
  97.         GpiDeleteBitmap(hbm);
  98.         if (Tiles[id].bSelected) {
  99.           rectl.xLeft   += 2*deltaX;
  100.           rectl.yBottom += 2*deltaX;
  101.           rectl.xRight  -= deltaX;
  102.           rectl.yTop    -= deltaX;
  103.           WinDrawBorder(hps, &rectl, 4, 4, SYSCLR_ACTIVEBORDER, CLR_WHITE,
  104.                         DB_PATCOPY);
  105.         } /* endif */
  106.         WinEndPaint(hps);
  107.       } break;
  108.     case WM_BUTTON1UP:
  109.       {
  110.       short id,ids;
  111.         id  = WinQueryWindowUShort(hwnd, QWS_ID);
  112.         ids = SHORT1FROMMR(WinSendMsg(hwndBoard, MJ_QUERY_SELECTED, NULL, NULL));
  113.         if (ids == NO_TILE) {
  114.           /* select this tile if possible */
  115.           if (IsTileSelectable(id)) {
  116.             Tiles[id].bSelected = TRUE;
  117.             WinSendMsg(hwndBoard, MJ_INFORM_SELECTED, MPFROMSHORT(id), NULL);
  118.             WinInvalidateRect(hwnd, NULL, FALSE);
  119.           } else {
  120.             if (bSound) DosBeep((USHORT)WinQuerySysValue(HWND_DESKTOP, SV_ERRORFREQ),
  121.                                 (USHORT)WinQuerySysValue(HWND_DESKTOP, SV_ERRORDURATION));
  122.           } /* endif */
  123.         } else if (ids == id) {
  124.           /* this tile is already selected -> toggle and deselect */
  125.           WinSendMsg(Tiles[ids].hwnd, MJ_DESELECT, NULL, NULL);
  126.           WinSendMsg(hwndBoard, MJ_INFORM_SELECTED, MPFROMSHORT(NO_TILE), NULL);
  127.         } else {
  128.           /* remove the tiles if possible */
  129.           if (IsTileSelectable(id)) {
  130.             /* do the tiles match ? */
  131.           if ((Tiles[ids].suit == Tiles[id].suit) &&
  132.                 (Tiles[ids].value == Tiles[id].value)) {
  133.               /* remove the tiles */
  134.               WinSendMsg(Tiles[ids].hwnd, MJ_REMOVE, NULL, NULL);
  135.               WinSendMsg(hwnd, MJ_REMOVE, NULL, NULL);
  136.               WinSendMsg(hwndBoard, MJ_INFORM_SELECTED, MPFROMSHORT(NO_TILE), NULL);
  137.               WinSendMsg(hwndBoard, MJ_DRAW_STATUS, NULL, NULL);
  138.               WinSendMsg(hwndBoard, MJ_PUSH_UNDOCMD, MPFROM2SHORT(id, ids), NULL);
  139.           } else {
  140.               /* the tiles did not match, deselect */
  141.               WinSendMsg(Tiles[ids].hwnd, MJ_DESELECT, NULL, NULL);
  142.               WinSendMsg(hwndBoard, MJ_INFORM_SELECTED, MPFROMSHORT(NO_TILE), NULL);
  143.             } /* endif */
  144.           } else {
  145.             if (bSound)
  146.                DosBeep((USHORT)WinQuerySysValue(HWND_DESKTOP, SV_ERRORFREQ),
  147.                        (USHORT)WinQuerySysValue(HWND_DESKTOP, SV_ERRORDURATION));
  148.           } /* endif */
  149.         } /* endif */
  150.       } break;
  151.     case MJ_SELECT:
  152.       {
  153.         INT id;
  154.         id  = WinQueryWindowUShort(hwnd, QWS_ID);
  155.         Tiles[id].bSelected = TRUE;
  156.         WinInvalidateRect(hwnd, NULL, FALSE);
  157.         WinUpdateWindow(hwnd);
  158.       } break;
  159.     case MJ_DESELECT:
  160.       { INT id;
  161.         id  = WinQueryWindowUShort(hwnd, QWS_ID);
  162.         Tiles[id].bSelected = FALSE;
  163.         WinInvalidateRect(hwnd, NULL, FALSE);
  164.         WinUpdateWindow(hwnd);
  165.       } break;
  166.     case MJ_REMOVE:
  167.       { INT id;
  168.         id  = WinQueryWindowUShort(hwnd, QWS_ID);
  169.         Tiles[id].bSelected = FALSE;
  170.         Tiles[id].bRemoved  = TRUE;
  171.         NbrOfTilesRemaining--;
  172.         if (NbrOfTilesRemaining == 0) {
  173.           WinPostMsg(hwndBoard, MJ_SUCCESS, NULL, NULL);
  174.         } /* endif */
  175.         WinSetWindowPos(hwnd, HWND_TOP, 0,0,0,0, SWP_HIDE);
  176.       } break;
  177.     default:
  178.       return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  179.   } /* endswitch */
  180.   return((MRESULT)NULL);
  181. }
  182.