1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #include "CMeshCache.h"
\r
6 #include "IAnimatedMesh.h"
\r
14 static const io::SNamedPath emptyNamedPath;
\r
17 CMeshCache::~CMeshCache()
\r
23 //! adds a mesh to the list
\r
24 void CMeshCache::addMesh(const io::path& filename, IAnimatedMesh* mesh)
\r
28 MeshEntry e ( filename );
\r
31 Meshes.push_back(e);
\r
35 //! Removes a mesh from the cache.
\r
36 void CMeshCache::removeMesh(const IMesh* const mesh)
\r
40 for (u32 i=0; i<Meshes.size(); ++i)
\r
42 if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh))
\r
44 Meshes[i].Mesh->drop();
\r
52 //! Returns amount of loaded meshes
\r
53 u32 CMeshCache::getMeshCount() const
\r
55 return Meshes.size();
\r
59 //! Returns current number of the mesh
\r
60 s32 CMeshCache::getMeshIndex(const IMesh* const mesh) const
\r
62 for (u32 i=0; i<Meshes.size(); ++i)
\r
64 if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh))
\r
72 //! Returns a mesh based on its index number
\r
73 IAnimatedMesh* CMeshCache::getMeshByIndex(u32 number)
\r
75 if (number >= Meshes.size())
\r
78 return Meshes[number].Mesh;
\r
82 //! Returns a mesh based on its name.
\r
83 IAnimatedMesh* CMeshCache::getMeshByName(const io::path& name)
\r
85 MeshEntry e ( name );
\r
86 s32 id = Meshes.binary_search(e);
\r
87 return (id != -1) ? Meshes[id].Mesh : 0;
\r
91 //! Get the name of a loaded mesh, based on its index.
\r
92 const io::SNamedPath& CMeshCache::getMeshName(u32 index) const
\r
94 if (index >= Meshes.size())
\r
95 return emptyNamedPath;
\r
97 return Meshes[index].NamedPath;
\r
101 //! Get the name of a loaded mesh, if there is any.
\r
102 const io::SNamedPath& CMeshCache::getMeshName(const IMesh* const mesh) const
\r
105 return emptyNamedPath;
\r
107 for (u32 i=0; i<Meshes.size(); ++i)
\r
109 if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh))
\r
110 return Meshes[i].NamedPath;
\r
113 return emptyNamedPath;
\r
116 //! Renames a loaded mesh.
\r
117 bool CMeshCache::renameMesh(u32 index, const io::path& name)
\r
119 if (index >= Meshes.size())
\r
122 Meshes[index].NamedPath.setPath(name);
\r
128 //! Renames a loaded mesh.
\r
129 bool CMeshCache::renameMesh(const IMesh* const mesh, const io::path& name)
\r
131 for (u32 i=0; i<Meshes.size(); ++i)
\r
133 if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh))
\r
135 Meshes[i].NamedPath.setPath(name);
\r
145 //! returns if a mesh already was loaded
\r
146 bool CMeshCache::isMeshLoaded(const io::path& name)
\r
148 return getMeshByName(name) != 0;
\r
152 //! Clears the whole mesh cache, removing all meshes.
\r
153 void CMeshCache::clear()
\r
155 for (u32 i=0; i<Meshes.size(); ++i)
\r
156 Meshes[i].Mesh->drop();
\r
161 //! Clears all meshes that are held in the mesh cache but not used anywhere else.
\r
162 void CMeshCache::clearUnusedMeshes()
\r
164 for (u32 i=0; i<Meshes.size(); ++i)
\r
166 if (Meshes[i].Mesh->getReferenceCount() == 1)
\r
168 Meshes[i].Mesh->drop();
\r
176 } // end namespace scene
\r
177 } // end namespace irr
\r