11#include " TMaterialLibrary.h"
22#include " TRender/TRenderInterface.h"
3+ #include " TKernel/TMemory.h"
4+ #include " TSpriteShader/Include/TSpriteShader.h"
35
46// -----------------------------------------------------------------------------
57// Enables memory debugging.
@@ -35,6 +37,10 @@ TMaterialLibrary::TMaterialLibrary(TMaterialLibraryManager *a_pManager)
3537 m_pManager = a_pManager;
3638 m_iNumTextures = 0 ;
3739 m_pTextures = TNULL;
40+ m_iNumMaterials = 0 ;
41+ m_pTextureNames = TNULL;
42+ m_pMaterials = TNULL;
43+ m_pMaterialNames = TNULL;
3844}
3945
4046TBOOL TMaterialLibrary::Create (TPCCHAR a_szFileName)
@@ -43,12 +49,15 @@ TBOOL TMaterialLibrary::Create(TPCCHAR a_szFileName)
4349 if (f) {
4450 if (ReadHeader (f)) {
4551 if (LoadTextures (f)) {
46-
52+ if (LoadMaterials (f)) {
53+ f->Destroy ();
54+ return TTRUE;
55+ }
4756 }
4857 }
4958 }
5059 f->Destroy ();
51- TDPRINTF (" Error loading material library %s\n " );
60+ TDPRINTF (" Error loading material library %s\n " , a_szFileName );
5261 Destroy ();
5362 return TFALSE;
5463}
@@ -60,19 +69,93 @@ void TMaterialLibrary::Destroy()
6069 m_pTextures = TNULL;
6170}
6271
72+ TBOOL TMaterialLibrary::LoadSpriteMaterial (TINT a_iIndex, TFile *file, const TMLDISKMATERIALHEADER &dh)
73+ {
74+ TSpriteShader *pShader = static_cast <TSpriteShader *>(m_pManager->GetShader (4 ));
75+ TVALIDADDRESS (pShader);
76+ TSpriteMaterial *pMat = pShader->CreateMaterial (TNULL);
77+ if (!pMat) {
78+ TDPRINTF (" TMaterialLibrary::LoadSpriteMaterial(): CreateMaterial() failed for %d\n " , a_iIndex);
79+ return TFALSE;
80+ }
81+ return TTRUE;
82+ }
83+
84+ TBOOL TMaterialLibrary::LoadMaterial (TINT a_iIndex, TFile *file)
85+ {
86+ TMLDISKMATERIALHEADER dh;
87+ TINT read = file->Read (&dh, sizeof (TMLDISKMATERIALHEADER));
88+ if (read != sizeof (TMLDISKMATERIALHEADER)) {
89+ TDPRINTF (" TMaterialLibrary::LoadMaterial(): Failed to read material header\n " );
90+ return TFALSE;
91+ }
92+ TDPRINTF (" Loading material \" %s\" (id %d)\n " , m_pMaterialNames[a_iIndex], dh.id );
93+ TMLDISKMATERIALSHADERTYPE eShaderType = (TMLDISKMATERIALSHADERTYPE)(dh.flags & TMLDISKMATERIALSHADERTYPE_MASK);
94+ switch (eShaderType)
95+ {
96+ case Toshi::TMaterialLibrary::TMLDISKMATERIALSHADERTYPE_SKIN:
97+ break ;
98+ case Toshi::TMaterialLibrary::TMLDISKMATERIALSHADERTYPE_TERRAIN:
99+ break ;
100+ case Toshi::TMaterialLibrary::TMLDISKMATERIALSHADERTYPE_TERRAINDECAL:
101+ break ;
102+ case Toshi::TMaterialLibrary::TMLDISKMATERIALSHADERTYPE_SYSTEM:
103+ break ;
104+ case Toshi::TMaterialLibrary::TMLDISKMATERIALSHADERTYPE_SPRITE:
105+ LoadSpriteMaterial (a_iIndex, file, dh);
106+ break ;
107+ case Toshi::TMaterialLibrary::TMLDISKMATERIALSHADERTYPE_MASK:
108+ break ;
109+ default :
110+ TASSERT (!" ************* TMaterialLibrary::LoadMaterial(): Unrecognised shader type" );
111+ }
112+ }
113+
114+ TBOOL TMaterialLibrary::LoadMaterials (TFile *file)
115+ {
116+ if (file->Read (&m_iNumMaterials, 4 ) != 4 ) {
117+ TDPRINTF (" TMaterialLibrary::LoadMaterials(): Failed to read number of materials\n " );
118+ return TFALSE;
119+ }
120+ m_pTextureNames = new TCHAR *[m_iNumMaterials];
121+ m_pMaterialNames = new MaterialName[m_iNumMaterials];
122+ if (file->Read (m_pMaterialNames, m_iNumMaterials * sizeof (MaterialName)) != m_iNumMaterials * sizeof (MaterialName)) {
123+ TDPRINTF (" TMaterialLibrary::LoadMaterials(): Failed to read number of materials\n " );
124+ return TFALSE;
125+ }
126+ m_pMaterials = new TMaterial *[m_iNumMaterials];
127+ for (TINT i = 0 ; i < m_iNumMaterials; i++) {
128+ m_pMaterials[i] = TNULL;
129+ m_pTextureNames[i] = m_pMaterialNames[i];
130+ }
131+ for (TINT i = 0 ; i < m_iNumMaterials; i++) {
132+ TMaterialLibraryManager *pManager = TRenderInterface::GetRenderer ()->GetMaterialLibraryManager ();
133+ for (auto it = pManager->m_oLibraries .Begin (); it != pManager->m_oLibraries .End (); it++) {
134+ TMaterial *pMat = it->GetMaterial (m_pMaterialNames[i]);
135+ if (pMat) {
136+ TDPRINTF (" ******** TMaterialLibraryManager::LoadMaterials() MATERIAL \" %s\" ALREADY LOADED BY ANOTHER LIBRARY!!!!!!! **********\n " , m_pMaterialNames[i]);
137+ }
138+ }
139+ if (!LoadMaterial (i, file)) {
140+ return TFALSE;
141+ }
142+ }
143+ return TTRUE;
144+ }
145+
63146TBOOL TMaterialLibrary::ReadHeader (TFile *file)
64147{
65- Header header ;
66- TINT read = file->Read (&header , sizeof (Header ));
67- if (read != sizeof (Header )) {
148+ TMLDISKHEADER dh ;
149+ TINT read = file->Read (&dh , sizeof (TMLDISKHEADER ));
150+ if (read != sizeof (TMLDISKHEADER )) {
68151 TDPRINTF (" TMaterialLibrary::ReadHeader(): Error failed to read header\n " );
69152 return TFALSE;
70153 }
71- if (header .m_iMagic != MAKEFOURCC (' T' , ' M' , ' L' , ' 1' )) {
154+ if (dh .m_iMagic != MAKEFOURCC (' T' , ' M' , ' L' , ' 1' )) {
72155 TDPRINTF (" TMaterialLibrary::ReadHeader(): Error incorrect magic number in header\n " );
73156 return TFALSE;
74157 }
75- if (header .m_iType != 0 ) {
158+ if (dh .m_iType != 0 ) {
76159 TDPRINTF (" TMaterialLibrary::ReadHeader(): Error incorrect type number in header\n " );
77160 return TFALSE;
78161 }
@@ -82,8 +165,8 @@ TBOOL TMaterialLibrary::ReadHeader(TFile *file)
82165// $TRenderInterface: FUNCTION 100100b0
83166TBOOL TMaterialLibrary::LoadTexture (TINT a_iIndex, TFile *file)
84167{
85- TextureHeader dh;
86- if (file->Read (&dh, sizeof (TextureHeader )) != sizeof (TextureHeader )) {
168+ TMLDISKTEXTUREHEADER dh;
169+ if (file->Read (&dh, sizeof (TMLDISKTEXTUREHEADER )) != sizeof (TMLDISKTEXTUREHEADER )) {
87170 TDPRINTF (" TMaterialLibrary::LoadTexture(): Failed to read texture header %d\n " );
88171 return TFALSE;
89172 }
@@ -117,7 +200,7 @@ TBOOL TMaterialLibrary::LoadTextures(TFile *file)
117200 return TFALSE;
118201 }
119202 m_pTextures = new TTextureResource *[m_iNumTextures];
120- TSystem::MemSet (m_pTextures, 0 , sizeof ( m_iNumTextures * 4 ) );
203+ TSystem::MemSet (m_pTextures, 0 , m_iNumTextures * 4 );
121204 for (TINT i = 0 ; i < m_iNumTextures; i++) {
122205 if (!LoadTexture (i, file)) {
123206 return TFALSE;
@@ -126,9 +209,30 @@ TBOOL TMaterialLibrary::LoadTextures(TFile *file)
126209 return TTRUE;
127210}
128211
212+ // $TRenderInterface: FUNCTION 10010f60
213+ TINT TMaterialLibrary::GetIndexForName (TPCCHAR a_szName, TPCHAR *a_pMaterialNames, TINT a_iNumMaterials)
214+ {
215+ for (TINT i = 0 ; i < a_iNumMaterials; i++) {
216+ if (i + 1 == a_iNumMaterials) {
217+ if (TSystem::StringCompareNoCase (a_szName, a_pMaterialNames[i], 32 ) != 0 ) {
218+ return -1 ;
219+ }
220+ return i;
221+ }
222+ if (TSystem::StringCompareNoCase (a_szName, a_pMaterialNames[(a_iNumMaterials - (i / 2 )) + i], 32 ) <= 0 ) {
223+ return (a_iNumMaterials - (i / 2 )) + i;
224+ }
225+ }
226+ return -1 ;
227+ }
228+
129229// $TRenderInterface: FUNCTION 1000ff40
130230TMaterial *TMaterialLibrary::GetMaterial (TPCCHAR a_szMaterial)
131231{
232+ TINT iIndex = GetIndexForName (a_szMaterial, m_pTextureNames, m_iNumMaterials);
233+ if (iIndex >= 0 && iIndex < m_iNumMaterials) {
234+ return m_pMaterials[iIndex];
235+ }
132236 return TNULL;
133237}
134238
@@ -141,7 +245,8 @@ TTextureResource *TMaterialLibrary::GetTexture(TINT a_iIndex)
141245// $TRenderInterface: FUNCTION 10011020
142246void TMaterialLibraryManager::Create ()
143247{
144- TMaterial *pSkin = reinterpret_cast <TMaterial *>(TRenderInterface::GetRenderer ()->GetSystemResource (TRenderInterface::SYSRESOURCE_SHSKIN));
248+ // TMaterial *pSkin = reinterpret_cast<TMaterial *>(TRenderInterface::GetRenderer()->GetSystemResource(TRenderInterface::SYSRESOURCE_SHSKIN));
249+
145250}
146251
147252// $TRenderInterface: FUNCTION 100110a0
0 commit comments