3DDrawManager.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:9k
源码类别:

游戏

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // File: 3DDrawManager.cpp
  3. //
  4. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  5. //-----------------------------------------------------------------------------
  6. #include "stdafx.h"
  7. //-----------------------------------------------------------------------------
  8. // Name: 
  9. // Desc: 
  10. //-----------------------------------------------------------------------------
  11. C3DDrawManager::C3DDrawManager(void)
  12. {
  13.     ZeroMemory( m_aPtList, sizeof(DRAW_POINT)*MAX_POINTS );
  14.     ZeroMemory( m_aLineList, sizeof(DRAW_LINE)*MAX_LINES );
  15.     m_dwNumLines = 0;
  16.     m_pSphere = NULL;
  17.     m_pLineVB = NULL;
  18. }
  19. //-----------------------------------------------------------------------------
  20. // Name: 
  21. // Desc: 
  22. //-----------------------------------------------------------------------------
  23. C3DDrawManager::~C3DDrawManager(void)
  24. {
  25.     FinalCleanup();
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Name: 
  29. // Desc: 
  30. //-----------------------------------------------------------------------------
  31. HRESULT C3DDrawManager::OneTimeSceneInit()
  32. {
  33.     return S_OK;
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Name: 
  37. // Desc: 
  38. //-----------------------------------------------------------------------------
  39. HRESULT C3DDrawManager::InitDeviceObjects()
  40. {
  41.     return S_OK;
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Name: 
  45. // Desc: 
  46. //-----------------------------------------------------------------------------
  47. HRESULT C3DDrawManager::RestoreDeviceObjects()
  48. {
  49.     D3DXCreateSphere( g_pd3dDevice, 1.0f, 8, 8, &m_pSphere, NULL );
  50.     HRESULT hr;
  51.     hr = g_pd3dDevice->CreateVertexBuffer( MAX_LINES * 2 * sizeof(DRAW_LINE_VERTEX),
  52.                                         D3DUSAGE_WRITEONLY,
  53.                                         DRAW_LINE_FVF, D3DPOOL_MANAGED,
  54.                                         &m_pLineVB, NULL );
  55.     if( FAILED(hr) )
  56.         return hr;
  57.     return S_OK;
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Name: 
  61. // Desc: 
  62. //-----------------------------------------------------------------------------
  63. HRESULT C3DDrawManager::FrameMove( const float fElapsedTime )
  64. {
  65.     int i;
  66.     for( i=0; i<MAX_POINTS; i++ )
  67.     {
  68.         if( m_aPtList[i].bInvalidateEveryFrame )
  69.             m_aPtList[i].bValid = false;
  70.     }
  71.     for( i=0; i<MAX_LINES; i++ )
  72.     {
  73.         if( m_aLineList[i].bInvalidateEveryFrame )
  74.             m_aLineList[i].bValid = false;
  75.     }
  76.     return S_OK;
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Name: 
  80. // Desc: 
  81. //-----------------------------------------------------------------------------
  82. HRESULT C3DDrawManager::Render()
  83. {
  84.     D3DXMATRIX matScale;
  85.     D3DXMATRIX matTranslate;
  86.     D3DXMATRIX mat;
  87.     DWORD i;
  88.     D3DMATERIAL9 mtrl;       
  89.     HRESULT hr;
  90.     if( !m_pLineVB )
  91.         return S_OK;
  92.     DRAW_LINE_VERTEX* pVertexBuffer = NULL;
  93.     m_dwNumLines = 0;
  94.     hr = m_pLineVB->Lock( 0, 0, (void**) &pVertexBuffer, 0 );
  95.     if( SUCCEEDED(hr) )
  96.     {
  97.         for( i=0; i<MAX_LINES; i++ )
  98.         {
  99.             if( m_aLineList[i].bValid )
  100.             {
  101.                 pVertexBuffer->p = m_aLineList[i].vPosFrom;
  102.                 pVertexBuffer->c = m_aLineList[i].dwColor;
  103.                 pVertexBuffer++;
  104.                 pVertexBuffer->p = m_aLineList[i].vPosTo;
  105.                 pVertexBuffer->c = m_aLineList[i].dwColor;
  106.                 pVertexBuffer++;
  107.                 m_dwNumLines++;
  108.             }
  109.         }
  110.         m_pLineVB->Unlock(); 
  111.     }
  112.     if( m_dwNumLines > 0 )
  113.     {
  114.         D3DXMatrixIdentity( &mat );
  115.         g_pd3dDevice->SetTransform( D3DTS_WORLD, &mat );
  116.         g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
  117.         g_pd3dDevice->SetTexture( 0, NULL );
  118.         ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
  119.         mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
  120.         mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
  121.         mtrl.Diffuse.b = mtrl.Ambient.b = 1.0f;
  122.         mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
  123.         g_pd3dDevice->SetMaterial( &mtrl );
  124.         g_pd3dDevice->SetFVF( DRAW_LINE_FVF );
  125.         g_pd3dDevice->SetStreamSource( 0, m_pLineVB, 0, sizeof(DRAW_LINE_VERTEX) );
  126.         g_pd3dDevice->DrawPrimitive( D3DPT_LINELIST, 0, m_dwNumLines );
  127.         g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
  128.     }
  129.     for( i=0; i<MAX_POINTS; i++ )
  130.     {
  131.         if( m_aPtList[i].bValid )
  132.         {
  133.             D3DXMatrixScaling( &matScale, m_aPtList[i].fSize, m_aPtList[i].fSize, m_aPtList[i].fSize );
  134.             D3DXMatrixTranslation( &matTranslate, m_aPtList[i].vPos.x, m_aPtList[i].vPos.y, m_aPtList[i].vPos.z );
  135.             D3DXMatrixMultiply( &mat, &matScale, &matTranslate );
  136.             float fR = ((m_aPtList[i].dwColor&0x00ff0000)>>16) / 255.0f;
  137.             float fG = ((m_aPtList[i].dwColor&0x0000ff00)>>8)  / 255.0f;
  138.             float fB = ((m_aPtList[i].dwColor&0x000000ff)>>0)  / 255.0f;
  139.             ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
  140.             mtrl.Diffuse.r = mtrl.Ambient.r = fR;
  141.             mtrl.Diffuse.g = mtrl.Ambient.g = fG;
  142.             mtrl.Diffuse.b = mtrl.Ambient.b = fB;
  143.             mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
  144.             g_pd3dDevice->SetTexture( 0, NULL );
  145.             g_pd3dDevice->SetMaterial( &mtrl );
  146.             g_pd3dDevice->SetTransform( D3DTS_WORLD, &mat );
  147.             m_pSphere->DrawSubset(0);
  148.         }
  149.     }
  150.     return S_OK;
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Name: 
  154. // Desc: 
  155. //-----------------------------------------------------------------------------
  156. HRESULT C3DDrawManager::InvalidateDeviceObjects()
  157. {
  158.     SAFE_RELEASE( m_pSphere );
  159.     SAFE_RELEASE( m_pLineVB );
  160.     return S_OK;
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Name: 
  164. // Desc: 
  165. //-----------------------------------------------------------------------------
  166. HRESULT C3DDrawManager::DeleteDeviceObjects()
  167. {
  168.     return S_OK;
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Name: 
  172. // Desc: 
  173. //-----------------------------------------------------------------------------
  174. VOID C3DDrawManager::FinalCleanup()
  175. {
  176. }
  177. //-----------------------------------------------------------------------------
  178. // Name: 
  179. // Desc: 
  180. //-----------------------------------------------------------------------------
  181. HRESULT C3DDrawManager::AddPoint( D3DXVECTOR3 vPos, float fSize, DWORD dwColor, DWORD dwID, bool bInvalidateEveryFrame )
  182. {
  183.     int iChoose;
  184.     for( iChoose=0; iChoose<MAX_POINTS; iChoose++ )
  185.     {
  186.         if( !m_aPtList[iChoose].bValid )
  187.             break;
  188.     }
  189.     if( iChoose < MAX_POINTS )
  190.     {
  191.         m_aPtList[iChoose].vPos    = vPos;
  192.         m_aPtList[iChoose].fSize   = fSize;
  193.         m_aPtList[iChoose].dwColor = dwColor;
  194.         m_aPtList[iChoose].dwID    = dwID;
  195.         m_aPtList[iChoose].bInvalidateEveryFrame = bInvalidateEveryFrame;
  196.         m_aPtList[iChoose].bValid  = true;
  197.     }
  198.     return S_OK;
  199. }
  200. //-----------------------------------------------------------------------------
  201. // Name: 
  202. // Desc: 
  203. //-----------------------------------------------------------------------------
  204. HRESULT C3DDrawManager::InvalidatePoints( DWORD dwID )
  205. {
  206.     for( int i=0; i<MAX_POINTS; i++ )
  207.     {
  208.         if( m_aPtList[i].dwID == dwID )
  209.             m_aPtList[i].bValid = false;
  210.     }
  211.     return S_OK;
  212. }
  213. //-----------------------------------------------------------------------------
  214. // Name: 
  215. // Desc: 
  216. //-----------------------------------------------------------------------------
  217. HRESULT C3DDrawManager::InvalidateLines( DWORD dwID )
  218. {
  219.     for( int i=0; i<MAX_LINES; i++ )
  220.     {
  221.         if( m_aLineList[i].dwID == dwID )
  222.             m_aLineList[i].bValid = false;
  223.     }
  224.     return S_OK;
  225. }
  226. //-----------------------------------------------------------------------------
  227. // Name: 
  228. // Desc: 
  229. //-----------------------------------------------------------------------------
  230. HRESULT C3DDrawManager::AddLine( D3DXVECTOR3 vPosFrom, D3DXVECTOR3 vPosTo, DWORD dwColor, DWORD dwID, bool bInvalidateEveryFrame )
  231. {
  232.     int iChoose;
  233.     for( iChoose=0; iChoose<MAX_LINES; iChoose++ )
  234.     {
  235.         if( !m_aLineList[iChoose].bValid )
  236.             break;
  237.     }
  238.     if( iChoose < MAX_POINTS )
  239.     {
  240.         m_aLineList[iChoose].vPosFrom   = vPosFrom;
  241.         m_aLineList[iChoose].vPosTo     = vPosTo;
  242.         m_aLineList[iChoose].dwColor    = dwColor;
  243.         m_aLineList[iChoose].dwID       = dwID;
  244.         m_aLineList[iChoose].bInvalidateEveryFrame = bInvalidateEveryFrame;
  245.         m_aLineList[iChoose].bValid     = true;
  246.     }
  247.     return S_OK;
  248. }