#include "CMeshCache.h"
#include "IAnimatedMesh.h"
#include "IMesh.h"
namespace irr
{
namespace scene
{
static const io::SNamedPath emptyNamedPath;
CMeshCache::~CMeshCache()
{
clear();
}
void CMeshCache::addMesh(const io::path& filename, IAnimatedMesh* mesh)
{
mesh->grab();
MeshEntry e ( filename );
e.Mesh = mesh;
Meshes.push_back(e);
}
void CMeshCache::removeMesh(const IMesh* const mesh)
{
if ( !mesh )
return;
for (u32 i=0; i<Meshes.size(); ++i)
{
if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh))
{
Meshes[i].Mesh->drop();
Meshes.erase(i);
return;
}
}
}
u32 CMeshCache::getMeshCount() const
{
return Meshes.size();
}
s32 CMeshCache::getMeshIndex(const IMesh* const mesh) const
{
for (u32 i=0; i<Meshes.size(); ++i)
{
if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh))
return (s32)i;
}
return -1;
}
IAnimatedMesh* CMeshCache::getMeshByIndex(u32 number)
{
if (number >= Meshes.size())
return 0;
return Meshes[number].Mesh;
}
IAnimatedMesh* CMeshCache::getMeshByName(const io::path& name)
{
MeshEntry e ( name );
s32 id = Meshes.binary_search(e);
return (id != -1) ? Meshes[id].Mesh : 0;
}
const io::SNamedPath& CMeshCache::getMeshName(u32 index) const
{
if (index >= Meshes.size())
return emptyNamedPath;
return Meshes[index].NamedPath;
}
const io::SNamedPath& CMeshCache::getMeshName(const IMesh* const mesh) const
{
if (!mesh)
return emptyNamedPath;
for (u32 i=0; i<Meshes.size(); ++i)
{
if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh))
return Meshes[i].NamedPath;
}
return emptyNamedPath;
}
bool CMeshCache::renameMesh(u32 index, const io::path& name)
{
if (index >= Meshes.size())
return false;
Meshes[index].NamedPath.setPath(name);
Meshes.sort();
return true;
}
bool CMeshCache::renameMesh(const IMesh* const mesh, const io::path& name)
{
for (u32 i=0; i<Meshes.size(); ++i)
{
if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh))
{
Meshes[i].NamedPath.setPath(name);
Meshes.sort();
return true;
}
}
return false;
}
bool CMeshCache::isMeshLoaded(const io::path& name)
{
return getMeshByName(name) != 0;
}
void CMeshCache::clear()
{
for (u32 i=0; i<Meshes.size(); ++i)
Meshes[i].Mesh->drop();
Meshes.clear();
}
void CMeshCache::clearUnusedMeshes()
{
for (u32 i=0; i<Meshes.size(); ++i)
{
if (Meshes[i].Mesh->getReferenceCount() == 1)
{
Meshes[i].Mesh->drop();
Meshes.erase(i);
--i;
}
}
}
}
}