ZFXDream.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:19k
源码类别:
DirextX编程
开发平台:
Visual C++
- // ZFXDream.cpp : 定义应用程序的入口点。
- //
- #include "stdafx.h"
- #include "d3dUtility.h"
- #include "particle.h"
- #include "ZFXDream.h"
- #include "zfxCamera.h"
- #include "zfxFrustum.h"
- #include "zfxLODTerrain.h"
- #include "zfxMeshX.h"
- #include "zfxBillBoard.h"
- #include "Vfw.h" // Video capture head file
- #define MAX_LOADSTRING 100
- #define FULL_SCREEN
- #define WALKLENTGH 5.0f
- #define AVI_CAPTURE
- // 全局变量:
- HINSTANCE hInst; // 当前实例
- HWND g_hWnd;
- TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
- TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
- HWND hWnd_Avi;
- D3DFILLMODE g_fillMode = D3DFILL_SOLID;
- BOOL g_bFog = TRUE;
- LPDIRECT3D9 g_pD3D = NULL;
- CZFXCamera g_camera; // camera
- LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
- LPDIRECTINPUT8 g_pDI = NULL; // The DirectInput object
- LPDIRECTINPUTDEVICE8 g_pKeyboard = NULL; // The keyboard device
- LPDIRECTINPUTDEVICE8 g_pMouse = NULL; // The mouse device
- LPD3DXFONT g_pFont = NULL;
- BOOL g_bKey_F = FALSE; // F键松开后,值变为FALSE,防止一次按下键后程序响应多次
- BOOL g_bKey_Q = FALSE; // F键松开后,值变为FALSE,防止一次按下键后程序响应多次
- CZFXLODTerrain* g_pLODTerrain = NULL;
- CZFXMesh* g_pSkyBoxMesh = NULL;
- CZFXMesh* g_pSu27 = NULL;
- ////// 雨雪粒子系统。。。/////////////////////////////
- #define SNOW_OR_RAIN
- //#define RENDER_SNOW
- using namespace ParticleSystem;
- #ifdef RENDER_SNOW
- typedef CSnowSystem CPSystem;
- #define NUMPERBATCH 200
- #define NUMTOTAL 5000
- #else
- typedef CRainSystem CPSystem;
- #define NUMPERBATCH 100
- #define NUMTOTAL 3000
- #endif
- CPSystem* g_pPSystem = NULL; //Paticle system
- ////////////////////////////////////////////////////
- ////////BillBoard for Trees..........////////////////
- #define TREE_NUM_TOTAL 32
- #define TREEMGR_NUM_TOTAL 4
- CZFXBillBoardMgr* g_pTreeMgrArray; //
- CZFXBillBoard* g_pTreeArray;
- ////////////////////////////////////////////////////
- BOOL InitD3D(HWND hWnd);
- BOOL InitDInput(HWND hWnd);
- BOOL InitDXFont();
- BOOL InitTerrain();
- BOOL InitPSystem();
- BOOL InitTrees();
- void OnRender();
- void RenderFPS();
- void UpdatePSystem();
- void GetDirectInput(HWND hWnd);
- // 此代码模块中包含的函数的前向声明:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- // TODO: 在此放置代码。
- MSG msg;
- HACCEL hAccelTable;
- // 初始化全局字符串
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_ZFXDREAM, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // 执行应用程序初始化:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ZFXDREAM);
- // 主消息循环:
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- GetDirectInput(msg.hwnd);
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int) msg.wParam;
- }
- //
- // 函数: 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_ZFXDREAM);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = (LPCTSTR)IDC_ZFXDREAM;
- 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;
- }
- g_hWnd = hWnd;
- #ifdef AVI_CAPTURE
- hWnd_Avi = capCreateCaptureWindow("My Own Capture Window", WS_CHILD | WS_VISIBLE,
- 0, 0, 160, 120, hWnd, 1376);
- capDriverConnect(hWnd_Avi, 0/*capture index*/);
- capFileSetCaptureFile(hWnd_Avi, "capture.avi");
- #endif
- if(FALSE == InitD3D(hWnd))
- return FALSE;
- if(FALSE == InitDInput(hWnd))
- return FALSE;
- if(FALSE == InitDXFont())
- return FALSE;
- if(FALSE == InitTerrain())
- return FALSE;
- if(FALSE == InitPSystem())
- return FALSE;
- if(FALSE == InitTrees())
- return FALSE;
- //SetTimer(hWnd, 0, 50, NULL);
- ShowWindow(hWnd, SW_MAXIMIZE);
- UpdateWindow(hWnd);
- #ifdef AVI_CAPTURE
- SetTimer(hWnd, 0, 2000, NULL);
- capCaptureSequence(hWnd_Avi);
- #endif
- 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: 在此添加任意绘图代码...
- OnRender();
- //GetDirectInput(hWnd);
- //EndPaint(hWnd, &ps);
- break;
- case WM_TIMER:
- #ifdef AVI_CAPTURE
- capCaptureStop(hWnd_Avi);
- capDriverDisconnect(hWnd_Avi);
- KillTimer(hWnd, 0);
- #endif
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_CLOSE:
- //delete g_pLODTerrain;
- //if(g_pPSystem != NULL)
- // delete g_pPSystem;
- SAFE_DELETE(g_pPSystem);
- SAFE_DELETE(g_pLODTerrain);
- SAFE_DELETE(g_pSkyBoxMesh);
- SAFE_DELETE(g_pSu27);
- delete [] g_pTreeMgrArray;
- delete [] g_pTreeArray;
- SAFE_RELEASE(g_pMouse);
- SAFE_RELEASE(g_pKeyboard);
- SAFE_RELEASE(g_pDI);
- SAFE_RELEASE(g_pFont);
- SAFE_RELEASE(g_pd3dDevice);
- DefWindowProc(hWnd, message, wParam, lParam);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- BOOL InitD3D(HWND hWnd)
- {
- // Create D3D device
- if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION )))
- return FALSE;
- D3DDISPLAYMODE d3ddm;
- if( FAILED( g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm )))
- return FALSE;//得到当前的显示模式
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory( &d3dpp, sizeof(d3dpp));
- #ifdef FULL_SCREEN
- d3dpp.Windowed = FALSE;
- d3dpp.hDeviceWindow = hWnd;
- d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
- d3dpp.BackBufferCount = 1; //有一个后台缓存
- d3dpp.BackBufferWidth = d3ddm.Width;//屏幕宽度为800像素
- d3dpp.BackBufferHeight = d3ddm.Height;//屏幕长度为600像素
- d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
- #else
- d3dpp.Windowed = TRUE;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- #endif
- d3dpp.BackBufferFormat = d3ddm.Format;
- d3dpp.EnableAutoDepthStencil = TRUE;
- d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
- if( FAILED( g_pD3D -> CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
- D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice)))
- return FALSE;
- // Init Meshs/////////////////////////////////////////////////////////////////////
- // 天空体初始化
- g_pSkyBoxMesh = new CZFXMesh();
- if(FAILED(g_pSkyBoxMesh->LoadMeshFromFile(g_pd3dDevice, "mesh\skybox.x", true)))
- {
- MessageBoxW(hWnd, L"加载天空体失败", L"Initialization Failed", MB_OK);
- g_pSkyBoxMesh = NULL;
- }
- // 飞机模型初始化
- g_pSu27 = new CZFXMesh();
- if(FAILED(g_pSu27->LoadMeshFromFile(g_pd3dDevice, "mesh\su27.x", true)))
- {
- MessageBoxW(hWnd, L"加载飞机模型失败", L"Initialization Failed", MB_OK);
- g_pSu27 = NULL;
- }
- return TRUE;
- }
- BOOL InitDInput(HWND hWnd)
- {
- // Create DirectInput device
- // Keyboard Input device
- HRESULT hr;
- if( FAILED( hr = DirectInput8Create( hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_pDI, NULL )))
- return FALSE;
- if( FAILED( hr = g_pDI->CreateDevice( GUID_SysKeyboard, &g_pKeyboard, NULL )))
- return FALSE;
- if( FAILED( hr = g_pKeyboard->SetDataFormat(&c_dfDIKeyboard)))
- return FALSE;
- if( FAILED( hr = g_pKeyboard->SetCooperativeLevel( hWnd, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE )))
- return FALSE;
- g_pKeyboard->Acquire();
- // Mouse Input device
- if( FAILED( hr = g_pDI->CreateDevice( GUID_SysMouse, &g_pMouse, NULL )))
- return FALSE;
- if( FAILED( hr = g_pMouse->SetDataFormat(&c_dfDIMouse2)))
- return FALSE;
- if( FAILED( hr = g_pMouse->SetCooperativeLevel( hWnd, DISCL_EXCLUSIVE|DISCL_FOREGROUND )))
- return FALSE;
- g_pMouse->Acquire();
- return TRUE;
- // End create DInput device
- }
- BOOL InitDXFont()
- {
- //创建字体
- D3DXFONT_DESC fontDesc;
- ZeroMemory( &fontDesc, sizeof(D3DXFONT_DESC) );
- fontDesc.Height = 15;
- fontDesc.Width = 8;
- fontDesc.Weight = 500;
- fontDesc.Italic = TRUE;
- strcpy( fontDesc.FaceName, "Times New Roman");
- if( SUCCEEDED( D3DXCreateFontIndirect( g_pd3dDevice, &fontDesc, &g_pFont )))
- return TRUE;
- else
- return FALSE;
- }
- BOOL InitTerrain()
- {
- // 加载地形数据
- try
- {
- g_pLODTerrain = new CZFXLODTerrain("map\perlin1025-1.map");
- }
- catch(...)
- {
- return FALSE;
- }
- g_pLODTerrain->LoadTextureFromFile( g_pd3dDevice, "texture\lowTexture.jpg",
- "texture\highTexture.jpg" );
- g_pLODTerrain->InitQuadTree();
- g_pLODTerrain->SetDimesion( 3000.0f, 3000.0f, D3DXVECTOR2(0, 0) );
- if(SUCCEEDED(g_pLODTerrain->InitShader(g_pd3dDevice)))
- return TRUE;
- else
- return FALSE;
- }
- //粒子系统初始化
- BOOL InitPSystem()
- {
- BoundingBox boundingBox( D3DXVECTOR3(-1500.0f, 0.0f, -1500.0f), D3DXVECTOR3(1500.0f, 750.0f, 1500.0f) );
- //第一次只加入NUMPERBATCH个粒子,之后没渲染一帧增加NUMPERBATCH个,直至粒子数达到NUMTOTAL
- #ifdef RENDER_SNOW
- g_pPSystem = new CPSystem(&boundingBox, NUMPERBATCH, 5);
- LPCTSTR texFileName = "texture\snowball.bmp";//纹理图
- #else
- g_pPSystem = new CPSystem(&boundingBox, NUMPERBATCH, 20);
- LPCTSTR texFileName = "texture\raindrop.bmp";//纹理图
- #endif
- if(!g_pPSystem->init(g_pd3dDevice, texFileName))
- {
- return FALSE;
- }
- return TRUE;
- }
- BOOL InitTrees()
- {
- g_pTreeArray = new CZFXBillBoard[TREE_NUM_TOTAL];
- D3DXVECTOR3 posVec[TREE_NUM_TOTAL];
- FILE* pFile = fopen("zfx.ini", "r");
- if(NULL == pFile)
- {
- MessageBoxW(g_hWnd, L"Open zfx.ini Failed", L"Initialization Failure", MB_OK);
- return FALSE;
- }
- for(int i=0;i<TREE_NUM_TOTAL;i++)
- {
- fscanf(pFile, "%f, %f", &posVec[i].x, &posVec[i].z);
- }
- fclose(pFile);
- for(int i=0;i<TREE_NUM_TOTAL;i++)
- {
- posVec[i].y = g_pLODTerrain->GetTerrainElev(posVec[i].x, posVec[i].z);
- g_pTreeArray[i].CreateBoard(g_pd3dDevice, posVec[i], 100, 100);
- }
- g_pTreeMgrArray = new CZFXBillBoardMgr[TREEMGR_NUM_TOTAL];
- if(FAILED(g_pTreeMgrArray[0].LoadTextureFromFile(g_pd3dDevice, "texture\Tree1.dds")))
- {
- MessageBoxW(g_hWnd, L"加载树纹理失败", L"Initialization Failure", MB_OK);
- return FALSE;
- }
- if(FAILED(g_pTreeMgrArray[1].LoadTextureFromFile(g_pd3dDevice, "texture\Tree2.dds")))
- {
- MessageBoxW(g_hWnd, L"加载树纹理失败", L"Initialization Failure", MB_OK);
- return FALSE;
- }
- if(FAILED(g_pTreeMgrArray[2].LoadTextureFromFile(g_pd3dDevice, "texture\Tree3.dds")))
- {
- MessageBoxW(g_hWnd, L"加载树纹理失败", L"Initialization Failure", MB_OK);
- return FALSE;
- }
- if(FAILED(g_pTreeMgrArray[3].LoadTextureFromFile(g_pd3dDevice, "texture\Tree4.dds")))
- {
- MessageBoxW(g_hWnd, L"加载树纹理失败", L"Initialization Failure", MB_OK);
- return FALSE;
- }
- for(int i=0;i<TREE_NUM_TOTAL;i++)
- {
- int j = i%4;
- g_pTreeMgrArray[j].AddBillBoard(g_pTreeArray[i]);
- }
- return TRUE;
- }
- void OnRender()
- {
- // pre rendering
- // 设置世界矩阵
- D3DXMATRIX matWorld;
- D3DXMatrixIdentity(&matWorld);
- // 设置视口矩阵
- D3DXMATRIX matView;
- // 根据地形的X,Y坐标来获取地形的高度,再拔高10.0f,即相机的高度
- g_camera.SetPosY( g_pLODTerrain->GetTerrainElev( g_camera.GetPosX(), g_camera.GetPosZ()) + 50.0f/*200.0f*/);
- g_camera.GetViewMatrix( &matView );
- g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
- // 设置投影矩阵
- D3DXMATRIX matProj;
- D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/2, 1.0f, 0.1f, 3500.0f );
- g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
- // 设置填充模式
- g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, g_fillMode );
- // set render state
- g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );
- g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
- // 创建地形
- g_pLODTerrain->CreateTerrain( g_pd3dDevice, &g_camera );
- g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0);
- if( SUCCEEDED(g_pd3dDevice->BeginScene()) )
- {
- // 渲染天空体
- if(g_pSkyBoxMesh != NULL)
- {
- D3DXMATRIX skyBoxMat;
- D3DXMatrixIdentity(&skyBoxMat);
- D3DXMatrixScaling(&skyBoxMat, 1.875f*2, 2, 1.875f*2);
- g_pd3dDevice->SetTransform(D3DTS_WORLD, &skyBoxMat);
- g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, false );
- g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
- g_pSkyBoxMesh->OnRender(g_pd3dDevice);
- g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );
- }
- // 渲染飞机模型
- if(g_pSu27 != NULL)
- {
- D3DXMATRIX Su27Mat;
- D3DXMatrixIdentity(&Su27Mat);
- D3DXMatrixTranslation(&Su27Mat, 200, 300, 200);
- D3DXMATRIX matScale, matRot, matProduct;
- D3DXMatrixScaling(&matScale, 3, 3, 3);
- D3DXMatrixRotationX(&matRot, -1.57f);
- D3DXMatrixMultiply(&matProduct, &matScale, &matRot);
- D3DXMatrixMultiply(&Su27Mat, &matProduct, &Su27Mat);
- //g_pd3dDevice->SetRenderState(D3DRS_AMBIENT, 0xffffffff);
- //D3DLIGHT9 Su27Light;
- //ZeroMemory(&Su27Light, sizeof(D3DLIGHT9));
- //Su27Light.Type = D3DLIGHT_DIRECTIONAL;
- //Su27Light.Diffuse.r = Su27Light.Diffuse.g = Su27Light.Diffuse.b = 0.3f;
- //Su27Light.Diffuse.a = 1.0f;
- //Su27Light.Direction = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
- ///*g_pd3dDevice->SetLight(0, &Su27Light);
- //g_pd3dDevice->LightEnable(0, TRUE);*/
- g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, true );
- g_pd3dDevice->SetTransform(D3DTS_WORLD, &Su27Mat);
- g_pSu27->OnRender(g_pd3dDevice);
- g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
- }
- // 渲染地形
- g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
- g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
- g_pLODTerrain->RenderTerrain(g_pd3dDevice, g_bFog);
- // 渲染树
- for(int i=0;i<TREEMGR_NUM_TOTAL;i++)
- {
- g_pTreeMgrArray[i].UpdateBoardOrient(&g_camera);
- g_pTreeMgrArray[i].OnRender(g_pd3dDevice);
- }
- // 渲染粒子系统
- g_pPSystem->render();
- UpdatePSystem();
- // 写字
- RenderFPS();
- g_pd3dDevice->EndScene();
- }
- g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
- }
- void RenderFPS()
- {
- //用于渲染侦FPS
- static int tmLastTime = GetTickCount();
- int tmNow = GetTickCount();
- //用于计算渲染FPS
- static int nFrameCount = 0;
- static int nFPS = 0;
- if(tmNow - tmLastTime > 1000)
- {
- //计算FPS
- tmLastTime = tmNow;
- nFPS = nFrameCount;
- nFrameCount = 0;
- }
- nFrameCount ++;
- // 输出字符串(FPS = ?, Triangles = ?)
- char str[500];
- RECT rect;
- rect.left = rect.top = 10;
- rect.bottom = 46;
- rect.right = 260;
- sprintf(str, "Current FPS:%d Triangles: %d Current Paticle num: %d View:x =%0.2f, y=%0.2f, z=%0.2f Please press F and Q to toggle Fog and WireFrame mode",
- nFPS, g_pLODTerrain->GetTriangles(), g_pPSystem->getParticleNum(), g_camera.GetPosX(),
- g_camera.GetPosY(), g_camera.GetPosZ());
- g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
- }
- // 更新粒子系统
- void UpdatePSystem()
- {
- #ifdef RENDER_SNOW
- g_pPSystem->update(0.3f);//每帧更新一个恒定值,0.3秒
- #else
- g_pPSystem->update(1.3f);//每帧更新一个恒定值,0.3秒
- #endif
- //如粒子数未超过总的粒子数--NUMTOTAL,新加入一批粒子
- //#ifndef RENDER_SNOW
- if( NUMTOTAL > g_pPSystem->getParticleNum() )
- {
- for(int i=0;i<NUMPERBATCH;i++)
- g_pPSystem->addParticle();
- }
- //#endif
- }
- void GetDirectInput(HWND hWnd)
- {
- // 获取鼠标输入
- int nMouseReAcquireTimes, nKeybdReAcquireTimes;
- nMouseReAcquireTimes = nKeybdReAcquireTimes = 0;
- DIMOUSESTATE2 dims2;
- HRESULT hr;
- if( g_pMouse == NULL )
- {
- MessageBox(hWnd, "Mouse device lost", "DirectInput exception", MB_OK);
- return;
- }
- while( FAILED( hr = g_pMouse->GetDeviceState(sizeof(DIMOUSESTATE2), &dims2)))
- {
- g_pMouse->Acquire();
- }
- g_camera.Yaw( dims2.lX * 0.005f );
- g_camera.Pitch( dims2.lY * -0.005f );
- // 获取键盘输入
- BYTE diks[256]; // DirectInput keyboard state buffer
- if( g_pKeyboard == NULL )
- {
- MessageBox(hWnd, "Keyboard device lost", "DirectInput exception", MB_OK);
- return;
- }
- while( DI_OK != g_pKeyboard->GetDeviceState( sizeof(diks), diks ))
- {
- g_pKeyboard->Acquire();
- }
- // 按下ESC键,退出程序
- if( diks[DIK_ESCAPE] & 0x80 )
- SendMessage(hWnd, WM_CLOSE, 0, 0);
- // 按下 A 键,摄像机左移
- if( diks[DIK_A] | diks[DIK_LEFT] & 0x80 )
- g_camera.Walk( -WALKLENTGH, 0 );
- // 按下 D 键,摄像机右移
- if( diks[DIK_D] | diks[DIK_RIGHT] & 0x80 )
- g_camera.Walk( WALKLENTGH, 0 );
- // 按下 W 键,摄像机前移
- if( diks[DIK_W] | diks[DIK_UP] & 0x80 )
- g_camera.Walk( 0, WALKLENTGH );
- // 按下 S 键,摄像机后移
- if( diks[DIK_S] | diks[DIK_DOWN] & 0x80 )
- g_camera.Walk( 0, -WALKLENTGH );
- // 渲染模式切换
- if( diks[DIK_Q] & 0x80 && !g_bKey_Q)
- {
- g_bKey_Q = TRUE;
- if(g_fillMode == D3DFILL_SOLID)
- g_fillMode = D3DFILL_WIREFRAME;
- else
- g_fillMode = D3DFILL_SOLID;
- }
- if(!(diks[DIK_Q] & 0x80))
- {
- g_bKey_Q = FALSE;
- }
- // 启用雾化切换
- if( diks[DIK_F] &0x80 && !g_bKey_F)
- {
- g_bKey_F = TRUE;
- g_bFog = !g_bFog;
- }
- if(!(diks[DIK_F] &0x80))
- {
- g_bKey_F = FALSE;
- }
- // 更新窗口
- SendMessage(hWnd, WM_PAINT, 0, 0);
- }
- // “关于”框的消息处理程序。
- 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;
- }