From 9f68ba33f3c52af34d033160d73dad4257d8c636 Mon Sep 17 00:00:00 2001 From: elliotjarnit Date: Wed, 22 Nov 2023 11:34:20 -0800 Subject: [PATCH] get fps --- .../elliotengine/Graphics/RenderingEngine.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/elliotjarnit/elliotengine/Graphics/RenderingEngine.java b/src/main/java/dev/elliotjarnit/elliotengine/Graphics/RenderingEngine.java index 10e5068..bfc0373 100644 --- a/src/main/java/dev/elliotjarnit/elliotengine/Graphics/RenderingEngine.java +++ b/src/main/java/dev/elliotjarnit/elliotengine/Graphics/RenderingEngine.java @@ -17,6 +17,8 @@ public class RenderingEngine extends JPanel { private EScene scene; private EOverlay overlay; private final ElliotEngine engine; + private double lastFrameTime = System.nanoTime(); + private int fps = 0; public RenderingEngine(ElliotEngine engine) { super(); @@ -29,6 +31,11 @@ public RenderingEngine(ElliotEngine engine) { public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; + // Calculate FPS + double currentTime = System.nanoTime(); + fps = (int) (1 / ((currentTime - lastFrameTime) / 1000000000)); + lastFrameTime = currentTime; + // Clear screen g2d.setColor(scene.getSkyColor().toAwtColor()); g2d.fillRect(0, 0, this.getWidth(), this.getHeight()); @@ -185,7 +192,11 @@ public void drawCenteredString(Graphics g, String text, Rectangle rect, Font fon g.drawString(text, x, y); } - public boolean sameSide(Vector3 A, Vector3 B, Vector3 C, Vector3 p){ + public int getFPS() { + return this.fps; + } + + private boolean sameSide(Vector3 A, Vector3 B, Vector3 C, Vector3 p){ Vector3 V1V2 = new Vector3(B.x - A.x,B.y - A.y,B.z - A.z); Vector3 V1V3 = new Vector3(C.x - A.x,C.y - A.y,C.z - A.z); Vector3 V1P = new Vector3(p.x - A.x,p.y - A.y,p.z - A.z);