aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr
diff options
context:
space:
mode:
authorpacien2018-02-04 13:46:08 +0100
committerpacien2018-02-04 13:46:08 +0100
commit4065355f84658220193bff3e65dcb6af4a0cd748 (patch)
tree1f9881c0eaf3da1b2277c02d9b9242927c383e56 /src/main/java/fr
parente2cdb2a3d02159c18ae48142781248e1aa4beb6f (diff)
downloadwallj-4065355f84658220193bff3e65dcb6af4a0cd748.tar.gz
Fix execution timer
Signed-off-by: pacien <pacien.trangirard@pacien.net>
Diffstat (limited to 'src/main/java/fr')
-rw-r--r--src/main/java/fr/umlv/java/wallj/viewer/Viewer.java8
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 }