home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 8 / Engine / RenderCache.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-03-29  |  3.9 KB  |  111 lines

  1. //-----------------------------------------------------------------------------
  2. // File: RenderCache.cpp
  3. //
  4. // Description: RenderCache.h implementation.
  5. //              Refer to the RenderCache.h interface for more details.
  6. //
  7. // Copyright (c) 2004 Vaughan Young
  8. //-----------------------------------------------------------------------------
  9. #include "Engine.h"
  10.  
  11. //-----------------------------------------------------------------------------
  12. // The render cache class constructor.
  13. //-----------------------------------------------------------------------------
  14. RenderCache::RenderCache( IDirect3DDevice9 *device, Material *material )
  15. {
  16.     m_device = device;
  17.  
  18.     m_material = material;
  19.  
  20.     m_indexBuffer = NULL;
  21.     m_totalIndices = 0;
  22. }
  23.  
  24. //-----------------------------------------------------------------------------
  25. // The render cache class destructor.
  26. //-----------------------------------------------------------------------------
  27. RenderCache::~RenderCache()
  28. {
  29.     SAFE_RELEASE( m_indexBuffer );
  30. }
  31.  
  32. //-----------------------------------------------------------------------------
  33. // Increases the size of the render cache to manage another face.
  34. //-----------------------------------------------------------------------------
  35. void RenderCache::AddFace()
  36. {
  37.     m_totalIndices += 3;
  38. }
  39.  
  40. //-----------------------------------------------------------------------------
  41. // Prepares the render cache for use. Must be called after setting the size.
  42. //-----------------------------------------------------------------------------
  43. void RenderCache::Prepare( unsigned long totalVertices )
  44. {
  45.     // Set the total vertices that are rendered.
  46.     m_totalVertices = totalVertices;
  47.  
  48.     // Create the render cache's index buffer.
  49.     m_device->CreateIndexBuffer( m_totalIndices * sizeof( unsigned short ), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_indexBuffer, NULL );
  50. }
  51.  
  52. //-----------------------------------------------------------------------------
  53. // Informs the render cache that rendering is about to begin.
  54. //-----------------------------------------------------------------------------
  55. void RenderCache::Begin()
  56. {
  57.     m_indexBuffer->Lock( 0, 0, (void**)&m_indexPointer, 0 );
  58.  
  59.     m_faces = 0;
  60. }
  61.  
  62. //-----------------------------------------------------------------------------
  63. // Adds the given face indicies to be rendered.
  64. //-----------------------------------------------------------------------------
  65. void RenderCache::RenderFace( unsigned short vertex0, unsigned short vertex1, unsigned short vertex2 )
  66. {
  67.     *m_indexPointer++ = vertex0;
  68.     *m_indexPointer++ = vertex1;
  69.     *m_indexPointer++ = vertex2;
  70.  
  71.     m_faces++;
  72. }
  73.  
  74. //-----------------------------------------------------------------------------
  75. // Informs the render cache that rendering is finished, so render the faces.
  76. //-----------------------------------------------------------------------------
  77. void RenderCache::End()
  78. {
  79.     // Unlock the index buffer.
  80.     m_indexBuffer->Unlock();
  81.  
  82.     // Check if there are any faces to render.
  83.     if( m_faces == 0 )
  84.         return;
  85.  
  86.     // Check if the material should ignore fog.
  87.     if( m_material->GetIgnoreFog() == true )
  88.         m_device->SetRenderState( D3DRS_FOGENABLE, false );
  89.  
  90.     // Set the material and texture.
  91.     m_device->SetMaterial( m_material->GetLighting() );
  92.     m_device->SetTexture( 0, m_material->GetTexture() );
  93.  
  94.     // Set the indicies to render the correct faces.
  95.     m_device->SetIndices( m_indexBuffer );
  96.  
  97.     // Render all the faces.
  98.     m_device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, m_totalVertices, 0, m_faces );
  99.  
  100.     // Restore the fog setting if it was changed.
  101.     if( m_material->GetIgnoreFog() == true )
  102.         m_device->SetRenderState( D3DRS_FOGENABLE, true );
  103. }
  104.  
  105. //-----------------------------------------------------------------------------
  106. // Returns a pointer to the material being used by this render cache.
  107. //-----------------------------------------------------------------------------
  108. Material *RenderCache::GetMaterial()
  109. {
  110.     return m_material;
  111. }