ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

spider.cpp
Go to the documentation of this file.
00001 /*
00002  * PROJECT:      Spider Solitaire
00003  * LICENSE:      See COPYING in top level directory
00004  * FILE:         base/applications/games/spider/spider.cpp
00005  * PURPOSE:      Window and message queue for Spider Solitaire
00006  * PROGRAMMER:   Gregor Schneider
00007  */
00008 
00009 #include "spider.h"
00010 
00011 TCHAR szHelpPath[MAX_PATH];
00012 
00013 DWORD        dwAppStartTime;
00014 HWND         hwndMain;
00015 HINSTANCE    hInstance;
00016 
00017 TCHAR szAppName[128];
00018 TCHAR MsgQuit[128];
00019 TCHAR MsgAbout[128];
00020 TCHAR MsgWin[128];
00021 TCHAR MsgDeal[128];
00022 DWORD dwDifficulty;
00023 
00024 CardWindow SpiderWnd;
00025 
00026 typedef struct _CardBack
00027 {
00028     HWND hSelf;
00029     WNDPROC hOldProc;
00030     INT hdcNum;
00031     INT imgNum;
00032     BOOL bSelected;
00033 } CARDBACK, *PCARDBACK;
00034 
00035 LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
00036 
00037 void MakePath(TCHAR *szDest, UINT nDestLen, const TCHAR *szExt)
00038 {
00039     TCHAR *ptr;
00040 
00041     ptr = szDest + GetModuleFileName(GetModuleHandle(0), szDest, nDestLen) - 1;
00042     while(*ptr-- != '.');
00043     lstrcpy(ptr + 1, szExt);
00044 }
00045 
00046 INT_PTR CALLBACK DifficultyDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
00047 {
00048     switch (uMsg)
00049     {
00050     case WM_INITDIALOG:
00051         CheckRadioButton(hDlg, IDC_DIF_ONECOLOR, IDC_DIF_FOURCOLORS, IDC_DIF_ONECOLOR);
00052         return TRUE;
00053 
00054     case WM_COMMAND:
00055         switch(LOWORD(wParam))
00056         {
00057         case IDOK:
00058             if (IsDlgButtonChecked(hDlg, IDC_DIF_ONECOLOR) == BST_CHECKED)
00059                 dwDifficulty = IDC_DIF_ONECOLOR;
00060             else if (IsDlgButtonChecked(hDlg, IDC_DIF_TWOCOLORS) == BST_CHECKED)
00061                 dwDifficulty = IDC_DIF_TWOCOLORS;
00062             else if (IsDlgButtonChecked(hDlg, IDC_DIF_FOURCOLORS) == BST_CHECKED)
00063                 dwDifficulty = IDC_DIF_FOURCOLORS;
00064 
00065             NewGame();
00066             EndDialog(hDlg, TRUE);
00067             return TRUE;
00068 
00069         case IDCANCEL:
00070             EndDialog(hDlg, FALSE);
00071             return TRUE;
00072         }
00073         break;
00074     }
00075     return FALSE;
00076 }
00077 
00078 int WINAPI _tWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int iCmdShow)
00079 {
00080     HWND        hwnd;
00081     MSG            msg;
00082     WNDCLASS    wndclass;
00083     INITCOMMONCONTROLSEX ice;
00084     HACCEL        hAccelTable;
00085 
00086     hInstance = hInst;
00087 
00088     /* Load application title */
00089     LoadString(hInst, IDS_SPI_NAME, szAppName, sizeof(szAppName) / sizeof(szAppName[0]));
00090     /* Load MsgBox() texts here to avoid loading them many times later */
00091     LoadString(hInst, IDS_SPI_ABOUT, MsgAbout, sizeof(MsgAbout) / sizeof(MsgAbout[0]));
00092     LoadString(hInst, IDS_SPI_QUIT, MsgQuit, sizeof(MsgQuit) / sizeof(MsgQuit[0]));
00093     LoadString(hInst, IDS_SPI_WIN, MsgWin, sizeof(MsgWin) / sizeof(MsgWin[0]));
00094     LoadString(hInst, IDS_SPI_DEAL, MsgDeal, sizeof(MsgDeal) / sizeof(MsgDeal[0]));
00095 
00096     /* Window class for the main application parent window */
00097     wndclass.style             = 0;
00098     wndclass.lpfnWndProc       = WndProc;
00099     wndclass.cbClsExtra        = 0;
00100     wndclass.cbWndExtra        = 0;
00101     wndclass.hInstance         = hInst;
00102     wndclass.hIcon             = LoadIcon (hInst, MAKEINTRESOURCE(IDI_SPIDER));
00103     wndclass.hCursor           = LoadCursor (NULL, IDC_ARROW);
00104     wndclass.hbrBackground     = (HBRUSH)NULL;
00105     wndclass.lpszMenuName      = MAKEINTRESOURCE(IDR_MENU1);
00106     wndclass.lpszClassName     = szAppName;
00107 
00108     RegisterClass(&wndclass);
00109 
00110     ice.dwSize = sizeof(ice);
00111     ice.dwICC = ICC_BAR_CLASSES;
00112     InitCommonControlsEx(&ice);
00113 
00114     srand((unsigned)GetTickCount());
00115 
00116     /* InitCardLib(); */
00117 
00118     /* Construct the path to our help file */
00119     MakePath(szHelpPath, MAX_PATH, _T(".hlp"));
00120 
00121     hwnd = CreateWindow(szAppName,
00122                 szAppName,
00123                 WS_OVERLAPPEDWINDOW,
00124                 CW_USEDEFAULT,
00125                 CW_USEDEFAULT,
00126                 0,  /*The real size will be computed in WndProc through WM_GETMINMAXINFO */
00127                 0,  /* The real size will be computed in WndProc through WM_GETMINMAXINFO */
00128                 NULL,
00129                 NULL,
00130                 hInst,
00131                 NULL);
00132 
00133     hwndMain = hwnd;
00134 
00135     ShowWindow(hwnd, iCmdShow);
00136     UpdateWindow(hwnd);
00137 
00138     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR1));
00139 
00140     DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIFFICULTY), hwnd, DifficultyDlgProc);
00141 
00142     while(GetMessage(&msg, NULL,0,0))
00143     {
00144         if(!TranslateAccelerator(hwnd, hAccelTable, &msg))
00145         {
00146             TranslateMessage(&msg);
00147             DispatchMessage(&msg);
00148         }
00149     }
00150     return msg.wParam;
00151 }
00152 
00153 LRESULT CALLBACK
00154 CardImageWndProc(HWND hwnd,
00155                  UINT msg,
00156                  WPARAM wParam,
00157                  LPARAM lParam)
00158 {
00159     PCARDBACK pCardBack = (PCARDBACK)GetWindowLongPtr(hwnd,
00160                                                       GWL_USERDATA);
00161     static WNDPROC hOldProc = NULL;
00162 
00163     if (!hOldProc && pCardBack)
00164         hOldProc = pCardBack->hOldProc;
00165 
00166     switch (msg)
00167     {
00168     case WM_PAINT:
00169     {
00170         HDC hdc;
00171         PAINTSTRUCT ps;
00172         HPEN hPen, hOldPen;
00173         HBRUSH hBrush, hOldBrush;
00174         RECT rc;
00175 
00176         hdc = BeginPaint(hwnd, &ps);
00177 
00178         if (pCardBack->bSelected)
00179         {
00180             hPen = CreatePen(PS_SOLID, 2, RGB(0,0,0));
00181         }
00182         else
00183         {
00184             DWORD Face = GetSysColor(COLOR_3DFACE);
00185             hPen = CreatePen(PS_SOLID, 2, Face);
00186         }
00187 
00188         GetClientRect(hwnd, &rc);
00189         hBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
00190         hOldPen = (HPEN)SelectObject(hdc, hPen);
00191         hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
00192 
00193         Rectangle(hdc,
00194                   rc.left+1,
00195                   rc.top+1,
00196                   rc.right,
00197                   rc.bottom);
00198 
00199         StretchBlt(hdc,
00200                    2,
00201                    2,
00202                    CARDBACK_OPTIONS_WIDTH,
00203                    CARDBACK_OPTIONS_HEIGHT,
00204                    __hdcCardBitmaps,
00205                    pCardBack->hdcNum * __cardwidth,
00206                    0,
00207                    __cardwidth,
00208                    __cardheight,
00209                    SRCCOPY);
00210 
00211         SelectObject(hdc, hOldPen);
00212         SelectObject(hdc, hOldBrush);
00213 
00214         EndPaint(hwnd, &ps);
00215 
00216         break;
00217     }
00218 
00219     case WM_LBUTTONDOWN:
00220         pCardBack->bSelected = pCardBack->bSelected ? FALSE : TRUE;
00221         break;
00222     }
00223 
00224     return CallWindowProc(hOldProc,
00225                           hwnd,
00226                           msg,
00227                           wParam,
00228                           lParam);
00229 }
00230 
00231 
00232 INT_PTR CALLBACK CardBackDlgProc(HWND hDlg,
00233                               UINT uMsg,
00234                               WPARAM wParam,
00235                               LPARAM lParam)
00236 {
00237     static PCARDBACK pCardBacks = NULL;
00238 
00239     switch (uMsg)
00240     {
00241     case WM_INITDIALOG:
00242     {
00243         INT i, c;
00244         SIZE_T size = sizeof(CARDBACK) * NUM_CARDBACKS;
00245 
00246         pCardBacks = (PCARDBACK)HeapAlloc(GetProcessHeap(),
00247                                           0,
00248                                           size);
00249 
00250         for (i = 0, c = CARDBACK_START; c <= CARDBACK_END; i++, c++)
00251         {
00252             pCardBacks[i].hSelf = GetDlgItem(hDlg, c);
00253             pCardBacks[i].bSelected = FALSE;
00254             pCardBacks[i].hdcNum = CARDBACK_RES_START + i;
00255             pCardBacks[i].imgNum = i + 1;
00256             pCardBacks[i].hOldProc = (WNDPROC)SetWindowLongPtr(pCardBacks[i].hSelf,
00257                                                                GWLP_WNDPROC,
00258                                                                (LONG_PTR)CardImageWndProc);
00259 
00260             SetWindowLongPtr(pCardBacks[i].hSelf,
00261                              GWL_USERDATA,
00262                              (LONG_PTR)&pCardBacks[i]);
00263         }
00264 
00265         return TRUE;
00266     }
00267 
00268     case WM_COMMAND:
00269         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
00270         {
00271             INT i, num = 0;
00272             for (i = 0; i < NUM_CARDBACKS; i++)
00273             {
00274                 if (pCardBacks[i].bSelected)
00275                 {
00276                     num = pCardBacks[i].imgNum;
00277                 }
00278             }
00279 
00280             EndDialog(hDlg, LOWORD(wParam) == IDOK ? num : FALSE);
00281             HeapFree(GetProcessHeap(), 0, pCardBacks);
00282             return TRUE;
00283         }
00284 
00285         if (HIWORD(wParam) == STN_CLICKED)
00286         {
00287             INT i;
00288             RECT rc;
00289             for (i = 0; i < NUM_CARDBACKS; i++)
00290             {
00291                 if (pCardBacks[i].hSelf == (HWND)lParam)
00292                 {
00293                     pCardBacks[i].bSelected = TRUE;
00294                 }
00295                 else
00296                     pCardBacks[i].bSelected = FALSE;
00297 
00298                 GetClientRect(pCardBacks[i].hSelf, &rc);
00299                 InvalidateRect(pCardBacks[i].hSelf, &rc, TRUE);
00300             }
00301 
00302             break;
00303         }
00304     }
00305 
00306     return FALSE;
00307 }
00308 
00309 
00310 VOID ShowDeckOptionsDlg(HWND hwnd)
00311 {
00312     INT cardBack;
00313 
00314     if ((cardBack = DialogBox(hInstance,
00315                               MAKEINTRESOURCE(IDD_CARDBACK),
00316                               hwnd,
00317                               CardBackDlgProc)))
00318     {
00319         SpiderWnd.SetBackCardIdx(CARDBACK_RES_START + (cardBack - 1));
00320         SpiderWnd.Redraw();
00321     }
00322 }
00323 
00324 LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
00325 {
00326     static int nWidth, nHeight;
00327 
00328     switch(iMsg)
00329     {
00330         case WM_CREATE:
00331         {
00332             SpiderWnd.Create(hwnd, WS_EX_CLIENTEDGE, WS_CHILD|WS_VISIBLE, 0, 0, 0, 0);
00333             dwDifficulty = IDC_DIF_ONECOLOR;
00334             CreateSpider();
00335 
00336             SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOZORDER);
00337 
00338             dwAppStartTime = GetTickCount();
00339 
00340             return 0;
00341         }
00342 
00343         case WM_DESTROY:
00344             PostQuitMessage(0);
00345             return 0;
00346 
00347         case WM_SIZE:
00348             nWidth  = LOWORD(lParam);
00349             nHeight = HIWORD(lParam);
00350 
00351             MoveWindow(SpiderWnd, 0, 0, nWidth, nHeight, TRUE);
00352             return 0;
00353 
00354         case WM_GETMINMAXINFO:
00355         {
00356             MINMAXINFO *mmi;
00357 
00358             mmi = (MINMAXINFO *)lParam;
00359             mmi->ptMinTrackSize.x = NUM_STACKS * __cardwidth + (NUM_STACKS + 3) * X_BORDER;
00360             mmi->ptMinTrackSize.y = GetSystemMetrics(SM_CYCAPTION) +
00361                                     GetSystemMetrics(SM_CYMENU) +
00362                                     2 * Y_BORDER +
00363                                     3 * __cardheight +
00364                                     6 * yRowStackCardOffset;
00365             return 0;
00366         }
00367 
00368         case WM_COMMAND:
00369             switch(LOWORD(wParam))
00370             {
00371             case IDM_GAME_NEW:
00372                 NewGame();
00373                 return 0;
00374 
00375             case IDM_GAME_DECK:
00376                 ShowDeckOptionsDlg(hwnd);
00377                 return 0;
00378 
00379             case IDM_HELP_CONTENTS:
00380                 WinHelp(hwnd, szHelpPath, HELP_CONTENTS, 0);//HELP_KEY, (DWORD)"How to play");
00381                 return 0;
00382 
00383             case IDM_HELP_ABOUT:
00384                 MessageBox(hwnd, MsgAbout, szAppName, MB_OK|MB_ICONINFORMATION);
00385                 return 0;
00386 
00387             case IDM_GAME_EXIT:
00388                 PostMessage(hwnd, WM_CLOSE, 0, 0);
00389                 return 0;
00390             }
00391 
00392             return 0;
00393 
00394         case WM_CLOSE:
00395             if (fGameStarted == false)
00396             {
00397                 DestroyWindow(hwnd);
00398                 return 0;
00399             }
00400             else
00401             {
00402                 int ret;
00403 
00404                 ret = MessageBox(hwnd, MsgQuit, szAppName, MB_YESNO|MB_ICONQUESTION);
00405                 if (ret == IDYES)
00406                 {
00407                     WinHelp(hwnd, szHelpPath, HELP_QUIT, 0);
00408                     DestroyWindow(hwnd);
00409                 }
00410             }
00411             return 0;
00412     }
00413     return DefWindowProc (hwnd, iMsg, wParam, lParam);
00414 }
00415 

Generated on Mon May 28 2012 04:16:39 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.