1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #ifndef __C_MESH_CACHE_H_INCLUDED__
6 #define __C_MESH_CACHE_H_INCLUDED__
7 
8 #include "IMeshCache.h"
9 #include "irrArray.h"
10 
11 namespace irr
12 {
13 
14 namespace scene
15 {
16 	class CMeshCache : public IMeshCache
17 	{
18 	public:
19 
20 		virtual ~CMeshCache();
21 
22 		//! Adds a mesh to the internal list of loaded meshes.
23 		/** Usually, ISceneManager::getMesh() is called to load a mesh from file.
24 		That method searches the list of loaded meshes if a mesh has already been loaded and
25 		returns a pointer to if	it is in that list and already in memory. Otherwise it loads
26 		the mesh. With IMeshCache::addMesh(), it is possible to pretend that a mesh already
27 		has been loaded. This method can be used for example by mesh loaders who need to
28 		load more than one mesh with one call. They can add additional meshes with this
29 		method to the scene manager. The COLLADA loader for example uses this method.
30 		\param filename: Filename of the mesh. When called ISceneManager::getMesh() with this
31 		parameter, the method will return the mesh parameter given with this method.
32 		\param mesh: Pointer to a mesh which will now be referenced by this name. */
33 		virtual void addMesh(const io::path& filename, IAnimatedMesh* mesh);
34 
35 		//! Removes a mesh from the cache.
36 		/** After loading a mesh with getMesh(), the mesh can be removed from the cache
37 		using this method, freeing a lot of memory. */
38 		virtual void removeMesh(const IMesh* const mesh);
39 
40 		//! Returns amount of loaded meshes in the cache.
41 		/** You can load new meshes into the cache using getMesh() and addMesh().
42 		If you ever need to access the internal mesh cache, you can do this using
43 		removeMesh(), getMeshNumber(), getMeshByIndex() and getMeshFilename() */
44 		virtual u32 getMeshCount() const;
45 
46 		//! Returns current index number of the mesh, and -1 if it is not in the cache.
47 		virtual s32 getMeshIndex(const IMesh* const mesh) const;
48 
49 		//! Returns a mesh based on its index number.
50 		/** \param index: Index of the mesh, number between 0 and getMeshCount()-1.
51 		Note that this number is only valid until a new mesh is loaded or removed *
52 		\return Returns pointer to the mesh or 0 if there is none with this number. */
53 		virtual IAnimatedMesh* getMeshByIndex(u32 index);
54 
55 		//! Returns a mesh based on its name.
56 		/** \param name Name of the mesh. Usually a filename.
57 		\return Pointer to the mesh or 0 if there is none with this number. */
58 		virtual IAnimatedMesh* getMeshByName(const io::path& name);
59 
60 		//! Get the name of a loaded mesh, based on its index.
61 		/** \param index: Index of the mesh, number between 0 and getMeshCount()-1.
62 		\return The name if mesh was found and has a name, else	the path is empty. */
63 		virtual const io::SNamedPath& getMeshName(u32 index) const;
64 
65 		//! Get the name of a loaded mesh, if there is any.
66 		/** \param mesh Pointer to mesh to query.
67 		\return The name if mesh was found and has a name, else	the path is empty. */
68 		virtual const io::SNamedPath& getMeshName(const IMesh* const mesh) const;
69 
70 		//! Renames a loaded mesh.
71 		/** Note that renaming meshes might change the ordering of the
72 		meshes, and so the index of the meshes as returned by
73 		getMeshIndex() or taken by some methods will change.
74 		\param index The index of the mesh in the cache.
75 		\param name New name for the mesh.
76 		\return True if mesh was renamed. */
77 		virtual bool renameMesh(u32 index, const io::path& name);
78 
79 		//! Renames a loaded mesh.
80 		/** Note that renaming meshes might change the ordering of the
81 		meshes, and so the index of the meshes as returned by
82 		getMeshIndex() or taken by some methods will change.
83 		\param mesh Mesh to be renamed.
84 		\param name New name for the mesh.
85 		\return True if mesh was renamed. */
86 		virtual bool renameMesh(const IMesh* const mesh, const io::path& name);
87 
88 		//! returns if a mesh already was loaded
89 		virtual bool isMeshLoaded(const io::path& name);
90 
91 		//! Clears the whole mesh cache, removing all meshes.
92 		virtual void clear();
93 
94 		//! Clears all meshes that are held in the mesh cache but not used anywhere else.
95 		virtual void clearUnusedMeshes();
96 
97 	protected:
98 
99 		struct MeshEntry
100 		{
MeshEntryMeshEntry101 			MeshEntry ( const io::path& name )
102 				: NamedPath ( name )
103 			{
104 			}
105 			io::SNamedPath NamedPath;
106 			IAnimatedMesh* Mesh;
107 
108 			bool operator < (const MeshEntry& other) const
109 			{
110 				return (NamedPath < other.NamedPath);
111 			}
112 		};
113 
114 		//! loaded meshes
115 		core::array<MeshEntry> Meshes;
116 	};
117 
118 
119 } // end namespace scene
120 } // end namespace irr
121 
122 #endif
123 
124