beautifulGame.cpp
上传用户:z3308516
上传日期:2013-01-23
资源大小:703k
文件大小:13k
源码类别:

2D图形编程

开发平台:

Visual C++

  1. // beautifulGame.cpp : 定义应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "beautifulGame.h"
  5. #include "GridPicture.h"
  6. #include "Registry.h"
  7. #include "TimeElapse.h"
  8. #define MAX_LOADSTRING 100
  9. // 全局变量:
  10. HINSTANCE hInst; // 当前实例
  11. TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
  12. TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
  13. // 此代码模块中包含的函数的前向声明:
  14. ATOM MyRegisterClass(HINSTANCE hInstance);
  15. BOOL InitInstance(HINSTANCE, int);
  16. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  17. LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  18. void LoadAndShowPicture(HWND,HDC hDC,LPCTSTR pRes,BOOL bLoadOnly=FALSE,BOOL bDiscard=FALSE);
  19. BOOL ExtractFile(const char *pszFile,LPCTSTR res);
  20. CGridPicture gridPic;
  21. CTimeElapse timeElapse;
  22. CRegistry reg;
  23. IPicture *pPicture;
  24. int APIENTRY _tWinMain(HINSTANCE hInstance,
  25.                      HINSTANCE hPrevInstance,
  26.                      LPTSTR    lpCmdLine,
  27.                      int       nCmdShow)
  28. {
  29.   // TODO: 在此放置代码。
  30. MSG msg;
  31. HACCEL hAccelTable;
  32. ///com库初始化
  33. CoInitialize(NULL);
  34. // 初始化全局字符串
  35. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  36. LoadString(hInstance, IDC_BEAUTIFULGAME, szWindowClass, MAX_LOADSTRING);
  37. MyRegisterClass(hInstance);
  38. // 执行应用程序初始化:
  39. if (!InitInstance (hInstance, nCmdShow)) 
  40. {
  41. return FALSE;
  42. }
  43. hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BEAUTIFULGAME);
  44. // 主消息循环:
  45. while (GetMessage(&msg, NULL, 0, 0)) 
  46. {
  47. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
  48. {
  49. TranslateMessage(&msg);
  50. DispatchMessage(&msg);
  51. }
  52. }
  53. if(pPicture){
  54. pPicture->Release();
  55. }
  56. CoUninitialize(); //com unload
  57. return (int) msg.wParam;
  58. }
  59. //
  60. //  函数:MyRegisterClass()
  61. //
  62. //  目的:注册窗口类。
  63. //
  64. //  注释:
  65. //
  66. //    仅当希望在已添加到 Windows 95 的
  67. //    “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
  68. //    才需要此函数及其用法。调用此函数
  69. //    十分重要,这样应用程序就可以获得关联的
  70. //   “格式正确的”小图标。
  71. //
  72. ATOM MyRegisterClass(HINSTANCE hInstance)
  73. {
  74. WNDCLASSEX wcex;
  75. wcex.cbSize = sizeof(WNDCLASSEX); 
  76. wcex.style = CS_HREDRAW | CS_VREDRAW;
  77. wcex.lpfnWndProc = (WNDPROC)WndProc;
  78. wcex.cbClsExtra = 0;
  79. wcex.cbWndExtra = 0;
  80. wcex.hInstance = hInstance;
  81. wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BEAUTIFULGAME);
  82. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  83. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  84. wcex.lpszMenuName = (LPCTSTR)IDC_BEAUTIFULGAME;
  85. wcex.lpszClassName = szWindowClass;
  86. wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  87. return RegisterClassEx(&wcex);
  88. }
  89. //
  90. //   函数:InitInstance(HANDLE, int)
  91. //
  92. //   目的:保存实例句柄并创建主窗口
  93. //
  94. //   注释:
  95. //
  96. //        在此函数中,我们在全局变量中保存实例句柄并
  97. //        创建和显示主程序窗口。
  98. //
  99. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  100. {
  101.    HWND hWnd;
  102.    hInst = hInstance; // 将实例句柄存储在全局变量中
  103.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  104.       0, 0, 800,600, NULL, NULL, hInstance, NULL);
  105. //   hWnd = CreateWindowEx(WS_EX_TOOLWINDOW,szWindowClass, szTitle, WS_POPUP,
  106. //      1024*3, 0, 2048,1536, NULL, NULL, hInstance, NULL);
  107. //   hWnd = CreateWindowEx(WS_EX_TOOLWINDOW,szWindowClass, szTitle, WS_POPUP,
  108. //      0, 0,800,600, NULL, NULL, hInstance, NULL);
  109. //   SetMenu(hWnd,NULL);
  110.    if (!hWnd)
  111.    {
  112.       return FALSE;
  113.    }
  114.    ShowWindow(hWnd, nCmdShow);
  115.    UpdateWindow(hWnd);
  116.    return TRUE;
  117. }
  118. //
  119. //  函数:WndProc(HWND, unsigned, WORD, LONG)
  120. //
  121. //  目的:处理主窗口的消息。
  122. //
  123. //  WM_COMMAND - 处理应用程序菜单
  124. //  WM_PAINT - 绘制主窗口
  125. //  WM_DESTROY - 发送退出消息并返回
  126. //
  127. //
  128. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  129. {
  130. POINT ptPos;
  131. int wmId, wmEvent;
  132. PAINTSTRUCT ps;
  133. HDC hdc;
  134. int id1,id2;
  135. BOOL bFound;
  136. static BOOL bGameStart=FALSE;
  137. HMENU hMenu;
  138. int lv,ifen;
  139. switch (message) 
  140. {
  141. case WM_CREATE:
  142. hMenu=GetMenu(hWnd);
  143. hMenu=GetSubMenu(hMenu,0);
  144. hMenu=GetSubMenu(hMenu,2);
  145. CheckMenuItem(hMenu,ID_SOUND,MF_BYCOMMAND|MF_CHECKED);
  146. SetTimer(hWnd,1,200,NULL);
  147. // SetTimer(hWnd,2,300,NULL);
  148. ExtractFile((const char*)"back",(LPCTSTR)IDR_BACKMIDI);
  149. break;
  150. case WM_TIMER:
  151. if(wParam==1){
  152. timeElapse.QueryStatus(); //sound play
  153. }else{
  154. bFound=FALSE;
  155. gridPic.FoundLink(&gridPic,&id1,&id2,bFound);
  156. if(bFound){
  157. gridPic.DrawLinkLine(id1,id2);
  158. gridPic.Explore(id1,id2);
  159. }else{
  160. gridPic.SetFenLevel(0,20,3);
  161. gridPic.CGridPictureInit(hWnd,22,11,IDB_BITMAP,TRUE);
  162. bGameStart=TRUE;
  163. InvalidateRect(hWnd,NULL,TRUE);
  164. UpdateWindow(hWnd);
  165. }
  166. }
  167. break;
  168. case OUTOF_TIME:
  169. timeElapse.Stop();
  170. gridPic.GetLevelAndFen(ifen,lv);
  171. MessageBox(hWnd,"由于时间的关系,你的生命走到尽头了,请休息一下再玩吧","信息",MB_OK|MB_ICONWARNING);
  172. switch(lv){
  173. case 0:
  174. reg.CompareAndSetValue(hWnd,enumLow,ifen);
  175. break;
  176. case 1:
  177. reg.CompareAndSetValue(hWnd,enumNormal,ifen);
  178. break;
  179. case 2:
  180. reg.CompareAndSetValue(hWnd,enumHigh,ifen);
  181. break;
  182. case 3:
  183. reg.CompareAndSetValue(hWnd,enumSpecial,ifen);
  184. break;
  185. }
  186. SendMessage(hWnd,WM_COMMAND,ID_DISCARD,0);
  187. break;
  188. case WM_COMMAND:
  189. wmId    = LOWORD(wParam); 
  190. wmEvent = HIWORD(wParam); 
  191. // 分析菜单选择:
  192. switch (wmId)
  193. {
  194. case ID_BACKMIDI:
  195. hMenu=GetMenu(hWnd);
  196. hMenu=GetSubMenu(hMenu,0);
  197. hMenu=GetSubMenu(hMenu,2);
  198. if(timeElapse.bBackMidi){
  199. KillTimer(hWnd,1);
  200. timeElapse.CloseMidiSound();
  201. CheckMenuItem(hMenu,ID_BACKMIDI,MF_BYCOMMAND|MF_UNCHECKED);
  202. }else{
  203. timeElapse.bBackMidi=TRUE;
  204. SetTimer(hWnd,1,200,NULL);
  205. timeElapse.PlayMidiSound();
  206. CheckMenuItem(hMenu,ID_BACKMIDI,MF_BYCOMMAND|MF_CHECKED);
  207. }
  208. break;
  209. case ID_SOUND:
  210. hMenu=GetMenu(hWnd);
  211. hMenu=GetSubMenu(hMenu,0);
  212. hMenu=GetSubMenu(hMenu,2);
  213. if(gridPic.GetSound()){
  214. CheckMenuItem(hMenu,ID_SOUND,MF_BYCOMMAND|MF_UNCHECKED);
  215. gridPic.SetSound(FALSE);
  216. }else{
  217. CheckMenuItem(hMenu,ID_SOUND,MF_BYCOMMAND|MF_CHECKED);
  218. gridPic.SetSound(TRUE);
  219. }
  220. break;
  221. case IDM_ABOUT:
  222. DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  223. break;
  224. case IDM_EXIT:
  225. DestroyWindow(hWnd);
  226. break;
  227. case ID_NORMAL:
  228. case ID_COMPLEX:
  229. case ID_EASY:
  230. case ID_SPECIAL:
  231. timeElapse.Stop();
  232. timeElapse.bPause =FALSE;
  233. Sleep(500);
  234. if(wmId==ID_EASY){
  235. gridPic.SetFenLevel(0,0,0);
  236. timeElapse.CTimeElapseInit(hWnd,600); //120 sec
  237. gridPic.CGridPictureInit(hWnd,12,7,IDB_BITMAP);
  238. }else if(wmId==ID_NORMAL){ //180 sec
  239. gridPic.SetFenLevel(0,0,1);
  240. timeElapse.CTimeElapseInit(hWnd,600,300);
  241. gridPic.CGridPictureInit(hWnd,14,8,IDB_BITMAP);
  242. }else if(wmId==ID_COMPLEX){ //240 sec
  243. gridPic.SetFenLevel(0,0,2);
  244. timeElapse.CTimeElapseInit(hWnd,600,400);
  245. gridPic.CGridPictureInit(hWnd,16,9,IDB_BITMAP);
  246. }else{
  247. gridPic.SetFenLevel(0,0,3);
  248. timeElapse.CTimeElapseInit(hWnd,600,600); //180 sec
  249. gridPic.CGridPictureInit(hWnd,22,11,IDB_BITMAP,TRUE);
  250. }
  251. bGameStart=TRUE;
  252. InvalidateRect(hWnd,NULL,TRUE);
  253. UpdateWindow(hWnd);
  254. timeElapse.Begin();
  255. timeElapse.InvalidIt(hWnd);
  256. break;
  257. case ID_DISCARD:
  258. if(bGameStart){
  259. gridPic.Discard();
  260. timeElapse.Stop();
  261. bGameStart=FALSE;
  262. InvalidateRect(hWnd,NULL,TRUE);
  263. }
  264. break;
  265. case ID_FIRSTEASY:
  266. reg.ShowAll(hWnd,enumLow);
  267. break;
  268. case ID_FIRSTNORMAL:
  269. reg.ShowAll(hWnd,enumNormal);
  270. break;
  271. case ID_FIRSTCOMPLEX:
  272. reg.ShowAll(hWnd,enumHigh);
  273. break;
  274. case ID_FIRSTSPECIALVER:
  275. reg.ShowAll(hWnd,enumSpecial);
  276. break;
  277. case ID_EXPLORE:
  278. SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_F4,0);
  279. break;
  280. case ID_REORDER:
  281. SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_F5,0);
  282. break;
  283. case ID_PAUSE:
  284. SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_F6,0);
  285. break;
  286. default:
  287. return DefWindowProc(hWnd, message, wParam, lParam);
  288. }
  289. break;
  290. case WM_KEYDOWN:
  291. if(bGameStart){
  292. switch(wParam){
  293. case VK_F5:
  294. gridPic.Shuffle();
  295. InvalidateRect(hWnd,NULL,TRUE);
  296. if(timeElapse.totalSecond-100<0)
  297. timeElapse.totalSecond =0;
  298. else
  299. timeElapse.totalSecond -=100;
  300. break;
  301. case VK_F4:
  302. bFound=FALSE;
  303. gridPic.FoundLink(&gridPic,&id1,&id2,bFound);
  304. if(bFound){
  305. gridPic.DrawLinkLine(id1,id2);
  306. gridPic.Explore(id1,id2);
  307. if(timeElapse.totalSecond-50<0)
  308. timeElapse.totalSecond =0;
  309. else
  310. timeElapse.totalSecond -=50;
  311. timeElapse.InvalidIt(hWnd);
  312. }
  313. break;
  314. case VK_F6:
  315. timeElapse.Pause();
  316. if(timeElapse.bPause){
  317. hdc=GetDC(hWnd);
  318. LoadAndShowPicture(hWnd,hdc,(LPCTSTR)IDR_PAUSEJPG,FALSE,TRUE);
  319. ReleaseDC(hWnd,hdc);
  320. }else{
  321. LoadAndShowPicture(hWnd,NULL,(LPCTSTR)IDR_MAINJPG,TRUE,TRUE);
  322. InvalidateRect(hWnd,NULL,TRUE);
  323. }
  324. break;
  325. }
  326. }
  327. break;
  328. case STOP_TIME:
  329. if(bGameStart){
  330. timeElapse.Stop();
  331. bGameStart=FALSE;
  332. InvalidateRect(hWnd,NULL,TRUE);
  333. }
  334. break;
  335. case WM_LBUTTONDOWN:
  336. if(bGameStart && !timeElapse.bPause){
  337. ptPos.x=LOWORD(lParam);
  338. ptPos.y=HIWORD(lParam);
  339. gridPic.Picture_Click(ptPos);
  340. }
  341. break;
  342. case WM_RBUTTONDOWN:
  343. if(bGameStart && !timeElapse.bPause){
  344. gridPic.CancelFocus();
  345. }
  346. break;
  347. case WM_PAINT:
  348. // HBRUSH hbr;
  349. // RECT rcBack;
  350. hdc = BeginPaint(hWnd, &ps);
  351. if(bGameStart && !timeElapse.bPause){
  352. /* hbr=CreateSolidBrush(RGB(0,0,0));
  353. GetClientRect(hWnd,&rcBack);
  354. FillRect(hdc,(CONST RECT*)&rcBack,hbr);
  355. DeleteObject(hbr);
  356. */
  357. gridPic.Draw(hdc);
  358. timeElapse.FillRegion();
  359. }else{
  360. LoadAndShowPicture(hWnd,hdc,(LPCTSTR)IDR_MAINJPG);
  361. }
  362. EndPaint(hWnd, &ps);
  363. break;
  364. case WM_DESTROY:
  365. PostQuitMessage(0);
  366. break;
  367. default:
  368. return DefWindowProc(hWnd, message, wParam, lParam);
  369. }
  370. return 0;
  371. }
  372. // “关于”框的消息处理程序。
  373. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  374. {
  375. switch (message)
  376. {
  377. case WM_INITDIALOG:
  378. return TRUE;
  379. case WM_COMMAND:
  380. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  381. {
  382. EndDialog(hDlg, LOWORD(wParam));
  383. return TRUE;
  384. }
  385. break;
  386. }
  387. return FALSE;
  388. }
  389. void LoadAndShowPicture(HWND hWnd,HDC hDC,LPCTSTR pRes,BOOL bLoadOnly,BOOL bDiscard){
  390. HRSRC hsrc; 
  391. HGLOBAL hGlobal;
  392. HGLOBAL hGlobal1; //memory must moveable
  393. HRESULT hr;
  394. long sz;
  395. LPVOID pData;
  396. LPVOID pData1;
  397. IStream *pStream;
  398. POINT imageSize;
  399. RECT rcBounds;
  400. RECT rcWindow;
  401. OLE_XSIZE_HIMETRIC cx;
  402. OLE_YSIZE_HIMETRIC cy;
  403. if(bDiscard){
  404. if(pPicture){
  405. pPicture->Release();
  406. pPicture=NULL;
  407. }
  408. }
  409. if(!pPicture){
  410. hsrc=FindResource((HMODULE)GetModuleHandle(NULL),(LPCTSTR)pRes,(LPCTSTR)"RT_JPGDATA");
  411. if(hsrc){
  412. sz=SizeofResource((HMODULE)GetModuleHandle(NULL),hsrc);
  413. hGlobal=LoadResource((HMODULE)GetModuleHandle(NULL),hsrc);
  414. if(hGlobal){
  415. hGlobal1=GlobalAlloc(GMEM_MOVEABLE,sz);
  416. pData=GlobalLock(hGlobal);
  417. pData1=GlobalLock(hGlobal1);
  418. memcpy((void*)pData1,(const void*)pData,sz);
  419. GlobalUnlock(hGlobal1);
  420. GlobalUnlock(hGlobal);
  421. hr=CreateStreamOnHGlobal(hGlobal1,TRUE,&pStream);
  422. if(SUCCEEDED(hr=OleLoadPicture(pStream,0,FALSE,IID_IPicture,(LPVOID*)&pPicture)) && !bLoadOnly){
  423. pPicture->get_Width(&cx);
  424. pPicture->get_Height(&cy);
  425. imageSize.x=cx;
  426. imageSize.y=cy;
  427. // SetMapMode(hDC,MM_HIMETRIC);
  428. // LPtoDP(hDC,(LPPOINT)&imageSize,1);
  429. // SetMapMode(hDC,MM_TEXT);
  430. GetClientRect(hWnd,&rcWindow);
  431. imageSize.x=rcWindow.right;
  432. imageSize.y=rcWindow.bottom;
  433. rcBounds.left =0;
  434. rcBounds.top =0;
  435. rcBounds.right =imageSize.x;
  436. rcBounds.bottom =imageSize.y;
  437. pPicture->Render(hDC,0,0,imageSize.x ,imageSize.y,0,cy,cx,-cy,&rcBounds);
  438. pStream->Release();
  439. }
  440. GlobalFree(hGlobal1);
  441. }
  442. FreeResource(hsrc);
  443. }
  444. }else{
  445. pPicture->get_Width(&cx);
  446. pPicture->get_Height(&cy);
  447. imageSize.x=cx;
  448. imageSize.y=cy;
  449. GetClientRect(hWnd,&rcWindow);
  450. imageSize.x=rcWindow.right;
  451. imageSize.y=rcWindow.bottom;
  452. rcBounds.left =0;
  453. rcBounds.top =0;
  454. rcBounds.right =imageSize.x;
  455. rcBounds.bottom =imageSize.y;
  456. pPicture->Render(hDC,0,0,imageSize.x ,imageSize.y,0,cy,cx,-cy,&rcBounds);
  457. }
  458. }
  459. BOOL ExtractFile(const char *pszFile,LPCTSTR res){
  460. HANDLE hFile;
  461. HRSRC hsrc;
  462. HGLOBAL hglobal;
  463. LPVOID lpv;
  464. DWORD ret;
  465. BOOL bret;
  466. bret=FALSE;
  467. if(_access(pszFile,0)==-1){
  468. hFile=CreateFile(pszFile,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
  469. NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  470. if(hFile){
  471. hsrc=FindResource(GetModuleHandle(NULL),(LPCTSTR)res,(LPCTSTR)"RT_JPGDATA");
  472. if(hsrc){
  473. hglobal=LoadResource((HMODULE)GetModuleHandle(NULL),hsrc);
  474. if(hglobal){
  475. lpv=LockResource(hglobal);
  476. if(lpv){
  477. WriteFile(hFile,lpv,SizeofResource(GetModuleHandle(NULL),hsrc),&ret,NULL);
  478. bret=TRUE;
  479. }
  480. }
  481. }
  482. CloseHandle(hFile);
  483. }
  484. }else
  485. bret=TRUE;
  486. return bret;
  487. }