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