-
Notifications
You must be signed in to change notification settings - Fork 13
Config Options
By default, ScreenManager#dispose()
(which is called by ManagedApplication#dispose()
) only disposes screens and transitions, which were pushed but not yet hidden (regardless of whether they already started being rendered). In other words, #dispose()
is called for:
- the current screen,
- a screen which is rendered as part of a current transition,
- a transition that is going on at the moment, as well as
- any screens still queued to be shown.
Since screenmanager doesn't keep track of any old screens, #dispose()
is not called on screens that were previously shown (as long as they aren't still part of a transition at the moment).
A way around this is to enable automatic disposal for screens and transitions after they were hidden: ScreenManager#setAutoDispose
. This means that #dispose()
is called on the screen or transition right after hide()
. See here for a detailed walkthrough of the call order. If you enable automatic disposal, your screens and transitions cannot be reused. They'll have to be recreated instead.
For example, let's say this is our app:
public class MyGdxGame extends ManagedGame<ManagedScreen, ScreenTransition> {
private HashMap<String, ManagedScreen> screens = new HashMap<>();
@Override
public final void create() {
super.create();
screens.put("firstScreen", new FirstScreen());
screens.put("secondScreen", new SecondScreen());
screenManager.setAutoDispose(true, false); // i.e., automatically dispose screens, but not transitions
screenManager.pushScreen(screens.get("firstScreen"), null);
}
}
The fist screen looks like this:
public class FirstScreen extends ManagedScreen {
SpriteBatch batch = new SpriteBatch();
Texture img = new Texture(Gdx.files.internal("my-cool-image.png"));
@Override
public void render(float delta) {
// In a real application, we'd apply our viewport before and set the projection matrix of the batch
batch.begin();
batch.draw(img, 150, 150);
batch.end();
}
@Override
public void dispose() {
batch.dispose();
img.dispose();
}
}
Now we push a second screen: screenManager.pushScreen(screens.get("secondScreen"), null)
. This leads to the first screen being disposed. If the first screen is pushed again (screenManager.pushScreen(screens.get("firstScreen"), null);
) batch
and img
have already been disposed, so rendering with them won't work. If you want to push the first screen again, you'll have to create a new instance: screenManager.pushScreen(new FirstScreen(), null);
The screen manager can be set to use depth via ScreenManager#setHasDepth(boolean)
. This takes care of attaching a depth buffer to the internally used framebuffers.
If you need more control over the framebuffers used within screenmanager (for example to modify the builder), you can override ScreenManager#createFrameBuffer
.
For example, if you want to use a 24 bit depth buffer (which is often times needed for 3D games to look good), you can do this as follows:
public class MyGdxGame extends ManagedGame<ManagedScreen, ScreenTransition> {
public MyGdxGame() {
super(new ScreenManager<ManagedScreen, ScreenTransition>() {
@Override
protected NestableFrameBuffer createFrameBuffer() {
NestableFrameBuffer.NestableFrameBufferBuilder frameBufferBuilder = new NestableFrameBuffer.NestableFrameBufferBuilder(
HdpiUtils.toBackBufferX(currentWidth),
HdpiUtils.toBackBufferY(currentHeight));
frameBufferBuilder.addBasicColorTextureAttachment(Pixmap.Format.RGBA8888);
frameBufferBuilder.addDepthRenderBuffer(GL30.GL_DEPTH_COMPONENT24);
return (NestableFrameBuffer) frameBufferBuilder.build();
}
});
}
}
- Home
- Setup
-
Usage
- Quickstart!
- Screen Lifecycle
- Available transitions
- Technical stuff