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.