gpt4 book ai didi

java - 在 LibGDX 游戏中显示 FPS

转载 作者:行者123 更新时间:2023-12-02 19:28:49 24 4
gpt4 key购买 nike

所以,我正在制作游戏,我需要在其中显示 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:制作静态相机的想法行不通。它只是字面上不动。如果我尝试将文本与相机坐标同步,它会移动,但会“摇晃”。

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

最佳答案

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

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

相机的位置可以根据您的需要设置,但是如果您将postion设置为viewportWidth/2.0viewportHeight/2.0您将获得 HUD 的视口(viewport),其中 (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 fileFONT .fnt file)的默认位图字体

HUD camera example

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();
}
}

关于java - 在 LibGDX 游戏中显示 FPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62115541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com