Reputation: 61
In the process of studying the framework libgdx I found a wonderful way to structure the project. And here's my example of loading a texture and displaying it.
realizing Singleton pattern of AssetManager
public class GameAsssetManager extends AssetManager {
private static GameAsssetManager instance;
public static GameAsssetManager getInstance(){
if(null == instance)
instance = new GameAsssetManager();
return instance;
}
public void init(){}
}
loading the picture
public class LoadScreen implements Screen{
private void loadAssets(){
GameAsssetManager.getInstance().load("badlogic.jpg",Texture.class);
}
@Override
public void show() {
GameAsssetManager.getInstance().init();
loadAssets();
}
@Override
public void render(float delta) {
//Метод update возвращает true в том случаи, если все ресурсы загружены
if(GameAsssetManager.getInstance().update()){
ScreenManager.getInstance().show(CustomScreen.GAME);
}
}
...
}
and finaly displaying the picture
public class GameScreen implements Screen {
...
@Override
public void show() {
batch = new SpriteBatch();
texture = GameAsssetManager.getInstance().get("badlogic.jpg");
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 1, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(
texture,
Gdx.graphics.getWidth()/2 - texture.getWidth()/2,
Gdx.graphics.getHeight()/2 - texture.getHeight()/2
);
batch.end();
}
...
So the problem is the following. When the application is compiled everything works fine. But if you close the application and launch the installed application anew, in the place where there should be displayed texture is displayed black square.
Upvotes: 1
Views: 109
Reputation: 1509
Try to implement Disposable to your GameAsssetManager class and release all resources in dispose() method. For my asset manager I use singleton as well and I don't have any memory issues and other stuff. I mean, release in life cycle methods like "onPause", "onStop", "onDestroy". And be sure that you will reload you resources when you start an application again.
Upvotes: 2
Reputation: 93591
If you are targeting Android or iOS, you shouldn't use a singleton (or any static reference) for your assets. They will leak and you'll end up with corrupted/blank textures when you leave the app and resume it.
For example, on Android, when you close the game (which finishes the Activity that hosts it), Android keeps your Application open, so static references are preserved. When the game is opened again, a new Activity is instantiating to host a new OpenGL surface. All your old textures and shader's will be from the obsolete surface, but your game doesn't know that. Also, you may have leaked all that texture memory.
This doesn't only happen when you close the game. Something similar (GL context loss) can sometimes happen merely from multitasking away and coming back.
Upvotes: 1
Reputation: 711
Android loosing GLContext after pausing activity. Please refer to http://www.badlogicgames.com/wordpress/?p=1073
Upvotes: 1