OpenBarnyard
 
Loading...
Searching...
No Matches
AKeyFrameLibraryManager.cpp
Go to the documentation of this file.
1#include "pch.h"
3#include "ALoadScreen.h"
4
6
7//-----------------------------------------------------------------------------
8// Enables memory debugging.
9// Note: Should be the last include!
10//-----------------------------------------------------------------------------
11#include <Core/TMemoryDebugOn.h>
12
14
16
18 : m_Libraries( AMemory::GetAllocator( AMemory::POOL_Misc ) ), m_LibrariesRefs( AMemory::GetAllocator( AMemory::POOL_Misc ) )
19{
20}
21
23{
24 UnloadAllLibraries();
25}
26
28{
30
31 if ( TNULL != a_pArray )
32 {
33 auto pArray = a_pArray->GetArray();
34
35 for ( TUINT i = 0; i < pArray->GetSize(); i++ )
36 {
37 LoadLibrary( TPString8( pArray->GetValue( i )->GetString() ), a_pTRB );
38 g_oLoadScreen.Update( 1.0f, TTRUE );
39 }
40
41 g_oLoadScreen.Update( 1.0f, TTRUE );
42 }
43}
44
45TBOOL AKeyFrameLibraryManager::LoadLibrary( const Toshi::TPString8& a_rLibraryName, TTRB* a_pTRB )
46{
48
49 if ( m_Libraries.IsValid( m_Libraries.Find( a_rLibraryName ) ) )
50 {
51 // The library is already loaded
52 // Increment reference count
53 m_LibrariesRefs.Find( a_rLibraryName )->GetSecond() += 1;
54 return TTRUE;
55 }
56
57 TKeyframeLibrary* pKeyFrameLibrary;
58 auto pRenderInterface = TRenderInterface::GetSingleton();
59 auto pKeyFrameLibMngr = &pRenderInterface->GetKeyframeLibraryManager();
60
61 if ( TNULL == a_pTRB )
62 {
63 LoadFromModelsFolder:
64 TString8 filePath;
65 filePath.Format( "data/models/%s.tkl", a_rLibraryName.GetString() );
66
67 pKeyFrameLibrary = pKeyFrameLibMngr->LoadLibrary( filePath );
68 }
69 else
70 {
71 TString8 symbolname;
72 symbolname.Format( "%s_keylib", a_rLibraryName.GetString() );
73
74 // If no symbol was found, load from models folder
75 if ( TNULL == a_pTRB->GetSymbol( symbolname ) )
76 goto LoadFromModelsFolder;
77
78 pKeyFrameLibrary = pKeyFrameLibMngr->LoadLibrary( a_pTRB, symbolname );
79
80 // If no success in loading library, try to load it from models folder
81 if ( !pKeyFrameLibrary )
82 goto LoadFromModelsFolder;
83 }
84
85 TVALIDPTR( pKeyFrameLibrary );
86
87 if ( TNULL != pKeyFrameLibrary )
88 m_Libraries.Insert( a_rLibraryName, pKeyFrameLibrary );
89
90 m_LibrariesRefs.Insert( a_rLibraryName, 1 );
91
92 return TTRUE;
93}
94
95TBOOL AKeyFrameLibraryManager::UnrefLibrary( const Toshi::TPString8& a_rLibraryName )
96{
98
99 auto pResultNode = m_Libraries.Find( a_rLibraryName );
100
101 if ( m_Libraries.IsValid( pResultNode ) )
102 {
103 auto pRefResultNode = m_LibrariesRefs.Find( pResultNode->GetFirst() );
104
105 if ( pRefResultNode != m_LibrariesRefs.End() )
106 {
107 auto& rNumRefs = pResultNode->GetSecond();
108
109 if ( rNumRefs != 0 )
110 rNumRefs--;
111
112 if ( rNumRefs == 0 )
113 {
114 UnloadLibrary( pResultNode );
115 m_LibrariesRefs.Remove( pRefResultNode );
116 }
117 }
118 }
119
120 return TTRUE;
121}
122
123void AKeyFrameLibraryManager::UnloadAllLibraries()
124{
126
127 for ( auto it = m_Libraries.Begin(); it != m_Libraries.End(); )
128 {
129 auto pNext = it.Next();
130 UnloadLibrary( it );
131 it = pNext;
132 }
133
134 m_Libraries.Clear();
135 m_LibrariesRefs.Clear();
136}
137
138void AKeyFrameLibraryManager::UnloadLibrary( LibraryMap::Iterator& a_rLibrary )
139{
141
142 if ( a_rLibrary != m_Libraries.End() )
143 {
144 auto pKeyFrameLibMngr = &TRenderInterface::GetSingleton()->GetKeyframeLibraryManager();
145 pKeyFrameLibMngr->UnloadLibrary( a_rLibrary.GetValue()->GetSecond() );
146 m_Libraries.Remove( a_rLibrary );
147 }
148}
Rendering system interface for the Toshi engine.
#define TOSHI_NAMESPACE_USING
Definition Defines.h:46
#define TVALIDPTR(PTR)
Definition Defines.h:139
#define TPROFILER_SCOPE()
Definition Profiler.h:17
#define TDEFINE_CLASS(...)
Definition TObject.h:120
unsigned int TUINT
Definition Typedefs.h:8
#define TNULL
Definition Typedefs.h:23
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
ALoadScreen g_oLoadScreen
Definition TTRB.h:253
TTRBSymbol * GetSymbol(TINT index) const
Definition TTRB.cpp:368
const class PBPropertyValueArray * GetArray() const
void UnloadLibrary(TKeyframeLibrary *a_pLibrary)
TKeyframeLibraryManager & GetKeyframeLibraryManager()
static TFORCEINLINE TRenderInterface * GetSingleton()
Definition TSingleton.h:49
TString8 & Format(const TCHAR *a_pcFormat,...)
Definition TString8.cpp:176
void LoadLibrariesFromProperties(const PBPropertyValue *a_pArray, Toshi::TTRB *a_pTRB)
TBOOL UnrefLibrary(const Toshi::TPString8 &a_rLibraryName)
TBOOL LoadLibrary(const Toshi::TPString8 &a_rLibraryName, Toshi::TTRB *a_pTRB=nullptr)