OpenBarnyard
 
Loading...
Searching...
No Matches
TModelManager.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "TModelManager.h"
3#include "TRenderInterface.h"
4
6
7//-----------------------------------------------------------------------------
8// Enables memory debugging.
9// Note: Should be the last include!
10//-----------------------------------------------------------------------------
11#include "Core/TMemoryDebugOn.h"
12
14
16 : m_pModel( TNULL )
17{
18 static TBOOL s_bFilledList = TFALSE;
19
20 if ( !s_bFilledList )
21 {
22 for ( TUINT i = 0; i < TModelManager::MAX_NUM_MODELS; i++ )
23 {
25 }
26
27 s_bFilledList = TTRUE;
28 }
29
30 m_pEntry = TNULL;
31}
32
34{
35 if ( m_pModel )
36 {
37 if ( m_pEntry )
38 {
39 if ( m_pEntry->GetRefCount() == 1 )
40 {
41 m_pModel->Delete();
42 m_pEntry->Remove();
43 TModelManager::ms_oFreeList.PushBack( m_pEntry );
44
45 m_pModel = TNULL;
46 m_pEntry = TNULL;
47 }
48 else
49 {
50 m_pEntry->DecRefCount();
51 m_pModel = TNULL;
52 m_pEntry = TNULL;
53 }
54 }
55 else
56 {
57 m_pModel->Delete();
58 m_pModel = TNULL;
59 m_pEntry = TNULL;
60 }
61 }
62}
63
69
71{
73 ms_oUsedList.Clear();
74 ms_oFreeList.Clear();
75
76 delete[] ms_pEntries;
78}
79
80TModelManager::ModelNode* TModelManager::CreateModel( const TCHAR* a_szFileName, TManagedModel& a_rModelRef, TTRB* a_pAssetTRB )
81{
82 if ( ms_oFreeList.IsEmpty() )
83 {
84 TASSERT( TFALSE, "Couldn't create new model since the registry is all used" );
85 return TNULL;
86 }
87
88 TCHAR filepath[ 248 ];
89 auto iFileNameLen = TStringManager::String8Length( a_szFileName );
90 TStringManager::String8Copy( filepath, a_szFileName );
91
92 for ( TUINT i = 0; i < iFileNameLen; i++ )
93 {
94 if ( filepath[ i ] == '/' )
95 {
96 filepath[ i ] = '\\';
97 }
98 }
99
100 auto crc32 = TUtil::CRC32( TREINTERPRETCAST( TBYTE*, filepath ), iFileNameLen );
101
102 for ( auto it = ms_oUsedList.Begin(); it != ms_oUsedList.End(); ++it )
103 {
104 if ( it->GetCRC32() == crc32 )
105 {
106 a_rModelRef.m_pModel = it->GetModel();
107 it->IncRefCount();
108
109 return it;
110 }
111 }
112
113 auto pEntry = ms_oFreeList.PopBack();
114 ms_oUsedList.PushFront( pEntry );
115
116 const TCHAR* szFileName;
117 TUINT8 ui8NameLen = 0;
118
119 if ( !a_pAssetTRB || !TModel::GetSkeletonAssetSymbolName( filepath, szFileName, ui8NameLen, a_pAssetTRB ) )
120 {
121 a_pAssetTRB = TNULL;
122 szFileName = filepath;
123 ui8NameLen = -1;
124 }
125
126 auto pModel = TRenderInterface::GetSingleton()->CreateModel( szFileName, TTRUE, a_pAssetTRB, ui8NameLen );
127 pEntry->Create( crc32, pModel );
128 pEntry->IncRefCount();
129 a_rModelRef.m_pModel = pModel;
130
131 return pEntry;
132}
133
134TBOOL TManagedModel::Create( const TCHAR* a_szFileName, TTRB* a_pTRB )
135{
136 m_pModel = TNULL;
137 m_pEntry = TModelManager::CreateModel( a_szFileName, *this, a_pTRB );
138
139 return m_pModel != TNULL;
140}
141
143{
144 TSceneObject* pSceneObject = new TSceneObject();
145 pSceneObject->Create( this );
146
147 return pSceneObject;
148}
149
Rendering system interface for the Toshi engine.
#define TASSERT(X,...)
Definition Defines.h:138
#define TREINTERPRETCAST(TYPE, VALUE)
Definition Defines.h:68
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
unsigned int TUINT
Definition Typedefs.h:8
char TCHAR
Definition Typedefs.h:20
uint8_t TUINT8
Definition Typedefs.h:17
#define TNULL
Definition Typedefs.h:23
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
uint8_t TBYTE
Definition Typedefs.h:19
Definition TTRB.h:253
static TBOOL GetSkeletonAssetSymbolName(const TCHAR *a_szFileName, const TCHAR *&a_rSymbolName, TUINT8 &a_rNameLen, TTRB *a_pTRB)
Definition TModel.cpp:217
virtual TBOOL Create(TTMD *a_pTMD, TBOOL a_bLoad)=0
static void Initialise()
static ModelNode * ms_pEntries
static constexpr TUINT MAX_NUM_MODELS
static ModelNode * CreateModel(const TCHAR *a_szFileName, TManagedModel &a_rModelRef, TTRB *a_pAssetTRB)
static T2DList< ModelNode > ms_oUsedList
static T2DList< ModelNode > ms_oFreeList
static void Uninitialise()
TSceneObject * CreateSceneObject()
TBOOL Create(const TCHAR *a_szFileName, TTRB *a_pTRB)
virtual TModel * CreateModel(TTMD *a_pTMD, TBOOL a_bLoad)=0
void Create(TManagedModel *a_pModelPtr)
static TFORCEINLINE TRenderInterface * GetSingleton()
Definition TSingleton.h:49
static TSIZE String8Length(const TCHAR *str)
static TCHAR * String8Copy(TCHAR *dst, const TCHAR *src, TSIZE size=-1)
static TUINT32 CRC32(void *buffer, TUINT32 len)
Definition TUtil.cpp:201