diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/viewer/Viewer.java | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java b/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java index e65389e..6b4f5c4 100644 --- a/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java +++ b/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java | |||
@@ -37,7 +37,7 @@ public final class Viewer { | |||
37 | Duration lastExecDuration = Duration.ZERO; | 37 | Duration lastExecDuration = Duration.ZERO; |
38 | while (!currentGame.isOver()) { | 38 | while (!currentGame.isOver()) { |
39 | Duration last = lastExecDuration; | 39 | Duration last = lastExecDuration; |
40 | long timeBeforeExec = System.currentTimeMillis(); | 40 | StopWatch stopWatch = new StopWatch(); |
41 | applicationContext.renderFrame(graphics2D -> { | 41 | applicationContext.renderFrame(graphics2D -> { |
42 | InputHandler inputHandler = new InputHandler(applicationContext); | 42 | InputHandler inputHandler = new InputHandler(applicationContext); |
43 | ScreenManager screenManager = new ScreenManager(applicationContext, graphics2D); | 43 | ScreenManager screenManager = new ScreenManager(applicationContext, graphics2D); |
@@ -47,11 +47,9 @@ public final class Viewer { | |||
47 | events.clear(); | 47 | events.clear(); |
48 | events.addAll(newEvents); //add the new events returned by updates | 48 | events.addAll(newEvents); //add the new events returned by updates |
49 | }); | 49 | }); |
50 | long timeAfterExec = System.currentTimeMillis(); | 50 | lastExecDuration = FRAME_DURATION.minus(stopWatch.peek()); |
51 | lastExecDuration = Duration.ofMillis(timeAfterExec - timeBeforeExec); | ||
52 | try { | 51 | try { |
53 | Duration sleepDuration = FRAME_DURATION.minus(lastExecDuration); | 52 | if (!lastExecDuration.isNegative()) Thread.sleep(lastExecDuration.toMillis()); |
54 | if (!sleepDuration.isNegative()) Thread.sleep(sleepDuration.toMillis()); | ||
55 | } catch (Exception e) { | 53 | } catch (Exception e) { |
56 | applicationContext.exit(-1); | 54 | applicationContext.exit(-1); |
57 | } | 55 | } |