在 LibGDX 游戏中显示 FPS

Displaying FPS in LibGDX game

所以,我正在制作游戏,我需要在其中显示 fps。 It is how it looks like

你可以认为这没关系,但如果我试图飞走,文字就会留在那儿。它没有移动。

    public void render(SpriteBatch batch) {
        batch.begin();
        Draw.draw();
        MainScreen.player.draw();
        TextManager.displayMessage("FPS: "+ Gdx.graphics.getFramesPerSecond(), true, false, false, false);
        PlayerControl.update();
        CamControl.update();
        UI.drawCurrentBlock();
        batch.end();
    }

这是一个显示fps的代码。

我需要它随着我的屏幕移动。

UPD:制作静态相机的想法行不通。它只是字面上不动。 如果我尝试将文本与相机坐标同步,它会移动,但它是 'shaking'.

是否有其他方法可以在屏幕上显示它,或者使其与相机正常同步?

这通常用双摄像头解决,一个摄像头是观察游戏世界的游戏摄像头,另一个是固定摄像头,即 HUD 的视图。

HUD 摄像头从不移动,并且配置为宽度和高度适合它应该显示的任何图形,这可以但不一定是 window 的像素尺寸.

摄像机的位置可以是任何适合您需要的位置,但是如果您将 postion 设置为 viewportWidth / 2.0viewportHeight / 2.0 您将获得 HUD 的视口,其中 (0, 0)是屏幕左下角:

例如:

hudCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
hudCamera.position.set(hudCamera.viewportWidth / 2.0f, hudCamera.viewportHeight / 2.0f, 1.0f);

render 方法中,这可以用来绘制文本:

hudCamera.update();
spriteBatch.setProjectionMatrix(hudCamera.combined);
spriteBatch.begin();
font.draw(spriteBatch, "Upper left, FPS=" + Gdx.graphics.getFramesPerSecond(), 0, hudCamera.viewportHeight);
font.draw(spriteBatch, "Lower left", 0, font.getLineHeight());
spriteBatch.end();

下图红圈为玩家,黄格为游戏世界。玩家在游戏世界中移动,游戏摄像机跟随它,有时是静止的,在这两个操作期间,HUD 文本是静止的,因为它是使用不移动的 HUD 摄像机渲染的。该示例的完整源代码包含在图像之后,它使用 libGDX github (Font PNG file and FONT .fnt file)[= 中的默认位图字体20=]

package somepackage;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;

public class HudExampleGame extends Game {

    private OrthographicCamera gameCamera;
    private OrthographicCamera hudCamera;
    private ShapeRenderer shapeRenderer;

    private Vector2 playerPosition = new Vector2();
    private boolean cameraFollowsPlayer = true;

    private SpriteBatch spriteBatch;
    private BitmapFont font;

    @Override
    public void create() {
        float aspectRatio = (float) Gdx.graphics.getHeight() / (float) Gdx.graphics.getWidth();
        float viewableWorldWidth = 32.0f;
        gameCamera = new OrthographicCamera(viewableWorldWidth, viewableWorldWidth * aspectRatio);
        gameCamera.position.set(playerPosition.x, playerPosition.y, 1.0f);

        hudCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        hudCamera.position.set(hudCamera.viewportWidth / 2.0f, hudCamera.viewportHeight / 2.0f, 1.0f);

        shapeRenderer = new ShapeRenderer();
        spriteBatch = new SpriteBatch();

        font = new BitmapFont(Gdx.files.internal("default.fnt"));
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Use the game camera to render the game world
        gameCamera.update();

        shapeRenderer.setProjectionMatrix(gameCamera.combined);
        shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
        shapeRenderer.setColor(Color.YELLOW);

        for (int x = -32; x <= 32; ++x)
            shapeRenderer.line(x, -32, x, 32);

        for (int y = -32; y <= 32; ++y)
            shapeRenderer.line(-32, y, 32, y);

        shapeRenderer.setColor(Color.RED);
        shapeRenderer.circle(playerPosition.x, playerPosition.y, 1.0f, 24);

        Vector2 movement = new Vector2();
        if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
            movement.x -= 1;
        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
            movement.x += 1;
        if (Gdx.input.isKeyPressed(Input.Keys.UP))
            movement.y += 1;
        if (Gdx.input.isKeyPressed(Input.Keys.DOWN))
            movement.y -= 1;
        if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE))
            cameraFollowsPlayer = !cameraFollowsPlayer;

        playerPosition.add(movement.scl(Gdx.graphics.getDeltaTime() * 8.0f));
        if (cameraFollowsPlayer)
            gameCamera.position.set(playerPosition.x, playerPosition.y, 1.0f);

        shapeRenderer.end();

        // Use the HUD camera to render the text
        hudCamera.update();
        spriteBatch.setProjectionMatrix(hudCamera.combined);
        spriteBatch.begin();
        font.draw(spriteBatch, "Upper left, FPS=" + Gdx.graphics.getFramesPerSecond(), 0, hudCamera.viewportHeight);
        font.draw(spriteBatch, "Lower left", 0, font.getLineHeight());
        spriteBatch.end();
    }
}