📄 ch21p1_firingrange.cpp
字号:
/*
#############################################################################
Ch21p1_FiringRange.cpp: a program that demonstrates several different
weaponry effects.
#############################################################################
*/
// include files ////////////////////////////////////////////////////////////
#define STRICT
#include <stdio.h>
#include <math.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "D3DHelperFuncs.h"
#include "Ch21p1_resource.h"
#include "Camera.h"
#include "InputManager.h"
#include "SkyBox.h"
#include "Timer.h"
#include "GroundPlane.h"
#include "Ch21p1_Gun.h"
#include "Ch21p1_PlasmaGun.h"
#include "Ch21p1_LaserGun.h"
#include "Ch21p1_MachineGun.h"
const int NUMSPRITES = 5;
//-----------------------------------------------------------------------------
// Name: class CMyD3DApplication
// Desc: Application class. The base class (CD3DApplication) provides the
// generic functionality needed in all Direct3D samples. CMyD3DApplication
// adds functionality specific to this sample program.
//-----------------------------------------------------------------------------
class CMyD3DApplication : public CD3DApplication
{
// Font for drawing text
CD3DFont* m_pFont;
CD3DFont* m_pFontSmall;
// Scene
CUserControlledCamera m_Camera;
// Mouse Input
CInputManager m_InputManager;
// skybox
CSkyBox m_SkyBox;
// ground plane
CGroundPlane m_GroundPlane;
CTimer m_TransitionTimer;
// da weaponry
CPlasmaGun m_PlasmaGun;
CPlasmaBulletArray m_PlasmaBulletArray;
CLaserGun m_LaserGun;
CGun *m_pCurGun;
CGun *m_pNextGun;
CMachineGun m_MachineGun;
protected:
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidateDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT FinalCleanup();
HRESULT Render();
HRESULT FrameMove();
HRESULT ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format );
LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
void BeginGunTransition(CGun *pNextGun);
void ProcessInput();
int m_iLightPosX;
int m_iLightPosY;
public:
CMyD3DApplication();
};
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything, and goes into a
// message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
CMyD3DApplication d3dApp;
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
return d3dApp.Run();
}
//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication()
{
m_strWindowTitle = _T("Ch21p1_FiringRange");
m_bUseDepthBuffer = TRUE;
m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
m_pFontSmall = new CD3DFont( _T("Arial"), 9, D3DFONT_BOLD );
}
//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
// permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
// the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
if (m_fElapsedTime < 0.0001f) {
m_fElapsedTime = 0.0001f;
}
CTimer::UpdateAll(m_fElapsedTime);
m_PlasmaBulletArray.UpdateAll(m_fElapsedTime);
if (m_pCurGun) m_pCurGun->Update(m_fElapsedTime);
if (m_TransitionTimer.IsRunning()) {
if (m_pCurGun) {
if (m_TransitionTimer.GetTime() < 0.25f) {
m_pCurGun->Pos() = D3DXVECTOR3(0.5f, -0.6f-(m_TransitionTimer.GetTime()*4), 1.0f);
}
else {
m_pCurGun = m_pNextGun;
m_pCurGun->Pos() = D3DXVECTOR3(0.5f, -1.6f+(m_TransitionTimer.GetTime()*4)-1.0f, 1.0f);
}
if (m_TransitionTimer.GetTime() > 0.5f) {
m_TransitionTimer.Stop();
m_pNextGun = NULL;
}
}
}
return S_OK;
}
void CMyD3DApplication::BeginGunTransition(CGun *pNextGun)
{
if (typeid(*pNextGun) == typeid(*m_pCurGun)) return;
if (m_TransitionTimer.IsRunning()) return;
m_pNextGun = pNextGun;
m_TransitionTimer.Begin();
}
void CMyD3DApplication::ProcessInput()
{
const float fSpeed = 0.5f;
unsigned char m_bKey[256];
ZeroMemory( m_bKey, 256 );
GetKeyboardState(m_bKey);
// Process keyboard input
if(m_bKey['D'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(fSpeed, 0.0f, 0.0f)); // Slide Right
if(m_bKey['A'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(-fSpeed, 0.0f, 0.0f));// Slide Left
if(m_bKey['Q'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, fSpeed, 0.0f)); // Slide Up
if(m_bKey['Z'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, -fSpeed, 0.0f));// Slide Down
if(m_bKey['W'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, 0.0f, fSpeed)); // Slide Foward
if(m_bKey['S'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, 0.0f, -fSpeed));// Slide Back
if(m_bKey['L'] & 128) m_Camera.AddToYawPitchRoll(fSpeed, 0.0f, 0.0f); // Turn Right
if(m_bKey['J'] & 128) m_Camera.AddToYawPitchRoll(-fSpeed, 0.0f, 0.0f); // Turn Left
if(m_bKey['K'] & 128) m_Camera.AddToYawPitchRoll(0.0f, fSpeed, 0.0f); // Turn Down
if(m_bKey['I'] & 128) m_Camera.AddToYawPitchRoll(0.0f, -fSpeed, 0.0f);// Turn Up
if(m_bKey['1'] & 128) BeginGunTransition(&m_MachineGun);
if(m_bKey['2'] & 128) BeginGunTransition(&m_PlasmaGun);
if(m_bKey['3'] & 128) BeginGunTransition(&m_LaserGun);
// mouse look
DIMOUSESTATE2 dims2;
m_InputManager.ReadMouse(dims2);
// play with the divisor constants to change the mouselook sensitivity.
// I've found that these values most accurately simulate my beloved Q3A setup. :)
m_Camera.AddToYawPitchRoll((float)dims2.lX/0.8f, (float)dims2.lY/0.8f, 0.0f);
// prevent camera from moving up or down.
D3DXVECTOR3 pos = m_Camera.GetPosition();
pos.y = 2.60f;
m_Camera.SetPosition(pos);
if (dims2.rgbButtons[0] && m_pCurGun && m_pCurGun->CanFire())
m_pCurGun->Fire(m_Camera);
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
// rendering. This function sets up render states, clears the
// viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -