home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / rpc / mazelord / mazedlg.c < prev    next >
C/C++ Source or Header  |  1997-10-10  |  16KB  |  509 lines

  1. /***********************************************************************
  2. File:   MazeDlg.c
  3.  
  4.  
  5. Abstract:
  6.  
  7.     This module contains dialog boxes for Maze Lords.
  8.  
  9.  
  10. Contents:
  11.  
  12.     DroneDlg() -- Drone Dialog Box winproc
  13.     AboutDlg() -- About Box
  14.     IntoneDlg() -- Dialog box for spoken messages
  15.     PlayerDlg() -- Dialog box for selecting player picture/maze
  16.     cwCenter() -- Center a given dialog box in the window
  17.  
  18.  
  19. Revision History:
  20.  
  21. ************************************************************************/
  22.  
  23. #include "winmaze.h"
  24. #include "mazproto.h"
  25. #include "mazedlg.h"
  26.  
  27.  
  28.  
  29. int iCurPic,iCurMaze;
  30.  
  31.  
  32. /*=====================================================================
  33. Function:   DroneDlg()
  34.  
  35. Inputs:     Standard Dialog inputs
  36.  
  37. Outputs:    Returns close status
  38.  
  39. Abstract:
  40.     This dialog box allows the user to input # of drones and their movement
  41.     speed.
  42. ======================================================================*/
  43.  
  44. BOOL FAR PASCAL DroneDlg(
  45.     HWND hWndDlg,
  46.     UINT Message,
  47.     WPARAM wParam,
  48.     LPARAM lParam
  49.     )
  50. {
  51.     BOOL bRet;  // holding variable only.
  52.     UINT uCmdId,uCmdCmd;
  53.     HWND hCmd;
  54.     static INT iOldNumDrones,iOldDroneSpeed;
  55.     int i;
  56.     char c[132];
  57.  
  58.  
  59.     switch(Message) {
  60.         case WM_INITDIALOG:
  61.             cwCenter(hWndDlg, 0);
  62.             iOldNumDrones = iNumDrones;
  63.             iOldDroneSpeed = iDroneSpeed;
  64.             SetDlgItemInt(hWndDlg,DLG_NUMDRONES,iNumDrones,TRUE);
  65.             SetDlgItemInt(hWndDlg,DLG_MOVESPEED,iDroneSpeed,TRUE);
  66.             break;
  67.  
  68.         case WM_CLOSE:
  69.             PostMessage(hWndDlg, WM_COMMAND, IDCANCEL, 0L);
  70.             break;
  71.  
  72.         case WM_COMMAND:
  73.             uCmdId = GET_WM_COMMAND_ID(wParam,lParam);
  74.             uCmdCmd = GET_WM_COMMAND_CMD(wParam,lParam);
  75.             hCmd = GET_WM_COMMAND_HWND(wParam,lParam);
  76.             switch(uCmdId) {
  77.                 case DLG_NUMDRONES:
  78.                     if (uCmdCmd == EN_CHANGE) {
  79.                         i = GetDlgItemInt(hWndDlg,DLG_NUMDRONES,&bRet,TRUE);
  80.                         if ((i < 0)||(i > MAX_DRONES)) {
  81.                             MessageBeep(MB_ICONHAND);
  82.                             sprintf(c,GetStringRes(IDS_FMT_BADNUMDRONES),
  83.                                     MAX_DRONES);
  84.                             MessageBox(hWndDlg,c,GetStringRes(IDS_ERRINPUT),MB_APPLMODAL|MB_ICONHAND);
  85.                             SetDlgItemInt(hWndDlg,DLG_NUMDRONES,iOldNumDrones,TRUE);
  86.                         }
  87.  
  88.                     }
  89.                     break;
  90.  
  91.                 case DLG_MOVESPEED:
  92.                     if (uCmdCmd == EN_CHANGE) {
  93.                         i = GetDlgItemInt(hWndDlg,DLG_MOVESPEED,&bRet,TRUE);
  94.                         if ((i < 0)||(i > MAX_DRONE_SPEED)) {
  95.                             MessageBeep(MB_ICONHAND);
  96.                             sprintf(c,GetStringRes(IDS_FMT_BADMOVESPERSEC),
  97.                                     MAX_DRONE_SPEED);
  98.                             MessageBox(hWndDlg,c,GetStringRes(IDS_ERRINPUT),MB_APPLMODAL|MB_ICONHAND);
  99.                             SetDlgItemInt(hWndDlg,DLG_MOVESPEED,iOldDroneSpeed,TRUE);
  100.                         }
  101.  
  102.                     }
  103.                     break;
  104.  
  105.                 case IDOK:
  106.                     iNumDrones = GetDlgItemInt(hWndDlg,DLG_NUMDRONES,&bRet,TRUE);
  107.                     iDroneSpeed = GetDlgItemInt(hWndDlg,DLG_MOVESPEED,&bRet,TRUE);
  108.                     if ((iOldNumDrones != iNumDrones)&&GameStarted) {
  109.                         PostMessage(hWndMaze,WM_COMMAND,IDM_REDRAW,(DWORD)NULL);
  110.                         }
  111.  
  112.                     EndDialog(hWndDlg, TRUE);
  113.                     break;
  114.  
  115.                 case IDCANCEL:
  116.                     EndDialog(hWndDlg, FALSE);
  117.                     break;
  118.                 }
  119.             break;
  120.  
  121.         default:
  122.             return FALSE;
  123.         }
  124.  
  125.     return(TRUE);
  126. }
  127.  
  128.  
  129.  
  130. /*=====================================================================
  131. Function:   AboutDlg()
  132.  
  133. Inputs:     Standard Dialog box parms
  134.  
  135. Outputs:    Returns close status
  136.  
  137. Abstract:
  138.     This procedure displays the About dialog box.
  139. ======================================================================*/
  140.  
  141. BOOL FAR PASCAL AboutDlg(
  142.     HWND hWndDlg,
  143.     UINT Message,
  144.     WPARAM wParam,
  145.     LPARAM lParam
  146.     )
  147. {
  148.     UINT uCmdId,uCmdCmd;
  149.     HWND hCmd;
  150.  
  151.     switch(Message) {
  152.  
  153.         case WM_INITDIALOG:
  154.             cwCenter(hWndDlg, 0);
  155.             break;
  156.  
  157.         case WM_CLOSE:
  158.             PostMessage(hWndDlg, WM_COMMAND, IDCANCEL, 0L);
  159.             break;
  160.  
  161.         case WM_COMMAND:
  162.             uCmdId = GET_WM_COMMAND_ID(wParam,lParam);
  163.             uCmdCmd = GET_WM_COMMAND_CMD(wParam,lParam);
  164.             hCmd = GET_WM_COMMAND_HWND(wParam,lParam);
  165.             switch(uCmdId) {
  166.                 case IDOK:
  167.                     EndDialog(hWndDlg, TRUE);
  168.                     break;
  169.                 }
  170.             break;
  171.  
  172.         default:
  173.             return FALSE;
  174.         }
  175.  
  176.     return(TRUE);
  177. }
  178.  
  179.  
  180. /*=====================================================================
  181. Function:   IntoneDlg()
  182.  
  183. Inputs:     Standard Dialog Box Parms
  184.  
  185. Outputs:    Returns dialog close conditions
  186.  
  187. Abstract:
  188.     This dialog allows the user to type a message to whisper or shout.
  189.     It is called by both commands, with the loudness having been set
  190.     before the dialog was invoked by the method it was invoked, either
  191.     whispering or shouting.
  192. ======================================================================*/
  193.  
  194. BOOL FAR PASCAL IntoneDlg(
  195.     HWND hWndDlg,
  196.     UINT Message,
  197.     WPARAM wParam,
  198.     LPARAM lParam
  199.     )
  200. {
  201.     IntoneType itTemp;
  202.     BOOL bScratch;
  203.     UINT uCmdId;
  204.  
  205.     switch(Message) {
  206.  
  207.         case WM_INITDIALOG:
  208.             cwCenter(hWndDlg, 0);
  209.             SetDlgItemInt(hWndDlg,DLG_INT_LOUDNESS,iLoudness,TRUE);
  210.             SetDlgItemText(hWndDlg,DLG_INT_SAY,"");
  211.             break;
  212.  
  213.         case WM_CLOSE:
  214.             PostMessage(hWndDlg, WM_COMMAND, IDCANCEL, 0L);
  215.             break;
  216.  
  217.         case WM_COMMAND:
  218.             uCmdId = GET_WM_COMMAND_ID(wParam,lParam);
  219.             switch(uCmdId) {
  220.  
  221.                 case DLG_INT_LOUDNESS:
  222.                     break;
  223.  
  224.                 case DLG_INT_SAY:
  225.                     break;
  226.  
  227.                 case IDOK:
  228.                     GetDlgItemText(hWndDlg,DLG_INT_SAY,itTemp.cBuff,131);
  229.                     itTemp.dwLen=lstrlen(itTemp.cBuff);
  230.                     iLoudness = GetDlgItemInt(hWndDlg,DLG_INT_LOUDNESS,&bScratch,FALSE);
  231.                     itTemp.iLoudness= iLoudness;
  232.                     itTemp.Pos = ptSelf.Pos;
  233.                     SendNetMessage(0,0,&itTemp,NP_INTONE);
  234.                     EndDialog(hWndDlg, TRUE);
  235.                     break;
  236.  
  237.                 case IDCANCEL:
  238.                     EndDialog(hWndDlg, FALSE);
  239.                     break;
  240.                 }
  241.             break;
  242.  
  243.         default:
  244.             return FALSE;
  245.         }
  246.  
  247.  
  248.     return(TRUE);
  249. }
  250.  
  251.  
  252.  
  253. /*=====================================================================
  254. Function:   PlayerDlg()
  255.  
  256. Inputs:     Standard Dialog box parms
  257.  
  258. Outputs:    returns dialog termination condition
  259.  
  260. Abstract:
  261.     This dialog box uses two custom list-boxes to allow the player to
  262.     select options. The first contains all possible player bitmaps. The
  263.     second contains all possible player sub-grids to choose from.
  264. ======================================================================*/
  265.  
  266. BOOL FAR PASCAL PlayerDlg(
  267.     HWND hWndDlg,
  268.     UINT Message,
  269.     WPARAM wParam,
  270.     LPARAM lParam
  271.     )
  272. {
  273.     int i,j,k,x,y;
  274.     MEASUREITEMSTRUCT FAR *miInfo;
  275.     DRAWITEMSTRUCT FAR *diInfo;
  276.     HDC hDC,hPicDC;
  277.     FullPicType FAR *fptTrav;
  278.     BOOL bFound;
  279.     LPPOINT p;
  280. //[5*5*5*2+2];
  281.     LPBYTE bType;
  282. //[5*5*5*2+2],
  283.     BYTE b;
  284.     int iNumPts,Step;
  285.     HGLOBAL hPMem,hBMem;
  286.     float fAspect;
  287.  
  288.  
  289.     switch(Message) {
  290.  
  291.         case WM_INITDIALOG:
  292.             cwCenter(hWndDlg, 0);
  293.  
  294.             iCurPic = iCurMaze = 0;
  295.             if (PIC_DRONE == 0) {
  296.                 iCurPic = 1;
  297.                 }
  298.             for (i=0;i<NUM_PICS;i++) {
  299.                 if (i != PIC_DRONE) {
  300.                     AddPic(i);
  301.                     SendDlgItemMessage(hWndDlg,PC_DLG_PIC,LB_ADDSTRING,0,i);
  302.                     }
  303.                 }
  304.             for (i=1;i<NUM_SUBGRIDS-1;i++) {
  305.                 SendDlgItemMessage(hWndDlg,PC_DLG_MAZE,LB_ADDSTRING,0,i);
  306.                 }
  307.             break;
  308.  
  309.         case WM_MEASUREITEM:
  310.             miInfo = (MEASUREITEMSTRUCT FAR *) lParam;
  311.             miInfo->CtlType = ODT_LISTBOX;
  312.             miInfo->itemID = 0;     // Not used
  313.  
  314.             if (wParam == PC_DLG_PIC) {
  315.                 miInfo->CtlID = PC_DLG_PIC;
  316.                 miInfo->itemWidth = 20;
  317.                 miInfo->itemHeight = 45;
  318.                 miInfo->itemData = 0;
  319.                 }
  320.             else {
  321.                 miInfo->CtlID = PC_DLG_MAZE;
  322.                 miInfo->itemWidth = 30;
  323.                 miInfo->itemHeight = 45;
  324.                 miInfo->itemData = 0;
  325.                 }
  326.             break;
  327.  
  328.         case WM_DRAWITEM:
  329.  
  330.             diInfo = (DRAWITEMSTRUCT FAR *) lParam;
  331.             hDC = diInfo->hDC;
  332.             switch(diInfo->CtlID) {
  333.  
  334.                 case PC_DLG_PIC:
  335.                     if (diInfo->itemAction == ODA_DRAWENTIRE) {
  336.                         hPicDC = CreateCompatibleDC(hDC);
  337.                         fptTrav = &fptPic;
  338.                         bFound = FALSE;
  339.                         while (fptTrav->next != (FullPicType FAR *)NULL) {
  340.                             fptTrav = fptTrav->next;
  341.                             if (fptTrav->iPicNum == (int)diInfo->itemData) {
  342.                                 bFound = TRUE;
  343.                                 break;
  344.                                 }
  345.                             }
  346.  
  347.                         if (bFound) {
  348.                             iCurPic = (int)diInfo->itemData;
  349.                             SelectObject(hPicDC,fptTrav->P[1].hBitmap);
  350.                             x = diInfo->rcItem.right - diInfo->rcItem.left;
  351.                             y = diInfo->rcItem.bottom - diInfo->rcItem.top;
  352.                             fAspect=((float) fptTrav->P[1].ySize)/
  353.                                     ((float) fptTrav->P[1].xSize);
  354.  
  355.                             StretchBlt(hDC,diInfo->rcItem.left,diInfo->rcItem.top,
  356.                                        (int) ((x < (y/fAspect))? x : y/fAspect),
  357.                                        (int) ((y < (x*fAspect))? y : x*fAspect),
  358.                                        hPicDC,0,0,
  359.                                        fptTrav->P[1].xSize,
  360.                                        fptTrav->P[1].ySize,
  361.                                        SRCCOPY);
  362.                             }
  363.  
  364.                         DeleteDC(hPicDC);
  365.                         }
  366.  
  367.                     break;
  368.  
  369.                 case PC_DLG_MAZE:
  370.                     hPMem = GlobalAlloc(GHND,(3*5*5*4*2 + 5)*sizeof(POINT));
  371.                     p = (LPPOINT) GlobalLock(hPMem);
  372.                     hBMem = GlobalAlloc(GHND,(3*5*5*4*2 + 5)*sizeof(BYTE));
  373.                     bType = (LPBYTE) GlobalLock(hBMem);
  374.  
  375.                     if (diInfo->itemAction == ODA_DRAWENTIRE) {
  376.                         x = diInfo->rcItem.left;
  377.                         y = diInfo->rcItem.top;
  378.                         k = (int)diInfo->itemData;
  379.                         iNumPts = 0;
  380.                         Step = 9;
  381.                         iCurMaze = (int)diInfo->itemData;
  382.  
  383.                         for (i = 0;i<5; i++) {
  384.                             for (j= 0; j<5; j++) {
  385.                                 b = SubGrids[k].Cell[i][j];
  386.                                 if (b&NORTH) {
  387.                                     p[iNumPts].x = i*Step;
  388.                                     p[iNumPts].y = j*Step;
  389.                                     bType[iNumPts++] = PT_MOVETO;
  390.                                     p[iNumPts].x = (i+1)*Step;
  391.                                     p[iNumPts].y = j*Step;
  392.                                     bType[iNumPts++] = PT_LINETO;
  393.                                     }
  394.                                 if (b&SOUTH) {
  395.                                     p[iNumPts].x = i*Step;
  396.                                     p[iNumPts].y = (j+1)*Step;
  397.                                     bType[iNumPts++] = PT_MOVETO;
  398.                                     p[iNumPts].x = (i+1)*Step;
  399.                                     p[iNumPts].y = (j+1)*Step;
  400.                                     bType[iNumPts++] = PT_LINETO;
  401.                                     }
  402.                                 if (b&WEST) {
  403.                                     p[iNumPts].x = i*Step;
  404.                                     p[iNumPts].y = j*Step;
  405.                                     bType[iNumPts++] = PT_MOVETO;
  406.                                     p[iNumPts].x = i*Step;
  407.                                     p[iNumPts].y = (j+1)*Step;
  408.                                     bType[iNumPts++] = PT_LINETO;
  409.                                     }
  410.                                 if (b&EAST) {
  411.                                     p[iNumPts].x = (i+1)*Step;
  412.                                     p[iNumPts].y = j*Step;
  413.                                     bType[iNumPts++] = PT_MOVETO;
  414.                                     p[iNumPts].x = (i+1)*Step;
  415.                                     p[iNumPts].y = (j+1)*Step;
  416.                                     bType[iNumPts++] = PT_LINETO;
  417.                                     }
  418.                                 }
  419.                             }
  420.                         PolyDraw95(hDC,p,bType,iNumPts);
  421.                         }
  422.  
  423.                     GlobalUnlock(hBMem);
  424.                     GlobalUnlock(hPMem);
  425.                     GlobalFree(hBMem);
  426.                     GlobalFree(hPMem);
  427.                     break;
  428.  
  429.                 default:
  430.                     break;
  431.                 }
  432. //BUGBUG -- shouldn't it be hWindow,hDC???
  433.             ReleaseDC((HWND) diInfo->hDC,hDC);
  434.             break;
  435.  
  436.         case WM_CLOSE:
  437.             for(i=0;i<NUM_PICS;i++) {
  438.                 if (ptSelf.iPicNum != i) {
  439.                     DelPic(i);
  440.                     }
  441.                 }
  442.             PostMessage(hWndDlg, WM_COMMAND, IDCANCEL, 0L);
  443.             break;
  444.  
  445.         case WM_COMMAND:
  446.             switch(wParam) {
  447.                 case IDOK:
  448.                     ptSelf.iGridNum = iCurMaze;
  449.                     ptSelf.iPicNum = iCurPic;
  450.                     EndDialog(hWndDlg, TRUE);
  451.                     break;
  452.                 case IDCANCEL:
  453.                     EndDialog(hWndDlg, FALSE);
  454.                     break;
  455.                 }
  456.             break;
  457.  
  458.         default:
  459.             return FALSE;
  460.         }
  461.  
  462.     return(TRUE);
  463. }
  464.  
  465.  
  466.  
  467. /*=====================================================================
  468. Function:   cwCenter()
  469.  
  470. Inputs:     Handle to window to center, top of window
  471.  
  472. Outputs:    none
  473.  
  474. Abstract:
  475.     Does a MoveWindow to center the window in question.
  476. ======================================================================*/
  477.  
  478. void cwCenter(
  479.     HWND hWnd,
  480.     int top
  481.     )
  482. {
  483.     POINT    pt;
  484.     RECT     swp;
  485.     RECT     rParent;
  486.     int      iwidth;
  487.     int      iheight;
  488.  
  489.     GetWindowRect(hWnd, &swp);
  490.     GetClientRect(hWndMain, &rParent);
  491.  
  492.     iwidth = swp.right - swp.left;
  493.     iheight = swp.bottom - swp.top;
  494.  
  495.     pt.x = (rParent.right - rParent.left) / 2;
  496.     pt.y = (rParent.bottom - rParent.top) / 2;
  497.     ClientToScreen(hWndMain, &pt);
  498.  
  499.     pt.x -= (iwidth / 2);
  500.     pt.y -= (iheight / 2);
  501.  
  502.     if (top) {
  503.         pt.y += top;
  504.         }
  505.  
  506.     MoveWindow(hWnd, pt.x, pt.y, iwidth, iheight, FALSE);
  507. }
  508.  
  509.