[LibGdx] Is this proper screen scalling

I’m trying to scale my game to fit multiple resolutions. So far what I’ve done is working, but It seems like it may become a headache in the future. How can I just code for one resolution and have it scale properly to whatever the window size is? I don’t want the black bars on the side of my game, and I don’t want the scaling to look very stretched (and…antialiased?). Here’s my current typical screen class. It scales the screen correctly, but have to use a lot of math for positioning that I don’t feel is necessary:


public class Play implements Screen{	
	//Util
	Core game;
	ShapeRenderer sr;
	SpriteBatch g;
	OrthographicCamera cam;
	Viewport vp;
	BitmapFont font;
	
	float worldWidth = 640, worldHeight = 480;//size aimed for
	
	
	public Play(Core game) {
		this.game = game;
	}
	
	
	public void show() {
		//Util
		sr = new ShapeRenderer();
		g = new SpriteBatch();
		cam = new OrthographicCamera();
		
		vp = new StretchViewport(worldWidth, worldHeight, cam);
		vp.apply();
		cam.position.set(worldWidth/2, worldHeight/2, 0);
		
		font = new BitmapFont();
		font.setColor(Color.BLUE);	
	}
	
	
	
	public void render(float delta) {
		Gdx.gl.glClearColor(0.8f, 0, 0.8f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        cam.update();
		sr.setProjectionMatrix(cam.combined);
		g.setProjectionMatrix(cam.combined);		
		
		//Utilities
		g.begin();
		font.draw(g, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 300);
		g.end();
		
		sr.begin(ShapeType.Filled);
		sr.setColor(Color.ORANGE);
		sr.rect(vp.getWorldWidth()/ 3 - 100, 30, 100, 27);
		sr.rect(vp.getWorldWidth()/ 3 + 50, 30, 100, 27);
		sr.rect(vp.getWorldWidth()/ 3 + 200, 30, 100, 27);
		sr.end();
		
	}
	
	public void resize(int width, int height) {
		vp.update(width, height);
	}
}

(I cut out dispose, hide methods to shorten code. they were empty)

Also, the font becomes larger but not scaled acoordingly so it looks quite ugly.


public class DesktopLauncher {
	public static void main(String[] arg) {
		LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
		config.width = Game.WIDTH * Game.SCALE;
		config.height = Game.HEIGHT * Game.SCALE;
		config.title = Game.title;
		new LwjglApplication(new Game(), config);
	}
}

I make my game using a single setting, and just scale it through here. I didn’t test it properly, but at least you won’t get black bars around the game, if there are any problems, do tell.

Doesn’t that just set the starting dimensions and title? When I resize my window It still worked the same.

I don’t know that much about LibGDX, but I was looking at this, and it seems there might be some things there that could meet your needs.

Forgive me if this is stuff you already know, but it seems either ScreenViewport or ExtendViewport would be most likely to give you the results you’re after. It seems ScreenViewport would eliminate artifacts due to e.g. fonts being scaled up, but on the other hand, at larger screen sizes, things would be (proportionally) smaller on screen, and players would be able to see much more content. ExtendViewport could introduce scaling artifacts, but sizes would be more consistent and the potential difference in the amount of content that was visible would be less. Also, ExtendViewport appears to have the nice feature that you can set maximum dimensions, meaning that past a certain size in each direction, letter/pillar-boxing will be used. (I know you said you didn’t want that, but for some games it might be a practical necessity due to art or gameplay constraints.)

In any case, if you haven’t tried ScreenViewport or ExtendViewport yet, that might be a good place to start.

Edit: Regarding having to use a lot of math for positioning, it may be that some manual positioning will be required, even with the above viewport types. This may be difficult to avoid though if you want to support different aspect ratios.