www.pudn.com > 3D_Tank.rar > d3dInit.cpp
#pragma once //系统文件 #include//标准库 #include #include #include //DirectX #include //项目文件 #include "d3dUtility.h" #include "Terrain.h" #include "Camera.h" #include "Particle.h" #include "Arithmetic.h" #include "GameObject.h" #include "Input.h" #include "GameFont.h" #include "Sound.h" #include "Sky.h" #include "Sprite.h" //COM接口 IDirect3DDevice9 *Device = 0; //D3D接口 CComPtr gDevice; CInput gInput; CTerrain terrain; //全局变量 const D3DXCOLOR clrDiffuse(1.0f, 1.0f, 1.0f, 0); D3DXVECTOR3 dirLight(0, -5.0f, 5.0f); //方向光的颜色 const float fBallRadius = 100.0f; BOOL bConnectServer = FALSE; D3DXVECTOR3 vLook(0, 0, 1); D3DXVECTOR3 vRight(1, 0, 0); D3DXVECTOR3 vUp(0, 1, 0); HWND hWindow; HINSTANCE instance_app; CCamera camera; //照相机 D3DXMATRIX matBallTranslation; //球的平移矩阵 CParticleSystem particle; CGameFont font; Tank boxtank; Tank boxtank2; CSoundSet sound; Sky sky; ImageDrawer sprite; bool Setup(HWND hwnd) { sound.InitSoundDevice(hWindow); sound.AddSoundEffect(".\\res\\Sound\\fire.wav"); boxtank.SetSoundPlayer(&sound); gDevice.Reset(Device); font.InitFont(gDevice.Get()); terrain.InitTerrainFromHeightmap( gDevice,".\\res\\terrain.raw", 128, 128, 10, 10); terrain.SetTexture (".\\res\\Texture\\ground.bmp"); terrain.PreComputerLight (dirLight, clrDiffuse); boxtank.InitObject(gDevice); boxtank.SetPosition(100, terrain.GetHeight(100, 100), 100); boxtank.SetCamera(&camera); boxtank2.InitObject(gDevice); boxtank2.SetPosition(200, terrain.GetHeight(200, 200), 200); sprite.Init(gDevice); sprite.AddImage(".\\res\\Texture\\minimap.dds", D3DCOLOR_XRGB(0, 0, 0)); sprite.AddImage(".\\res\\Texture\\redpoint.dds", D3DCOLOR_XRGB(0, 0, 0)); gInput.InitKeyboardAndMouse(hwnd, instance_app) ; particle.Init(gDevice, ".\\res\\Texture\\fire.dds"); particle.SetBoundingBox(D3DXVECTOR3(0, 0, 0),D3DXVECTOR3(128 * 20, 300, 128 * 20)); boxtank.SetParticle(&particle); boxtank2.SetParticle(&particle); sky.Init(gDevice); D3DXMATRIX matView; camera.GetViewMarix (&matView); gDevice->SetTransform(D3DTS_VIEW, &matView); D3DXMATRIX matProj; D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 2.0f, 640.0f / 480.0f, 1.0f, 3000.0f); gDevice->SetTransform (D3DTS_PROJECTION, &matProj); //设置投影矩阵 return true; } VOID ProcessInput(float timeDelta) { gInput.GetKeyboardInput(); if (gInput.m_KeyboardState[DIK_W] & 0x80) { D3DXVECTOR3 pos; D3DXVECTOR3 dir; boxtank.GetPosition(&pos); boxtank.GetMoveDirection(&dir); pos += dir * 0.1f; if (terrain.IsInTerrain(pos.x, pos.z)) boxtank.Walk(0.1f); } if (gInput.m_KeyboardState[DIK_S] & 0x80) { D3DXVECTOR3 pos; D3DXVECTOR3 dir; boxtank.GetPosition(&pos); boxtank.GetMoveDirection(&dir); pos -= dir * 0.1f; if (terrain.IsInTerrain(pos.x, pos.z)) boxtank.Walk(-0.1f); } if (gInput.m_KeyboardState[DIK_A] & 0x80) { boxtank.BodyYaw(-0.1f); } if (gInput.m_KeyboardState[DIK_D] & 0x80) { boxtank.BodyYaw(0.1f); } if (gInput.m_KeyboardState[DIK_T] & 0x80) { D3DXVECTOR3 pos; D3DXVECTOR3 dir; boxtank2.GetPosition(&pos); boxtank2.GetMoveDirection(&dir); pos += dir * 0.1f; if (terrain.IsInTerrain(pos.x, pos.z)) boxtank2.Walk(0.1f); } if (gInput.m_KeyboardState[DIK_G] & 0x80) { D3DXVECTOR3 pos; D3DXVECTOR3 dir; boxtank2.GetPosition(&pos); boxtank2.GetMoveDirection(&dir); pos -= dir * 0.1f; if (terrain.IsInTerrain(pos.x, pos.z)) boxtank2.Walk(-0.1f); } if (gInput.m_KeyboardState[DIK_F] & 0x80) { boxtank2.BodyYaw(-0.1f); } if (gInput.m_KeyboardState[DIK_H] & 0x80) { boxtank2.BodyYaw(0.1f); } gInput.GetMouseInput(); if (gInput.m_MouseState.lX) { boxtank.GunYaw(gInput.m_MouseState.lX / 5.0f); } if (gInput.m_MouseState.lY) { boxtank.GunPitch(gInput.m_MouseState.lY / 5.0f); } if (gInput.m_MouseState.rgbButtons[0] & 0x80) { boxtank.Fire(); } if (gInput.m_MouseState.rgbButtons[1] & 0x80) { boxtank.ChangeMode(); } } void Cleanup() { // Nothing to cleanup in this sample. } bool Display(float timeDelta) { if( Device ) // Only use Device methods if we have a valid device. { particle.Update(timeDelta); boxtank.TimeElapse(timeDelta); ProcessInput(timeDelta); D3DXVECTOR3 pos; boxtank.GetPosition(&pos); D3DXVECTOR3 pos2; boxtank2.GetPosition(&pos2); pos.y = terrain.GetHeight (pos.x, pos.z); pos2.y = terrain.GetHeight (pos2.x, pos2.z); boxtank.SetPosition(pos.x, pos.y, pos.z); boxtank2.SetPosition(pos2.x, pos2.y, pos2.z); D3DXVECTOR3 vNewNormal; terrain.GetNormal(pos.x, pos.z, &vNewNormal); D3DXVECTOR3 vNewNormal2; terrain.GetNormal(pos2.x, pos2.z, &vNewNormal2); D3DXVec3Normalize(&vNewNormal, &vNewNormal); D3DXVec3Normalize(&vNewNormal2, &vNewNormal2); float fPitch; float fRoll; GetRotationAngle(vNewNormal, &fPitch, &fRoll); boxtank.SetPitchRoll(fPitch, fRoll); float fPitch2; float fRoll2; GetRotationAngle(vNewNormal, &fPitch2, &fRoll2); boxtank.SetPitchRoll(fPitch2, fRoll2); D3DXMATRIX matView; camera.GetViewMarix (&matView); gDevice->SetTransform(D3DTS_VIEW, &matView); gDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); gDevice->BeginScene(); terrain.Render(); boxtank.Render(); boxtank2.Render(); sky.Render(); particle.Render(); D3DXVECTOR3 vPosition = D3DXVECTOR3(640.0f - 128.0f, 0.0f, 0.0f); sprite.Draw(".\\res\\Texture\\minimap.dds", vPosition, D3DXSPRITE_ALPHABLEND); vPosition = D3DXVECTOR3(640.f - 64.0f, 64.0f, 0.0f); gDevice->EndScene(); // Swap the back and front buffers. gDevice->Present(0, 0, 0, 0); } return true; } LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch( msg ) { case WM_DESTROY: ::PostQuitMessage(0); break; case WM_KEYDOWN: if( wParam == VK_ESCAPE ) ::DestroyWindow(hwnd); break; } return ::DefWindowProc(hwnd, msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) { if(!d3d::InitD3D(hinstance, 640, 480, true, D3DDEVTYPE_HAL, &Device, &hWindow, &instance_app)) { ::MessageBox(0, "InitD3D() - FAILED", 0, 0); return 0; } if(!Setup(hWindow)) { ::MessageBox(0, "Setup() - FAILED", 0, 0); return 0; } d3d::EnterMsgLoop( Display ); Cleanup(); return 0; }