📄 game.cpp
字号:
return TRUE;
}
LPDIRECT3DDEVICE8 CGame::GetDevice()
{
return m_pD3DDevice;
}
void CGame::GameLoop()
{
MSG msg;
BOOL fMessage;
PeekMessage(&msg,NULL,0U,0U,PM_NOREMOVE);
m_dwStartTime=timeGetTime();
m_dwOldTime=timeGetTime();
while(msg.message!=WM_QUIT && !m_bQuit)
{
fMessage=PeekMessage(&msg,NULL,0U,0U,PM_REMOVE);
if(fMessage)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//Control FPS......
if(m_dwFPS>65)
{
if(m_dwPedotime<17)
m_dwPedotime++;
else
m_dwPedotime=16;
}
else if(m_dwPedotime>2 && m_dwCountFrames<=0)
{
m_dwPedotime--;
m_dwCountFrames=20;
}
m_dwCountFrames--;
m_dwNewTime=timeGetTime();
if(m_dwNewTime-m_dwOldTime>=m_dwPedotime)
{
Render();
m_dwOldTime=m_dwNewTime;
}
}
}
m_dwEndTime=timeGetTime();
}
void CGame::Render()
{
if(m_pD3DDevice==NULL)
return;
//Check system energy
int i;
m_fSysEnergy=0.0f;
for(i=0;i<SUMBALL;i++)
m_fSysEnergy+=m_pSphere[i]->CheckSphereEnergy();
if(m_fSysEnergy==0)
m_bSendBall=FALSE;
//Process keyboard and mouse user input
ProcessKeyboard();
ProcessMouse();
if(!m_bQuit)
{
//Clear the back buffer and depth buffer
m_pD3DDevice->Clear(0,NULL,
D3DCLEAR_TARGET|
D3DCLEAR_ZBUFFER|
D3DCLEAR_STENCIL,
D3DCOLOR_XRGB(0,0,0),1.0f,0);
m_pD3DDevice->BeginScene();
Setup3DCamera();
Render3D();
RenderText();
m_pD3DDevice->EndScene();
//Filp the back and front buffers so that whatever has been rendered on
//the back buffer will now be visible on screen(front buffer)
m_pD3DDevice->Present(NULL,NULL,NULL,NULL);
m_dwFrames++;
}
}
void CGame::Render3D()
{
int i;
for(i=0;i<SUMBALL;i++)
{
if(m_pSphere[i]->GetSphereState())
{
if(!m_bPlanform)
{
//Step 1
SphereHitSphereProcess(i);
//Step 2
//Improve performance
if(m_pSphere[i]->GetSphereVelocity() != 0.0f)
SphereHitBarProcess(i);
}
//Step 3
SphereFallInHoleProcess(i);
if(i != 0)
m_pSphere[i]->MoveSphere();
else if(!m_bPlanform)
m_pSphere[0]->MoveSphere();
else
m_pSphere[0]->MoveSphereForUser(m_iMouseX/20.0f,-m_iMouseY/20.0f);
m_dwTotalPolygons+=m_pSphere[i]->Render();
}
}
//I will Set LookAt point at here because m_vecRayPos
//has been loaded MAXNUM's ball position
m_pSphere[0]->GetSpherePosition(m_vecRayPos);
m_dwTotalPolygons+=m_pTableMesh->Render();
if(m_fSysEnergy == 0 && !m_bPlanform)
{
m_pSphere[0]->GetSpherePosition(m_vecCuePosition);
m_pCue->TransformCue(m_vecCuePosition,m_vecCueDir,(int)m_iMoveCue/4);
m_dwTotalPolygons+=m_pCue->Render();
}
}
void CGame::Setup3DCamera()
{
D3DXMATRIX matView,matRotationY;
D3DXVECTOR4 vecTransformedPos;
D3DXVECTOR3 vecSavedCameraPos;
float fLimitY=0.0f;
if(!m_bPlanform)
{
if(m_fSysEnergy == 0 && !m_bCtrl)
{
D3DXMatrixRotationY(&matRotationY,m_iMouseStateX/100.0f);
D3DXVec3Transform(&vecTransformedPos,&m_vecCameraPosition,&matRotationY);
//*******************************************
//for x
m_vecCameraPosition.x=vecTransformedPos.x;
//for y
fLimitY=vecTransformedPos.y+m_iMouseStateY/5.0f;
if(fLimitY<=60.0f && fLimitY>=8.0f)
m_vecCameraPosition.y=fLimitY;
//for z
m_vecCameraPosition.z=vecTransformedPos.z;
//*******************************************
//*******************************************
//***************Scale scene*****************
vecSavedCameraPos=m_vecCameraPosition*m_fScale;
//*******************************************
//*******************************************
D3DXMatrixLookAtLH(&matView,&vecSavedCameraPos, //Camera Position
&m_vecRayPos, //Look at Position
&D3DXVECTOR3(0.0f,1.0f,0.0f));//Up Direction
m_vecCueDir=vecSavedCameraPos-m_vecRayPos;
m_vecCueDir.y=0.0f;
m_vecSavedCameraPosition=vecSavedCameraPos;
m_vecSavedLookAtPosition=m_vecRayPos;
}
else
{
D3DXMatrixLookAtLH(&matView,&m_vecSavedCameraPosition,
&m_vecSavedLookAtPosition,
&D3DXVECTOR3(0.0f,1.0f,0.0f));
}
}
else
{
D3DXMatrixLookAtLH(&matView,&D3DXVECTOR3(0.0f,80.0f,0.0f), //Camera Position
&D3DXVECTOR3(0.0f,0.0f,0.0f), //Look at Position
&D3DXVECTOR3(0.0f,0.0f,1.0f)); //Up Direction
}
m_pD3DDevice->SetTransform(D3DTS_VIEW,&matView);
//specify the field of view
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,1.25f,1.0f,500.0f);
m_pD3DDevice->SetTransform(D3DTS_PROJECTION,&matProj);
}
BOOL CGame::Initialize(HWND hwnd,UINT nWidth,UINT nHeight,HINSTANCE hInstance)
{
//Initialize Direct3D
if(!InitializeD3D(hwnd,nWidth,nHeight))
return FALSE;
//Initialize DirectInput
if(!InitializeDirectInput(hwnd,hInstance))
return FALSE;
//Initialize Audio
if(!InitializeDirectAudio(hwnd))
m_bSoundCardState=false;
else
m_bSoundCardState=true;
//Initialize Lighting
if(!InitializeLights())
return FALSE;
//Initialize Game Objects
if(!InitializeGame())
return FALSE;
return TRUE;
}
BOOL CGame::InitializeGame()
{
LogInfo("<br>Initialize Game:");
m_pFont=new CFont(m_pD3DDevice,"Verdana",15,TRUE,FALSE,FALSE);
m_pCue=new CCue(m_pD3DDevice,25.0f,0.5f,10);
m_pCue->SetSideTexture("textures/cueside.bmp");
m_pCue->SetEndTexture("textures/cueend.bmp");
int i;
for(i=0;i<SUMBALL;i++)
{
m_pSphere[i]=new CSphere(m_pD3DDevice,10,10);
}
m_pSphere[0]->SetTexture("textures/0.bmp");
m_pSphere[1]->SetTexture("textures/1.bmp");
m_pSphere[2]->SetTexture("textures/2.bmp");
m_pSphere[3]->SetTexture("textures/3.bmp");
m_pSphere[4]->SetTexture("textures/4.bmp");
m_pSphere[5]->SetTexture("textures/5.bmp");
m_pSphere[6]->SetTexture("textures/6.bmp");
m_pSphere[7]->SetTexture("textures/7.bmp");
m_pSphere[8]->SetTexture("textures/8.bmp");
m_pSphere[9]->SetTexture("textures/9.bmp");
//add code for others textures
InitilizeSpheresPosition();
m_pTableMesh=new CTable(m_pD3DDevice,"meshs/table.x");
//This is current time to set LPD3DXMESH pointer
//bescaue the mesh of table has been loaded.
m_pMesh=m_pTableMesh->GetMeshTablePointer();
if(m_bSoundCardState)
{
m_pSoundHitCue0 = new CAudio();
m_pSoundHitCue0->InitializeForWavMidi(m_pDirectAudioPerformance, m_pDirectAudioLoader);
m_pSoundHitCue0->LoadSound("Hitcue0.wav");
m_pSoundHitCue1 = new CAudio();
m_pSoundHitCue1->InitializeForWavMidi(m_pDirectAudioPerformance, m_pDirectAudioLoader);
m_pSoundHitCue1->LoadSound("Hitcue1.wav");
m_pSoundHitBar = new CAudio();
m_pSoundHitBar->InitializeForWavMidi(m_pDirectAudioPerformance, m_pDirectAudioLoader);
m_pSoundHitBar->LoadSound("HitBar.wav");
m_pSoundBimpacting0 = new CAudio();
m_pSoundBimpacting0->InitializeForWavMidi(m_pDirectAudioPerformance, m_pDirectAudioLoader);
m_pSoundBimpacting0->LoadSound("Bimpacting0.wav");
m_pSoundBimpacting1 = new CAudio();
m_pSoundBimpacting1->InitializeForWavMidi(m_pDirectAudioPerformance, m_pDirectAudioLoader);
m_pSoundBimpacting1->LoadSound("Bimpacting1.wav");
m_pSoundFallInHole = new CAudio();
m_pSoundFallInHole->InitializeForWavMidi(m_pDirectAudioPerformance, m_pDirectAudioLoader);
m_pSoundFallInHole->LoadSound("FallInHole.wav");
// m_pSoundBG = new CSound();
// m_pSoundBG->InitialiseForMP3();
// m_pSoundBG->LoadSound("Aai_Took-Needthew-3776_hifi.mp3");
}
return TRUE;
}
void CGame::RenderText()
{
char buffer[255];
DWORD dwDuration=(timeGetTime()-m_dwStartTime)/1000;
if(dwDuration>0)
{
m_dwFPS=m_dwFrames/dwDuration;
sprintf(buffer,"Duration: %d seconds. Frames: %d. FPS: %d.",dwDuration,m_dwFrames,m_dwFPS);
}
else
sprintf(buffer,"Calculating...");
m_pFont->DrawText(buffer,0,0,D3DCOLOR_XRGB(255,255,255));
}
BOOL CGame::InitializeDirectInput(HWND hwnd,HINSTANCE hInstance)
{
LogInfo("<br>Initialize DirectInput:");
//Create the DirectInput object
if(FAILED(DirectInput8Create(hInstance,DIRECTINPUT_VERSION,IID_IDirectInput8,
(void**)&m_pDirectInput,NULL)))
{
LogError("<li>Unable to create DirectInput interface.");
return FALSE;
}
else
LogInfo("<li>DirectInput interface create OK");
//Keyboard************Create the keyboard device object
if(FAILED(m_pDirectInput->CreateDevice(GUID_SysKeyboard,&m_pKeyboard,NULL)))
{
CleanUpDirectInput();
LogError("<li>Unable to create DirectInput keyboard device interface.");
return FALSE;
}
else
LogInfo("<li>DirectInput keyboard device interface created OK.");
//Set the data format for the keyboard
if(FAILED(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard)))
{
CleanUpDirectInput();
LogError("<li>Unable to set the keyboard data format.");
return FALSE;
}
else
LogInfo("<li>Set the keyboard data format OK.");
//Set the cooperative level for the keyboard
if(FAILED(m_pKeyboard->SetCooperativeLevel(hwnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE)))
{
CleanUpDirectInput();
LogError("<li>Unable to set the keyboard cooperative level.");
return FALSE;
}
else
LogInfo("<li>Set the keyboard cooperative level OK.");
//Acquire the keyboard
if(m_pKeyboard)
m_pKeyboard->Acquire();
//MOUSE******************Create the mouse device object
if(FAILED(m_pDirectInput->CreateDevice(GUID_SysMouse,&m_pMouse,NULL)))
{
CleanUpDirectInput();
LogError("<li>Unable to create DirectInput mouse device interface.");
return FALSE;
}
else
LogInfo("<li>DirectInput mouse device interface create OK.");
//Set the data format for the mouse
if(FAILED(m_pMouse->SetDataFormat(&c_dfDIMouse)))
{
CleanUpDirectInput();
LogError("<li>Unable to set the mouse data format.");
return FALSE;
}
else
LogInfo("<li>Set the mouse data format OK.");
//Set the cooperative level for the mouse
if(FAILED(m_pMouse->SetCooperativeLevel(hwnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE)))
{
CleanUpDirectInput();
LogError("<li>Unable to set the mouse cooperative level.");
return FALSE;
}
else
LogInfo("<li>Set the mouse cooperative level OK.");
//Acquire the mouse
if(m_pMouse)
m_pMouse->Acquire();
return TRUE;
}
void CGame::CleanUpDirectInput()
{
if(m_pKeyboard)
m_pKeyboard->Unacquire();
if(m_pMouse)
m_pMouse->Unacquire();
SafeRelease(m_pMouse);
SafeRelease(m_pKeyboard);
SafeRelease(m_pDirectInput);
LogInfo("<li>CleanUpDirectInput finished.");
}
void CGame::CleanUpDirectAudio()
{
//Stop all sounds.
m_pDirectAudioPerformance->Stop(NULL, NULL, 0, 0);
//CleanUp
m_pDirectAudioPerformance->CloseDown();
SafeRelease(m_pDirectAudioLoader);
SafeRelease(m_pDirectAudioPerformance);
LogInfo("<li>CleanUpDirectAudio finished.");
}
void CGame::CleanUpDirect3D()
{
SafeRelease(m_pD3DDevice);
SafeRelease(m_pD3D);
LogInfo("<li>CleanUpDirect3D finished.");
}
void CGame::CleanUpGame()
{
SafeDelete(m_pFont);
SafeDelete(m_pTableMesh);
SafeDelete(m_pCue);
int i;
for(i=0;i<SUMBALL;i++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -