@@ -34,6 +34,10 @@ void CreateMaterial(GameOriginInfo *info)
3434 // it will free the dynamic tex itself, the material it created, and the texture too, so it nicely wraps it all up for you
3535 // this way there is not a chunk of memory permanently dedicated to game origin icons (even if it is not a large amount)
3636 tex = MemAlloc (0x20 , 0 );
37+ if (tex == NULL ) {
38+ RB3E_DEBUG ("MemAlloc failed for dynamic tex '%s'" , info -> gameOrigin );
39+ return ;
40+ }
3741
3842 // build the ark path (so dont include /gen/ or the _platform extension etc.)
3943 RB3E_DEBUG ("Creating dynamic tex for game origin '%s'" , info -> gameOrigin );
@@ -43,6 +47,13 @@ void CreateMaterial(GameOriginInfo *info)
4347
4448 // create and pray
4549 DynamicTexConstructor (tex , path , info -> gameOrigin , 1 , 0 );
50+ if (tex -> mMat == NULL || tex -> mTex == NULL ) {
51+ RB3E_DEBUG ("DynamicTex::__ct failed for dynamic tex '%s'" , path );
52+
53+ // we should prob do cleanup here but that is a
54+ // TODO
55+ return ;
56+ }
4657
4758 textures [info -> num ] = tex ;
4859
@@ -60,6 +71,8 @@ void CreateMaterial(GameOriginInfo *info)
6071 // print the material name
6172 RB3E_DEBUG ("Dynamic tex created at %p with material '%s'" , textures [info -> num ], textures [info -> num ]-> mMatName .buf );
6273
74+ RB3E_DEBUG ("Dynamic tex created at %p with mat %p tex %p fileloader %p" , tex , tex -> mMat , tex -> mTex , tex -> mFileLoader );
75+
6376
6477}
6578
@@ -123,6 +136,64 @@ RndMat *MusicLibraryMatHook(MusicLibrary *thisMusicLibrary, int data, int idx, U
123136 return MusicLibraryMat (thisMusicLibrary , data , idx , listSlot );
124137}
125138
139+ // called when entering the music library
140+ // allocate the memory and load the textures here
141+ void MusicLibraryOnEnterHook (void * thisMusicLibrary )
142+ {
143+ int i ;
144+
145+ // allocate and create the materials for any game origins we found while loading songs
146+ for (i = 0 ; i < numGameOrigins ; i ++ ) {
147+ int slot = originInfo [i ].num ;
148+ if (slot >= 0 && slot < 100 ) {
149+ if (textures [slot ] == NULL ) {
150+ CreateMaterial (& originInfo [i ]);
151+ }
152+ }
153+ }
154+
155+ // do the original logic
156+ MusicLibraryOnEnter (thisMusicLibrary );
157+ }
158+
159+ // called when the music library panel is unloaded
160+ void MusicLibraryOnUnloadHook (void * thisMusicLibrary )
161+ {
162+ int i ;
163+
164+ MusicLibraryOnUnload (thisMusicLibrary );
165+
166+ // call the dynamictex destructor for any textures we created
167+ // lets just sweep all 100 to be safe
168+ for (i = 0 ; i < 100 ; i ++ ) {
169+ if (textures [i ] != NULL ) {
170+ // we can't call the DynamicTexDestructor because the FileLoader it used has already been freed by this point, so we must destruct everything but the FileLoader
171+ // extremely nasty but it seems to work
172+ if (textures [i ]-> mMat != NULL ) {
173+ ((Object * )textures [i ]-> mMat )-> table -> destructor ((Object * )textures [i ]-> mMat );
174+ textures [i ]-> mMat = NULL ;
175+ }
176+ if (textures [i ]-> mTex != NULL ) {
177+ ((Object * )textures [i ]-> mTex )-> table -> destructor ((Object * )textures [i ]-> mTex );
178+ textures [i ]-> mTex = NULL ;
179+ }
180+ // TODO: set up a proper vtable for String instead of doign this shit
181+ if (textures [i ]-> mMatName .length != 0 ) {
182+ if (textures [i ]-> mMatName .vtable != NULL ) {
183+ #ifdef RB3E_WII
184+ ((void (* )(String * ))textures [i ]-> mMatName .vtable [2 ])(& textures [i ]-> mMatName );
185+ #else
186+ ((void (* )(String * ))textures [i ]-> mMatName .vtable [0 ])(& textures [i ]-> mMatName );
187+ #endif
188+ }
189+ }
190+ textures [i ] = NULL ;
191+ }
192+ }
193+
194+ RB3E_DEBUG ("Freed game origin icon textures" , NULL );
195+ }
196+
126197int numGameOrigins ;
127198GameOriginInfo originInfo [100 ] = {0 };
128199
@@ -150,7 +221,6 @@ void AddGameOriginToIconList(char *gameOrigin)
150221
151222 originInfo [numGameOrigins ].gameOrigin = gameOrigin ;
152223 originInfo [numGameOrigins ].num = numGameOrigins ;
153- CreateMaterial (& originInfo [numGameOrigins ]);
154224 numGameOrigins ++ ;
155225 RB3E_DEBUG ("Adding game origin '%s' to icon list, total is now %i" , gameOrigin , numGameOrigins );
156226 }
0 commit comments