Reputation: 49
I am creating a simple pet simulator, it is my first project created for an assignment. Most of the functionality is working fine, I have re-written it many times as I have gotten better at setting out projects, however while adding in a timer I have encountered a massive floor.
After running the project my game seems to work fine, images are being rendered (Perhaps not the most efficiently) and my timer/FPS counter works well. However ever since I have added this timing/FPS code, it has been slowly getting slower and slower in FPS and then freezing up and crashing.
I followed Ninja Cave's timing tutorial for LWJGL. http://ninjacave.com/lwjglbasics4
Here is my source code, not all classes are included as there are quite a few, but can if need be. I have tried to just include the rendering focussed ones.
Main Class http://pastebin.com/BpkHHnnj
Rendering Class http://pastebin.com/QtJeYw1a
Texture Loader Class http://pastebin.com/RX5iDXQm
Main Game State Class http://pastebin.com/pvgDLkeM
The Pet Class http://pastebin.com/VF6cq9S4
Thanks
Upvotes: 0
Views: 148
Reputation: 708
I'm currently working on fixing your issue, but your renderer.readyTexture()
just spins wildly out of control, and is essentially leaking memory, and fast, which explains the drop in speed.
Edit: I got the memory usage to stabilize.
Add public Map<String, Texture> loadedTextures = new HashMap<String, Texture>();
to your renderer
class in render.java and change your renderer.readyTexture()
method to this:
public void readyTexture(String textureDir){
if (!loadedTextures.containsKey(textureDir) || loadedTextures.get(textureDir) == null) {
texture = lt.loadTexture(textureDir);
loadedTextures.put(textureDir, texture);
} else {
texture = loadedTextures.get(textureDir);
}
textureDirString = textureDir;
texture.bind();
texLoaded = true;
System.out.println("Loaded: " + textureDirString);
}
Now that you have the code, the Map/HashMap
stores the already loaded textures. In the renderer.readyTexture()
method, I have it check if the Map
does not contain the key textureDir
and if it does, I check to see if it is null. If the entry is not in the Map
or the contained Texture
is null
, we load the texture, and store it in the map. If the texture is stored, we pull it out from the Map
and bind it.
Before, you were loading up the image every time, and the garbage collector was not removing it, this is possibly an issue with Slick, but if you cache everything properly, it works just fine.
I hope this helps.
Upvotes: 1