Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application loading the wrong textures on opening again

I'm developing a game using libgdx library.

When I load the program for the first time, the textures load perfectly and everything is fine

enter image description here

When I close the application, and load it again (I'm assuming Android is somehow caching it from memory) - the wrong textures get loaded.

enter image description here

If I clear the game from history, and try again, it works perfectly.

-- The way it works currently is as follows - I use a SpriteBatch to draw the actual game. I have seperate SpriteBatches to draw the background and Interface (which are loading just fine). On disposing a level, I dispose the SpriteBatch.

for (Block block : world.getDrawableBlocks(this.width, this.height))
        {
            spriteBatch.draw(block.getTexture(1f), block.getPosition().x, block.getPosition().y, block.SIZE_X, block.SIZE_Y);
        }

--

The textures I load using a cache I wrote myself to prevent the same image being loaded more than once. I clear the cache upon the creation of the application. I then keep a Texture / TextureRegion in the object itself, which is obtained through .getTexture()

And here's my code which I use to load the Textures

public static Texture loadTexture(String path)
    {
        //Do we have the texture cached?
        if (textures.containsKey(path))
        {
            //return it
            return textures.get(path);
        }
        else 
        {
            //load it from the filesystem
            Texture texture = new Texture(Gdx.files.internal(path));

            //cache it
            textures.put(path, texture);

            //return it
            return texture;


        }
    }

I attached a debugger and the textures being loaded DO have the correct path.

In the picture example, the texture being swapped happens to be part of the font, which is nothing which is EVER stored in my cache.

--

So, I'm rather stuck here.

Right now I'm using the naughty solution of killing the process manually on dispose:

 @Override
    public void onDestroy()
    {
        super.onDestroy();
        this.finish();
        android.os.Process.killProcess( android.os.Process.myPid()  ); 
    }

This works but is pretty dirty.

When the process fails due to an exception, the bug does not occur.


I'm guessing that somehow the library is caching its own textures which are somehow getting corrupted, but I have no idea how to check, nor how to clear them.

So, any ideas?

like image 541
Haedrian Avatar asked Dec 24 '13 09:12

Haedrian


2 Answers

You need to Dispose() all Textures you have created, for textures loop through all its value() and dispose it on a separate method then use that method in main game's screen dispose() function.

the switched texture seems is BitmapFont, and if those font textures used in UI, then you might have wrong in UI scene or so, also are you loading with JSON file?

seems to me that when you close the game your last texture is font texture is used, and it fills the texture is used on scene.

like image 139
daniel Avatar answered Nov 05 '22 18:11

daniel


"new Texture", TextureAtlas, BitmapFont, etc are automatically reloaded after pause/resume or when the OpenGL context is otherwise lost.

I'm guessing your problem must be on the way you handle the manual reloading.

If you want to make sure that no texture is loaded more than once, use AssetManager instead. No need to do all that manually.

like image 43
Lestat Avatar answered Nov 05 '22 18:11

Lestat