所以,我正在制作一个游戏,我需要在其中显示 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();
}
Run Code Online (Sandbox Code Playgroud)
这是显示 fps 的代码。
我需要它随着我的屏幕移动。
UPD:制作静态相机的想法行不通。它只是字面上不动。如果我尝试将文本与相机坐标同步,它会移动,但会“晃动”。
是否有其他方法可以在屏幕上按字面显示它,或者使其与相机正常同步?
这通常通过双摄像头来解决,一个摄像头是观察游戏世界的游戏摄像头,另一个是固定摄像头,用于 HUD 的视图。
HUD 摄像头永远不会移动,并且经过配置,宽度和高度适合其要显示的任何图形,这可以是但不一定是窗口的像素尺寸。
相机的位置可以是任何适合您需要的位置,但如果您将 设为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);
Run Code Online (Sandbox Code Playgroud)
在该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();
Run Code Online (Sandbox Code Playgroud)
在下面的示例中,红色圆圈是玩家,黄色网格是游戏世界。玩家在游戏世界中移动,游戏摄像机跟随它,有时是静止的,在这两种操作期间,HUD 文本都是静止的,因为使用不移动的 HUD 摄像机渲染文本。该示例的完整源代码包含在图像后面,它使用libGDX github 中的默认位图字体(字体 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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3000 次 |
| 最近记录: |