home *** CD-ROM | disk | FTP | other *** search
- /*┌──────────────────────────────────────────────────────────────────────────┐*/
- /*│ │*/
- /*│ PROGRAM NAME: MAHJONGG │*/
- /*│ ------------- │*/
- /*│ A PM version of a Chinese game similar to 'concentration' but without │*/
- /*│ the background puzzle. │*/
- /*│ │*/
- /*│ COPYRIGHT: │*/
- /*│ ---------- │*/
- /*│ Copyright (C) International Business Machines Corp., 1991,1992. │*/
- /*│ │*/
- /*│ DISCLAIMER OF WARRANTIES: │*/
- /*│ ------------------------- │*/
- /*│ The following [enclosed] code is sample code created by IBM Corporation.│*/
- /*│ This sample code is not part of any standard IBM product and is provided│*/
- /*│ to you solely for the purpose of assisting you in the development of │*/
- /*│ your applications. The code is provided "AS IS", without warranty of │*/
- /*│ any kind. IBM shall not be liable for any damages arising out of your │*/
- /*│ use of the sample code, even if they have been advised of the │*/
- /*│ possibility of such damages. │*/
- /*│ │*/
- /*│ For details on what this program does etc., please see the MAHJONGG.C │*/
- /*│ file. │*/
- /*│ │*/
- /*└──────────────────────────────────────────────────────────────────────────┘*/
-
- /*┌──────────────────────────────────────────────────────────────────────────┐*/
- /*│ TILE.C │*/
- /*│ │*/
- /*│ Generic Tile Object source file │*/
- /*└──────────────────────────────────────────────────────────────────────────┘*/
-
- /*┌──────────────────────────────────────────────────────────────────────────┐*/
- /*│ Includes │*/
- /*└──────────────────────────────────────────────────────────────────────────┘*/
- #include "mahjongg.h"
-
- PFNWP WCTextProc;
- FNWP NbrSubClass;
-
- /*┌──────────────────────────────────────────────────────────────────────────┐*/
- /*│ is a tile selectable ? │*/
- /*└──────────────────────────────────────────────────────────────────────────┘*/
- BOOL IsTileSelectable(INT id)
- {
- if (Tiles[id].bRemoved) return(FALSE);
-
- /* No tile above the current tile ? */
- if (Neighbors[Tiles[id].iPos][2] != NO_TILE) {
- /* there might be a tile above */
- if (!Tiles[Shuffled[Neighbors[Tiles[id].iPos][2]]].bRemoved) {
- /* there is a tile above */
- return(FALSE);
- } /* endif */
- } /* endif */
-
- /* is the place to the right or left free ? */
- if (Neighbors[Tiles[id].iPos][0] == NO_TILE) {
- return(TRUE);
- } else {
- if (Tiles[Shuffled[Neighbors[Tiles[id].iPos][0]]].bRemoved) {
- return(TRUE);
- } /* endif */
- } /* endif */
- if (Neighbors[Tiles[id].iPos][1] == NO_TILE) {
- return(TRUE);
- } else {
- if (Tiles[Shuffled[Neighbors[Tiles[id].iPos][1]]].bRemoved) {
- return(TRUE);
- } /* endif */
- } /* endif */
- return(FALSE);
- }
-
- /*┌──────────────────────────────────────────────────────────────────────────┐*/
- /*│ Default Tile Window Procedure │*/
- /*└──────────────────────────────────────────────────────────────────────────┘*/
- MRESULT TileWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
-
- {
- switch (msg) {
- case WM_ERASEBACKGROUND:
- return((MRESULT)FALSE);
- break;
- case WM_PAINT:
- { RECTL rectl;
- HPS hps;
- INT id;
- HBITMAP hbm;
-
- hps = WinBeginPaint(hwnd, NULLH, &rectl);
- WinQueryWindowRect(hwnd, &rectl);
- id = WinQueryWindowUShort(hwnd, QWS_ID);
- hbm = GpiLoadBitmap(hps, NULLH, Tiles[id].iTxt+IDB_TILE_C_1, 0L, 0L);
- WinDrawBitmap(hps, hbm, NULL, (PPOINTL)&rectl, 0L, 0L,
- DBM_NORMAL | DBM_STRETCH);
- GpiDeleteBitmap(hbm);
- if (Tiles[id].bSelected) {
- rectl.xLeft += 2*deltaX;
- rectl.yBottom += 2*deltaX;
- rectl.xRight -= deltaX;
- rectl.yTop -= deltaX;
- WinDrawBorder(hps, &rectl, 4, 4, SYSCLR_ACTIVEBORDER, CLR_WHITE,
- DB_PATCOPY);
- } /* endif */
- WinEndPaint(hps);
- } break;
- case WM_BUTTON1UP:
- {
- short id,ids;
- id = WinQueryWindowUShort(hwnd, QWS_ID);
- ids = SHORT1FROMMR(WinSendMsg(hwndBoard, MJ_QUERY_SELECTED, NULL, NULL));
- if (ids == NO_TILE) {
- /* select this tile if possible */
- if (IsTileSelectable(id)) {
- Tiles[id].bSelected = TRUE;
- WinSendMsg(hwndBoard, MJ_INFORM_SELECTED, MPFROMSHORT(id), NULL);
- WinInvalidateRect(hwnd, NULL, FALSE);
- } else {
- if (bSound) DosBeep((USHORT)WinQuerySysValue(HWND_DESKTOP, SV_ERRORFREQ),
- (USHORT)WinQuerySysValue(HWND_DESKTOP, SV_ERRORDURATION));
- } /* endif */
- } else if (ids == id) {
- /* this tile is already selected -> toggle and deselect */
- WinSendMsg(Tiles[ids].hwnd, MJ_DESELECT, NULL, NULL);
- WinSendMsg(hwndBoard, MJ_INFORM_SELECTED, MPFROMSHORT(NO_TILE), NULL);
- } else {
- /* remove the tiles if possible */
- if (IsTileSelectable(id)) {
- /* do the tiles match ? */
- if ((Tiles[ids].suit == Tiles[id].suit) &&
- (Tiles[ids].value == Tiles[id].value)) {
- /* remove the tiles */
- WinSendMsg(Tiles[ids].hwnd, MJ_REMOVE, NULL, NULL);
- WinSendMsg(hwnd, MJ_REMOVE, NULL, NULL);
- WinSendMsg(hwndBoard, MJ_INFORM_SELECTED, MPFROMSHORT(NO_TILE), NULL);
- WinSendMsg(hwndBoard, MJ_DRAW_STATUS, NULL, NULL);
- WinSendMsg(hwndBoard, MJ_PUSH_UNDOCMD, MPFROM2SHORT(id, ids), NULL);
- } else {
- /* the tiles did not match, deselect */
- WinSendMsg(Tiles[ids].hwnd, MJ_DESELECT, NULL, NULL);
- WinSendMsg(hwndBoard, MJ_INFORM_SELECTED, MPFROMSHORT(NO_TILE), NULL);
- } /* endif */
- } else {
- if (bSound)
- DosBeep((USHORT)WinQuerySysValue(HWND_DESKTOP, SV_ERRORFREQ),
- (USHORT)WinQuerySysValue(HWND_DESKTOP, SV_ERRORDURATION));
- } /* endif */
- } /* endif */
- } break;
- case MJ_SELECT:
- {
- INT id;
- id = WinQueryWindowUShort(hwnd, QWS_ID);
- Tiles[id].bSelected = TRUE;
- WinInvalidateRect(hwnd, NULL, FALSE);
- WinUpdateWindow(hwnd);
- } break;
- case MJ_DESELECT:
- { INT id;
- id = WinQueryWindowUShort(hwnd, QWS_ID);
- Tiles[id].bSelected = FALSE;
- WinInvalidateRect(hwnd, NULL, FALSE);
- WinUpdateWindow(hwnd);
- } break;
- case MJ_REMOVE:
- { INT id;
- id = WinQueryWindowUShort(hwnd, QWS_ID);
- Tiles[id].bSelected = FALSE;
- Tiles[id].bRemoved = TRUE;
- NbrOfTilesRemaining--;
- if (NbrOfTilesRemaining == 0) {
- WinPostMsg(hwndBoard, MJ_SUCCESS, NULL, NULL);
- } /* endif */
- WinSetWindowPos(hwnd, HWND_TOP, 0,0,0,0, SWP_HIDE);
- } break;
- default:
- return(WinDefWindowProc(hwnd, msg, mp1, mp2));
- } /* endswitch */
- return((MRESULT)NULL);
- }