📄 game_panel.cpp
字号:
#include "Game_User.h"
//////////////////////////////////////////////////////////////////////
// panel.cpp: implementation for the panel class.
//
//////////////////////////////////////////////////////////////////////
//构造
//******************************************************************************************************************
CPanel::CPanel(LPDIRECT3DDEVICE9 pD3DDevice, float nWidth, float nHeight, int nScreenWidth, int nScreenHeight, DWORD dwColour, DWORD dwType)
{
m_pD3DDevice = pD3DDevice;
m_pVertexBuffer = NULL;
m_pTexture = NULL;
m_fDepth = 1.0f;
m_fRate = 1.0f;
m_bPick = false;
//m_nX = nScreenWidth;
//m_nY = nScreenHeight;
m_nX = 0;
m_nY = 0;
m_fAngle = 0.0f;
//面板大小
m_nWidth = nWidth;
m_nHeight = nHeight;
//屏幕大小
m_nScreenWidth = nScreenWidth;
m_nScreenHeight = nScreenHeight;
//顶点色
m_dwColour = dwColour;
//类型
m_dwType = dwType;
//初始化顶点缓冲
if(CreateVertexBuffer())
{
if(UpdateVertices())
{
return;
}
}
}
//******************************************************************************************************************
//创建顶点缓冲
//******************************************************************************************************************
bool CPanel::CreateVertexBuffer()
{
if(FAILED(m_pD3DDevice->CreateVertexBuffer(4 * sizeof(PANEL_CUSTOMVERTEX),
0, PANEL_D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &m_pVertexBuffer,NULL)))
{
return false;
}
return true;
}
//******************************************************************************************************************
//刷新顶点
//******************************************************************************************************************
bool CPanel::UpdateVertices(float fBUV,float fAUV)
{
PANEL_CUSTOMVERTEX* pVertices = NULL;
m_pVertexBuffer->Lock(0, 4 * sizeof(PANEL_CUSTOMVERTEX), (void**)&pVertices, 0);
if(m_dwColour == -1)
{
//设置颜色
m_dwColour = D3DCOLOR_XRGB(255, 255, 255);
}
//把所有的顶点初始化颜色
pVertices[0].colour = m_dwColour;
pVertices[1].colour = m_dwColour;
pVertices[2].colour = m_dwColour;
pVertices[3].colour = m_dwColour;
//设置位置
switch(m_dwType)
{
case 0:
pVertices[0].x = m_nX -(m_nWidth) / 2.0f;
pVertices[0].y = m_nY -(m_nHeight) / 2.0f;
pVertices[1].x = m_nX -(m_nWidth) / 2.0f;
pVertices[1].y = m_nY + m_nHeight / 2.0f;
pVertices[2].x = m_nX + (m_nWidth) / 2.0f * m_fRate;
pVertices[2].y = m_nY - (m_nHeight) / 2.0f;
pVertices[3].x = m_nX + (m_nWidth) / 2.0f * m_fRate;
pVertices[3].y = m_nY + m_nHeight / 2.0f;
break;
case 1:
pVertices[0].x = 0;
pVertices[0].y = 0;
pVertices[1].x = 0;
pVertices[1].y = (float)m_nHeight;
pVertices[2].x = (m_nWidth) * m_fRate;
pVertices[2].y = 0;
pVertices[3].x = (m_nWidth)* m_fRate;
pVertices[3].y = (float)m_nHeight;
break;
}
pVertices[0].z = m_fDepth;
pVertices[1].z = m_fDepth;
pVertices[2].z = m_fDepth;
pVertices[3].z = m_fDepth;
//设置纹理左边
pVertices[0].u = fAUV;
pVertices[0].v = 1.0f;
pVertices[1].u = fAUV;
pVertices[1].v = 0.0f;
pVertices[2].u = fBUV;
pVertices[2].v = 1.0f;
pVertices[3].u = fBUV;
pVertices[3].v = 0.0f;
m_pVertexBuffer->Unlock();
return true;
}
//bool CPanel::UpdateVertices()
//{
// PANEL_CUSTOMVERTEX* pVertices = NULL;
// m_pVertexBuffer->Lock(0, 4 * sizeof(PANEL_CUSTOMVERTEX), (void**)&pVertices, 0);
//
//
//
// //设置位置
// pVertices[0].x =m_nX;
// pVertices[0].y = m_nY+m_nHeight;
//
// pVertices[1].x = m_nX;
// pVertices[1].y = m_nY;
//
// pVertices[2].x = m_nX+m_nWidth;
// pVertices[2].y = m_nY;
//
// pVertices[3].x = m_nX+m_nWidth;
// pVertices[3].y = m_nY+m_nHeight;
//
// pVertices[0].z = 1.0f;
// pVertices[1].z = 1.0f;
// pVertices[2].z = 1.0f;
// pVertices[3].z = 1.0f;
//
// pVertices[0].colour =m_dwColour;
// pVertices[1].colour =m_dwColour;
// pVertices[2].colour =m_dwColour;
// pVertices[3].colour =m_dwColour;
//
//
// //设置纹理左边
// pVertices[0].u = 0.0f;
// pVertices[0].v = 1.0f;
//
// pVertices[1].u = 0.0f;
// pVertices[1].v = 0.0f;
//
// pVertices[2].u = 1.0f;
// pVertices[2].v = 0.0f;
//
// pVertices[3].u = 1.0f;
// pVertices[3].v = 1.0f;
//
// m_pVertexBuffer->Unlock();
//
// return true;
//}
void CPanel::SetPro(float x,float y)
{
m_nX=x;
m_nY=y;
this->UpdateVertices();
}
//******************************************************************************************************************
//载入纹理
//******************************************************************************************************************
bool CPanel::SetTexture(const char *szTextureFilePath, DWORD dwKeyColour)
{
if(S_OK == (D3DXCreateTextureFromFileEx(m_pD3DDevice, szTextureFilePath, 0, 0, 0, 0,
D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
D3DX_DEFAULT, dwKeyColour, NULL, NULL, &m_pTexture)))
{
return true;
}
return false;
}
//******************************************************************************************************************
//渲染
//******************************************************************************************************************
bool CPanel::Render()
{
m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(PANEL_CUSTOMVERTEX));
m_pD3DDevice->SetFVF(PANEL_D3DFVF_CUSTOMVERTEX);
if(m_pTexture != NULL)
{
m_pD3DDevice->SetTexture(0, m_pTexture);
}
else
{
m_pD3DDevice->SetTexture(0, NULL);
}
//设置R混合和测试
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, 0x08 );
m_pD3DDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );
//画图形
m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
//关闭R混合和测试
//m_pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
//m_pD3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, FALSE);
return true;
}
//******************************************************************************************************************
//移动二维窗口的位置
//******************************************************************************************************************
void CPanel::MoveTo(float x, float y)
{
//移动二维窗口的位置
static D3DXMATRIX matMove,matRota;
x -= (m_nScreenWidth / 2) - (m_nWidth / 2);
y -= (m_nScreenHeight / 2) - (m_nHeight / 2);
D3DXMatrixTranslation(&matMove, (float)x, -(float)y, 0.0f);
D3DXMatrixRotationZ(&matRota, m_fAngle);
if(m_fAngle != 0.0f)
D3DXMatrixMultiply(&matMove, &matRota, &matMove);
m_pD3DDevice->SetTransform(D3DTS_WORLD, &matMove);
}
void CPanel::MoveToMouse(float x, float y)
{
D3DXMATRIX matMove;
m_nX += x;
m_nY +=y;
this->UpdateVertices();
}
//******************************************************************************************************************
//判断点是否在其上
//******************************************************************************************************************
bool CPanel::IsPointInsidePanel(float x, float y)
{
//Is the coordinate passed in inside (over) this panel
if((x >= m_nX) && (x < (m_nX + m_nWidth - 1)) && (y >= m_nY) && (y < (m_nY + m_nHeight - 1)))
{
return true;
}
else
{
return false;
}
}
//bool CPanel::IsPointInsidePanel(float x, float y)
//{
// //Is the coordinate passed in inside (over) this panel
// if((x >= m_nX-m_nWidth/2) && (x < (m_nX + m_nWidth/2 - 1))
// && (y >= m_nScreenHeight-m_nY-m_nHeight/2)
// && (y < (m_nScreenHeight-m_nY + m_nHeight/2 - 1)))
// {
// return true;
// }
// else
// {
// return false;
// }
//}
//******************************************************************************************************************
//设置尺寸
//******************************************************************************************************************
//void CPanel::setSize(float nWidth, float nHeight, float fBUV, float fAUV)
//{
// m_nWidth = nWidth;
// m_nHeight = nHeight;
// UpdateVertices(fBUV,fAUV);
//}
void CPanel::setSize( float nWidth, float nHeight)
{
m_nWidth = nWidth;
m_nHeight = nHeight;
UpdateVertices();
}
//******************************************************************************************************************
//析构函数
//******************************************************************************************************************
CPanel::~CPanel()
{
SAFE_RELEASE(m_pTexture);
SAFE_RELEASE(m_pVertexBuffer);
SAFE_RELEASE(m_pD3DDevice);
}
void CPanel::SetRoleHP(float hp)
{
MoveTo(78, 57);
SetWidth(hp);
}
void CPanel::SetRoleMP(float mp)
{
MoveTo(78, 72);
SetWidth(mp);
}
void CPanel::SetEnemyHP(float hp)
{
MoveTo(455, 33);
SetWidth(hp);
}
void CPanel::SetEnemyMP(float mp)
{
MoveTo(455, 46);
SetWidth(mp);
}
void CPanel::Button1(CSkinMesh* RoleMesh)
{
RoleMesh->SetAnimationName("skill2");
RoleMesh->m_CURMP-=5;
}
void CPanel::Button2(CSkinMesh* RoleMesh)
{
RoleMesh->SetAnimationName("skil1");
RoleMesh->m_CURMP-=5;
}
void CPanel::Button3(CSkinMesh* RoleMesh)
{
RoleMesh->m_CURHP+=30;
}
void CPanel::Button4(CSkinMesh* RoleMesh)
{
RoleMesh->m_CURMP+=30;
}
void CPanel::Button5(CSkinMesh* RoleMesh)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -