Created
May 12, 2012 22:32
-
-
Save pplante/2669506 to your computer and use it in GitHub Desktop.
This is a custom wrapper around the AssetManager in LibGDX providing the ability to track how much texture memory is allocated.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2012. HappyDroids LLC <[email protected]> | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
package com.happydroids.droidtowers; | |
import com.badlogic.gdx.assets.AssetLoaderParameters; | |
import com.badlogic.gdx.assets.AssetManager; | |
import com.badlogic.gdx.graphics.Texture; | |
import com.badlogic.gdx.graphics.TextureData; | |
import java.util.HashMap; | |
import java.util.Map; | |
import static com.badlogic.gdx.assets.AssetLoaderParameters.LoadedCallback; | |
import static com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter; | |
public class MemoryTrackingAssetManager extends AssetManager { | |
private int currentMemory; | |
private Map<String, Integer> memoryPerFile; | |
public MemoryTrackingAssetManager() { | |
super(); | |
currentMemory = 0; | |
memoryPerFile = new HashMap<String, Integer>(); | |
} | |
@SuppressWarnings("unchecked") | |
private int calculateTextureSize(AssetManager assetManager, String fileName, Class type) { | |
if (memoryPerFile.containsKey(fileName)) { | |
return memoryPerFile.get(fileName); | |
} | |
Texture texture = (Texture) assetManager.get(fileName, type); | |
TextureData textureData = texture.getTextureData(); | |
int textureSize = textureData.getWidth() * textureData.getHeight(); | |
switch (textureData.getFormat()) { | |
case RGB565: | |
textureSize *= 2; | |
break; | |
case RGB888: | |
textureSize *= 3; | |
break; | |
case RGBA4444: | |
textureSize *= 2; | |
break; | |
case RGBA8888: | |
textureSize *= 4; | |
break; | |
} | |
memoryPerFile.put(fileName, textureSize); | |
return textureSize; | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public synchronized <T> void load(String fileName, Class<T> type, AssetLoaderParameters<T> parameter) { | |
if (type.equals(Texture.class)) { | |
if (parameter == null) { | |
parameter = (AssetLoaderParameters<T>) new TextureParameter(); | |
} | |
final LoadedCallback prevCallback = parameter.loadedCallback; | |
parameter.loadedCallback = new LoadedCallback() { | |
public void finishedLoading(AssetManager assetManager, String fileName, Class type) { | |
if (prevCallback != null) { | |
prevCallback.finishedLoading(assetManager, fileName, type); | |
} | |
currentMemory += calculateTextureSize(assetManager, fileName, type); | |
} | |
}; | |
} | |
super.load(fileName, type, parameter); | |
} | |
@Override | |
public synchronized void unload(String fileName) { | |
super.unload(fileName); | |
if (memoryPerFile.containsKey(fileName)) { | |
currentMemory -= memoryPerFile.get(fileName); | |
} | |
} | |
public float getMemoryInMegabytes() { | |
return (float) currentMemory / 1024f / 1024f; | |
} | |
} |
Oh snap forgot about my mipmaps. Good catch!
…On May 13, 2012 4:06 PM, "Justin Graves" < ***@***.***> wrote:
Don't forget to add 1/3 more memory if you are using mip-map generation.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2669506
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't forget to add 1/3 more memory if you are using mip-map generation.