Ends the definition of a cache, returning the cache ID to be used with #draw(int).
()
| 196 | |
| 197 | /** Ends the definition of a cache, returning the cache ID to be used with {@link #draw(int)}. */ |
| 198 | public int endCache () { |
| 199 | if (currentCache == null) throw new IllegalStateException("beginCache must be called before endCache."); |
| 200 | Cache cache = currentCache; |
| 201 | int cacheCount = mesh.getVerticesBuffer(false).position() - cache.offset; |
| 202 | if (cache.textures == null) { |
| 203 | // New cache. |
| 204 | cache.maxCount = cacheCount; |
| 205 | cache.textureCount = textures.size; |
| 206 | cache.textures = textures.toArray(Texture[]::new); |
| 207 | cache.counts = new int[cache.textureCount]; |
| 208 | for (int i = 0, n = counts.size; i < n; i++) |
| 209 | cache.counts[i] = counts.get(i); |
| 210 | |
| 211 | ((Buffer)mesh.getVerticesBuffer(true)).flip(); |
| 212 | } else { |
| 213 | // Redefine existing cache. |
| 214 | if (cacheCount > cache.maxCount) { |
| 215 | throw new GdxRuntimeException( |
| 216 | "If a cache is not the last created, it cannot be redefined with more entries than when it was first created: " |
| 217 | + cacheCount + " (" + cache.maxCount + " max)"); |
| 218 | } |
| 219 | |
| 220 | cache.textureCount = textures.size; |
| 221 | |
| 222 | if (cache.textures.length < cache.textureCount) cache.textures = new Texture[cache.textureCount]; |
| 223 | for (int i = 0, n = cache.textureCount; i < n; i++) |
| 224 | cache.textures[i] = textures.get(i); |
| 225 | |
| 226 | if (cache.counts.length < cache.textureCount) cache.counts = new int[cache.textureCount]; |
| 227 | for (int i = 0, n = cache.textureCount; i < n; i++) |
| 228 | cache.counts[i] = counts.get(i); |
| 229 | |
| 230 | FloatBuffer vertices = mesh.getVerticesBuffer(true); |
| 231 | ((Buffer)vertices).position(0); |
| 232 | Cache lastCache = caches.get(caches.size - 1); |
| 233 | ((Buffer)vertices).limit(lastCache.offset + lastCache.maxCount); |
| 234 | } |
| 235 | |
| 236 | currentCache = null; |
| 237 | textures.clear(); |
| 238 | counts.clear(); |
| 239 | |
| 240 | return cache.id; |
| 241 | } |
| 242 | |
| 243 | /** Invalidates all cache IDs and resets the SpriteCache so new caches can be added. */ |
| 244 | public void clear () { |