Panel.cpp
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:8k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "Panel.h"
  3. #include "Game.h"
  4. CPanel::CPanel(LPDIRECT3DDEVICE9 d3dd,int nWidth,int nHeight,int nScreenWidth,int nScreenHeight)
  5. {
  6. m_pD3DDevice=d3dd;
  7. m_pBuffer=NULL;
  8. m_pTexture=NULL;
  9. m_nWidth=nWidth;
  10. m_nHeight=nHeight;
  11. m_nScreenWidth=nScreenWidth;
  12. m_nScreenHeight=nScreenHeight;
  13. m_Rect.left=0.0f;
  14. m_Rect.top=0.0f;
  15.     NIcon=3;
  16. IconVer=NIcon*4;
  17.     TxOfIcon=NIcon*2;
  18. NumOfPos=new POSS[NIcon];
  19. RECT rect0;rect0.left=250; rect0.top=350; rect0.right=300; rect0.bottom=100;//退出面板
  20. RECT rect1={rect0.left+10,rect0.top-60,63,26,};//“是”按钮
  21. RECT rect2={rect0.left+227,rect0.top-60,63,26,};//“否”按钮
  22. //RECT rect1;rect1.left=0; rect1.top=0; rect1.right=63; rect1.bottom=26;
  23. RECT rect3={0.0f,100.0f,800.0f,100.0f};
  24. RolePanel=Calculate(rect3);
  25. NumOfPos[0]=Calculate(rect0);
  26.     NumOfPos[1]=Calculate(rect1);
  27. NumOfPos[2]=Calculate(rect2);
  28. m_ArrayOfTx=new LPDIRECT3DTEXTURE9[TxOfIcon];
  29.     Result=new bool[NIcon];
  30. ButtonPress=new bool[NIcon];//false表示没被按下,true表示已按下
  31. CallPress=new bool[NIcon];//false表示按钮没被调用,true表示被调用
  32. for(int i=0;i<NIcon;i++)
  33. {
  34. ButtonPress[i]=false;
  35. CallPress[i]=false;
  36. Result[i]=false;
  37. }
  38.     SetVertexBuffer();
  39. UpdateVertex();
  40.     SetTexture();
  41. }
  42. CPanel::~CPanel()
  43. {
  44. m_pBuffer->Release();
  45. m_pTexture->Release();
  46. m_pTexture_1->Release();
  47.     m_pIconBuffer->Release();
  48. for(int i=0;i<TxOfIcon;i++)
  49. {
  50. m_ArrayOfTx[i]->Release();
  51. }
  52. DeleteObject(m_ArrayOfTx);
  53. }
  54. void CPanel::SetVertexBuffer()
  55. {
  56. m_pD3DDevice->CreateVertexBuffer(8*sizeof(VERTEX),0,PANEL_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pBuffer,NULL);
  57. m_pD3DDevice->CreateVertexBuffer(IconVer*sizeof(VERTEX),0,PANEL_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pIconBuffer,NULL);
  58. }
  59. void CPanel::UpdateVertex()
  60. {
  61. VERTEX * pVertex;
  62. m_pBuffer->Lock(0,8*sizeof(VERTEX),(void **)&pVertex,0);
  63. pVertex[0].x=300;
  64. pVertex[0].y=580;
  65. pVertex[0].z=0.0f;
  66. pVertex[1].x=500;
  67. pVertex[1].y=580;
  68. pVertex[1].z=0.0f;
  69. pVertex[2].x=300;
  70. pVertex[2].y=520;
  71. pVertex[2].z=0.0f;
  72.     pVertex[3].x=500;
  73. pVertex[3].y=520;
  74. pVertex[3].z=0.0f;
  75. pVertex[4].x=RolePanel.ltx;
  76. pVertex[4].y=RolePanel.lty;
  77. pVertex[4].z=0.0f;
  78. pVertex[5].x=RolePanel.rtx;
  79. pVertex[5].y=RolePanel.rty;
  80. pVertex[5].z=0.0f;
  81. pVertex[6].x=RolePanel.lbx;
  82. pVertex[6].y=RolePanel.lby;
  83. pVertex[6].z=0.0f;
  84. pVertex[7].x=RolePanel.rbx;
  85. pVertex[7].y=RolePanel.lby;
  86. pVertex[7].z=0.0f;
  87. pVertex[0].tu=0.0f;
  88. pVertex[0].tv=0.0f;
  89. pVertex[1].tu=1.0f;
  90. pVertex[1].tv=0.0f;
  91. pVertex[2].tu=0.0f;
  92. pVertex[2].tv=1.0f;
  93. pVertex[3].tu=1.0f;
  94. pVertex[3].tv=1.0f;
  95. pVertex[4].tu=0.0f;
  96. pVertex[4].tv=0.0f;
  97.           
  98.     pVertex[5].tu=1.0f;
  99. pVertex[5].tv=0.0f;
  100.   
  101.         
  102. pVertex[6].tu=0.0f;
  103. pVertex[6].tv=1.0f;
  104.      
  105.        
  106. pVertex[7].tu=1.0f;
  107. pVertex[7].tv=1.0f;
  108.      
  109. m_pBuffer->Unlock();
  110.     pVertex=NULL;
  111. m_pIconBuffer->Lock(0,IconVer*sizeof(VERTEX),(void **)&pVertex,0);
  112. int k=0; 
  113. for(int i=0;i<NIcon;i++)
  114. {
  115. pVertex[k].x=NumOfPos[i].ltx;
  116. pVertex[k].y=NumOfPos[i].lty;
  117. pVertex[k].z=0;
  118.             
  119. pVertex[k+1].x=NumOfPos[i].rtx;
  120. pVertex[k+1].y=NumOfPos[i].rty;
  121.         pVertex[k+1].z=0.0f;
  122. pVertex[k+2].x=NumOfPos[i].lbx;
  123. pVertex[k+2].y=NumOfPos[i].lby;
  124.         pVertex[k+2].z=0.0f;
  125.             pVertex[k+3].x=NumOfPos[i].rbx;
  126. pVertex[k+3].y=NumOfPos[i].rby;
  127.         pVertex[k+3].z=0.0f;
  128.      
  129.    
  130.         pVertex[k+0].tu=0.0f;
  131.         pVertex[k+0].tv=0.0f;
  132.             pVertex[k+0].tu1=0.0f;
  133.         pVertex[k+0].tv1=0.0f;
  134.         
  135. pVertex[k+1].tu=1.0f;
  136.         pVertex[k+1].tv=0.0f;
  137.             pVertex[k+1].tu1=1.0f;
  138.         pVertex[k+1].tv1=0.0f;
  139.         
  140. pVertex[k+2].tu=0.0f;
  141.         pVertex[k+2].tv=1.0f;
  142.             pVertex[k+2].tu1=0.0f;
  143.         pVertex[k+2].tv1=1.0f;
  144.        
  145. pVertex[k+3].tu=1.0f;
  146.         pVertex[k+3].tv=1.0f;
  147.             pVertex[k+3].tu1=1.0f;
  148.         pVertex[k+3].tv1=1.0f;
  149.               k+=4;
  150. }
  151. m_pIconBuffer->Unlock();
  152. }
  153. HRESULT CPanel::SetTexture()
  154. {
  155. if(FAILED(D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/bar_2.tga", 0, 0, 0, 0,
  156.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  157.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTexture)))
  158. {
  159. E_FAIL;
  160. }
  161.     D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/bar_1.tga", 0, 0, 0, 0,
  162.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  163.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTexture_1);
  164.     D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/exit.jpg", 0, 0, 0, 0,
  165.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  166.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[0]);//退出面板
  167. D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/exit.jpg", 0, 0, 0, 0,
  168.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  169.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[1]);//退出面板
  170.     D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/exit_buttonY_up.jpg", 0, 0, 0, 0,
  171.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  172.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[2]);//退出面板,是按钮弹起
  173.     D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/exit_buttonY_down.jpg", 0, 0, 0, 0,
  174.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  175.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[3]);//退出面板,是按钮按下
  176.     D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/exit_buttonN_up.jpg", 0, 0, 0, 0,
  177.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  178.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[4]);//退出面板,否按钮弹起
  179.     D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/exit_buttonN_down.jpg", 0, 0, 0, 0,
  180.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  181.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[5]);//退出面板,否按钮按下
  182. return S_OK;
  183. }
  184. void CPanel::Translate(int x,int y)
  185. {
  186. D3DXMATRIX matMove;
  187. D3DXMatrixTranslation(&matMove, (float)x, -(float)y, 0.0f);
  188. m_pD3DDevice->SetTransform(D3DTS_WORLD, &matMove);
  189. }
  190. void CPanel::RenderPanel()
  191. {
  192.     
  193. m_pD3DDevice->SetStreamSource(0,m_pIconBuffer,0,sizeof(VERTEX));
  194. m_pD3DDevice->SetFVF(PANEL_D3DFVF_VERTEXBUFFER);
  195. int k=0;int j=0;
  196. for(int i=0; i<NIcon;i++)
  197. {
  198. if(CallPress[i]==true)
  199. {
  200. if(ButtonPress[i]==false)
  201. {
  202.      m_pD3DDevice->SetTexture(0,m_ArrayOfTx[k]);
  203. //m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  204. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
  205. //m_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_DISABLE); 
  206. m_pD3DDevice->SetTexture(1,m_ArrayOfTx[k+1]);//m_ArrayOfTx[k+1]
  207. //m_pD3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
  208. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1,D3DTA_CURRENT);
  209. //m_pD3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_DISABLE); 
  210. }
  211. else
  212. {
  213. m_pD3DDevice->SetTexture(0,m_ArrayOfTx[k+1]);//m_ArrayOfTx[k+1]
  214. //m_pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
  215. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
  216. //m_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_DISABLE); 
  217.         m_pD3DDevice->SetTexture(1,m_ArrayOfTx[k]);
  218. //m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  219. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1,D3DTA_CURRENT);
  220. //m_pD3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_DISABLE); 
  221.         
  222. }
  223.      m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,j,2);
  224. }
  225. k+=2;
  226. j+=4;
  227. }
  228.     m_pD3DDevice->SetStreamSource(0,m_pBuffer,0,sizeof(VERTEX));
  229. m_pD3DDevice->SetFVF(PANEL_D3DFVF_VERTEXBUFFER);
  230. m_pD3DDevice->SetTexture(0,m_pTexture_1);
  231. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,4,2);
  232. }
  233. POSS CPanel::Calculate(RECT rect)
  234. {
  235. POSS a;
  236. a.ltx=rect.left; a.lty=rect.top; a.rtx=rect.left+rect.right; a.rty=rect.top;
  237. a.lbx=rect.left; a.lby=rect.top-rect.bottom; a.rbx=rect.left+rect.right; a.rby=rect.top-rect.bottom;
  238. return a;
  239. }
  240. void CPanel::Render()
  241. {
  242.     m_pD3DDevice->SetStreamSource(0,m_pBuffer,0,sizeof(VERTEX));
  243. m_pD3DDevice->SetFVF(PANEL_D3DFVF_VERTEXBUFFER);
  244. m_pD3DDevice->SetTexture(0,m_pTexture);
  245. //m_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_MODULATE);
  246. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
  247. }