feat: seperated HUD batch from GameScreen batch

This commit is contained in:
Laureηt 2021-04-05 19:01:25 +02:00
parent 4b36703287
commit f02f41e2e7
5 changed files with 32 additions and 18 deletions

View file

@ -27,7 +27,6 @@ abstract class Entity {
// ---------- GETs ----------
Vector2 getPosition() {
return this.position;
}

View file

@ -42,6 +42,7 @@ class GameScreen extends ScreenAdapter {
private void update(float delta) {
SagittariusGame.update(delta);
hud.update();
camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
camera.update();
}
@ -52,26 +53,25 @@ class GameScreen extends ScreenAdapter {
clearScreen();
// ---------- batch draw ----------
// ---------- batch ----------
batch.setProjectionMatrix(camera.combined);
batch.begin();
// ---------- HUD ----------
hud.render(batch, font);
batch.end();
// ---------- shapeRenderer draw ----------
// ---------- shapeRenderer ----------
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
// ---------- planets ----------
// planets
for (Planet planet : SagittariusGame.planetList) {
planet.render(shapeRenderer);
}
shapeRenderer.end();
// HUD
hud.render();
}
@Override

View file

@ -3,21 +3,34 @@ package bzh.fainsin.sagittarius;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Disposable;
class HUD {
class HUD implements Disposable {
// ATTRIBUTS
// ---------- ATTRIBUTEs ----------
private int frameRate;
// MÉTHODES
private BitmapFont font = new BitmapFont();
private Batch batch = new SpriteBatch();
// ---------- METHODs ----------
public void update() {
frameRate = Gdx.graphics.getFramesPerSecond();
}
public void render(Batch batch, BitmapFont font) {
font.draw(batch, frameRate + " fps", 3, GameScreen.HEIGHT - 3);
public void render() {
batch.begin();
font.draw(batch, frameRate + " fps", 3, Gdx.graphics.getHeight() - 3);
batch.end();
}
@Override
public void dispose() {
batch.dispose();
font.dispose();
}
}

View file

@ -24,7 +24,7 @@ class Planet extends Entity {
this.radius = radius;
}
// ---------- METHODS ----------
// ---------- METHODs ----------
void render(ShapeRenderer shapeRenderer) {
shapeRenderer.setColor(color);

View file

@ -4,6 +4,7 @@ import java.util.ArrayList;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
@ -26,11 +27,12 @@ public class SagittariusGame extends Game {
planetList = new ArrayList<Planet>();
planetList.add( new Planet(new Vector2(100, 100), 1, 50) );
planetList.add( new Planet(new Vector2(1500, 1000), 1, 200, Color.CYAN) );
}
static void update(float delta) {
// ---------- cursors ----------
// cursors
screenCursor = new Vector2(Gdx.input.getX(), Gdx.input.getY());
Vector3 unprojectedCursor = GameScreen.camera.unproject(new Vector3(screenCursor, 0));
worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y);