I’m creating a game for Android with LibGDX, and aside from supporting multiple screen resolutions (any information on how to would be great, just as a side request!), its going good. But I’ve run into an issue. When a user hits the back button on their phone, the “app” is pretty much closed down completely and the memory is restored to the phone from the game. The home button simply “pauses” the app, so the data is still in memory. I think I’m right, but correct me if I’m wrong.
Now, my issue is loading textures upon resume after hitting the back key. I know about managed and unmanaged textures, so I don’t think I need more information on that. Let me start out by explaining what happens when I try resuming the game.
-
I have a UI in game that is made up of separately loaded textures (IE not from a texture atlas). On resume, they render fine.
-
The levels are made up of tiles loaded in using the TextureRegion class. I get the texture regions from a statically created texture (Texture texture = new Texture(blahhh…) The tiles do not reload after resuming, I just get white where the tiles should be.
-
The “mobs” (the player and all the NPCs) are also loaded from the same Texture that the tiles are loaded from (the mobs are TextureRegions). These also do not reload after resume.
I think the issue has to do with my texture atlas. I statically create it like so:
public static Texture tiles = new Texture(Gdx.files.internal("data/tiles.png"));
I then call it like this (Example is for a grass tile):
private TextureRegion texture;
public TileGrass(){
texture = new TextureRegion(Constants.tiles, 16, 0, 16, 16);
}
Constants.tiles refers to the static texture I showed above.
Now, what did I do to try to solve the issue? I tried reloading the textures in my resume function like this:
@Override
public void resume() {
Constants.tiles = new Texture(Gdx.files.internal("data/tiles.png"));
Tile.initTiles();
for(Mob m : mobs) {
m.onResume();
}
player.onResume();
}
Tile.initTiles is this:
public static void initTiles(){
Grass = new TileGrass();
Stone = new TileStone();
StoneWall = new TileStoneWall();
StoneCracked = new TileStoneCracked();
StoneCracked1 = new TileStoneCracked1();
Defense = new TileDefense();
Seed1 = new TileSeed1();
Seed2 = new TileSeed2();
Flower = new TileFlower();
tiles.put((byte) 1, Grass);
tiles.put((byte) 2, Seed1);
tiles.put((byte) 3, Seed2);
tiles.put((byte) 4, Flower);
tiles.put((byte) 5, Stone);
tiles.put((byte) 6, StoneWall);
tiles.put((byte) 7, StoneCracked);
tiles.put((byte) 8, StoneCracked1);
tiles.put((byte) 9, Defense);
}
It recreates the tiles (therefore recreating the TextureRegions), and adds them to a hashmap.
onResume() is this:
public void onResume() {
texture = new TextureRegion(Constants.tiles, tx, ty, 16, 16);
}
Which again, just recreates the TextureRegion from the static Texture. I then call resume from my main class like this (the main class extends Game, the LibGDX class):
@Override
public void resume() {
super.resume();
if(currentState == State.MENU)
menu.resume();
if(currentState == State.GAME)
world.resume();
}
Now, what can I conclude? It seems texture regions do not reload after the application is “destroyed” by hitting the back button. I hope I am wrong though. It seems that the Texture class handles reloading itself though, because every texture that isn’t a texture region works fine even after resuming after hitting the back key.
Any insight?