diff options
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CMeshCache.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CMeshCache.cpp | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CMeshCache.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CMeshCache.cpp new file mode 100644 index 0000000..5bcce19 --- /dev/null +++ b/libraries/irrlicht-1.8/source/Irrlicht/CMeshCache.cpp | |||
@@ -0,0 +1,178 @@ | |||
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 | #include "CMeshCache.h" | ||
6 | #include "IAnimatedMesh.h" | ||
7 | #include "IMesh.h" | ||
8 | |||
9 | namespace irr | ||
10 | { | ||
11 | namespace scene | ||
12 | { | ||
13 | |||
14 | static const io::SNamedPath emptyNamedPath; | ||
15 | |||
16 | |||
17 | CMeshCache::~CMeshCache() | ||
18 | { | ||
19 | clear(); | ||
20 | } | ||
21 | |||
22 | |||
23 | //! adds a mesh to the list | ||
24 | void CMeshCache::addMesh(const io::path& filename, IAnimatedMesh* mesh) | ||
25 | { | ||
26 | mesh->grab(); | ||
27 | |||
28 | MeshEntry e ( filename ); | ||
29 | e.Mesh = mesh; | ||
30 | |||
31 | Meshes.push_back(e); | ||
32 | } | ||
33 | |||
34 | |||
35 | //! Removes a mesh from the cache. | ||
36 | void CMeshCache::removeMesh(const IMesh* const mesh) | ||
37 | { | ||
38 | if ( !mesh ) | ||
39 | return; | ||
40 | for (u32 i=0; i<Meshes.size(); ++i) | ||
41 | { | ||
42 | if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh)) | ||
43 | { | ||
44 | Meshes[i].Mesh->drop(); | ||
45 | Meshes.erase(i); | ||
46 | return; | ||
47 | } | ||
48 | } | ||
49 | } | ||
50 | |||
51 | |||
52 | //! Returns amount of loaded meshes | ||
53 | u32 CMeshCache::getMeshCount() const | ||
54 | { | ||
55 | return Meshes.size(); | ||
56 | } | ||
57 | |||
58 | |||
59 | //! Returns current number of the mesh | ||
60 | s32 CMeshCache::getMeshIndex(const IMesh* const mesh) const | ||
61 | { | ||
62 | for (u32 i=0; i<Meshes.size(); ++i) | ||
63 | { | ||
64 | if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh)) | ||
65 | return (s32)i; | ||
66 | } | ||
67 | |||
68 | return -1; | ||
69 | } | ||
70 | |||
71 | |||
72 | //! Returns a mesh based on its index number | ||
73 | IAnimatedMesh* CMeshCache::getMeshByIndex(u32 number) | ||
74 | { | ||
75 | if (number >= Meshes.size()) | ||
76 | return 0; | ||
77 | |||
78 | return Meshes[number].Mesh; | ||
79 | } | ||
80 | |||
81 | |||
82 | //! Returns a mesh based on its name. | ||
83 | IAnimatedMesh* CMeshCache::getMeshByName(const io::path& name) | ||
84 | { | ||
85 | MeshEntry e ( name ); | ||
86 | s32 id = Meshes.binary_search(e); | ||
87 | return (id != -1) ? Meshes[id].Mesh : 0; | ||
88 | } | ||
89 | |||
90 | |||
91 | //! Get the name of a loaded mesh, based on its index. | ||
92 | const io::SNamedPath& CMeshCache::getMeshName(u32 index) const | ||
93 | { | ||
94 | if (index >= Meshes.size()) | ||
95 | return emptyNamedPath; | ||
96 | |||
97 | return Meshes[index].NamedPath; | ||
98 | } | ||
99 | |||
100 | |||
101 | //! Get the name of a loaded mesh, if there is any. | ||
102 | const io::SNamedPath& CMeshCache::getMeshName(const IMesh* const mesh) const | ||
103 | { | ||
104 | if (!mesh) | ||
105 | return emptyNamedPath; | ||
106 | |||
107 | for (u32 i=0; i<Meshes.size(); ++i) | ||
108 | { | ||
109 | if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh)) | ||
110 | return Meshes[i].NamedPath; | ||
111 | } | ||
112 | |||
113 | return emptyNamedPath; | ||
114 | } | ||
115 | |||
116 | //! Renames a loaded mesh. | ||
117 | bool CMeshCache::renameMesh(u32 index, const io::path& name) | ||
118 | { | ||
119 | if (index >= Meshes.size()) | ||
120 | return false; | ||
121 | |||
122 | Meshes[index].NamedPath.setPath(name); | ||
123 | Meshes.sort(); | ||
124 | return true; | ||
125 | } | ||
126 | |||
127 | |||
128 | //! Renames a loaded mesh. | ||
129 | bool CMeshCache::renameMesh(const IMesh* const mesh, const io::path& name) | ||
130 | { | ||
131 | for (u32 i=0; i<Meshes.size(); ++i) | ||
132 | { | ||
133 | if (Meshes[i].Mesh == mesh || (Meshes[i].Mesh && Meshes[i].Mesh->getMesh(0) == mesh)) | ||
134 | { | ||
135 | Meshes[i].NamedPath.setPath(name); | ||
136 | Meshes.sort(); | ||
137 | return true; | ||
138 | } | ||
139 | } | ||
140 | |||
141 | return false; | ||
142 | } | ||
143 | |||
144 | |||
145 | //! returns if a mesh already was loaded | ||
146 | bool CMeshCache::isMeshLoaded(const io::path& name) | ||
147 | { | ||
148 | return getMeshByName(name) != 0; | ||
149 | } | ||
150 | |||
151 | |||
152 | //! Clears the whole mesh cache, removing all meshes. | ||
153 | void CMeshCache::clear() | ||
154 | { | ||
155 | for (u32 i=0; i<Meshes.size(); ++i) | ||
156 | Meshes[i].Mesh->drop(); | ||
157 | |||
158 | Meshes.clear(); | ||
159 | } | ||
160 | |||
161 | //! Clears all meshes that are held in the mesh cache but not used anywhere else. | ||
162 | void CMeshCache::clearUnusedMeshes() | ||
163 | { | ||
164 | for (u32 i=0; i<Meshes.size(); ++i) | ||
165 | { | ||
166 | if (Meshes[i].Mesh->getReferenceCount() == 1) | ||
167 | { | ||
168 | Meshes[i].Mesh->drop(); | ||
169 | Meshes.erase(i); | ||
170 | --i; | ||
171 | } | ||
172 | } | ||
173 | } | ||
174 | |||
175 | |||
176 | } // end namespace scene | ||
177 | } // end namespace irr | ||
178 | |||