0

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

Anim8
  • 49
  • 7
  • You need to profile your application in order to determine where the bottlenecks are. [Here is a profiling overview](http://stackoverflow.com/a/2425217/1827903) – Zim-Zam O'Pootertoot Aug 03 '13 at 23:38

1 Answers1

1

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.

Pandacoder
  • 708
  • 7
  • 11
  • +1 for better resource management. Thus can also occur when creating lots of short lived objects repeatedly – MadProgrammer Aug 04 '13 at 01:40
  • The reason behind this isn't Slicks fault - Slick is loading up a new texture into memory every time. It's doing exactly what you asked. You need a resource manager that does smart loading and can hopefully unload smartly. – Vaughan Hilts Aug 04 '13 at 02:23
  • Robbie, you solved it :). A couple of days ago I tried to fix the loading of textures repetitively, but forgot about it when it did not work. Thanks for this knowledge on Maps and HashMaps, more reading to do :) They seem very useful. – Anim8 Aug 04 '13 at 03:48
  • You are very welcome. Another thing is that at a passing glance over slick, I could not figure out a way to release the resources being used by the slick `Texture` (Note: `Texture.release()` did **not** work for me), so I recommend looking that up if you intend on releasing any resources before your program closes. – Pandacoder Aug 04 '13 at 04:31
  • Awesome will look into that, also since youve had a look at my code, do you know why I cant render multiple of one quad in a for loop.... `rend.readyTexture("health"); for(int i = 0; i < 10; i++){ rend.tileRender(heartX + XOffset,heartY); }` – Anim8 Aug 04 '13 at 13:54