Panel.cpp
上传用户:whgydz
上传日期:2007-01-12
资源大小:2259k
文件大小:4k
源码类别:

其他书籍

开发平台:

HTML/CSS

  1. // Panel.cpp: implementation of the CPanel class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "Panel.h"
  5. //////////////////////////////////////////////////////////////////////
  6. // Construction/Destruction
  7. //////////////////////////////////////////////////////////////////////
  8. CPanel::CPanel(LPDIRECT3DDEVICE8 pD3DDevice, int nWidth, int nHeight, int nScreenWidth, int nScreenHeight, DWORD dwColour)
  9. {
  10. m_pD3DDevice = pD3DDevice;
  11. m_pVertexBuffer = NULL;
  12. m_pTexture = NULL;
  13. m_nWidth = nWidth;
  14. m_nHeight = nHeight;
  15. m_nScreenWidth = nScreenWidth;
  16. m_nScreenHeight = nScreenHeight;
  17. m_dwColour = dwColour;
  18. //Initialize Vertex Buffer
  19.     if(CreateVertexBuffer())
  20. {
  21. if(UpdateVertices())
  22. {
  23. LogInfo("<li>Panel created OK");
  24. return;
  25. }
  26. }
  27. LogError("<li>Panel failed to create");
  28. }
  29. CPanel::~CPanel()
  30. {
  31. SafeRelease(m_pTexture);
  32. SafeRelease(m_pVertexBuffer);
  33. LogInfo("<li>Panel destroyed OK");
  34. }
  35. bool CPanel::CreateVertexBuffer()
  36. {
  37. //Create the vertex buffer from our device.
  38.     if(FAILED(m_pD3DDevice->CreateVertexBuffer(4 * sizeof(PANEL_CUSTOMVERTEX),
  39.                                                0, PANEL_D3DFVF_CUSTOMVERTEX,
  40.                                                D3DPOOL_DEFAULT, &m_pVertexBuffer)))
  41.     {
  42.         LogError("<li>CPanel: Unable to create vertex buffer.");
  43. return false;
  44.     }
  45.     return true;
  46. }
  47. bool CPanel::UpdateVertices()
  48. {
  49. PANEL_CUSTOMVERTEX* pVertices = NULL;
  50. m_pVertexBuffer->Lock(0, 4 * sizeof(PANEL_CUSTOMVERTEX), (BYTE**)&pVertices, 0);
  51. if(m_dwColour == -1)
  52. {
  53. //No colour was set, so default to white
  54. m_dwColour = D3DCOLOR_XRGB(255, 255, 255);
  55. }
  56. //Set all the vertices to selected colour
  57. pVertices[0].colour = m_dwColour;
  58. pVertices[1].colour = m_dwColour;
  59. pVertices[2].colour = m_dwColour;
  60. pVertices[3].colour = m_dwColour;
  61. //Set the positions of the vertices
  62. pVertices[0].x = -(m_nWidth) / 2.0f;
  63. pVertices[0].y = -(m_nHeight) / 2.0f;
  64. pVertices[1].x = -(m_nWidth) / 2.0f;
  65. pVertices[1].y = m_nHeight / 2.0f;
  66. pVertices[2].x = (m_nWidth) / 2.0f;
  67. pVertices[2].y = -(m_nHeight) / 2.0f;
  68. pVertices[3].x = (m_nWidth) / 2.0f;
  69. pVertices[3].y = m_nHeight / 2.0f;
  70. pVertices[0].z = 1.0f;
  71. pVertices[1].z = 1.0f;
  72. pVertices[2].z = 1.0f; 
  73. pVertices[3].z = 1.0f;
  74. //Set the texture coordinates of the vertices
  75. pVertices[0].u = 0.0f;
  76. pVertices[0].v = 1.0f;
  77. pVertices[1].u = 0.0f;
  78. pVertices[1].v = 0.0f;
  79. pVertices[2].u = 1.0f;
  80. pVertices[2].v = 1.0f;
  81. pVertices[3].u = 1.0f;
  82. pVertices[3].v = 0.0f;
  83. m_pVertexBuffer->Unlock();
  84. return true;
  85. }
  86. bool CPanel::SetTexture(const char *szTextureFilePath, DWORD dwKeyColour)
  87. {
  88. if(FAILED(D3DXCreateTextureFromFileEx(m_pD3DDevice, szTextureFilePath, 0, 0, 0, 0,
  89.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  90.   D3DX_DEFAULT, dwKeyColour, NULL, NULL, &m_pTexture)))
  91. {
  92. return false;
  93. }
  94. return true;
  95. }
  96. DWORD CPanel::Render()
  97. {
  98. m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, sizeof(PANEL_CUSTOMVERTEX));
  99. m_pD3DDevice->SetVertexShader(PANEL_D3DFVF_CUSTOMVERTEX);
  100. if(m_pTexture != NULL)
  101. {
  102. m_pD3DDevice->SetTexture(0, m_pTexture);
  103. m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  104. }
  105. else
  106. {
  107. m_pD3DDevice->SetTexture(0, NULL);
  108. }
  109. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
  110. return 2; //Return the number of polygons rendered
  111. }
  112. void CPanel::MoveTo(int x, int y)
  113. {
  114. //x and y specify the top left corner of the panel in screen coordinates
  115. D3DXMATRIX matMove;
  116. x -= (m_nScreenWidth / 2) - (m_nWidth / 2);
  117. y -= (m_nScreenHeight / 2) - (m_nHeight / 2);
  118. D3DXMatrixTranslation(&matMove, (float)x, -(float)y, 0.0f);
  119. m_pD3DDevice->SetTransform(D3DTS_WORLD, &matMove);
  120. }