Game.cpp
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:46k
源码类别:

游戏

开发平台:

Visual C++

  1. // Game.cpp : 定义应用程序的入口点。
  2. //在class Game的private中声明静态模型和动态模型
  3. #include "stdafx.h"
  4. #include "Game.h"
  5. #include <d3dx9.h>
  6. #include <d3dx9math.h>
  7. #include <mmsystem.h>
  8. #include "Font.h"
  9. #include <stdio.h>
  10. #include "Mesh.h"
  11. #include "Panel.h"
  12. #include "Input.h"
  13. //#include "Terrain.h"
  14. #include "npc.h"
  15. #include "Sky.h"
  16. #include "Snow.h"
  17. #include "Water.h"
  18. #include "SkinMesh.h"
  19. #include "Open.h"
  20. //#include "Sound.h"
  21. //#include "Music.h"
  22. #define MAX_LOADSTRING 100
  23. // 全局变量:
  24. HINSTANCE hInst; // 当前实例
  25. TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
  26. TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
  27. #define SafeRelease(pObject) if(pObject!=NULL){pObject->Release();pObject=NULL;}
  28. #define SafeDelete(object) if(object!=NULL){delete object;object=NULL;}
  29. #define MESH_D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
  30. // 此代码模块中包含的函数的前向声明:
  31. ATOM MyRegisterClass(HINSTANCE hInstance);
  32. BOOL InitInstance(HINSTANCE, int);
  33. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  34. LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  35. class CGame
  36. {
  37. public:
  38. CGame(HINSTANCE hinst,HWND hwnd);
  39. virtual ~CGame();
  40. void GameLoop();
  41. void Render();
  42.     POSITION ViewPos;
  43. POSITION PreViewPos;
  44.     COLLIPE * object;
  45. NPOSITION BossPos;
  46. int flash;
  47. void InitialTools();
  48. HRESULT InitialD3D(HWND hWnd);
  49. HRESULT InitialLight();
  50.     CInput * m_pInput;
  51.     
  52.     CTerrain * m_pTerrain;
  53.     bool  ViewFlag;//镜头碰到地面的判断
  54. D3DXVECTOR3 vecUp,sub;;//sub为视点位置与摄像机位置的矢量差,vecUp为sub相对变化矢量
  55. float fUp;
  56.  
  57. bool shoot;//判断鼠标左键是否点下,按下为true
  58.     CPanel * m_pPanel;
  59. bool nomoney;
  60. private:
  61. D3DLIGHT9 d3dLight;
  62.     int iBianhao;
  63. POSITION SetNpcPosition(float x,float z);
  64. void SetCamera2D();
  65. void Render2D();
  66. void SetCamera3D();
  67. void Render3D();
  68. void RenderText();
  69. void SetFog();
  70. void SetWaterFog();
  71. void GetMouse();
  72. void GetMouse1();
  73. LPDIRECT3D9  m_pD3D;
  74. LPDIRECT3DDEVICE9 m_pD3DDevice;
  75. DWORD m_dwFrame;
  76. DWORD m_dwStartTime;
  77. CFont * m_pFont;
  78.     CFont * m_pWrong;//当角色想攻击npc,但背朝对方时使用
  79. CWater * m_pWater;
  80. //CSnow * m_pSnow;
  81. CSky * m_pSky;
  82. int m_nScreenWidth;
  83. int m_nScreenHeight;
  84. HWND hWnd;
  85. HINSTANCE hInst;
  86. POINT m_Point;
  87. D3DXVECTOR3 m_Proj;
  88. RECT m_Rect;
  89. BOOL present;
  90. D3DXVECTOR3 DirWorld,OriginWorld;
  91.     D3DXMATRIX matProj;
  92.     void ScreenTo3D(int mx,int my,D3DXVECTOR3 * p1,D3DXVECTOR3 * p2);
  93.     float cy;//镜头的高度
  94.     
  95. void AI();//npc的人工智能,包括行走和动作
  96.     void RoleAction();//主角的动作
  97. bool Collipe(COLLIPE col);//npc、主角和场景的碰撞检测
  98.     CSound * m_pMusic;
  99. CSound * m_pSound;
  100.      ///////////////
  101.     //声明模型的地方,声明过后请到InitialTools()中初始化模型,静态和动态的初始化有区别
  102. //////////////
  103. //静态模型用CNpc类,动态模型用CSkinMesh类
  104. CNpc * m_pTree_1_1;
  105. CNpc * m_pTree_1_2;
  106. CNpc * m_pTree_1_3;
  107. CNpc * m_pTree_1_4;
  108. CNpc * m_pTree_1_5;
  109. CNpc * m_pTree_1_6;
  110. CNpc * m_pTree_1_7;
  111. CNpc * m_pTree_1_8;
  112. CNpc * m_pTree_1_9;
  113. CNpc * m_pTree_1_10;
  114. CNpc * m_pTree_1_11;
  115. CNpc * m_pTree_1_12;
  116.     CNpc * m_pTree_1_13;
  117. CNpc * m_pTree_1_14;
  118. CNpc * m_pTree_1_15;
  119. CNpc * m_pTree_1_16;
  120. CNpc * m_pTree_1_17;
  121. CNpc * m_pTree_1_18;
  122. CNpc * m_pTree_1_19;
  123. CNpc * m_pTree_1_20;
  124. CNpc * m_pTree_1_21;
  125. CNpc * m_pTree_u_1;
  126. CNpc * m_pTree_u_2;
  127. CNpc * m_pTree_u_3;
  128. CNpc * m_pTree_u_4;
  129. CNpc * m_pTree_u_5;
  130. CNpc * m_pDead_cow_1;
  131. CNpc * m_pRock_1_1;
  132. CNpc * m_pRock_1_2;
  133. CNpc * m_pRock_1_3;
  134. CNpc * m_pRock_1_4;
  135. CNpc * m_pRock_1_5;
  136. CNpc * m_pRock_1_6;
  137. CNpc * m_pRock_2_1;
  138. CNpc * m_pRock_2_2;
  139. CNpc * m_pRock_2_3;
  140. CNpc * m_pRock_2_4;
  141. CNpc * m_pRock_2_5;
  142. CNpc * m_pRock_2_6;
  143. CNpc * m_pRock_3_1;
  144. CNpc * m_pRock_4_1;
  145.     CNpc * m_pTree_2_1;
  146.     CNpc * m_pTree_2_2;
  147. CNpc * m_pBridge;
  148. CNpc * m_pHole;
  149.     CMesh * m_pBoss;
  150.     CMesh * m_pMesh;
  151.     CSkinMesh * m_pSkinMesh;
  152. CSkinMesh * m_pSBoss;
  153. };
  154. CGame::CGame(HINSTANCE hinst,HWND hwnd)
  155. {   hWnd=hwnd;
  156.     hInst=hinst;
  157. m_pD3D=NULL;
  158. m_pD3DDevice=NULL;
  159. m_pFont=NULL;
  160. m_dwFrame=0;
  161. m_dwStartTime=timeGetTime();
  162. flash=0;
  163. ViewPos.x=3037.0f;//500
  164. ViewPos.y=0.0f;
  165. ViewPos.z=3205.0f;//500
  166. ViewPos.a=180.0f;
  167. ViewPos.b=0.0f;
  168. ViewPos.ca=0.0f;
  169. ViewPos.cb=45.0f;
  170. ViewPos.cc=200.0f;
  171. PreViewPos=ViewPos;
  172.     BossPos.x=2417;
  173. BossPos.y=254;
  174. BossPos.z=1185;
  175. BossPos.a=0;
  176. fUp=0.0f;
  177. shoot=false;
  178.     nomoney=false;
  179. }
  180. POSITION CGame::SetNpcPosition(float x,float z)
  181. {
  182. POSITION MonsterPos;
  183. MonsterPos.x=x;MonsterPos.z=z;
  184. MonsterPos.y=m_pTerrain->GetHeight(x,z);
  185. return MonsterPos;
  186. }
  187. void CGame::InitialTools()
  188. {   
  189.        HFONT hNewFont = CreateFont(50, 0, 0, 0, 0, 0, 0, 0, 
  190. ANSI_CHARSET, 0, 0, 0, 0, "shit");
  191.         bool  hihi=false;
  192. HFONT hOldFont;
  193. HDC hwndDC;
  194. HDC dcMem;
  195. HBITMAP bmpMem;
  196. HBITMAP bmpMem_2;
  197. HBITMAP bmpMem_3;
  198. LOGBRUSH logB={BS_SOLID|DIB_RGB_COLORS,RGB(100,60,160),HS_VERTICAL};
  199. HBRUSH hbr=CreateBrushIndirect(&logB);
  200. //LPCTSTR pStr="载入中...";
  201. //LPCRSTR pEnd="请按鼠标左键进入游戏";
  202.         int length=50;
  203. hwndDC=GetDC(hWnd);
  204. hOldFont=(HFONT)SelectObject(hwndDC,hNewFont);
  205. RECT rect,FrameRect,DrawRect;
  206. GetClientRect(hWnd,&rect); 
  207. FrameRect.left=220;FrameRect.top=450;FrameRect.right=620;FrameRect.bottom=470;
  208. DrawRect.left=220;DrawRect.top=450;DrawRect.right=220;DrawRect.bottom=470;
  209. dcMem=CreateCompatibleDC(hwndDC);
  210. bmpMem=(HBITMAP)LoadImage(NULL,"./res/loading.bmp",IMAGE_BITMAP,rect.right ,rect.bottom,LR_LOADFROMFILE);
  211.         bmpMem_2=(HBITMAP)LoadImage(NULL,"./res/loading_1.bmp",IMAGE_BITMAP,311 ,36,LR_LOADFROMFILE);
  212.         bmpMem_3=(HBITMAP)LoadImage(NULL,"./res/loading_2.bmp",IMAGE_BITMAP,311 ,36,LR_LOADFROMFILE);
  213.        
  214. SelectObject(dcMem,bmpMem);
  215. BitBlt(hwndDC,0,0,rect.right,rect.bottom,dcMem,0,0,SRCCOPY);  
  216. SetBkMode(hwndDC,TRANSPARENT); 
  217.     Rectangle(hwndDC, FrameRect.left ,FrameRect.top,FrameRect.right,FrameRect.bottom); 
  218. SelectObject(dcMem,bmpMem_2);
  219. BitBlt(hwndDC,250,500,311,36,dcMem,0,0,SRCCOPY);  
  220. SetBkMode(hwndDC,TRANSPARENT); 
  221.         //TextOut(hwndDC,250, 450,"载入中", 6);
  222.         
  223. m_pFont=new CFont(m_pD3DDevice,"vadana");
  224.     m_pWrong=new CFont(m_pD3DDevice,"vadana");
  225.     m_pMesh=new CMesh(m_pD3DDevice,"./mod/kuangjia.X");
  226. m_pSkinMesh=new CSkinMesh(m_pD3DDevice);
  227. m_pSkinMesh->LoadFromXFile("./mod/role.X");
  228. DrawRect.right+=length;
  229. FillRect(hwndDC,&DrawRect,hbr);
  230. m_pPanel=new CPanel(m_pD3DDevice,900,64,m_nScreenWidth,m_nScreenHeight);
  231.     DrawRect.right+=length;
  232. FillRect(hwndDC,&DrawRect,hbr);
  233. m_pTerrain=new CTerrain(hInst,m_pD3DDevice);
  234. m_pWater=new CWater(hInst,m_pD3DDevice,m_pTerrain->m_dwRow,m_pTerrain->m_dwCol);
  235.     DrawRect.right+=length;
  236. FillRect(hwndDC,&DrawRect,hbr);
  237.     
  238. ViewPos.y=m_pTerrain->GetHeight(ViewPos.x,ViewPos.z);
  239.     DrawRect.right+=length;
  240. FillRect(hwndDC,&DrawRect,hbr);
  241.    
  242.     
  243.     object=new COLLIPE[44];
  244.     DrawRect.right+=length;
  245. FillRect(hwndDC,&DrawRect,hbr);
  246.    
  247. COLLIPE col0={1662,254,3729,50,};
  248.     COLLIPE col1={2009,253,3679,50,};
  249.     COLLIPE col2={2294,253,3693,50,};
  250.     COLLIPE col3={2605,253,3671,50,};
  251.     COLLIPE col4={3292,253,2558,50,};
  252.     COLLIPE col5={3619,253,2353,50,};
  253.     COLLIPE col6={3609,253,2254,50,};
  254.     COLLIPE col7={3686,253,1992,50,};
  255.     COLLIPE col8={3699,253,1873,50,};
  256.     COLLIPE col9={3660,253,1653,50,};
  257.     COLLIPE col10={141,253,3216,50,};
  258.     COLLIPE col11={155,253,2950,50,};
  259.     COLLIPE col12={2568,253,2574,50,};
  260.     COLLIPE col13={1851,253,3599,50,};
  261.     COLLIPE col14={2184,253,3570,50,};
  262.     COLLIPE col15={3637,253,2109,50,};
  263.     COLLIPE col16={3448,253,2465,50,};
  264.     COLLIPE col17={2083,303,2568,50,};
  265.     COLLIPE col18={2458,253,3598,50,};
  266.     COLLIPE col19={2180,303,2567,50,};
  267.     COLLIPE col20={2773,310,2569,50,};
  268.     COLLIPE col21={3135,255,2665,50,};
  269.     COLLIPE col22={2293,315,2573,50,};
  270.     COLLIPE col23={2409,315,2566,50,};
  271.     COLLIPE col24={2539,268,3503,50,};
  272.     COLLIPE col25={2330,293,3586,50,};
  273.     COLLIPE col26={2975,220,345,50,};
  274.     COLLIPE col27={2885,295,2566,50,};
  275.     COLLIPE col28={3010,315,2575,50,};
  276.     COLLIPE col29={3185,305,2581,50,};
  277.     COLLIPE col30={3056,295,2474,50,};
  278.     COLLIPE col31={3010,295,2635,50,};
  279.     COLLIPE col32={3185,305,2521,50,};
  280.     COLLIPE col33={2925,295,2560,50,};
  281.     COLLIPE col34={2955,295,2570,50,};
  282.     COLLIPE col35={3060,315,2570,50,};
  283.     COLLIPE col36={2925,295,2610,50,};
  284.     COLLIPE col37={2955,295,2620,50,};
  285.     COLLIPE col38={3060,305,2620,50,};
  286.     COLLIPE col39={420,200,3340,50,};
  287.     COLLIPE col40={163,300,2711,150,};
  288.     COLLIPE col41={1166,250,814,400,};
  289.     COLLIPE col42={3357,200,409,100,};
  290.     COLLIPE col43={3638,254,3252,250,};
  291. object[0]=col0;
  292.     object[1]=col1;
  293. object[2]=col2;
  294. object[3]=col3;
  295. object[4]=col4;
  296. object[5]=col5;
  297. object[6]=col6;
  298. object[7]=col7;
  299. object[8]=col8;
  300. object[9]=col9;
  301. object[10]=col10;
  302. object[11]=col11;
  303. object[12]=col12;
  304. object[13]=col13;
  305. object[14]=col14;
  306. object[15]=col15;
  307. object[16]=col16;
  308. object[17]=col17;
  309. object[18]=col18;
  310. object[19]=col19;
  311. object[20]=col20;
  312. object[21]=col21;
  313. object[22]=col22;
  314. object[23]=col23;
  315. object[24]=col24;
  316. object[25]=col25;
  317. object[26]=col26;
  318. object[27]=col27;
  319. object[28]=col28;
  320. object[29]=col29;
  321. object[30]=col30;
  322. object[31]=col31;
  323. object[32]=col32;
  324. object[33]=col33;
  325. object[34]=col34;
  326. object[35]=col35;
  327. object[36]=col36;
  328. object[37]=col37;
  329. object[38]=col38;
  330. object[39]=col39;
  331. object[40]=col40;
  332. object[41]=col41;
  333. object[42]=col42;
  334. object[43]=col43;
  335.     DrawRect.right+=length;
  336. FillRect(hwndDC,&DrawRect,hbr);
  337.     
  338.    /*
  339.     m_pTree_1_1->Translation(1662,254,3729,0,5);//0
  340.     m_pTree_1_2->Translation(2009,253,3679,180,4.3);//1
  341.     m_pTree_1_3->Translation(2294,253,3693,90,4);//2
  342. m_pTree_1_4->Translation(2605,253,3671,45,8);//3
  343. m_pTree_1_5->Translation(3292,253,2558,0,6);//4
  344. m_pTree_1_6->Translation(3619,253,2353,180,6);//5
  345. m_pTree_1_7->Translation(3609,253,2254,90,5);//6
  346. m_pTree_1_8->Translation(3686,253,1992,45,4);//7
  347. m_pTree_1_9->Translation(3699,253,1873,270,5);//8
  348. m_pTree_1_10->Translation(3660,253,1653,0,6);//9
  349. m_pTree_1_11->Translation(141,253,3216,90,6);//10
  350. m_pTree_1_12->Translation(155,253,2950,90,3);//11
  351. m_pTree_1_13->Translation(2568,253,2574,270,6);//12
  352. m_pTree_1_14->Translation(1851,253,3599,270,5);//13
  353. m_pTree_1_15->Translation(2184,253,3570,270,3);//14
  354. m_pTree_1_16->Translation(3637,253,2109,90,4);//15
  355. m_pTree_1_17->Translation(3448,253,2465,180,6);//16
  356. m_pTree_1_18->Translation(2083,303,2568,90,3);//17
  357. m_pTree_1_19->Translation(2458,253,3598,180,3);//18
  358. m_pTree_1_20->Translation(2180,303,2567,270,3);//19
  359. m_pTree_1_21->Translation(2773,310,2569,0,3);//20  
  360.     m_pTree_u_1->Translation(3135,255,2665,0,2);//21
  361. m_pTree_u_2->Translation(2293,315,2573,90,2.5);//22  
  362. m_pTree_u_3->Translation(2409,315,2566,270,1.8);//23
  363. m_pTree_u_4->Translation(2539,268,3503,0,1);//24
  364. m_pTree_u_5->Translation(2330,293,3586,270,1.8);//25
  365. m_pDead_cow_1->Translation(2975,220,345,180,3);//26
  366. m_pRock_1_1->Translation(2885,295,2566,0,3);//27
  367. m_pRock_1_2->Translation(3010,315,2575,180,3);//28
  368. m_pRock_1_3->Translation(3185,305,2581,270,3);//29
  369.     m_pRock_1_6->Translation(3056,295,2474,90,3);//30
  370. m_pRock_1_5->Translation(3010,295,2635,0,3);//31
  371. m_pRock_1_4->Translation(3185,305,2521,180,3);//32
  372. m_pRock_2_1->Translation(2925,295,2560,0,3);//33
  373. m_pRock_2_2->Translation(2955,295,2570,180,3);//34
  374. m_pRock_2_3->Translation(3060,315,2570,90,3);//35
  375. m_pRock_2_6->Translation(2925,295,2610,270,3);//36
  376. m_pRock_2_5->Translation(2955,295,2620,0,3);//37
  377. m_pRock_2_4->Translation(3060,305,2620,180,3);//38
  378. m_pRock_3_1->Translation(420,200,3340,180,3);//39
  379. m_pRock_4_1->Translation(163,300,2711,0,3);//40
  380. m_pTree_2_1->Translation(1166,250,814,0,12);//41
  381. m_pTree_2_1->Translation(3357,200,409,270,8);//42
  382. m_pHole->Translation(3638,254,3252,180,5);//43
  383. */
  384.     ///////////////
  385. //初始化动静态模型的地方
  386. ///////////////
  387.    //以下是静态模型,参数是3D设备和.X文件的路径
  388. //缺口1
  389. m_pTree_1_1=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  390. m_pTree_1_2=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  391.     m_pTree_1_3=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  392. m_pTree_1_4=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  393. //缺口2
  394. m_pTree_1_5=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  395. m_pTree_1_6=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  396. m_pTree_1_7=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  397. m_pTree_1_8=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  398. m_pTree_1_9=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  399. m_pTree_1_10=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  400. //边上
  401. m_pTree_1_11=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  402. m_pTree_1_12=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  403. //突出
  404. m_pTree_1_13=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  405. //树1补充
  406. m_pTree_1_14=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  407. m_pTree_1_15=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  408. m_pTree_1_16=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  409. m_pTree_1_17=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  410. m_pTree_1_18=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  411. m_pTree_1_19=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  412. m_pTree_1_20=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  413. m_pTree_1_21=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
  414. //树桩
  415.     m_pTree_u_1=new CNpc(m_pD3DDevice,"./mod/tree_under.X");
  416. m_pTree_u_2=new CNpc(m_pD3DDevice,"./mod/tree_under.X");
  417. m_pTree_u_3=new CNpc(m_pD3DDevice,"./mod/tree_under.X");
  418. m_pTree_u_4=new CNpc(m_pD3DDevice,"./mod/tree_under.X");
  419. m_pTree_u_5=new CNpc(m_pD3DDevice,"./mod/tree_under.X");
  420. //死牛
  421. m_pDead_cow_1=new CNpc(m_pD3DDevice,"./mod/dead_cow.X");
  422. //岩石
  423. m_pRock_1_1=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
  424. m_pRock_1_2=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
  425. m_pRock_1_3=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
  426. m_pRock_1_4=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
  427. m_pRock_1_5=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
  428. m_pRock_1_6=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
  429. m_pRock_2_1=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
  430. m_pRock_2_2=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
  431. m_pRock_2_3=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
  432. m_pRock_2_4=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
  433. m_pRock_2_5=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
  434. m_pRock_2_6=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
  435. m_pRock_3_1=new CNpc(m_pD3DDevice,"./mod/rock_3.X");
  436. m_pRock_4_1=new CNpc(m_pD3DDevice,"./mod/rock_4.X");
  437.    
  438.     //巨树
  439. m_pTree_2_1=new CNpc (m_pD3DDevice,"./mod/tree_2.X");
  440. m_pTree_2_2=new CNpc (m_pD3DDevice,"./mod/tree_2.X");
  441. //洞
  442. m_pHole=new CNpc(m_pD3DDevice,"./mod/rockdoor.X");
  443.      //桥
  444.     m_pBridge=new CNpc(m_pD3DDevice,"./mod/bridge.X");
  445. m_pBoss=new CMesh(m_pD3DDevice,"./mod/kuangjia.X");
  446. //以下是动态模型,初始化结束后请到Render3D()中确定模型的位置和渲染模型
  447. m_pSBoss=new CSkinMesh(m_pD3DDevice);
  448. m_pSBoss->LoadFromXFile("./mod/boss.X");
  449.     DrawRect.right+=length;
  450. FillRect(hwndDC,&DrawRect,hbr);
  451.     
  452. m_pInput=new CInput(hInst,hWnd,m_pPanel->NIcon);
  453.     DrawRect.right+=length;
  454. FillRect(hwndDC,&DrawRect,hbr);
  455.     
  456. m_pSky=new CSky(m_pD3DDevice);
  457. m_pMusic=new CSound(hWnd);
  458.     m_pSound=new CSound(hWnd);
  459.     DeleteObject(bmpMem_2);
  460.         SelectObject(dcMem,bmpMem_3);
  461. BitBlt(hwndDC,250,500,311,36,dcMem,0,0,SRCCOPY);  
  462. SetBkMode(hwndDC,TRANSPARENT); 
  463.    // TextOut(hwndDC,100, 50,"请按空格进入游戏", 16);
  464. while(hihi==false)
  465. {
  466. m_pInput->Update();
  467. m_pInput->GetOut(&hihi);
  468. }
  469. // m_pSnow=new CSnow(m_pD3DDevice,10,10,10,25,1.0f);
  470.     SelectObject(hwndDC,hOldFont);
  471. DeleteObject(hNewFont);
  472. DeleteObject(bmpMem);
  473. DeleteObject(bmpMem_3);
  474. DeleteObject(dcMem);
  475. DeleteObject(hwndDC);
  476. m_pMusic->Play(0,"./res/Midi_2.mid");
  477. }
  478. CGame::~CGame()
  479. {  // SafeDelete(m_pSnow);
  480. SafeDelete(m_pSound);
  481. SafeDelete(m_pMusic);
  482.   
  483. SafeDelete(m_pSky);
  484. SafeDelete(m_pTerrain);
  485. SafeDelete(m_pInput);
  486. SafeDelete(m_pPanel);
  487. SafeDelete(m_pFont);
  488.     SafeDelete(m_pMesh);
  489. SafeDelete(m_pSkinMesh);
  490. SafeRelease(m_pD3DDevice);
  491.     SafeRelease(m_pD3D);
  492. }
  493. void CGame::ScreenTo3D(int mx,int my,D3DXVECTOR3 * p1,D3DXVECTOR3 * p2)
  494. {
  495.     float dy,dx;
  496. float HalfGameWidth = m_Rect.right / 2.0f;
  497. float HalfGameHeight = m_Rect.bottom / 2.0f;
  498. float tang = tanf((float)(D3DX_PI/4.0f) * 0.5f);
  499.     D3DXMATRIX matView,matWorld;
  500.     m_pD3DDevice->GetTransform(D3DTS_VIEW,&matView);
  501. m_pD3DDevice->GetTransform(D3DTS_WORLD,&matWorld);
  502. dx = tang * (mx / HalfGameWidth - 1.0f) / (float)(1.25);
  503. dy = tang * (1.0f - my / HalfGameHeight);
  504. //calculate the beginning and end points of the ray
  505. p1->x = dx * 1.0f;p1->y = dy * 1.0f;p1->z = 1.0f;
  506. p2->x = dx * 10000.0f;p2->y = dy * 10000.0f;p2->z = 10000.0f;
  507. //inverse them by the view matrix
  508. D3DXMATRIX invMat; //the inverse of the view matrix
  509. D3DXMatrixInverse(&invMat,NULL,&matView); //inverse the view mat
  510. D3DXVec3TransformCoord(p2,p2,&invMat);
  511. D3DXVec3TransformCoord(p1,p1,&invMat);
  512. //inverse them by the world matrix
  513. //D3DXMATRIX invwMat; //the inverse of the view matrix
  514. //D3DXMatrixInverse(&invwMat,NULL,&matWorld); //inverse the view mat
  515. //D3DXVec3TransformCoord(p2,p2,&invwMat);
  516. //D3DXVec3TransformCoord(p1,p1,&invwMat);
  517.    // D3DXMatrixInverse(&invwMat,NULL,&m_pMesh->matWorld); 
  518. //D3DXVec3TransformCoord(p2,p2,&invwMat);
  519. //D3DXVec3TransformCoord(p1,p1,&invwMat);
  520. //D3DXVec3Normalize(p2,&(*p2 - *p1));
  521. }
  522. void CGame::GetMouse()
  523. {  
  524.  
  525. if(shoot==true)
  526. {
  527.     BOOL hit;
  528.     FLOAT  distance;
  529. hit=FALSE;
  530. GetCursorPos(&m_Point);
  531.    // ScreenToClient(hWnd,&m_Point);
  532.     D3DXMATRIX  matInv;
  533. D3DXVECTOR3 start,dir;
  534. //ScreenTo3D(m_Point.x,m_Point.y,&start,&dir);
  535.         D3DVIEWPORT9 viewport;
  536. D3DXMATRIX matProj,matView,matWorld;
  537.     m_pD3DDevice->GetViewport(&viewport);
  538.         m_pD3DDevice->GetTransform(D3DTS_PROJECTION,&matProj);
  539.         m_pD3DDevice->GetTransform(D3DTS_VIEW,&matView);
  540.         m_pD3DDevice->GetTransform(D3DTS_WORLD,&matWorld);
  541.         D3DXVec3Unproject(&dir,&D3DXVECTOR3(m_Point.x,m_Point.y,1.0f),&viewport,&matProj,&matView,&matWorld);
  542.         D3DXMatrixInverse(&matInv,NULL,&matView);
  543.         start.x=matInv._41;
  544.         start.y=matInv._42;
  545.         start.z=matInv._43;
  546.          D3DXMatrixInverse(&matInv,NULL,&m_pMesh->matWorld); 
  547.      D3DXVec3TransformCoord(&dir,&dir,&matInv);
  548.      D3DXVec3TransformCoord(&start,&start,&matInv);
  549.  dir=dir-start;
  550.     D3DXIntersect(m_pMesh->m_pMesh,&start,&dir,&hit,NULL,NULL,NULL,&distance,NULL,NULL);
  551. //present=hit;
  552. /*if(hit==TRUE)
  553. {
  554. MessageBox(NULL,"Mouse over mesh","blah",NULL);
  555. }*/
  556. }
  557. /*else
  558. {
  559. present=FALSE;
  560. }*/
  561. }
  562. void CGame::GetMouse1()
  563. {
  564.   
  565. if(shoot==true)
  566. {
  567.     BOOL hit;
  568.     FLOAT  distance;
  569. hit=FALSE;
  570. GetCursorPos(&m_Point);
  571.    // ScreenToClient(hWnd,&m_Point);
  572.     D3DXMATRIX  matInv;
  573. D3DXVECTOR3 start,dir;
  574. //ScreenTo3D(m_Point.x,m_Point.y,&start,&dir);
  575.         D3DVIEWPORT9 viewport;
  576. D3DXMATRIX matProj,matView,matWorld;
  577.     m_pD3DDevice->GetViewport(&viewport);
  578.         m_pD3DDevice->GetTransform(D3DTS_PROJECTION,&matProj);
  579.         m_pD3DDevice->GetTransform(D3DTS_VIEW,&matView);
  580.         m_pD3DDevice->GetTransform(D3DTS_WORLD,&matWorld);
  581.         D3DXVec3Unproject(&dir,&D3DXVECTOR3(m_Point.x,m_Point.y,1.0f),&viewport,&matProj,&matView,&matWorld);
  582.         D3DXMatrixInverse(&matInv,NULL,&matView);
  583.         start.x=matInv._41;
  584.         start.y=matInv._42;
  585.         start.z=matInv._43;
  586.          D3DXMatrixInverse(&matInv,NULL,&m_pBoss->matWorld); 
  587.      D3DXVec3TransformCoord(&dir,&dir,&matInv);
  588.      D3DXVec3TransformCoord(&start,&start,&matInv);
  589.  dir=dir-start;
  590.     D3DXIntersect(m_pBoss->m_pMesh,&start,&dir,&hit,NULL,NULL,NULL,&distance,NULL,NULL);
  591. present=hit;
  592. if(hit==TRUE)
  593. {
  594. m_pSound->PlaySound("./res/sound_1.wav");
  595. }
  596. }
  597. }
  598. void CGame::SetFog()
  599. {   float start=400.0f;
  600.     float end=4500.0f;
  601. m_pD3DDevice->SetRenderState(D3DRS_FOGENABLE,TRUE);
  602. m_pD3DDevice->SetRenderState(D3DRS_FOGCOLOR,D3DCOLOR_XRGB(255,255,255));
  603. D3DCAPS9 stCaps;
  604. m_pD3DDevice->GetDeviceCaps(&stCaps);
  605. if(stCaps.RasterCaps&D3DPRASTERCAPS_FOGRANGE)
  606. {
  607. m_pD3DDevice->SetRenderState(D3DRS_RANGEFOGENABLE,TRUE);
  608. }
  609. m_pD3DDevice->SetRenderState(D3DRS_FOGSTART,*(DWORD*)&start);
  610. m_pD3DDevice->SetRenderState(D3DRS_FOGEND,*(DWORD*)&end);
  611. m_pD3DDevice->SetRenderState(D3DRS_FOGTABLEMODE,D3DFOG_LINEAR);
  612. }
  613. void CGame::SetWaterFog()
  614. {   
  615. m_pD3DDevice->SetRenderState(D3DRS_FOGENABLE,TRUE);
  616. m_pD3DDevice->SetRenderState(D3DRS_FOGCOLOR,D3DCOLOR_XRGB(90,184,194));
  617. D3DCAPS9 stCaps;
  618. m_pD3DDevice->GetDeviceCaps(&stCaps);
  619. if(stCaps.RasterCaps&D3DPRASTERCAPS_FOGRANGE)
  620. {
  621. m_pD3DDevice->SetRenderState(D3DRS_RANGEFOGENABLE,TRUE);
  622. }
  623. float density=0.002;
  624. m_pD3DDevice->SetRenderState(D3DRS_FOGDENSITY,*(DWORD*)&density);
  625. m_pD3DDevice->SetRenderState(D3DRS_FOGTABLEMODE,D3DFOG_EXP);
  626. }
  627. void CGame::RenderText()
  628. {   char buffer[200];
  629. DWORD dwDuration=(timeGetTime()-m_dwStartTime)/1000;
  630. //DWORD fps;
  631. //fps=(m_dwFrame/dwDuration);
  632. if(dwDuration<1)
  633. {
  634. sprintf(buffer,"Calculating...");
  635. }
  636. else
  637. {
  638. //sprintf(buffer,"Duration:%d second, Frame:%3d, FPS:%d",dwDuration,m_dwFrame,(m_dwFrame/dwDuration));
  639.          sprintf(buffer,"x:%f second, y:%f, z:%f, FPS:%d",ViewPos.x,ViewPos.y,ViewPos.z,(m_dwFrame/dwDuration));
  640. }     //sprintf(buffer,"x:%f second, y:%f, z:%f, FPS:%d",ViewPos.x,ViewPos.y,ViewPos.z,(m_dwFrame/dwDuration));
  641. m_pFont->DrawText(buffer,0.0f,0.0f);
  642. }
  643. void CGame::Render()
  644. {
  645.     
  646. m_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,200),1.0F,0);
  647. m_pD3DDevice->BeginScene();
  648. SetCamera3D();
  649. AI();
  650. Render3D();
  651.     GetMouse();
  652.  GetMouse1();
  653.     SetCamera2D();
  654.   //  if(present==TRUE)
  655. //{
  656.      Render2D();
  657. // }
  658.     
  659. RenderText();
  660. m_pD3DDevice->EndScene();
  661. m_pD3DDevice->Present(NULL,NULL,NULL,NULL);
  662. m_dwFrame++;
  663. }
  664. void CGame::AI()
  665. {  
  666. static bool walk=true;
  667. static bool follow=false;
  668. static bool back=false;
  669. static NPOSITION preBossPos=BossPos;
  670. static DWORD time=timeGetTime()/1000.0f;
  671. DWORD timeNow=timeGetTime()/1000.0F;
  672. D3DXVECTOR3 BossPoint=D3DXVECTOR3(2417.0F,254.0F,1185.0F);
  673.     D3DXVECTOR3 BossPointNow=D3DXVECTOR3(BossPos.x,BossPos.y,BossPos.z);
  674. D3DXVECTOR3 ViewPosNow=D3DXVECTOR3(ViewPos.x,ViewPos.y,ViewPos.z);
  675.     float distance=D3DXVec3Length(&(BossPointNow-ViewPosNow));
  676. float original=D3DXVec3Length(&(BossPoint-BossPointNow));
  677. if((follow==false)&(back==false))
  678. {
  679. walk=true;
  680. }
  681.     if((distance<=300)&(walk==true)&(back==false))
  682. {
  683. follow=true;
  684. walk=false;
  685. back=false;
  686. }
  687. if((distance>=400)&(follow==true))
  688. {
  689. walk=false;
  690. follow=false;
  691. back=true;
  692. }
  693. if((original<=50)&(back==true))
  694. {
  695. walk=true;
  696. follow=false;
  697. back=false;
  698. }
  699. if(walk==true)
  700. {
  701.      m_pSBoss->SetAnimationName("walk");
  702. if(timeNow-time>=4)
  703. {
  704. BossPos.a+=rand()%360;
  705.      while((BossPos.a>180)|(BossPos.a<=-180))
  706. {
  707.               if(BossPos.a>180)
  708.   BossPos.a-=360;
  709.   if(BossPos.a<=-180)
  710.   BossPos.a+=360;
  711. }     
  712. time=timeNow;
  713. }
  714. BossPos.x+=0.5*cos(D3DX_PI/180*BossPos.a);
  715. BossPos.z+=0.5*sin(D3DX_PI/180*BossPos.a);
  716. BossPos.y=m_pTerrain->GetHeight(BossPos.x,BossPos.z);
  717. BossPointNow=D3DXVECTOR3(BossPos.x,BossPos.y,BossPos.z);
  718. float length=D3DXVec3Length(&(BossPointNow-BossPoint));
  719. if(length>=500)
  720. {
  721. BossPos=preBossPos;
  722. }
  723. else
  724. {
  725. preBossPos=BossPos;
  726. }
  727. }
  728.  
  729. if(follow==true)
  730. {
  731.         if(distance<=40)
  732. {
  733.  static int dongzuo=rand()%2;
  734.  if(timeNow-time>=0.8f)
  735.  {
  736.  dongzuo=rand()%2;
  737.  time=timeNow;
  738.  }
  739. switch(dongzuo)
  740. {
  741. case 0:
  742.                 m_pSBoss->SetAnimationName("skill1");
  743. break;
  744. case 1:
  745.                 m_pSBoss->SetAnimationName("skill2");
  746. break;
  747. }
  748. }
  749. else
  750. {
  751.         m_pSBoss->SetAnimationName("run");
  752. }
  753.         D3DXVECTOR3 Distance;
  754. D3DXVECTOR3 Dir;
  755. D3DXVec3Normalize(&Dir,&D3DXVECTOR3(2*cos(D3DX_PI/180*BossPos.a),0,2*sin(D3DX_PI/180*BossPos.a)));
  756. D3DXVec3Normalize(&Distance,&(D3DXVECTOR3(ViewPos.x,0,ViewPos.z)-D3DXVECTOR3(BossPos.x,0,BossPos.z)));
  757. static int preangle=0;
  758.  float god=acosf(D3DXVec3Dot(&Dir,&Distance));
  759.  
  760.  int angle=(int)god/(D3DX_PI/180);
  761.  
  762.    if(abs(preangle-angle)<=1)
  763.    {
  764.           angle=preangle;
  765.    }
  766.    else
  767.    {
  768.           preangle=angle;
  769.    }
  770.    
  771.         BossPos.a+=angle;
  772.  while((BossPos.a>180)|(BossPos.a<=-180))
  773. {
  774.               if(BossPos.a>180)
  775.   BossPos.a-=360;
  776.   if(BossPos.a<=-180)
  777.   BossPos.a+=360;
  778. }     
  779. if(distance>=30)
  780. {
  781.         BossPos.x+=1.5*cos(D3DX_PI/180*BossPos.a);
  782.     BossPos.z+=1.5*sin(D3DX_PI/180*BossPos.a);
  783.     BossPos.y=m_pTerrain->GetHeight(BossPos.x,BossPos.z);
  784. }
  785. }
  786. if(back==true)
  787. {
  788.         m_pSBoss->SetAnimationName("run");
  789.         D3DXVECTOR3 Distance;
  790. D3DXVECTOR3 Dir;
  791. D3DXVec3Normalize(&Dir,&D3DXVECTOR3(2*cos(D3DX_PI/180*BossPos.a),0,2*sin(D3DX_PI/180*BossPos.a)));
  792. D3DXVec3Normalize(&Distance,&(D3DXVECTOR3(BossPoint.x,0,BossPoint.z)-D3DXVECTOR3(BossPos.x,0,BossPos.z)));
  793. static int preangle1=0;
  794.  float god=acosf(D3DXVec3Dot(&Dir,&Distance));
  795.  
  796.  int angle=(int)god/(D3DX_PI/180);
  797.  
  798.    if(abs(preangle1-angle)<=1)
  799.    {
  800.           angle=preangle1;
  801.    }
  802.    else
  803.    {
  804.           preangle1=angle;
  805.    }
  806.    
  807.         BossPos.a+=angle;
  808.  while((BossPos.a>180)|(BossPos.a<=-180))
  809. {
  810.               if(BossPos.a>180)
  811.   BossPos.a-=360;
  812.   if(BossPos.a<=-180)
  813.   BossPos.a+=360;
  814. }     
  815.         BossPos.x+=2*cos(D3DX_PI/180*BossPos.a);
  816.     BossPos.z+=2*sin(D3DX_PI/180*BossPos.a);
  817.     BossPos.y=m_pTerrain->GetHeight(BossPos.x,BossPos.z);
  818. }
  819. }
  820. void CGame::RoleAction()
  821. {   static bool attack=false;
  822. static int Preflash=0;
  823.     static int record=0;
  824.     static DWORD time=timeGetTime()/1000.0f;
  825. DWORD timeNow=timeGetTime()/1000.0F;
  826. static DWORD time_1=0.0f;
  827. DWORD timeNow_1=timeGetTime()/1000.0f;
  828.     if(Preflash!=flash)
  829. {
  830.      m_pSkinMesh->SetAnimationName("idle");record=0;
  831. if((PreViewPos.a!=ViewPos.a)|(PreViewPos.b!=ViewPos.b))
  832. {
  833.             m_pSkinMesh->SetAnimationName("walk");
  834. record=1;
  835. }
  836. if((PreViewPos.x!=ViewPos.x)|(PreViewPos.z!=ViewPos.z))
  837. {
  838.            m_pSkinMesh->SetAnimationName("run");
  839.    record=2;
  840. }
  841.     D3DXVECTOR3 BossPointNow=D3DXVECTOR3(BossPos.x,BossPos.y,BossPos.z);
  842. D3DXVECTOR3 ViewPosNow=D3DXVECTOR3(ViewPos.x,ViewPos.y,ViewPos.z);
  843.     float distance=D3DXVec3Length(&(BossPointNow-ViewPosNow));
  844.       D3DXVECTOR3 Distance;
  845.   D3DXVECTOR3 Dir;
  846.   D3DXVec3Normalize(&Dir,&D3DXVECTOR3(2*cos(D3DX_PI/180*ViewPos.a),0,2*sin(D3DX_PI/180*ViewPos.a)));
  847.   D3DXVec3Normalize(&Distance,&(D3DXVECTOR3(BossPos.x,0,BossPos.z)-D3DXVECTOR3(ViewPos.x,0,ViewPos.z)));
  848.   static int preangle=0;
  849.   float god=acosf(D3DXVec3Dot(&Dir,&Distance));
  850.  
  851.   int angle=(int)god/(D3DX_PI/180);
  852.  
  853.    if(abs(preangle-angle)<=1)
  854.    {
  855.           angle=preangle;
  856.    }
  857.    else
  858.    {
  859.           preangle=angle;
  860.    }
  861.         while((angle>180)|(angle<=-180))
  862. {
  863.               if(angle>180)
  864.   angle-=360;
  865.   if(angle<=-180)
  866.  angle+=360;
  867. }
  868.        
  869. if((distance<=40)&(present==true))//人物和npc距离小于40,且鼠标右键被按下
  870. {
  871.         if((angle<=80)&(angle>=-80))
  872. {
  873.           m_pSkinMesh->SetAnimationName("skill1");
  874.            record=3;
  875.    
  876. }
  877. else
  878. {
  879.              time=timeNow;  
  880. }
  881. }
  882.       
  883.   
  884. Preflash=flash;
  885. }
  886. else
  887. {
  888. switch(record)
  889. {
  890. case 0:
  891.               m_pSkinMesh->SetAnimationName("idle");
  892.   break;
  893. case 1:
  894.   m_pSkinMesh->SetAnimationName("walk");
  895.   break;
  896. case 2:
  897.   m_pSkinMesh->SetAnimationName("run");
  898.   break;
  899. case 3:
  900.   m_pSkinMesh->SetAnimationName("skill1");
  901.   break;
  902. }
  903. }
  904. if(present==true)
  905. {
  906. if((timeNow-time)<=3)
  907. {
  908.              char buffer[200];
  909.          sprintf(buffer,"You must correct your direction");
  910.          m_pWrong->DrawText(buffer,250.0f,200.0f);
  911. }
  912. }
  913. if(nomoney==true)
  914. {
  915. time_1=timeNow_1;
  916. }
  917. if(timeNow_1-time_1<=3)
  918. {
  919.          char buffer[200];
  920.      sprintf(buffer,"It's a demo.So.....");
  921.      m_pWrong->DrawText_1(buffer,250.0f,470.0f);
  922. }
  923.      
  924. }
  925. bool CGame::Collipe(COLLIPE col)
  926. {
  927.     D3DXVECTOR3 RolePoint=D3DXVECTOR3(ViewPos.x,ViewPos.y,ViewPos.z);
  928. D3DXVECTOR3 dest=D3DXVECTOR3(col.x,col.y,col.z);
  929.     float distance=D3DXVec3Length(&(dest-RolePoint));
  930. if(distance<=col.dis)
  931. {
  932.      return true;
  933. }
  934. else
  935. {
  936.      return false;
  937. }
  938. }
  939. void CGame::Render3D()
  940.     m_pTerrain->Translate();
  941. m_pTerrain->RenerTerrain();
  942.     m_pSky->Translate(ViewPos);
  943. m_pSky->RenderSky();
  944.     
  945. m_pMesh->Translate(ViewPos);
  946. m_pMesh->RenderMesh();
  947. m_pBoss->Translation(BossPos);
  948. m_pBoss->RenderMesh();
  949.     ///////////////
  950. //渲染模型和模型定位的地方
  951. ////////////
  952. //静态模型的定位,参数分别是X,Y,Z,旋转角度,放大倍数
  953. //静态模型渲染
  954.   
  955. //静态模型的定位,参数分别是X,Y,Z,旋转角度,放大倍数
  956. m_pTree_1_1->Translation(1662,254,3729,0,5);//0
  957. m_pTree_1_1->RenderNpc();//静态模型渲染
  958.     m_pTree_1_2->Translation(2009,253,3679,180,4.3);//1
  959.     m_pTree_1_2->RenderNpc();
  960.     m_pTree_1_3->Translation(2294,253,3693,90,4);//2
  961.     m_pTree_1_3->RenderNpc();
  962. m_pTree_1_4->Translation(2605,253,3671,45,8);//3
  963.     m_pTree_1_4->RenderNpc();
  964. m_pTree_1_5->Translation(3292,253,2558,0,6);//4
  965.     m_pTree_1_5->RenderNpc();
  966. m_pTree_1_6->Translation(3619,253,2353,180,6);//5
  967.     m_pTree_1_6->RenderNpc();
  968. m_pTree_1_7->Translation(3609,253,2254,90,5);//6
  969.     m_pTree_1_7->RenderNpc();
  970. m_pTree_1_8->Translation(3686,253,1992,45,4);//7
  971.     m_pTree_1_8->RenderNpc();
  972. m_pTree_1_9->Translation(3699,253,1873,270,5);//8
  973.     m_pTree_1_9->RenderNpc();
  974. m_pTree_1_10->Translation(3660,253,1653,0,6);//9
  975.     m_pTree_1_10->RenderNpc();
  976. m_pTree_1_11->Translation(141,253,3216,90,6);//10
  977.     m_pTree_1_11->RenderNpc();
  978. m_pTree_1_12->Translation(155,253,2950,90,3);//11
  979.     m_pTree_1_12->RenderNpc();
  980. m_pTree_1_13->Translation(2568,253,2574,270,6);//12
  981.     m_pTree_1_13->RenderNpc();
  982. m_pTree_1_14->Translation(1851,253,3599,270,5);//13
  983.     m_pTree_1_14->RenderNpc();
  984. m_pTree_1_15->Translation(2184,253,3570,270,3);//14
  985.     m_pTree_1_15->RenderNpc();
  986. m_pTree_1_16->Translation(3637,253,2109,90,4);//15
  987.     m_pTree_1_16->RenderNpc();
  988. m_pTree_1_17->Translation(3448,253,2465,180,6);//16
  989.     m_pTree_1_17->RenderNpc();
  990. m_pTree_1_18->Translation(2083,303,2568,90,3);//17
  991.     m_pTree_1_18->RenderNpc();
  992. m_pTree_1_19->Translation(2458,253,3598,180,3);//18
  993.     m_pTree_1_19->RenderNpc();
  994. m_pTree_1_20->Translation(2180,303,2567,270,3);//19
  995.     m_pTree_1_20->RenderNpc();
  996. m_pTree_1_21->Translation(2773,310,2569,0,3);//20
  997.     m_pTree_1_21->RenderNpc();
  998.     m_pTree_u_1->Translation(3135,255,2665,0,2);//21
  999.     m_pTree_u_1->RenderNpc();
  1000. m_pTree_u_2->Translation(2293,315,2573,90,2.5);//22
  1001.     m_pTree_u_2->RenderNpc();
  1002. m_pTree_u_3->Translation(2409,315,2566,270,1.8);//23
  1003.     m_pTree_u_3->RenderNpc();
  1004. m_pTree_u_4->Translation(2539,268,3503,0,1);//24
  1005.     m_pTree_u_4->RenderNpc();
  1006. m_pTree_u_5->Translation(2330,293,3586,270,1.8);//25
  1007.     m_pTree_u_5->RenderNpc();
  1008. m_pDead_cow_1->Translation(2975,220,345,180,3);//26
  1009.     m_pDead_cow_1->RenderNpc();
  1010. m_pRock_1_1->Translation(2885,295,2566,0,3);//27
  1011.     m_pRock_1_1->RenderNpc();
  1012. m_pRock_1_2->Translation(3010,315,2575,180,3);//28
  1013.     m_pRock_1_2->RenderNpc();
  1014. m_pRock_1_3->Translation(3185,305,2581,270,3);//29
  1015.     m_pRock_1_3->RenderNpc();
  1016.     m_pRock_1_6->Translation(3056,295,2474,90,3);//30
  1017.     m_pRock_1_6->RenderNpc();
  1018. m_pRock_1_5->Translation(3010,295,2635,0,3);//31
  1019.     m_pRock_1_5->RenderNpc();
  1020. m_pRock_1_4->Translation(3185,305,2521,180,3);//32
  1021.     m_pRock_1_4->RenderNpc();
  1022. m_pRock_2_1->Translation(2925,295,2560,0,3);//33
  1023.     m_pRock_2_1->RenderNpc();
  1024. m_pRock_2_2->Translation(2955,295,2570,180,3);//34
  1025.     m_pRock_2_2->RenderNpc();
  1026. m_pRock_2_3->Translation(3060,315,2570,90,3);//35
  1027.     m_pRock_2_3->RenderNpc();
  1028. m_pRock_2_6->Translation(2925,295,2610,270,3);//36
  1029.     m_pRock_2_6->RenderNpc();
  1030. m_pRock_2_5->Translation(2955,295,2620,0,3);//37
  1031.     m_pRock_2_5->RenderNpc();
  1032. m_pRock_2_4->Translation(3060,305,2620,180,3);//38
  1033.     m_pRock_2_4->RenderNpc();
  1034. m_pRock_3_1->Translation(420,200,3340,180,3);//39
  1035.     m_pRock_3_1->RenderNpc();
  1036. m_pRock_4_1->Translation(163,300,2711,0,3);//40
  1037.     m_pRock_4_1->RenderNpc();
  1038. m_pTree_2_1->Translation(1166,250,814,0,12);//41
  1039. m_pTree_2_1->RenderNpc();
  1040. m_pTree_2_1->Translation(3357,200,409,270,8);//42
  1041. m_pTree_2_1->RenderNpc();
  1042. m_pHole->Translation(3638,254,3252,180,5);//43
  1043. m_pHole->RenderNpc();
  1044. m_pBridge->Translation(780,-80,3000,100,3.5);
  1045. m_pBridge->RenderNpc();
  1046.     static float timeStart=timeGetTime()/1000.0f;
  1047. static float timeNow=timeGetTime()/1000.0f;
  1048. float tFrameTime=timeNow;
  1049. timeNow=timeGetTime()/1000.0f;
  1050. tFrameTime=timeNow-tFrameTime;
  1051.      
  1052. /*switch (((long)floor(timeNow/2))%5)
  1053. {
  1054. case 0: m_pSkinMesh->SetAnimationName("idle");break;
  1055. case 1: m_pSkinMesh->SetAnimationName("walk");break;
  1056. case 2: m_pSkinMesh->SetAnimationName("run");break;
  1057. case 3: m_pSkinMesh->SetAnimationName("skill1");break;
  1058. case 4: m_pSkinMesh->SetAnimationName("skill2");break;
  1059. }*/
  1060.    
  1061. RoleAction();
  1062.    
  1063. //m_pSkinMesh->Translation(ViewPos);
  1064. ///////////////////
  1065. //动态模型定位和渲染
  1066. ////////////////
  1067. //第一个参数不用管,复制就行,D3DXVECTOR3中代表X,Y,Z坐标,旋转角度,放大倍数
  1068.    
  1069. m_pSkinMesh->Render(tFrameTime,D3DXVECTOR3(ViewPos.x,ViewPos.y,ViewPos.z),-D3DX_PI/180*(ViewPos.a+90.0f));//D3DXVECTOR3(ViewPos.x,ViewPos.y+65.0f,ViewPos.z),-D3DX_PI/180*(ViewPos.a),5.0f);
  1070.     PreViewPos=ViewPos;
  1071.    // m_pSBoss->SetAnimationName("walk");
  1072. m_pSBoss->Render(tFrameTime,D3DXVECTOR3(BossPos.x,BossPos.y,BossPos.z),-D3DX_PI/180*(BossPos.a+90.0f),1);
  1073. m_pWater->Translate();
  1074. m_pWater->RenerWater(cy);
  1075. //m_pSnow->MoveSnow(DirWorld,OriginWorld);
  1076.    // m_pSnow->RenderSnow();
  1077. }
  1078. void CGame::Render2D()
  1079. m_pPanel->Translate(-400,+300);
  1080. m_pPanel->RenderPanel();
  1081. if(present==true)
  1082. {
  1083. m_pPanel->Render();
  1084. }
  1085. }
  1086. void CGame::SetCamera3D()
  1087. {
  1088. D3DXMATRIX matView,matPers;
  1089.     D3DXVECTOR3 ViewVec=D3DXVECTOR3((ViewPos.x+ViewPos.cc*cos(D3DX_PI/180*ViewPos.cb)*cos(D3DX_PI/180*ViewPos.ca)),
  1090.                                  (ViewPos.y+ViewPos.cc*sin(D3DX_PI/180*ViewPos.cb)),
  1091.  (ViewPos.z+ViewPos.cc*cos(D3DX_PI/180*ViewPos.cb)*sin(D3DX_PI/180*ViewPos.ca)));
  1092.     static D3DXVECTOR3 PreViewVec1=ViewVec;
  1093. if((ViewVec.x<10)|(ViewVec.z<10)|(ViewVec.x>128*30)|(ViewVec.z>128*30))
  1094. {
  1095. ViewVec=PreViewVec1;
  1096. }
  1097. else
  1098. {
  1099. PreViewVec1=ViewVec;
  1100. }
  1101. D3DXVECTOR3 ViewVec1=D3DXVECTOR3(ViewPos.x,ViewPos.y+25.0f,ViewPos.z);
  1102. D3DXVECTOR3 PreViewVec=ViewVec;
  1103.     
  1104. float ViewMinHeight=m_pTerrain->GetHeight(ViewVec.x,ViewVec.z)+10.0f;
  1105. if(ViewVec.y<=ViewMinHeight)
  1106. {
  1107. ViewVec.y=ViewMinHeight;
  1108.         
  1109. ViewFlag=true;//可移动视点
  1110. }
  1111. else
  1112. {
  1113. ViewFlag=false;//可移动摄像机
  1114. }
  1115. if(ViewFlag==false)
  1116. {
  1117. D3DXMatrixLookAtLH(&matView,&ViewVec,&D3DXVECTOR3(ViewPos.x,ViewPos.y+25.0f,ViewPos.z),&D3DXVECTOR3(0.0F,1.0F,0.0F));
  1118. }
  1119. else
  1120. {
  1121.         ViewVec.y=ViewMinHeight;
  1122. D3DXVECTOR3 axis,Nsub;D3DXMATRIX matRo;
  1123.     D3DXVECTOR3 sub=ViewVec1-ViewVec;
  1124.      
  1125. D3DXVec3Normalize(&Nsub,&sub);
  1126.     D3DXVec3Normalize(&vecUp,&vecUp);
  1127. D3DXVec3Cross(&axis,&vecUp,&Nsub);
  1128. if(sub.y>0)
  1129. D3DXMatrixRotationAxis(&matRo,&axis,D3DX_PI/180*fUp);
  1130. else
  1131. D3DXMatrixRotationAxis(&matRo,&axis,-D3DX_PI/180*fUp);
  1132. D3DXVec3TransformCoord(&sub,&sub,&matRo);
  1133. sub=sub+ViewVec;
  1134.     D3DXMatrixLookAtLH(&matView,&ViewVec,&sub,&D3DXVECTOR3(0.0F,1.0F,0.0F));
  1135. }
  1136. PreViewVec=ViewVec;
  1137. cy=ViewVec.y;
  1138. if(ViewVec.y<=m_pWater->y)
  1139. {
  1140. m_pD3DDevice->SetRenderState(D3DRS_FOGENABLE,FALSE);
  1141. SetWaterFog();
  1142. }
  1143. else
  1144. {
  1145.         m_pD3DDevice->SetRenderState(D3DRS_FOGENABLE,FALSE);
  1146. SetFog();
  1147. }
  1148. m_pD3DDevice->SetTransform(D3DTS_VIEW,&matView);
  1149. D3DXMatrixPerspectiveFovLH(&matPers,D3DX_PI/4,1.25f,1.0F,10000.0F);
  1150.    
  1151. m_pD3DDevice->SetTransform(D3DTS_PROJECTION,&matPers);
  1152. matProj=matView;
  1153.     m_pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
  1154. m_pD3DDevice->SetRenderState(D3DRS_ZFUNC,D3DCMP_LESS);
  1155. m_pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
  1156. m_pD3DDevice->SetRenderState(D3DRS_LIGHTING,true);
  1157.    
  1158. }
  1159. void CGame::SetCamera2D()
  1160. {
  1161. D3DXMATRIX matPers,matView;
  1162. D3DXMatrixOrthoLH(&matPers,(float)m_nScreenWidth,(float)m_nScreenHeight,0.0f,1.0f);
  1163. D3DXMatrixIdentity(&matView);
  1164. m_pD3DDevice->SetTransform(D3DTS_PROJECTION,&matPers);
  1165. m_pD3DDevice->SetTransform(D3DTS_WORLD,&matView);
  1166. m_pD3DDevice->SetTransform(D3DTS_VIEW,&matView);
  1167. m_pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE);
  1168. m_pD3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
  1169. }
  1170. void CGame::GameLoop()
  1171. {
  1172. MSG msg;
  1173. BOOL bmessage;
  1174. PeekMessage(&msg,NULL,0U,0U,PM_NOREMOVE);
  1175. while(msg.message!=WM_QUIT)
  1176. {
  1177. bmessage=PeekMessage(&msg,NULL,0U,0U,PM_REMOVE);
  1178. if(bmessage)
  1179. {
  1180. TranslateMessage(&msg);
  1181. DispatchMessage(&msg);
  1182. }
  1183. else
  1184. {
  1185. Render();
  1186. }
  1187. }
  1188. }
  1189. HRESULT CGame::InitialD3D(HWND hWnd)
  1190. {
  1191. m_pD3D=Direct3DCreate9(D3D_SDK_VERSION);
  1192. if(m_pD3D==NULL)
  1193. {
  1194. E_FAIL;
  1195. }
  1196. D3DDISPLAYMODE d3ddm;
  1197. m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm);
  1198. if(d3ddm.Format!=D3DFMT_UNKNOWN)
  1199. {
  1200. d3ddm.Width=800;
  1201. d3ddm.Height=600;
  1202. }
  1203. m_nScreenWidth=d3ddm.Width;
  1204. m_nScreenHeight=d3ddm.Height;
  1205. D3DPRESENT_PARAMETERS d3dpp;
  1206. ZeroMemory(&d3dpp,sizeof(d3dpp));
  1207. d3dpp.Windowed=true;
  1208. d3dpp.BackBufferCount=1;
  1209. d3dpp.BackBufferFormat=d3ddm.Format;
  1210. d3dpp.BackBufferHeight=d3ddm.Height;
  1211. d3dpp.BackBufferWidth=d3ddm.Width;
  1212. d3dpp.hDeviceWindow=hWnd;
  1213. d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
  1214. d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
  1215. d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_ONE;
  1216. if(SUCCEEDED(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,d3ddm.Format,D3DUSAGE_DEPTHSTENCIL,D3DRTYPE_SURFACE,D3DFMT_D32)))
  1217. {
  1218. d3dpp.AutoDepthStencilFormat=D3DFMT_D32;
  1219. d3dpp.EnableAutoDepthStencil=true;
  1220. }
  1221. else if(SUCCEEDED(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,d3ddm.Format,D3DUSAGE_DEPTHSTENCIL,D3DRTYPE_SURFACE,D3DFMT_D24X8)))
  1222. {
  1223. d3dpp.AutoDepthStencilFormat=D3DFMT_D24X8;
  1224. d3dpp.EnableAutoDepthStencil=true;
  1225. }
  1226. else if(SUCCEEDED(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,d3ddm.Format,D3DUSAGE_DEPTHSTENCIL,D3DRTYPE_SURFACE,D3DFMT_D16)))
  1227. {
  1228. d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
  1229. d3dpp.EnableAutoDepthStencil=true;
  1230. }
  1231. else
  1232. {
  1233. d3dpp.EnableAutoDepthStencil=false;
  1234. }
  1235. if(FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&m_pD3DDevice)))
  1236. {
  1237. return E_FAIL;
  1238. }
  1239. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW)))
  1240. {
  1241. return E_FAIL;
  1242. }
  1243. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE)))
  1244. {
  1245. return E_FAIL;
  1246. }
  1247. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID))) 
  1248. //if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME)))
  1249. {
  1250.         return E_FAIL;
  1251. }
  1252. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS,TRUE)))
  1253. {
  1254. return E_FAIL;
  1255. }
  1256. m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
  1257.     m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
  1258. m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
  1259.     m_pD3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
  1260.     m_pD3DDevice->SetRenderState( D3DRS_ALPHAREF,        0x0f);
  1261.     m_pD3DDevice->SetRenderState( D3DRS_ALPHAFUNC,D3DCMP_GREATEREQUAL );
  1262.     m_pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
  1263. m_pD3DDevice->SetRenderState(D3DRS_ZFUNC,D3DCMP_LESS);
  1264. m_pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
  1265.     GetWindowRect(hWnd,&m_Rect);
  1266. return S_OK;
  1267. }
  1268. HRESULT CGame::InitialLight()
  1269. {
  1270. ZeroMemory(&d3dLight,sizeof(D3DLIGHT9));
  1271. d3dLight.Type=D3DLIGHT_POINT;
  1272. d3dLight.Position.x=1922;//ViewPos.x-500.0f;
  1273. d3dLight.Position.y=1000.0f;
  1274. d3dLight.Position.z=2229;//ViewPos.z-500.0f;
  1275. d3dLight.Attenuation0=1.0F;
  1276. d3dLight.Attenuation1=0.0f;
  1277. d3dLight.Attenuation2=0.0f;
  1278. d3dLight.Range=5000.0f;
  1279. d3dLight.Diffuse.r=1.0f;
  1280. d3dLight.Diffuse.g=1.0f;
  1281. d3dLight.Diffuse.b=1.0f;
  1282. d3dLight.Ambient.r=0.0f;
  1283. d3dLight.Ambient.g=0.0f;
  1284. d3dLight.Ambient.b=0.0f;
  1285. d3dLight.Specular.r=1.0f;
  1286. d3dLight.Specular.g=1.0f;
  1287. d3dLight.Specular.b=1.0f;
  1288. if(FAILED(m_pD3DDevice->SetLight(0,&d3dLight)))
  1289. {
  1290. E_FAIL;
  1291. }
  1292. if(FAILED(m_pD3DDevice->LightEnable(0,true)))
  1293. {
  1294. E_FAIL;
  1295. }
  1296. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_LIGHTING,TRUE)))
  1297. {
  1298. E_FAIL;
  1299. }
  1300. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_AMBIENT,D3DCOLOR_XRGB(60,60,60))))
  1301. {
  1302. E_FAIL;
  1303. }
  1304. return S_OK;
  1305. }
  1306. COpen *g_pOpen=NULL;
  1307. CGame * g_pGame=NULL;
  1308. bool * bOpen;
  1309. bool bOpenGame;
  1310. int APIENTRY _tWinMain(HINSTANCE hInstance,
  1311.                      HINSTANCE hPrevInstance,
  1312.                      LPTSTR    lpCmdLine,
  1313.                      int       nCmdShow)
  1314. {
  1315. hInst=hInstance;
  1316. WNDCLASSEX wc={sizeof(WNDCLASSEX),CS_CLASSDC,WndProc,0,0,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,"place",NULL};
  1317. RegisterClassEx(&wc);
  1318. HWND hWnd=CreateWindow("place","placekk",WS_DLGFRAME,0,0,800,600,NULL,NULL,wc.hInstance,NULL);//WS_OVERLAPPEDWINDOW
  1319.     bOpen=new bool[2];
  1320. for(int i=0;i<2;i++)
  1321. {
  1322. bOpen[i]=true;
  1323. }
  1324. bOpenGame=false;
  1325. g_pOpen=new COpen(hInst,hWnd);
  1326.     ShowWindow(hWnd,SW_SHOWDEFAULT);
  1327. UpdateWindow(hWnd);
  1328. g_pOpen->OpenLoop();
  1329.     SafeDelete(g_pOpen);
  1330.     if(bOpen[0]==false)
  1331. {
  1332.  
  1333. g_pGame=new CGame(hInst,hWnd);
  1334. if(SUCCEEDED(g_pGame->InitialD3D(hWnd)))
  1335. {
  1336.         g_pGame->InitialTools();
  1337.     g_pGame->InitialLight();
  1338. ShowWindow(hWnd,SW_SHOWDEFAULT);
  1339. UpdateWindow(hWnd);
  1340.     bOpenGame=true;
  1341. g_pGame->GameLoop();
  1342. }
  1343. SafeDelete(g_pGame);
  1344. }
  1345. UnregisterClass("place",wc.hInstance);
  1346. return 0;
  1347. }
  1348. //
  1349. //  函数: MyRegisterClass()
  1350. //
  1351. //  目的: 注册窗口类。
  1352. //
  1353. //  注释: 
  1354. //
  1355. //    仅当希望在已添加到 Windows 95 的
  1356. //    “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
  1357. //    才需要此函数及其用法。调用此函数
  1358. //    十分重要,这样应用程序就可以获得关联的
  1359. //   “格式正确的”小图标。
  1360. //
  1361. ATOM MyRegisterClass(HINSTANCE hInstance)
  1362. {
  1363. WNDCLASSEX wcex;
  1364. wcex.cbSize = sizeof(WNDCLASSEX); 
  1365. wcex.style = CS_HREDRAW | CS_VREDRAW;
  1366. wcex.lpfnWndProc = (WNDPROC)WndProc;
  1367. wcex.cbClsExtra = 0;
  1368. wcex.cbWndExtra = 0;
  1369. wcex.hInstance = hInstance;
  1370. wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_GAME);
  1371. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  1372. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  1373. wcex.lpszMenuName = (LPCTSTR)IDC_GAME;
  1374. wcex.lpszClassName = szWindowClass;
  1375. wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  1376. return RegisterClassEx(&wcex);
  1377. }
  1378. //
  1379. //   函数: InitInstance(HANDLE, int)
  1380. //
  1381. //   目的: 保存实例句柄并创建主窗口
  1382. //
  1383. //   注释: 
  1384. //
  1385. //        在此函数中,我们在全局变量中保存实例句柄并
  1386. //        创建和显示主程序窗口。
  1387. //
  1388. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  1389. {
  1390.    HWND hWnd;
  1391.    hInst = hInstance; // 将实例句柄存储在全局变量中
  1392.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  1393.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  1394.    if (!hWnd)
  1395.    {
  1396.       return FALSE;
  1397.    }
  1398.    ShowWindow(hWnd, nCmdShow);
  1399.    UpdateWindow(hWnd);
  1400.    return TRUE;
  1401. }
  1402. //
  1403. //  函数: WndProc(HWND, unsigned, WORD, LONG)
  1404. //
  1405. //  目的: 处理主窗口的消息。
  1406. //
  1407. //  WM_COMMAND - 处理应用程序菜单
  1408. //  WM_PAINT - 绘制主窗口
  1409. //  WM_DESTROY - 发送退出消息并返回
  1410. //
  1411. //
  1412. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  1413. {
  1414. /*int wmId, wmEvent;
  1415. PAINTSTRUCT ps;
  1416. HDC hdc;
  1417. switch (message) 
  1418. {
  1419. case WM_COMMAND:
  1420. wmId    = LOWORD(wParam); 
  1421. wmEvent = HIWORD(wParam); 
  1422. // 分析菜单选择:
  1423. switch (wmId)
  1424. {
  1425. case IDM_ABOUT:
  1426. DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  1427. break;
  1428. case IDM_EXIT:
  1429. DestroyWindow(hWnd);
  1430. break;
  1431. default:
  1432. return DefWindowProc(hWnd, message, wParam, lParam);
  1433. }
  1434. break;
  1435. case WM_PAINT:
  1436. hdc = BeginPaint(hWnd, &ps);
  1437. // TODO: 在此添加任意绘图代码...
  1438. EndPaint(hWnd, &ps);
  1439. break;
  1440. case WM_DESTROY:
  1441. PostQuitMessage(0);
  1442. break;
  1443. default:
  1444. return DefWindowProc(hWnd, message, wParam, lParam);
  1445. }
  1446. return 0;*/
  1447. switch(message)
  1448. {
  1449. case WM_DESTROY:
  1450. PostQuitMessage(0);
  1451. break;
  1452. return 0;
  1453. case WM_KEYUP:
  1454. switch(wParam)
  1455. {
  1456. case VK_ESCAPE:
  1457. if((bOpen[0]==false)&(bOpenGame==true))
  1458. g_pGame->m_pInput->run=false;
  1459. break;
  1460. case VK_F1:
  1461. KillTimer(hWnd,1);
  1462.             //bOpen=false;
  1463. if(g_pOpen!=NULL)
  1464. g_pOpen->bOpen=false;
  1465. DestroyWindow(hWnd);
  1466. break;
  1467.             return 0;
  1468. }
  1469. break;
  1470. case WM_CREATE:
  1471.          SetTimer(hWnd,1,20,NULL);
  1472.  
  1473. break;
  1474. case WM_TIMER:
  1475. if(bOpen[0]==true)
  1476. g_pOpen->Update();
  1477. if((bOpen[0]==false)&(bOpenGame==true))
  1478. g_pGame->m_pInput->Update();
  1479. switch(wParam)
  1480. {
  1481. case 1:
  1482.    if(bOpen[0]==true)
  1483.    {
  1484.    g_pOpen->Control(bOpen);
  1485.    g_pOpen->bOpen=bOpen[0];
  1486.    }
  1487.    if(bOpen[1]==false)
  1488.    {
  1489.    bOpen[1]=true;
  1490.    DestroyWindow(hWnd);
  1491.    }
  1492.    if((bOpen[0]==false)&(bOpenGame==true))
  1493.    {
  1494.    g_pGame->m_pInput->Control(&g_pGame->ViewFlag,&g_pGame->vecUp,&g_pGame->fUp,&g_pGame->shoot,
  1495.                                            g_pGame->m_pPanel->NumOfPos,g_pGame->m_pPanel->CallPress
  1496.    ,g_pGame->m_pPanel->ButtonPress,g_pGame->m_pPanel->Result,
  1497.    &g_pGame->ViewPos,g_pGame->object,g_pGame->m_pTerrain,
  1498.    &g_pGame->BossPos,&g_pGame->nomoney);
  1499.    g_pGame->flash++;
  1500.    if(g_pGame->flash==100)
  1501.    {
  1502.    g_pGame->flash=0;
  1503.    }
  1504.    
  1505.    if(g_pGame->m_pPanel->Result[1]==true)
  1506.    
  1507.    { KillTimer(hWnd,1);
  1508.            
  1509.        DestroyWindow(hWnd);
  1510.    }
  1511.    }
  1512. break;
  1513. }
  1514. break;
  1515. }
  1516. return DefWindowProc(hWnd,message,wParam,lParam);
  1517. }
  1518. // “关于”框的消息处理程序。
  1519. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  1520. {
  1521. switch (message)
  1522. {
  1523. case WM_INITDIALOG:
  1524. return TRUE;
  1525. case WM_COMMAND:
  1526. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  1527. {
  1528. EndDialog(hDlg, LOWORD(wParam));
  1529. return TRUE;
  1530. }
  1531. break;
  1532. }
  1533. return FALSE;
  1534. }