0001: //-----------------------------------------------------------------------------
0002: // File: D3DFile.h
0003: //
0004: // Desc: Support code for loading DirectX .X files.
0005: //-----------------------------------------------------------------------------
0006: #ifndef D3DFILE_H
0007: #define D3DFILE_H
0008: #include <tchar.h>
0009: #include <d3d9.h>
0010: #include <d3dx9.h>
0011: 
0012: 
0013: 
0014: 
0015: 
0016: //-----------------------------------------------------------------------------
0017: // Name: class CD3DMesh
0018: // Desc: Class for loading and rendering file-based meshes
0019: //-----------------------------------------------------------------------------
0020: class CD3DMesh
0021: {
0022: public:
0023:     TCHAR               m_strName[512];
0024: 
0025:     LPD3DXMESH          m_pSysMemMesh;    // SysMem mesh, lives through resize
0026:     LPD3DXMESH          m_pLocalMesh;     // Local mesh, rebuilt on resize
0027:     
0028:     DWORD               m_dwNumMaterials; // Materials for the mesh
0029:     D3DMATERIAL9*       m_pMaterials;
0030:     LPDIRECT3DTEXTURE9* m_pTextures;
0031:     bool                m_bUseMaterials;
0032: 
0033: public:
0034:     // Rendering
0035:     HRESULT Render( LPDIRECT3DDEVICE9 pd3dDevice, 
0036:                     bool bDrawOpaqueSubsets = true,
0037:                     bool bDrawAlphaSubsets = true );
0038: 
0039:     // Mesh access
0040:     LPD3DXMESH GetSysMemMesh() { return m_pSysMemMesh; }
0041:     LPD3DXMESH GetLocalMesh()  { return m_pLocalMesh; }
0042: 
0043:     // Rendering options
0044:     void    UseMeshMaterials( bool bFlag ) { m_bUseMaterials = bFlag; }
0045:     HRESULT SetFVF( LPDIRECT3DDEVICE9 pd3dDevice, DWORD dwFVF );
0046: 
0047:     // Initializing
0048:     HRESULT RestoreDeviceObjects( LPDIRECT3DDEVICE9 pd3dDevice );
0049:     HRESULT InvalidateDeviceObjects();
0050: 
0051:     // Creation/destruction
0052:     HRESULT Create( LPDIRECT3DDEVICE9 pd3dDevice, TCHAR* strFilename );
0053:     HRESULT Create( LPDIRECT3DDEVICE9 pd3dDevice, LPDIRECTXFILEDATA pFileData );
0054:     HRESULT Destroy();
0055: 
0056:     CD3DMesh( TCHAR* strName = _T("CD3DFile_Mesh") );
0057:     virtual ~CD3DMesh();
0058: };
0059: 
0060: 
0061: 
0062: 
0063: //-----------------------------------------------------------------------------
0064: // Name: class CD3DFrame
0065: // Desc: Class for loading and rendering file-based meshes
0066: //-----------------------------------------------------------------------------
0067: class CD3DFrame
0068: {
0069: public:
0070:     TCHAR      m_strName[512];
0071:     D3DXMATRIX m_mat;
0072:     CD3DMesh*  m_pMesh;
0073: 
0074:     CD3DFrame* m_pNext;
0075:     CD3DFrame* m_pChild;
0076: 
0077: public:
0078:     // Matrix access
0079:     void        SetMatrix( D3DXMATRIX* pmat ) { m_mat = *pmat; }
0080:     D3DXMATRIX* GetMatrix()                   { return &m_mat; }
0081: 
0082:     CD3DMesh*   FindMesh( TCHAR* strMeshName );
0083:     CD3DFrame*  FindFrame( TCHAR* strFrameName );
0084:     bool        EnumMeshes( bool (*EnumMeshCB)(CD3DMesh*,void*), 
0085:                             void* pContext );
0086: 
0087:     HRESULT Destroy();
0088:     HRESULT RestoreDeviceObjects( LPDIRECT3DDEVICE9 pd3dDevice );
0089:     HRESULT InvalidateDeviceObjects();
0090:     HRESULT Render( LPDIRECT3DDEVICE9 pd3dDevice, 
0091:                     bool bDrawOpaqueSubsets = true,
0092:                     bool bDrawAlphaSubsets = true,
0093:                     D3DXMATRIX* pmatWorldMartix = NULL);
0094:     
0095:     CD3DFrame( TCHAR* strName = _T("CD3DFile_Frame") );
0096:     virtual ~CD3DFrame();
0097: };
0098: 
0099: 
0100: 
0101: 
0102: //-----------------------------------------------------------------------------
0103: // Name: class CD3DFile
0104: // Desc: Class for loading and rendering file-based meshes
0105: //-----------------------------------------------------------------------------
0106: class CD3DFile : public CD3DFrame
0107: {
0108:     HRESULT LoadMesh( LPDIRECT3DDEVICE9 pd3dDevice, LPDIRECTXFILEDATA pFileData, 
0109:                       CD3DFrame* pParentFrame );
0110:     HRESULT LoadFrame( LPDIRECT3DDEVICE9 pd3dDevice, LPDIRECTXFILEDATA pFileData, 
0111:                        CD3DFrame* pParentFrame );
0112: public:
0113:     HRESULT Create( LPDIRECT3DDEVICE9 pd3dDevice, TCHAR* strFilename );
0114:     HRESULT CreateFromResource( LPDIRECT3DDEVICE9 pd3dDevice, TCHAR* strResource, TCHAR* strType );
0115:     // For pure devices, specify the world transform. If the world transform is not
0116:     // specified on pure devices, this function will fail.
0117:     HRESULT Render( LPDIRECT3DDEVICE9 pd3dDevice, D3DXMATRIX* pmatWorldMatrix = NULL );
0118: 
0119:     CD3DFile() : CD3DFrame( _T("CD3DFile_Root") ) {}
0120: };
0121: 
0122: 
0123: 
0124: #endif
0125: 
0126: 
0127: 
0128: