Game.cpp
资源名称:3DRPG.rar [点击查看]
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:46k
源码类别:
游戏
开发平台:
Visual C++
- // Game.cpp : 定义应用程序的入口点。
- //在class Game的private中声明静态模型和动态模型
- #include "stdafx.h"
- #include "Game.h"
- #include <d3dx9.h>
- #include <d3dx9math.h>
- #include <mmsystem.h>
- #include "Font.h"
- #include <stdio.h>
- #include "Mesh.h"
- #include "Panel.h"
- #include "Input.h"
- //#include "Terrain.h"
- #include "npc.h"
- #include "Sky.h"
- #include "Snow.h"
- #include "Water.h"
- #include "SkinMesh.h"
- #include "Open.h"
- //#include "Sound.h"
- //#include "Music.h"
- #define MAX_LOADSTRING 100
- // 全局变量:
- HINSTANCE hInst; // 当前实例
- TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
- TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
- #define SafeRelease(pObject) if(pObject!=NULL){pObject->Release();pObject=NULL;}
- #define SafeDelete(object) if(object!=NULL){delete object;object=NULL;}
- #define MESH_D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
- // 此代码模块中包含的函数的前向声明:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- class CGame
- {
- public:
- CGame(HINSTANCE hinst,HWND hwnd);
- virtual ~CGame();
- void GameLoop();
- void Render();
- POSITION ViewPos;
- POSITION PreViewPos;
- COLLIPE * object;
- NPOSITION BossPos;
- int flash;
- void InitialTools();
- HRESULT InitialD3D(HWND hWnd);
- HRESULT InitialLight();
- CInput * m_pInput;
- CTerrain * m_pTerrain;
- bool ViewFlag;//镜头碰到地面的判断
- D3DXVECTOR3 vecUp,sub;;//sub为视点位置与摄像机位置的矢量差,vecUp为sub相对变化矢量
- float fUp;
- bool shoot;//判断鼠标左键是否点下,按下为true
- CPanel * m_pPanel;
- bool nomoney;
- private:
- D3DLIGHT9 d3dLight;
- int iBianhao;
- POSITION SetNpcPosition(float x,float z);
- void SetCamera2D();
- void Render2D();
- void SetCamera3D();
- void Render3D();
- void RenderText();
- void SetFog();
- void SetWaterFog();
- void GetMouse();
- void GetMouse1();
- LPDIRECT3D9 m_pD3D;
- LPDIRECT3DDEVICE9 m_pD3DDevice;
- DWORD m_dwFrame;
- DWORD m_dwStartTime;
- CFont * m_pFont;
- CFont * m_pWrong;//当角色想攻击npc,但背朝对方时使用
- CWater * m_pWater;
- //CSnow * m_pSnow;
- CSky * m_pSky;
- int m_nScreenWidth;
- int m_nScreenHeight;
- HWND hWnd;
- HINSTANCE hInst;
- POINT m_Point;
- D3DXVECTOR3 m_Proj;
- RECT m_Rect;
- BOOL present;
- D3DXVECTOR3 DirWorld,OriginWorld;
- D3DXMATRIX matProj;
- void ScreenTo3D(int mx,int my,D3DXVECTOR3 * p1,D3DXVECTOR3 * p2);
- float cy;//镜头的高度
- void AI();//npc的人工智能,包括行走和动作
- void RoleAction();//主角的动作
- bool Collipe(COLLIPE col);//npc、主角和场景的碰撞检测
- CSound * m_pMusic;
- CSound * m_pSound;
- ///////////////
- //声明模型的地方,声明过后请到InitialTools()中初始化模型,静态和动态的初始化有区别
- //////////////
- //静态模型用CNpc类,动态模型用CSkinMesh类
- CNpc * m_pTree_1_1;
- CNpc * m_pTree_1_2;
- CNpc * m_pTree_1_3;
- CNpc * m_pTree_1_4;
- CNpc * m_pTree_1_5;
- CNpc * m_pTree_1_6;
- CNpc * m_pTree_1_7;
- CNpc * m_pTree_1_8;
- CNpc * m_pTree_1_9;
- CNpc * m_pTree_1_10;
- CNpc * m_pTree_1_11;
- CNpc * m_pTree_1_12;
- CNpc * m_pTree_1_13;
- CNpc * m_pTree_1_14;
- CNpc * m_pTree_1_15;
- CNpc * m_pTree_1_16;
- CNpc * m_pTree_1_17;
- CNpc * m_pTree_1_18;
- CNpc * m_pTree_1_19;
- CNpc * m_pTree_1_20;
- CNpc * m_pTree_1_21;
- CNpc * m_pTree_u_1;
- CNpc * m_pTree_u_2;
- CNpc * m_pTree_u_3;
- CNpc * m_pTree_u_4;
- CNpc * m_pTree_u_5;
- CNpc * m_pDead_cow_1;
- CNpc * m_pRock_1_1;
- CNpc * m_pRock_1_2;
- CNpc * m_pRock_1_3;
- CNpc * m_pRock_1_4;
- CNpc * m_pRock_1_5;
- CNpc * m_pRock_1_6;
- CNpc * m_pRock_2_1;
- CNpc * m_pRock_2_2;
- CNpc * m_pRock_2_3;
- CNpc * m_pRock_2_4;
- CNpc * m_pRock_2_5;
- CNpc * m_pRock_2_6;
- CNpc * m_pRock_3_1;
- CNpc * m_pRock_4_1;
- CNpc * m_pTree_2_1;
- CNpc * m_pTree_2_2;
- CNpc * m_pBridge;
- CNpc * m_pHole;
- CMesh * m_pBoss;
- CMesh * m_pMesh;
- CSkinMesh * m_pSkinMesh;
- CSkinMesh * m_pSBoss;
- };
- CGame::CGame(HINSTANCE hinst,HWND hwnd)
- { hWnd=hwnd;
- hInst=hinst;
- m_pD3D=NULL;
- m_pD3DDevice=NULL;
- m_pFont=NULL;
- m_dwFrame=0;
- m_dwStartTime=timeGetTime();
- flash=0;
- ViewPos.x=3037.0f;//500
- ViewPos.y=0.0f;
- ViewPos.z=3205.0f;//500
- ViewPos.a=180.0f;
- ViewPos.b=0.0f;
- ViewPos.ca=0.0f;
- ViewPos.cb=45.0f;
- ViewPos.cc=200.0f;
- PreViewPos=ViewPos;
- BossPos.x=2417;
- BossPos.y=254;
- BossPos.z=1185;
- BossPos.a=0;
- fUp=0.0f;
- shoot=false;
- nomoney=false;
- }
- POSITION CGame::SetNpcPosition(float x,float z)
- {
- POSITION MonsterPos;
- MonsterPos.x=x;MonsterPos.z=z;
- MonsterPos.y=m_pTerrain->GetHeight(x,z);
- return MonsterPos;
- }
- void CGame::InitialTools()
- {
- HFONT hNewFont = CreateFont(50, 0, 0, 0, 0, 0, 0, 0,
- ANSI_CHARSET, 0, 0, 0, 0, "shit");
- bool hihi=false;
- HFONT hOldFont;
- HDC hwndDC;
- HDC dcMem;
- HBITMAP bmpMem;
- HBITMAP bmpMem_2;
- HBITMAP bmpMem_3;
- LOGBRUSH logB={BS_SOLID|DIB_RGB_COLORS,RGB(100,60,160),HS_VERTICAL};
- HBRUSH hbr=CreateBrushIndirect(&logB);
- //LPCTSTR pStr="载入中...";
- //LPCRSTR pEnd="请按鼠标左键进入游戏";
- int length=50;
- hwndDC=GetDC(hWnd);
- hOldFont=(HFONT)SelectObject(hwndDC,hNewFont);
- RECT rect,FrameRect,DrawRect;
- GetClientRect(hWnd,&rect);
- FrameRect.left=220;FrameRect.top=450;FrameRect.right=620;FrameRect.bottom=470;
- DrawRect.left=220;DrawRect.top=450;DrawRect.right=220;DrawRect.bottom=470;
- dcMem=CreateCompatibleDC(hwndDC);
- bmpMem=(HBITMAP)LoadImage(NULL,"./res/loading.bmp",IMAGE_BITMAP,rect.right ,rect.bottom,LR_LOADFROMFILE);
- bmpMem_2=(HBITMAP)LoadImage(NULL,"./res/loading_1.bmp",IMAGE_BITMAP,311 ,36,LR_LOADFROMFILE);
- bmpMem_3=(HBITMAP)LoadImage(NULL,"./res/loading_2.bmp",IMAGE_BITMAP,311 ,36,LR_LOADFROMFILE);
- SelectObject(dcMem,bmpMem);
- BitBlt(hwndDC,0,0,rect.right,rect.bottom,dcMem,0,0,SRCCOPY);
- SetBkMode(hwndDC,TRANSPARENT);
- Rectangle(hwndDC, FrameRect.left ,FrameRect.top,FrameRect.right,FrameRect.bottom);
- SelectObject(dcMem,bmpMem_2);
- BitBlt(hwndDC,250,500,311,36,dcMem,0,0,SRCCOPY);
- SetBkMode(hwndDC,TRANSPARENT);
- //TextOut(hwndDC,250, 450,"载入中", 6);
- m_pFont=new CFont(m_pD3DDevice,"vadana");
- m_pWrong=new CFont(m_pD3DDevice,"vadana");
- m_pMesh=new CMesh(m_pD3DDevice,"./mod/kuangjia.X");
- m_pSkinMesh=new CSkinMesh(m_pD3DDevice);
- m_pSkinMesh->LoadFromXFile("./mod/role.X");
- DrawRect.right+=length;
- FillRect(hwndDC,&DrawRect,hbr);
- m_pPanel=new CPanel(m_pD3DDevice,900,64,m_nScreenWidth,m_nScreenHeight);
- DrawRect.right+=length;
- FillRect(hwndDC,&DrawRect,hbr);
- m_pTerrain=new CTerrain(hInst,m_pD3DDevice);
- m_pWater=new CWater(hInst,m_pD3DDevice,m_pTerrain->m_dwRow,m_pTerrain->m_dwCol);
- DrawRect.right+=length;
- FillRect(hwndDC,&DrawRect,hbr);
- ViewPos.y=m_pTerrain->GetHeight(ViewPos.x,ViewPos.z);
- DrawRect.right+=length;
- FillRect(hwndDC,&DrawRect,hbr);
- object=new COLLIPE[44];
- DrawRect.right+=length;
- FillRect(hwndDC,&DrawRect,hbr);
- COLLIPE col0={1662,254,3729,50,};
- COLLIPE col1={2009,253,3679,50,};
- COLLIPE col2={2294,253,3693,50,};
- COLLIPE col3={2605,253,3671,50,};
- COLLIPE col4={3292,253,2558,50,};
- COLLIPE col5={3619,253,2353,50,};
- COLLIPE col6={3609,253,2254,50,};
- COLLIPE col7={3686,253,1992,50,};
- COLLIPE col8={3699,253,1873,50,};
- COLLIPE col9={3660,253,1653,50,};
- COLLIPE col10={141,253,3216,50,};
- COLLIPE col11={155,253,2950,50,};
- COLLIPE col12={2568,253,2574,50,};
- COLLIPE col13={1851,253,3599,50,};
- COLLIPE col14={2184,253,3570,50,};
- COLLIPE col15={3637,253,2109,50,};
- COLLIPE col16={3448,253,2465,50,};
- COLLIPE col17={2083,303,2568,50,};
- COLLIPE col18={2458,253,3598,50,};
- COLLIPE col19={2180,303,2567,50,};
- COLLIPE col20={2773,310,2569,50,};
- COLLIPE col21={3135,255,2665,50,};
- COLLIPE col22={2293,315,2573,50,};
- COLLIPE col23={2409,315,2566,50,};
- COLLIPE col24={2539,268,3503,50,};
- COLLIPE col25={2330,293,3586,50,};
- COLLIPE col26={2975,220,345,50,};
- COLLIPE col27={2885,295,2566,50,};
- COLLIPE col28={3010,315,2575,50,};
- COLLIPE col29={3185,305,2581,50,};
- COLLIPE col30={3056,295,2474,50,};
- COLLIPE col31={3010,295,2635,50,};
- COLLIPE col32={3185,305,2521,50,};
- COLLIPE col33={2925,295,2560,50,};
- COLLIPE col34={2955,295,2570,50,};
- COLLIPE col35={3060,315,2570,50,};
- COLLIPE col36={2925,295,2610,50,};
- COLLIPE col37={2955,295,2620,50,};
- COLLIPE col38={3060,305,2620,50,};
- COLLIPE col39={420,200,3340,50,};
- COLLIPE col40={163,300,2711,150,};
- COLLIPE col41={1166,250,814,400,};
- COLLIPE col42={3357,200,409,100,};
- COLLIPE col43={3638,254,3252,250,};
- object[0]=col0;
- object[1]=col1;
- object[2]=col2;
- object[3]=col3;
- object[4]=col4;
- object[5]=col5;
- object[6]=col6;
- object[7]=col7;
- object[8]=col8;
- object[9]=col9;
- object[10]=col10;
- object[11]=col11;
- object[12]=col12;
- object[13]=col13;
- object[14]=col14;
- object[15]=col15;
- object[16]=col16;
- object[17]=col17;
- object[18]=col18;
- object[19]=col19;
- object[20]=col20;
- object[21]=col21;
- object[22]=col22;
- object[23]=col23;
- object[24]=col24;
- object[25]=col25;
- object[26]=col26;
- object[27]=col27;
- object[28]=col28;
- object[29]=col29;
- object[30]=col30;
- object[31]=col31;
- object[32]=col32;
- object[33]=col33;
- object[34]=col34;
- object[35]=col35;
- object[36]=col36;
- object[37]=col37;
- object[38]=col38;
- object[39]=col39;
- object[40]=col40;
- object[41]=col41;
- object[42]=col42;
- object[43]=col43;
- DrawRect.right+=length;
- FillRect(hwndDC,&DrawRect,hbr);
- /*
- m_pTree_1_1->Translation(1662,254,3729,0,5);//0
- m_pTree_1_2->Translation(2009,253,3679,180,4.3);//1
- m_pTree_1_3->Translation(2294,253,3693,90,4);//2
- m_pTree_1_4->Translation(2605,253,3671,45,8);//3
- m_pTree_1_5->Translation(3292,253,2558,0,6);//4
- m_pTree_1_6->Translation(3619,253,2353,180,6);//5
- m_pTree_1_7->Translation(3609,253,2254,90,5);//6
- m_pTree_1_8->Translation(3686,253,1992,45,4);//7
- m_pTree_1_9->Translation(3699,253,1873,270,5);//8
- m_pTree_1_10->Translation(3660,253,1653,0,6);//9
- m_pTree_1_11->Translation(141,253,3216,90,6);//10
- m_pTree_1_12->Translation(155,253,2950,90,3);//11
- m_pTree_1_13->Translation(2568,253,2574,270,6);//12
- m_pTree_1_14->Translation(1851,253,3599,270,5);//13
- m_pTree_1_15->Translation(2184,253,3570,270,3);//14
- m_pTree_1_16->Translation(3637,253,2109,90,4);//15
- m_pTree_1_17->Translation(3448,253,2465,180,6);//16
- m_pTree_1_18->Translation(2083,303,2568,90,3);//17
- m_pTree_1_19->Translation(2458,253,3598,180,3);//18
- m_pTree_1_20->Translation(2180,303,2567,270,3);//19
- m_pTree_1_21->Translation(2773,310,2569,0,3);//20
- m_pTree_u_1->Translation(3135,255,2665,0,2);//21
- m_pTree_u_2->Translation(2293,315,2573,90,2.5);//22
- m_pTree_u_3->Translation(2409,315,2566,270,1.8);//23
- m_pTree_u_4->Translation(2539,268,3503,0,1);//24
- m_pTree_u_5->Translation(2330,293,3586,270,1.8);//25
- m_pDead_cow_1->Translation(2975,220,345,180,3);//26
- m_pRock_1_1->Translation(2885,295,2566,0,3);//27
- m_pRock_1_2->Translation(3010,315,2575,180,3);//28
- m_pRock_1_3->Translation(3185,305,2581,270,3);//29
- m_pRock_1_6->Translation(3056,295,2474,90,3);//30
- m_pRock_1_5->Translation(3010,295,2635,0,3);//31
- m_pRock_1_4->Translation(3185,305,2521,180,3);//32
- m_pRock_2_1->Translation(2925,295,2560,0,3);//33
- m_pRock_2_2->Translation(2955,295,2570,180,3);//34
- m_pRock_2_3->Translation(3060,315,2570,90,3);//35
- m_pRock_2_6->Translation(2925,295,2610,270,3);//36
- m_pRock_2_5->Translation(2955,295,2620,0,3);//37
- m_pRock_2_4->Translation(3060,305,2620,180,3);//38
- m_pRock_3_1->Translation(420,200,3340,180,3);//39
- m_pRock_4_1->Translation(163,300,2711,0,3);//40
- m_pTree_2_1->Translation(1166,250,814,0,12);//41
- m_pTree_2_1->Translation(3357,200,409,270,8);//42
- m_pHole->Translation(3638,254,3252,180,5);//43
- */
- ///////////////
- //初始化动静态模型的地方
- ///////////////
- //以下是静态模型,参数是3D设备和.X文件的路径
- //缺口1
- m_pTree_1_1=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_2=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_3=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_4=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- //缺口2
- m_pTree_1_5=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_6=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_7=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_8=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_9=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_10=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- //边上
- m_pTree_1_11=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_12=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- //突出
- m_pTree_1_13=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- //树1补充
- m_pTree_1_14=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_15=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_16=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_17=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_18=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_19=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_20=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- m_pTree_1_21=new CNpc(m_pD3DDevice,"./mod/tree_1.X");
- //树桩
- m_pTree_u_1=new CNpc(m_pD3DDevice,"./mod/tree_under.X");
- m_pTree_u_2=new CNpc(m_pD3DDevice,"./mod/tree_under.X");
- m_pTree_u_3=new CNpc(m_pD3DDevice,"./mod/tree_under.X");
- m_pTree_u_4=new CNpc(m_pD3DDevice,"./mod/tree_under.X");
- m_pTree_u_5=new CNpc(m_pD3DDevice,"./mod/tree_under.X");
- //死牛
- m_pDead_cow_1=new CNpc(m_pD3DDevice,"./mod/dead_cow.X");
- //岩石
- m_pRock_1_1=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
- m_pRock_1_2=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
- m_pRock_1_3=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
- m_pRock_1_4=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
- m_pRock_1_5=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
- m_pRock_1_6=new CNpc(m_pD3DDevice,"./mod/rock_1.X");
- m_pRock_2_1=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
- m_pRock_2_2=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
- m_pRock_2_3=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
- m_pRock_2_4=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
- m_pRock_2_5=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
- m_pRock_2_6=new CNpc(m_pD3DDevice,"./mod/rock_2.X");
- m_pRock_3_1=new CNpc(m_pD3DDevice,"./mod/rock_3.X");
- m_pRock_4_1=new CNpc(m_pD3DDevice,"./mod/rock_4.X");
- //巨树
- m_pTree_2_1=new CNpc (m_pD3DDevice,"./mod/tree_2.X");
- m_pTree_2_2=new CNpc (m_pD3DDevice,"./mod/tree_2.X");
- //洞
- m_pHole=new CNpc(m_pD3DDevice,"./mod/rockdoor.X");
- //桥
- m_pBridge=new CNpc(m_pD3DDevice,"./mod/bridge.X");
- m_pBoss=new CMesh(m_pD3DDevice,"./mod/kuangjia.X");
- //以下是动态模型,初始化结束后请到Render3D()中确定模型的位置和渲染模型
- m_pSBoss=new CSkinMesh(m_pD3DDevice);
- m_pSBoss->LoadFromXFile("./mod/boss.X");
- DrawRect.right+=length;
- FillRect(hwndDC,&DrawRect,hbr);
- m_pInput=new CInput(hInst,hWnd,m_pPanel->NIcon);
- DrawRect.right+=length;
- FillRect(hwndDC,&DrawRect,hbr);
- m_pSky=new CSky(m_pD3DDevice);
- m_pMusic=new CSound(hWnd);
- m_pSound=new CSound(hWnd);
- DeleteObject(bmpMem_2);
- SelectObject(dcMem,bmpMem_3);
- BitBlt(hwndDC,250,500,311,36,dcMem,0,0,SRCCOPY);
- SetBkMode(hwndDC,TRANSPARENT);
- // TextOut(hwndDC,100, 50,"请按空格进入游戏", 16);
- while(hihi==false)
- {
- m_pInput->Update();
- m_pInput->GetOut(&hihi);
- }
- // m_pSnow=new CSnow(m_pD3DDevice,10,10,10,25,1.0f);
- SelectObject(hwndDC,hOldFont);
- DeleteObject(hNewFont);
- DeleteObject(bmpMem);
- DeleteObject(bmpMem_3);
- DeleteObject(dcMem);
- DeleteObject(hwndDC);
- m_pMusic->Play(0,"./res/Midi_2.mid");
- }
- CGame::~CGame()
- { // SafeDelete(m_pSnow);
- SafeDelete(m_pSound);
- SafeDelete(m_pMusic);
- SafeDelete(m_pSky);
- SafeDelete(m_pTerrain);
- SafeDelete(m_pInput);
- SafeDelete(m_pPanel);
- SafeDelete(m_pFont);
- SafeDelete(m_pMesh);
- SafeDelete(m_pSkinMesh);
- SafeRelease(m_pD3DDevice);
- SafeRelease(m_pD3D);
- }
- void CGame::ScreenTo3D(int mx,int my,D3DXVECTOR3 * p1,D3DXVECTOR3 * p2)
- {
- float dy,dx;
- float HalfGameWidth = m_Rect.right / 2.0f;
- float HalfGameHeight = m_Rect.bottom / 2.0f;
- float tang = tanf((float)(D3DX_PI/4.0f) * 0.5f);
- D3DXMATRIX matView,matWorld;
- m_pD3DDevice->GetTransform(D3DTS_VIEW,&matView);
- m_pD3DDevice->GetTransform(D3DTS_WORLD,&matWorld);
- dx = tang * (mx / HalfGameWidth - 1.0f) / (float)(1.25);
- dy = tang * (1.0f - my / HalfGameHeight);
- //calculate the beginning and end points of the ray
- p1->x = dx * 1.0f;p1->y = dy * 1.0f;p1->z = 1.0f;
- p2->x = dx * 10000.0f;p2->y = dy * 10000.0f;p2->z = 10000.0f;
- //inverse them by the view matrix
- D3DXMATRIX invMat; //the inverse of the view matrix
- D3DXMatrixInverse(&invMat,NULL,&matView); //inverse the view mat
- D3DXVec3TransformCoord(p2,p2,&invMat);
- D3DXVec3TransformCoord(p1,p1,&invMat);
- //inverse them by the world matrix
- //D3DXMATRIX invwMat; //the inverse of the view matrix
- //D3DXMatrixInverse(&invwMat,NULL,&matWorld); //inverse the view mat
- //D3DXVec3TransformCoord(p2,p2,&invwMat);
- //D3DXVec3TransformCoord(p1,p1,&invwMat);
- // D3DXMatrixInverse(&invwMat,NULL,&m_pMesh->matWorld);
- //D3DXVec3TransformCoord(p2,p2,&invwMat);
- //D3DXVec3TransformCoord(p1,p1,&invwMat);
- //D3DXVec3Normalize(p2,&(*p2 - *p1));
- }
- void CGame::GetMouse()
- {
- if(shoot==true)
- {
- BOOL hit;
- FLOAT distance;
- hit=FALSE;
- GetCursorPos(&m_Point);
- // ScreenToClient(hWnd,&m_Point);
- D3DXMATRIX matInv;
- D3DXVECTOR3 start,dir;
- //ScreenTo3D(m_Point.x,m_Point.y,&start,&dir);
- D3DVIEWPORT9 viewport;
- D3DXMATRIX matProj,matView,matWorld;
- m_pD3DDevice->GetViewport(&viewport);
- m_pD3DDevice->GetTransform(D3DTS_PROJECTION,&matProj);
- m_pD3DDevice->GetTransform(D3DTS_VIEW,&matView);
- m_pD3DDevice->GetTransform(D3DTS_WORLD,&matWorld);
- D3DXVec3Unproject(&dir,&D3DXVECTOR3(m_Point.x,m_Point.y,1.0f),&viewport,&matProj,&matView,&matWorld);
- D3DXMatrixInverse(&matInv,NULL,&matView);
- start.x=matInv._41;
- start.y=matInv._42;
- start.z=matInv._43;
- D3DXMatrixInverse(&matInv,NULL,&m_pMesh->matWorld);
- D3DXVec3TransformCoord(&dir,&dir,&matInv);
- D3DXVec3TransformCoord(&start,&start,&matInv);
- dir=dir-start;
- D3DXIntersect(m_pMesh->m_pMesh,&start,&dir,&hit,NULL,NULL,NULL,&distance,NULL,NULL);
- //present=hit;
- /*if(hit==TRUE)
- {
- MessageBox(NULL,"Mouse over mesh","blah",NULL);
- }*/
- }
- /*else
- {
- present=FALSE;
- }*/
- }
- void CGame::GetMouse1()
- {
- if(shoot==true)
- {
- BOOL hit;
- FLOAT distance;
- hit=FALSE;
- GetCursorPos(&m_Point);
- // ScreenToClient(hWnd,&m_Point);
- D3DXMATRIX matInv;
- D3DXVECTOR3 start,dir;
- //ScreenTo3D(m_Point.x,m_Point.y,&start,&dir);
- D3DVIEWPORT9 viewport;
- D3DXMATRIX matProj,matView,matWorld;
- m_pD3DDevice->GetViewport(&viewport);
- m_pD3DDevice->GetTransform(D3DTS_PROJECTION,&matProj);
- m_pD3DDevice->GetTransform(D3DTS_VIEW,&matView);
- m_pD3DDevice->GetTransform(D3DTS_WORLD,&matWorld);
- D3DXVec3Unproject(&dir,&D3DXVECTOR3(m_Point.x,m_Point.y,1.0f),&viewport,&matProj,&matView,&matWorld);
- D3DXMatrixInverse(&matInv,NULL,&matView);
- start.x=matInv._41;
- start.y=matInv._42;
- start.z=matInv._43;
- D3DXMatrixInverse(&matInv,NULL,&m_pBoss->matWorld);
- D3DXVec3TransformCoord(&dir,&dir,&matInv);
- D3DXVec3TransformCoord(&start,&start,&matInv);
- dir=dir-start;
- D3DXIntersect(m_pBoss->m_pMesh,&start,&dir,&hit,NULL,NULL,NULL,&distance,NULL,NULL);
- present=hit;
- if(hit==TRUE)
- {
- m_pSound->PlaySound("./res/sound_1.wav");
- }
- }
- }
- void CGame::SetFog()
- { float start=400.0f;
- float end=4500.0f;
- m_pD3DDevice->SetRenderState(D3DRS_FOGENABLE,TRUE);
- m_pD3DDevice->SetRenderState(D3DRS_FOGCOLOR,D3DCOLOR_XRGB(255,255,255));
- D3DCAPS9 stCaps;
- m_pD3DDevice->GetDeviceCaps(&stCaps);
- if(stCaps.RasterCaps&D3DPRASTERCAPS_FOGRANGE)
- {
- m_pD3DDevice->SetRenderState(D3DRS_RANGEFOGENABLE,TRUE);
- }
- m_pD3DDevice->SetRenderState(D3DRS_FOGSTART,*(DWORD*)&start);
- m_pD3DDevice->SetRenderState(D3DRS_FOGEND,*(DWORD*)&end);
- m_pD3DDevice->SetRenderState(D3DRS_FOGTABLEMODE,D3DFOG_LINEAR);
- }
- void CGame::SetWaterFog()
- {
- m_pD3DDevice->SetRenderState(D3DRS_FOGENABLE,TRUE);
- m_pD3DDevice->SetRenderState(D3DRS_FOGCOLOR,D3DCOLOR_XRGB(90,184,194));
- D3DCAPS9 stCaps;
- m_pD3DDevice->GetDeviceCaps(&stCaps);
- if(stCaps.RasterCaps&D3DPRASTERCAPS_FOGRANGE)
- {
- m_pD3DDevice->SetRenderState(D3DRS_RANGEFOGENABLE,TRUE);
- }
- float density=0.002;
- m_pD3DDevice->SetRenderState(D3DRS_FOGDENSITY,*(DWORD*)&density);
- m_pD3DDevice->SetRenderState(D3DRS_FOGTABLEMODE,D3DFOG_EXP);
- }
- void CGame::RenderText()
- { char buffer[200];
- DWORD dwDuration=(timeGetTime()-m_dwStartTime)/1000;
- //DWORD fps;
- //fps=(m_dwFrame/dwDuration);
- if(dwDuration<1)
- {
- sprintf(buffer,"Calculating...");
- }
- else
- {
- //sprintf(buffer,"Duration:%d second, Frame:%3d, FPS:%d",dwDuration,m_dwFrame,(m_dwFrame/dwDuration));
- sprintf(buffer,"x:%f second, y:%f, z:%f, FPS:%d",ViewPos.x,ViewPos.y,ViewPos.z,(m_dwFrame/dwDuration));
- } //sprintf(buffer,"x:%f second, y:%f, z:%f, FPS:%d",ViewPos.x,ViewPos.y,ViewPos.z,(m_dwFrame/dwDuration));
- m_pFont->DrawText(buffer,0.0f,0.0f);
- }
- void CGame::Render()
- {
- m_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,200),1.0F,0);
- m_pD3DDevice->BeginScene();
- SetCamera3D();
- AI();
- Render3D();
- GetMouse();
- GetMouse1();
- SetCamera2D();
- // if(present==TRUE)
- //{
- Render2D();
- // }
- RenderText();
- m_pD3DDevice->EndScene();
- m_pD3DDevice->Present(NULL,NULL,NULL,NULL);
- m_dwFrame++;
- }
- void CGame::AI()
- {
- static bool walk=true;
- static bool follow=false;
- static bool back=false;
- static NPOSITION preBossPos=BossPos;
- static DWORD time=timeGetTime()/1000.0f;
- DWORD timeNow=timeGetTime()/1000.0F;
- D3DXVECTOR3 BossPoint=D3DXVECTOR3(2417.0F,254.0F,1185.0F);
- D3DXVECTOR3 BossPointNow=D3DXVECTOR3(BossPos.x,BossPos.y,BossPos.z);
- D3DXVECTOR3 ViewPosNow=D3DXVECTOR3(ViewPos.x,ViewPos.y,ViewPos.z);
- float distance=D3DXVec3Length(&(BossPointNow-ViewPosNow));
- float original=D3DXVec3Length(&(BossPoint-BossPointNow));
- if((follow==false)&(back==false))
- {
- walk=true;
- }
- if((distance<=300)&(walk==true)&(back==false))
- {
- follow=true;
- walk=false;
- back=false;
- }
- if((distance>=400)&(follow==true))
- {
- walk=false;
- follow=false;
- back=true;
- }
- if((original<=50)&(back==true))
- {
- walk=true;
- follow=false;
- back=false;
- }
- if(walk==true)
- {
- m_pSBoss->SetAnimationName("walk");
- if(timeNow-time>=4)
- {
- BossPos.a+=rand()%360;
- while((BossPos.a>180)|(BossPos.a<=-180))
- {
- if(BossPos.a>180)
- BossPos.a-=360;
- if(BossPos.a<=-180)
- BossPos.a+=360;
- }
- time=timeNow;
- }
- BossPos.x+=0.5*cos(D3DX_PI/180*BossPos.a);
- BossPos.z+=0.5*sin(D3DX_PI/180*BossPos.a);
- BossPos.y=m_pTerrain->GetHeight(BossPos.x,BossPos.z);
- BossPointNow=D3DXVECTOR3(BossPos.x,BossPos.y,BossPos.z);
- float length=D3DXVec3Length(&(BossPointNow-BossPoint));
- if(length>=500)
- {
- BossPos=preBossPos;
- }
- else
- {
- preBossPos=BossPos;
- }
- }
- if(follow==true)
- {
- if(distance<=40)
- {
- static int dongzuo=rand()%2;
- if(timeNow-time>=0.8f)
- {
- dongzuo=rand()%2;
- time=timeNow;
- }
- switch(dongzuo)
- {
- case 0:
- m_pSBoss->SetAnimationName("skill1");
- break;
- case 1:
- m_pSBoss->SetAnimationName("skill2");
- break;
- }
- }
- else
- {
- m_pSBoss->SetAnimationName("run");
- }
- D3DXVECTOR3 Distance;
- D3DXVECTOR3 Dir;
- D3DXVec3Normalize(&Dir,&D3DXVECTOR3(2*cos(D3DX_PI/180*BossPos.a),0,2*sin(D3DX_PI/180*BossPos.a)));
- D3DXVec3Normalize(&Distance,&(D3DXVECTOR3(ViewPos.x,0,ViewPos.z)-D3DXVECTOR3(BossPos.x,0,BossPos.z)));
- static int preangle=0;
- float god=acosf(D3DXVec3Dot(&Dir,&Distance));
- int angle=(int)god/(D3DX_PI/180);
- if(abs(preangle-angle)<=1)
- {
- angle=preangle;
- }
- else
- {
- preangle=angle;
- }
- BossPos.a+=angle;
- while((BossPos.a>180)|(BossPos.a<=-180))
- {
- if(BossPos.a>180)
- BossPos.a-=360;
- if(BossPos.a<=-180)
- BossPos.a+=360;
- }
- if(distance>=30)
- {
- BossPos.x+=1.5*cos(D3DX_PI/180*BossPos.a);
- BossPos.z+=1.5*sin(D3DX_PI/180*BossPos.a);
- BossPos.y=m_pTerrain->GetHeight(BossPos.x,BossPos.z);
- }
- }
- if(back==true)
- {
- m_pSBoss->SetAnimationName("run");
- D3DXVECTOR3 Distance;
- D3DXVECTOR3 Dir;
- D3DXVec3Normalize(&Dir,&D3DXVECTOR3(2*cos(D3DX_PI/180*BossPos.a),0,2*sin(D3DX_PI/180*BossPos.a)));
- D3DXVec3Normalize(&Distance,&(D3DXVECTOR3(BossPoint.x,0,BossPoint.z)-D3DXVECTOR3(BossPos.x,0,BossPos.z)));
- static int preangle1=0;
- float god=acosf(D3DXVec3Dot(&Dir,&Distance));
- int angle=(int)god/(D3DX_PI/180);
- if(abs(preangle1-angle)<=1)
- {
- angle=preangle1;
- }
- else
- {
- preangle1=angle;
- }
- BossPos.a+=angle;
- while((BossPos.a>180)|(BossPos.a<=-180))
- {
- if(BossPos.a>180)
- BossPos.a-=360;
- if(BossPos.a<=-180)
- BossPos.a+=360;
- }
- BossPos.x+=2*cos(D3DX_PI/180*BossPos.a);
- BossPos.z+=2*sin(D3DX_PI/180*BossPos.a);
- BossPos.y=m_pTerrain->GetHeight(BossPos.x,BossPos.z);
- }
- }
- void CGame::RoleAction()
- { static bool attack=false;
- static int Preflash=0;
- static int record=0;
- static DWORD time=timeGetTime()/1000.0f;
- DWORD timeNow=timeGetTime()/1000.0F;
- static DWORD time_1=0.0f;
- DWORD timeNow_1=timeGetTime()/1000.0f;
- if(Preflash!=flash)
- {
- m_pSkinMesh->SetAnimationName("idle");record=0;
- if((PreViewPos.a!=ViewPos.a)|(PreViewPos.b!=ViewPos.b))
- {
- m_pSkinMesh->SetAnimationName("walk");
- record=1;
- }
- if((PreViewPos.x!=ViewPos.x)|(PreViewPos.z!=ViewPos.z))
- {
- m_pSkinMesh->SetAnimationName("run");
- record=2;
- }
- D3DXVECTOR3 BossPointNow=D3DXVECTOR3(BossPos.x,BossPos.y,BossPos.z);
- D3DXVECTOR3 ViewPosNow=D3DXVECTOR3(ViewPos.x,ViewPos.y,ViewPos.z);
- float distance=D3DXVec3Length(&(BossPointNow-ViewPosNow));
- D3DXVECTOR3 Distance;
- D3DXVECTOR3 Dir;
- D3DXVec3Normalize(&Dir,&D3DXVECTOR3(2*cos(D3DX_PI/180*ViewPos.a),0,2*sin(D3DX_PI/180*ViewPos.a)));
- D3DXVec3Normalize(&Distance,&(D3DXVECTOR3(BossPos.x,0,BossPos.z)-D3DXVECTOR3(ViewPos.x,0,ViewPos.z)));
- static int preangle=0;
- float god=acosf(D3DXVec3Dot(&Dir,&Distance));
- int angle=(int)god/(D3DX_PI/180);
- if(abs(preangle-angle)<=1)
- {
- angle=preangle;
- }
- else
- {
- preangle=angle;
- }
- while((angle>180)|(angle<=-180))
- {
- if(angle>180)
- angle-=360;
- if(angle<=-180)
- angle+=360;
- }
- if((distance<=40)&(present==true))//人物和npc距离小于40,且鼠标右键被按下
- {
- if((angle<=80)&(angle>=-80))
- {
- m_pSkinMesh->SetAnimationName("skill1");
- record=3;
- }
- else
- {
- time=timeNow;
- }
- }
- Preflash=flash;
- }
- else
- {
- switch(record)
- {
- case 0:
- m_pSkinMesh->SetAnimationName("idle");
- break;
- case 1:
- m_pSkinMesh->SetAnimationName("walk");
- break;
- case 2:
- m_pSkinMesh->SetAnimationName("run");
- break;
- case 3:
- m_pSkinMesh->SetAnimationName("skill1");
- break;
- }
- }
- if(present==true)
- {
- if((timeNow-time)<=3)
- {
- char buffer[200];
- sprintf(buffer,"You must correct your direction");
- m_pWrong->DrawText(buffer,250.0f,200.0f);
- }
- }
- if(nomoney==true)
- {
- time_1=timeNow_1;
- }
- if(timeNow_1-time_1<=3)
- {
- char buffer[200];
- sprintf(buffer,"It's a demo.So.....");
- m_pWrong->DrawText_1(buffer,250.0f,470.0f);
- }
- }
- bool CGame::Collipe(COLLIPE col)
- {
- D3DXVECTOR3 RolePoint=D3DXVECTOR3(ViewPos.x,ViewPos.y,ViewPos.z);
- D3DXVECTOR3 dest=D3DXVECTOR3(col.x,col.y,col.z);
- float distance=D3DXVec3Length(&(dest-RolePoint));
- if(distance<=col.dis)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void CGame::Render3D()
- {
- m_pTerrain->Translate();
- m_pTerrain->RenerTerrain();
- m_pSky->Translate(ViewPos);
- m_pSky->RenderSky();
- m_pMesh->Translate(ViewPos);
- m_pMesh->RenderMesh();
- m_pBoss->Translation(BossPos);
- m_pBoss->RenderMesh();
- ///////////////
- //渲染模型和模型定位的地方
- ////////////
- //静态模型的定位,参数分别是X,Y,Z,旋转角度,放大倍数
- //静态模型渲染
- //静态模型的定位,参数分别是X,Y,Z,旋转角度,放大倍数
- m_pTree_1_1->Translation(1662,254,3729,0,5);//0
- m_pTree_1_1->RenderNpc();//静态模型渲染
- m_pTree_1_2->Translation(2009,253,3679,180,4.3);//1
- m_pTree_1_2->RenderNpc();
- m_pTree_1_3->Translation(2294,253,3693,90,4);//2
- m_pTree_1_3->RenderNpc();
- m_pTree_1_4->Translation(2605,253,3671,45,8);//3
- m_pTree_1_4->RenderNpc();
- m_pTree_1_5->Translation(3292,253,2558,0,6);//4
- m_pTree_1_5->RenderNpc();
- m_pTree_1_6->Translation(3619,253,2353,180,6);//5
- m_pTree_1_6->RenderNpc();
- m_pTree_1_7->Translation(3609,253,2254,90,5);//6
- m_pTree_1_7->RenderNpc();
- m_pTree_1_8->Translation(3686,253,1992,45,4);//7
- m_pTree_1_8->RenderNpc();
- m_pTree_1_9->Translation(3699,253,1873,270,5);//8
- m_pTree_1_9->RenderNpc();
- m_pTree_1_10->Translation(3660,253,1653,0,6);//9
- m_pTree_1_10->RenderNpc();
- m_pTree_1_11->Translation(141,253,3216,90,6);//10
- m_pTree_1_11->RenderNpc();
- m_pTree_1_12->Translation(155,253,2950,90,3);//11
- m_pTree_1_12->RenderNpc();
- m_pTree_1_13->Translation(2568,253,2574,270,6);//12
- m_pTree_1_13->RenderNpc();
- m_pTree_1_14->Translation(1851,253,3599,270,5);//13
- m_pTree_1_14->RenderNpc();
- m_pTree_1_15->Translation(2184,253,3570,270,3);//14
- m_pTree_1_15->RenderNpc();
- m_pTree_1_16->Translation(3637,253,2109,90,4);//15
- m_pTree_1_16->RenderNpc();
- m_pTree_1_17->Translation(3448,253,2465,180,6);//16
- m_pTree_1_17->RenderNpc();
- m_pTree_1_18->Translation(2083,303,2568,90,3);//17
- m_pTree_1_18->RenderNpc();
- m_pTree_1_19->Translation(2458,253,3598,180,3);//18
- m_pTree_1_19->RenderNpc();
- m_pTree_1_20->Translation(2180,303,2567,270,3);//19
- m_pTree_1_20->RenderNpc();
- m_pTree_1_21->Translation(2773,310,2569,0,3);//20
- m_pTree_1_21->RenderNpc();
- m_pTree_u_1->Translation(3135,255,2665,0,2);//21
- m_pTree_u_1->RenderNpc();
- m_pTree_u_2->Translation(2293,315,2573,90,2.5);//22
- m_pTree_u_2->RenderNpc();
- m_pTree_u_3->Translation(2409,315,2566,270,1.8);//23
- m_pTree_u_3->RenderNpc();
- m_pTree_u_4->Translation(2539,268,3503,0,1);//24
- m_pTree_u_4->RenderNpc();
- m_pTree_u_5->Translation(2330,293,3586,270,1.8);//25
- m_pTree_u_5->RenderNpc();
- m_pDead_cow_1->Translation(2975,220,345,180,3);//26
- m_pDead_cow_1->RenderNpc();
- m_pRock_1_1->Translation(2885,295,2566,0,3);//27
- m_pRock_1_1->RenderNpc();
- m_pRock_1_2->Translation(3010,315,2575,180,3);//28
- m_pRock_1_2->RenderNpc();
- m_pRock_1_3->Translation(3185,305,2581,270,3);//29
- m_pRock_1_3->RenderNpc();
- m_pRock_1_6->Translation(3056,295,2474,90,3);//30
- m_pRock_1_6->RenderNpc();
- m_pRock_1_5->Translation(3010,295,2635,0,3);//31
- m_pRock_1_5->RenderNpc();
- m_pRock_1_4->Translation(3185,305,2521,180,3);//32
- m_pRock_1_4->RenderNpc();
- m_pRock_2_1->Translation(2925,295,2560,0,3);//33
- m_pRock_2_1->RenderNpc();
- m_pRock_2_2->Translation(2955,295,2570,180,3);//34
- m_pRock_2_2->RenderNpc();
- m_pRock_2_3->Translation(3060,315,2570,90,3);//35
- m_pRock_2_3->RenderNpc();
- m_pRock_2_6->Translation(2925,295,2610,270,3);//36
- m_pRock_2_6->RenderNpc();
- m_pRock_2_5->Translation(2955,295,2620,0,3);//37
- m_pRock_2_5->RenderNpc();
- m_pRock_2_4->Translation(3060,305,2620,180,3);//38
- m_pRock_2_4->RenderNpc();
- m_pRock_3_1->Translation(420,200,3340,180,3);//39
- m_pRock_3_1->RenderNpc();
- m_pRock_4_1->Translation(163,300,2711,0,3);//40
- m_pRock_4_1->RenderNpc();
- m_pTree_2_1->Translation(1166,250,814,0,12);//41
- m_pTree_2_1->RenderNpc();
- m_pTree_2_1->Translation(3357,200,409,270,8);//42
- m_pTree_2_1->RenderNpc();
- m_pHole->Translation(3638,254,3252,180,5);//43
- m_pHole->RenderNpc();
- m_pBridge->Translation(780,-80,3000,100,3.5);
- m_pBridge->RenderNpc();
- static float timeStart=timeGetTime()/1000.0f;
- static float timeNow=timeGetTime()/1000.0f;
- float tFrameTime=timeNow;
- timeNow=timeGetTime()/1000.0f;
- tFrameTime=timeNow-tFrameTime;
- /*switch (((long)floor(timeNow/2))%5)
- {
- case 0: m_pSkinMesh->SetAnimationName("idle");break;
- case 1: m_pSkinMesh->SetAnimationName("walk");break;
- case 2: m_pSkinMesh->SetAnimationName("run");break;
- case 3: m_pSkinMesh->SetAnimationName("skill1");break;
- case 4: m_pSkinMesh->SetAnimationName("skill2");break;
- }*/
- RoleAction();
- //m_pSkinMesh->Translation(ViewPos);
- ///////////////////
- //动态模型定位和渲染
- ////////////////
- //第一个参数不用管,复制就行,D3DXVECTOR3中代表X,Y,Z坐标,旋转角度,放大倍数
- 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);
- PreViewPos=ViewPos;
- // m_pSBoss->SetAnimationName("walk");
- m_pSBoss->Render(tFrameTime,D3DXVECTOR3(BossPos.x,BossPos.y,BossPos.z),-D3DX_PI/180*(BossPos.a+90.0f),1);
- m_pWater->Translate();
- m_pWater->RenerWater(cy);
- //m_pSnow->MoveSnow(DirWorld,OriginWorld);
- // m_pSnow->RenderSnow();
- }
- void CGame::Render2D()
- {
- m_pPanel->Translate(-400,+300);
- m_pPanel->RenderPanel();
- if(present==true)
- {
- m_pPanel->Render();
- }
- }
- void CGame::SetCamera3D()
- {
- D3DXMATRIX matView,matPers;
- D3DXVECTOR3 ViewVec=D3DXVECTOR3((ViewPos.x+ViewPos.cc*cos(D3DX_PI/180*ViewPos.cb)*cos(D3DX_PI/180*ViewPos.ca)),
- (ViewPos.y+ViewPos.cc*sin(D3DX_PI/180*ViewPos.cb)),
- (ViewPos.z+ViewPos.cc*cos(D3DX_PI/180*ViewPos.cb)*sin(D3DX_PI/180*ViewPos.ca)));
- static D3DXVECTOR3 PreViewVec1=ViewVec;
- if((ViewVec.x<10)|(ViewVec.z<10)|(ViewVec.x>128*30)|(ViewVec.z>128*30))
- {
- ViewVec=PreViewVec1;
- }
- else
- {
- PreViewVec1=ViewVec;
- }
- D3DXVECTOR3 ViewVec1=D3DXVECTOR3(ViewPos.x,ViewPos.y+25.0f,ViewPos.z);
- D3DXVECTOR3 PreViewVec=ViewVec;
- float ViewMinHeight=m_pTerrain->GetHeight(ViewVec.x,ViewVec.z)+10.0f;
- if(ViewVec.y<=ViewMinHeight)
- {
- ViewVec.y=ViewMinHeight;
- ViewFlag=true;//可移动视点
- }
- else
- {
- ViewFlag=false;//可移动摄像机
- }
- if(ViewFlag==false)
- {
- D3DXMatrixLookAtLH(&matView,&ViewVec,&D3DXVECTOR3(ViewPos.x,ViewPos.y+25.0f,ViewPos.z),&D3DXVECTOR3(0.0F,1.0F,0.0F));
- }
- else
- {
- ViewVec.y=ViewMinHeight;
- D3DXVECTOR3 axis,Nsub;D3DXMATRIX matRo;
- D3DXVECTOR3 sub=ViewVec1-ViewVec;
- D3DXVec3Normalize(&Nsub,&sub);
- D3DXVec3Normalize(&vecUp,&vecUp);
- D3DXVec3Cross(&axis,&vecUp,&Nsub);
- if(sub.y>0)
- D3DXMatrixRotationAxis(&matRo,&axis,D3DX_PI/180*fUp);
- else
- D3DXMatrixRotationAxis(&matRo,&axis,-D3DX_PI/180*fUp);
- D3DXVec3TransformCoord(&sub,&sub,&matRo);
- sub=sub+ViewVec;
- D3DXMatrixLookAtLH(&matView,&ViewVec,&sub,&D3DXVECTOR3(0.0F,1.0F,0.0F));
- }
- PreViewVec=ViewVec;
- cy=ViewVec.y;
- if(ViewVec.y<=m_pWater->y)
- {
- m_pD3DDevice->SetRenderState(D3DRS_FOGENABLE,FALSE);
- SetWaterFog();
- }
- else
- {
- m_pD3DDevice->SetRenderState(D3DRS_FOGENABLE,FALSE);
- SetFog();
- }
- m_pD3DDevice->SetTransform(D3DTS_VIEW,&matView);
- D3DXMatrixPerspectiveFovLH(&matPers,D3DX_PI/4,1.25f,1.0F,10000.0F);
- m_pD3DDevice->SetTransform(D3DTS_PROJECTION,&matPers);
- matProj=matView;
- m_pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
- m_pD3DDevice->SetRenderState(D3DRS_ZFUNC,D3DCMP_LESS);
- m_pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
- m_pD3DDevice->SetRenderState(D3DRS_LIGHTING,true);
- }
- void CGame::SetCamera2D()
- {
- D3DXMATRIX matPers,matView;
- D3DXMatrixOrthoLH(&matPers,(float)m_nScreenWidth,(float)m_nScreenHeight,0.0f,1.0f);
- D3DXMatrixIdentity(&matView);
- m_pD3DDevice->SetTransform(D3DTS_PROJECTION,&matPers);
- m_pD3DDevice->SetTransform(D3DTS_WORLD,&matView);
- m_pD3DDevice->SetTransform(D3DTS_VIEW,&matView);
- m_pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE);
- m_pD3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
- }
- void CGame::GameLoop()
- {
- MSG msg;
- BOOL bmessage;
- PeekMessage(&msg,NULL,0U,0U,PM_NOREMOVE);
- while(msg.message!=WM_QUIT)
- {
- bmessage=PeekMessage(&msg,NULL,0U,0U,PM_REMOVE);
- if(bmessage)
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- Render();
- }
- }
- }
- HRESULT CGame::InitialD3D(HWND hWnd)
- {
- m_pD3D=Direct3DCreate9(D3D_SDK_VERSION);
- if(m_pD3D==NULL)
- {
- E_FAIL;
- }
- D3DDISPLAYMODE d3ddm;
- m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm);
- if(d3ddm.Format!=D3DFMT_UNKNOWN)
- {
- d3ddm.Width=800;
- d3ddm.Height=600;
- }
- m_nScreenWidth=d3ddm.Width;
- m_nScreenHeight=d3ddm.Height;
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory(&d3dpp,sizeof(d3dpp));
- d3dpp.Windowed=true;
- d3dpp.BackBufferCount=1;
- d3dpp.BackBufferFormat=d3ddm.Format;
- d3dpp.BackBufferHeight=d3ddm.Height;
- d3dpp.BackBufferWidth=d3ddm.Width;
- d3dpp.hDeviceWindow=hWnd;
- d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
- d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
- d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_ONE;
- if(SUCCEEDED(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,d3ddm.Format,D3DUSAGE_DEPTHSTENCIL,D3DRTYPE_SURFACE,D3DFMT_D32)))
- {
- d3dpp.AutoDepthStencilFormat=D3DFMT_D32;
- d3dpp.EnableAutoDepthStencil=true;
- }
- else if(SUCCEEDED(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,d3ddm.Format,D3DUSAGE_DEPTHSTENCIL,D3DRTYPE_SURFACE,D3DFMT_D24X8)))
- {
- d3dpp.AutoDepthStencilFormat=D3DFMT_D24X8;
- d3dpp.EnableAutoDepthStencil=true;
- }
- else if(SUCCEEDED(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,d3ddm.Format,D3DUSAGE_DEPTHSTENCIL,D3DRTYPE_SURFACE,D3DFMT_D16)))
- {
- d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
- d3dpp.EnableAutoDepthStencil=true;
- }
- else
- {
- d3dpp.EnableAutoDepthStencil=false;
- }
- if(FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&m_pD3DDevice)))
- {
- return E_FAIL;
- }
- if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW)))
- {
- return E_FAIL;
- }
- if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE)))
- {
- return E_FAIL;
- }
- if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID)))
- //if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME)))
- {
- return E_FAIL;
- }
- if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS,TRUE)))
- {
- return E_FAIL;
- }
- m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
- m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
- m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
- m_pD3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
- m_pD3DDevice->SetRenderState( D3DRS_ALPHAREF, 0x0f);
- m_pD3DDevice->SetRenderState( D3DRS_ALPHAFUNC,D3DCMP_GREATEREQUAL );
- m_pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
- m_pD3DDevice->SetRenderState(D3DRS_ZFUNC,D3DCMP_LESS);
- m_pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
- GetWindowRect(hWnd,&m_Rect);
- return S_OK;
- }
- HRESULT CGame::InitialLight()
- {
- ZeroMemory(&d3dLight,sizeof(D3DLIGHT9));
- d3dLight.Type=D3DLIGHT_POINT;
- d3dLight.Position.x=1922;//ViewPos.x-500.0f;
- d3dLight.Position.y=1000.0f;
- d3dLight.Position.z=2229;//ViewPos.z-500.0f;
- d3dLight.Attenuation0=1.0F;
- d3dLight.Attenuation1=0.0f;
- d3dLight.Attenuation2=0.0f;
- d3dLight.Range=5000.0f;
- d3dLight.Diffuse.r=1.0f;
- d3dLight.Diffuse.g=1.0f;
- d3dLight.Diffuse.b=1.0f;
- d3dLight.Ambient.r=0.0f;
- d3dLight.Ambient.g=0.0f;
- d3dLight.Ambient.b=0.0f;
- d3dLight.Specular.r=1.0f;
- d3dLight.Specular.g=1.0f;
- d3dLight.Specular.b=1.0f;
- if(FAILED(m_pD3DDevice->SetLight(0,&d3dLight)))
- {
- E_FAIL;
- }
- if(FAILED(m_pD3DDevice->LightEnable(0,true)))
- {
- E_FAIL;
- }
- if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_LIGHTING,TRUE)))
- {
- E_FAIL;
- }
- if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_AMBIENT,D3DCOLOR_XRGB(60,60,60))))
- {
- E_FAIL;
- }
- return S_OK;
- }
- COpen *g_pOpen=NULL;
- CGame * g_pGame=NULL;
- bool * bOpen;
- bool bOpenGame;
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- hInst=hInstance;
- WNDCLASSEX wc={sizeof(WNDCLASSEX),CS_CLASSDC,WndProc,0,0,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,"place",NULL};
- RegisterClassEx(&wc);
- HWND hWnd=CreateWindow("place","placekk",WS_DLGFRAME,0,0,800,600,NULL,NULL,wc.hInstance,NULL);//WS_OVERLAPPEDWINDOW
- bOpen=new bool[2];
- for(int i=0;i<2;i++)
- {
- bOpen[i]=true;
- }
- bOpenGame=false;
- g_pOpen=new COpen(hInst,hWnd);
- ShowWindow(hWnd,SW_SHOWDEFAULT);
- UpdateWindow(hWnd);
- g_pOpen->OpenLoop();
- SafeDelete(g_pOpen);
- if(bOpen[0]==false)
- {
- g_pGame=new CGame(hInst,hWnd);
- if(SUCCEEDED(g_pGame->InitialD3D(hWnd)))
- {
- g_pGame->InitialTools();
- g_pGame->InitialLight();
- ShowWindow(hWnd,SW_SHOWDEFAULT);
- UpdateWindow(hWnd);
- bOpenGame=true;
- g_pGame->GameLoop();
- }
- SafeDelete(g_pGame);
- }
- UnregisterClass("place",wc.hInstance);
- return 0;
- }
- //
- // 函数: MyRegisterClass()
- //
- // 目的: 注册窗口类。
- //
- // 注释:
- //
- // 仅当希望在已添加到 Windows 95 的
- // “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
- // 才需要此函数及其用法。调用此函数
- // 十分重要,这样应用程序就可以获得关联的
- // “格式正确的”小图标。
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = (WNDPROC)WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_GAME);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = (LPCTSTR)IDC_GAME;
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
- return RegisterClassEx(&wcex);
- }
- //
- // 函数: InitInstance(HANDLE, int)
- //
- // 目的: 保存实例句柄并创建主窗口
- //
- // 注释:
- //
- // 在此函数中,我们在全局变量中保存实例句柄并
- // 创建和显示主程序窗口。
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- hInst = hInstance; // 将实例句柄存储在全局变量中
- hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
- if (!hWnd)
- {
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- //
- // 函数: WndProc(HWND, unsigned, WORD, LONG)
- //
- // 目的: 处理主窗口的消息。
- //
- // WM_COMMAND - 处理应用程序菜单
- // WM_PAINT - 绘制主窗口
- // WM_DESTROY - 发送退出消息并返回
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- /*int wmId, wmEvent;
- PAINTSTRUCT ps;
- HDC hdc;
- switch (message)
- {
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- // 分析菜单选择:
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- // TODO: 在此添加任意绘图代码...
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;*/
- switch(message)
- {
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- return 0;
- case WM_KEYUP:
- switch(wParam)
- {
- case VK_ESCAPE:
- if((bOpen[0]==false)&(bOpenGame==true))
- g_pGame->m_pInput->run=false;
- break;
- case VK_F1:
- KillTimer(hWnd,1);
- //bOpen=false;
- if(g_pOpen!=NULL)
- g_pOpen->bOpen=false;
- DestroyWindow(hWnd);
- break;
- return 0;
- }
- break;
- case WM_CREATE:
- SetTimer(hWnd,1,20,NULL);
- break;
- case WM_TIMER:
- if(bOpen[0]==true)
- g_pOpen->Update();
- if((bOpen[0]==false)&(bOpenGame==true))
- g_pGame->m_pInput->Update();
- switch(wParam)
- {
- case 1:
- if(bOpen[0]==true)
- {
- g_pOpen->Control(bOpen);
- g_pOpen->bOpen=bOpen[0];
- }
- if(bOpen[1]==false)
- {
- bOpen[1]=true;
- DestroyWindow(hWnd);
- }
- if((bOpen[0]==false)&(bOpenGame==true))
- {
- g_pGame->m_pInput->Control(&g_pGame->ViewFlag,&g_pGame->vecUp,&g_pGame->fUp,&g_pGame->shoot,
- g_pGame->m_pPanel->NumOfPos,g_pGame->m_pPanel->CallPress
- ,g_pGame->m_pPanel->ButtonPress,g_pGame->m_pPanel->Result,
- &g_pGame->ViewPos,g_pGame->object,g_pGame->m_pTerrain,
- &g_pGame->BossPos,&g_pGame->nomoney);
- g_pGame->flash++;
- if(g_pGame->flash==100)
- {
- g_pGame->flash=0;
- }
- if(g_pGame->m_pPanel->Result[1]==true)
- { KillTimer(hWnd,1);
- DestroyWindow(hWnd);
- }
- }
- break;
- }
- break;
- }
- return DefWindowProc(hWnd,message,wParam,lParam);
- }
- // “关于”框的消息处理程序。
- LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_INITDIALOG:
- return TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return TRUE;
- }
- break;
- }
- return FALSE;
- }