[英]Displaying FPS in LibGDX game
所以,我正在制作一個游戲,我需要在其中顯示 fps。 這就是它的樣子
你可以認為這沒關系,但如果我試圖飛走,文字會留在那里。 它不動。
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:制作 static 相機的想法不起作用。 它只是字面上不動。 如果我嘗試將文本與相機坐標同步,它會移動,但它會“搖晃”。
是否有另一種方法可以在屏幕上逐字顯示,或使其與相機正常同步?
這通常通過雙攝像頭來解決,一個攝像頭是您觀察游戲世界的游戲攝像頭,另一個是固定攝像頭,它是 HUD 的視圖。
HUD 攝像頭從不移動,並且經過配置,寬度和高度適合它應該顯示的任何圖形,這可以是但不一定是 window 的像素尺寸。
相機的 position 可以滿足您的需求,但如果您將postion
設置為viewportWidth / 2.0
、 viewportHeight / 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 的默認 bitmap 字體( 字體 PNG 文件和FONT.fnt )
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.