Skip to content
Giacomo Tommaso Petrucci edited this page Sep 16, 2022 · 3 revisions

For managing how your game will look in different screens and screen sizes it is easiest to use a Viewport.

Viewports in Mini2Dx are very simple to use.

Examples

FitViewport

//Constructing the viewport
FitViewport fitViewport;
float gameWidth = 800;
float gameHeight = 600;

@Override
public void initialize() {
    fitViewport = new FitViewport(gameWidth, gameHeight);
}

//Inside the render() method
@Override
public void render(Graphics g) {
    fitViewport.apply(g);
    //do your rendering inside the viewport
}

Now when you run with this viewport your world aspect ratio will be fit to the 800x600 viewport size. When you resize the window, it will grow to fit the screen, but by maintaining the aspect ratio will apply black borders to the edges.

For more examples of how the viewports each look see LibGDx Viewports

ExtendViewport

FillViewport

FitViewport

ScalingViewport

ScreenViewport

StretchViewport

Clone this wiki locally