From f02f41e2e7251112a41bea1501bdf1809b8c47b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Mon, 5 Apr 2021 19:01:25 +0200 Subject: [PATCH] feat: seperated HUD batch from GameScreen batch --- core/src/bzh/fainsin/sagittarius/Entity.java | 1 - .../bzh/fainsin/sagittarius/GameScreen.java | 20 ++++++++-------- core/src/bzh/fainsin/sagittarius/HUD.java | 23 +++++++++++++++---- core/src/bzh/fainsin/sagittarius/Planet.java | 2 +- .../fainsin/sagittarius/SagittariusGame.java | 4 +++- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/core/src/bzh/fainsin/sagittarius/Entity.java b/core/src/bzh/fainsin/sagittarius/Entity.java index 9e6d110..6951c1d 100644 --- a/core/src/bzh/fainsin/sagittarius/Entity.java +++ b/core/src/bzh/fainsin/sagittarius/Entity.java @@ -27,7 +27,6 @@ abstract class Entity { // ---------- GETs ---------- - Vector2 getPosition() { return this.position; } diff --git a/core/src/bzh/fainsin/sagittarius/GameScreen.java b/core/src/bzh/fainsin/sagittarius/GameScreen.java index 0b92aa3..1b472b9 100644 --- a/core/src/bzh/fainsin/sagittarius/GameScreen.java +++ b/core/src/bzh/fainsin/sagittarius/GameScreen.java @@ -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 ---------- - for (Planet planet : SagittariusGame.planetList) { - planet.render(shapeRenderer); - } + // planets + for (Planet planet : SagittariusGame.planetList) { + planet.render(shapeRenderer); + } shapeRenderer.end(); + // HUD + hud.render(); + } @Override diff --git a/core/src/bzh/fainsin/sagittarius/HUD.java b/core/src/bzh/fainsin/sagittarius/HUD.java index 574a32a..2321b33 100644 --- a/core/src/bzh/fainsin/sagittarius/HUD.java +++ b/core/src/bzh/fainsin/sagittarius/HUD.java @@ -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(); } } diff --git a/core/src/bzh/fainsin/sagittarius/Planet.java b/core/src/bzh/fainsin/sagittarius/Planet.java index aef6924..aa1a103 100644 --- a/core/src/bzh/fainsin/sagittarius/Planet.java +++ b/core/src/bzh/fainsin/sagittarius/Planet.java @@ -24,7 +24,7 @@ class Planet extends Entity { this.radius = radius; } -// ---------- METHODS ---------- +// ---------- METHODs ---------- void render(ShapeRenderer shapeRenderer) { shapeRenderer.setColor(color); diff --git a/core/src/bzh/fainsin/sagittarius/SagittariusGame.java b/core/src/bzh/fainsin/sagittarius/SagittariusGame.java index 2eabd58..a46d878 100644 --- a/core/src/bzh/fainsin/sagittarius/SagittariusGame.java +++ b/core/src/bzh/fainsin/sagittarius/SagittariusGame.java @@ -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(); 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);