fix framebuffer scaling, aspect ratio

This commit is contained in:
2025-11-14 10:47:28 +01:00
parent 17c59049f3
commit 9a59d15859
4 changed files with 40 additions and 19 deletions

View File

@@ -40,7 +40,7 @@ public class GameScreen implements Screen {
private SpriteBatch screenBatch; private SpriteBatch screenBatch;
private static final int RETRO_WIDTH = 320; private static final int RETRO_WIDTH = 320;
private static final int RETRO_HEIGHT = 180; private static final int RETRO_HEIGHT = 240;
private boolean showHud = false; private boolean showHud = false;
@@ -51,8 +51,8 @@ public class GameScreen implements Screen {
private void initCamera() { private void initCamera() {
camera = new PerspectiveCamera( camera = new PerspectiveCamera(
67f, 67f,
Gdx.graphics.getWidth(), RETRO_WIDTH,
Gdx.graphics.getHeight() RETRO_HEIGHT
); );
// near/far + initial direction handled in controller constructor // near/far + initial direction handled in controller constructor
@@ -167,26 +167,46 @@ public class GameScreen implements Screen {
frameBuffer.end(); frameBuffer.end();
// -- scale framebuffer to screen // -- scale framebuffer to screen, preserve aspect ratio --
int screenW = Gdx.graphics.getWidth(); int windowW = Gdx.graphics.getWidth(); // logical size
int screenH = Gdx.graphics.getHeight(); int windowH = Gdx.graphics.getHeight();
int fbWidth = Gdx.graphics.getBackBufferWidth();
int fbHeight = Gdx.graphics.getBackBufferHeight();
Gdx.gl.glViewport(0, 0, fbWidth, fbHeight); int backW = Gdx.graphics.getBackBufferWidth(); // actual GL framebuffer
int backH = Gdx.graphics.getBackBufferHeight();
// compute how much we can scale the FBO into the window
float scale = Math.min(
windowW / (float) RETRO_WIDTH,
windowH / (float) RETRO_HEIGHT
);
// strict integer scaling
// scale = (float)Math.max(1, Math.floor(scale));
int drawW = Math.round(RETRO_WIDTH * scale);
int drawH = Math.round(RETRO_HEIGHT * scale);
// center the image
int offsetX = (windowW - drawW) / 2;
int offsetY = (windowH - drawH) / 2;
// viewport in backbuffer coordinates
Gdx.gl.glViewport(0, 0, backW, backH);
Gdx.gl.glClearColor(0f, 0f, 0f, 1f); Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// projection in window (logical) coordinates
screenBatch.getProjectionMatrix().setToOrtho2D(0, 0, windowW, windowH);
screenBatch.begin(); screenBatch.begin();
// draw retro texture stretched to window size screenBatch.draw(frameRegion, offsetX, offsetY, drawW, drawH);
screenBatch.draw(frameRegion, 0, 0, screenW, screenH);
screenBatch.end(); screenBatch.end();
} }
@Override @Override
public void resize(int width, int height) { public void resize(int width, int height) {
camera.viewportWidth = width; camera.viewportWidth = RETRO_WIDTH;
camera.viewportHeight = height; camera.viewportHeight = RETRO_HEIGHT;
camera.update(); camera.update();
cameraController.resize(width, height); cameraController.resize(width, height);
@@ -207,7 +227,7 @@ public class GameScreen implements Screen {
modelBatch.dispose(); modelBatch.dispose();
hud.dispose(); hud.dispose();
if (shadowBatch != null) shadowBatch.dispose(); shadowBatch.dispose();
if (shadowLight != null) shadowLight.dispose(); shadowLight.dispose();
} }
} }

View File

@@ -17,7 +17,7 @@ public class FpsCameraController {
private float yaw = 0f; private float yaw = 0f;
private float pitch = 0f; private float pitch = 0f;
private float eyeHeight = 1.6f; private float eyeHeight = 1.8f;
private float velocityY = 0f; private float velocityY = 0f;
private float gravity = -50f; private float gravity = -50f;
private float jumpSpeed = 12f; private float jumpSpeed = 12f;

View File

@@ -42,10 +42,10 @@ public class World3D {
); );
// plane size (halfSize in X/Z) // plane size (halfSize in X/Z)
float halfSize = 20f; float halfSize = 32f;
// times the texture repeats across the whole plane // times the texture repeats across the whole plane
float tileScale = 8f; float tileScale = 16f;
modelBuilder.begin(); modelBuilder.begin();

View File

@@ -29,6 +29,7 @@ public class Lwjgl3Launcher {
//// You may also need to configure GPU drivers to fully disable Vsync; this can cause screen tearing. //// You may also need to configure GPU drivers to fully disable Vsync; this can cause screen tearing.
configuration.setWindowedMode(640, 480); configuration.setWindowedMode(640, 480);
//// You can change these files; they are in lwjgl3/src/main/resources/ . //// You can change these files; they are in lwjgl3/src/main/resources/ .
//// They can also be loaded from the root of assets/ . //// They can also be loaded from the root of assets/ .
configuration.setWindowIcon("libgdx128.png", "libgdx64.png", "libgdx32.png", "libgdx16.png"); configuration.setWindowIcon("libgdx128.png", "libgdx64.png", "libgdx32.png", "libgdx16.png");
@@ -42,4 +43,4 @@ public class Lwjgl3Launcher {
return configuration; return configuration;
} }
} }