From ccbadd632894de2e8e5cf63a2a7447c912527d1a Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Mon, 11 May 2020 09:35:59 +0200 Subject: [PATCH] added fps counter --- core/src/netwerkprog/game/FrameRate.java | 62 +++++++++++++++++++ .../game/GameApplicationConfiguration.java | 1 + core/src/netwerkprog/game/MainGame.java | 4 ++ .../game/desktop/DesktopLauncher.java | 2 - 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 core/src/netwerkprog/game/FrameRate.java diff --git a/core/src/netwerkprog/game/FrameRate.java b/core/src/netwerkprog/game/FrameRate.java new file mode 100644 index 0000000..085bd84 --- /dev/null +++ b/core/src/netwerkprog/game/FrameRate.java @@ -0,0 +1,62 @@ +package netwerkprog.game; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.utils.Disposable; +import com.badlogic.gdx.utils.TimeUtils; + +/** + * A nicer class for showing framerate that doesn't spam the console + * like Logger.log() + * + * @author William Hartman + */ +public class FrameRate implements Disposable{ + long lastTimeCounted; + private float sinceChange; + private float frameRate; + private BitmapFont font; + private SpriteBatch batch; + private OrthographicCamera cam; + + + public FrameRate() { + lastTimeCounted = TimeUtils.millis(); + sinceChange = 0; + frameRate = Gdx.graphics.getFramesPerSecond(); + font = new BitmapFont(); + batch = new SpriteBatch(); + cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + } + + public void resize(int screenWidth, int screenHeight) { + cam = new OrthographicCamera(screenWidth, screenHeight); + cam.translate(screenWidth / 2, screenHeight / 2); + cam.update(); + batch.setProjectionMatrix(cam.combined); + } + + public void update() { + long delta = TimeUtils.timeSinceMillis(lastTimeCounted); + lastTimeCounted = TimeUtils.millis(); + + sinceChange += delta; + if(sinceChange >= 1000) { + sinceChange = 0; + frameRate = Gdx.graphics.getFramesPerSecond(); + } + } + + public void render() { + batch.begin(); + font.draw(batch, (int)frameRate + " fps", 3, Gdx.graphics.getHeight() - 3); + batch.end(); + } + + public void dispose() { + font.dispose(); + batch.dispose(); + } +} diff --git a/core/src/netwerkprog/game/GameApplicationConfiguration.java b/core/src/netwerkprog/game/GameApplicationConfiguration.java index f1b67de..fb37fdb 100644 --- a/core/src/netwerkprog/game/GameApplicationConfiguration.java +++ b/core/src/netwerkprog/game/GameApplicationConfiguration.java @@ -1,6 +1,7 @@ package netwerkprog.game; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; +import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration; /** * custom application config class for the game diff --git a/core/src/netwerkprog/game/MainGame.java b/core/src/netwerkprog/game/MainGame.java index f91f753..e66162d 100644 --- a/core/src/netwerkprog/game/MainGame.java +++ b/core/src/netwerkprog/game/MainGame.java @@ -17,6 +17,7 @@ public class MainGame extends ApplicationAdapter { float yPos = 500; float xUpdate; float yUpdate; + private FrameRate frameRate; @Override public void create() { @@ -25,6 +26,7 @@ public class MainGame extends ApplicationAdapter { float ratio = (float) Gdx.graphics.getWidth() / Gdx.graphics.getHeight(); xUpdate = ratio; yUpdate = ratio; + frameRate = new FrameRate(); // play music Music music = Gdx.audio.newMusic(Gdx.files.getFileHandle("core/assets/music.mp3", Files.FileType.Internal)); @@ -41,6 +43,8 @@ public class MainGame extends ApplicationAdapter { updatePos(); batch.draw(img, xPos, yPos); batch.end(); + frameRate.update(); + frameRate.render(); } private void updatePos() { diff --git a/desktop/src/netwerkprog/game/desktop/DesktopLauncher.java b/desktop/src/netwerkprog/game/desktop/DesktopLauncher.java index 4f6ba0a..9f91943 100644 --- a/desktop/src/netwerkprog/game/desktop/DesktopLauncher.java +++ b/desktop/src/netwerkprog/game/desktop/DesktopLauncher.java @@ -2,9 +2,7 @@ package netwerkprog.game.desktop; import com.badlogic.gdx.backends.lwjgl.LwjglApplication; import netwerkprog.game.MainGame; -import temp.Animator; import netwerkprog.game.GameApplicationConfiguration; -import temp.Networking; public class DesktopLauncher { public static void main (String[] arg) {