diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/viewer/Viewer.java | 14 |
1 files changed, 9 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 a424f99..030dc97 100644 --- a/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java +++ b/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java | |||
@@ -28,6 +28,7 @@ import java.util.Objects; | |||
28 | * @author Adam NAILI | 28 | * @author Adam NAILI |
29 | */ | 29 | */ |
30 | public final class Viewer { | 30 | public final class Viewer { |
31 | private static final Duration FRAME_DURATION = Duration.ofMillis((long) 1000.0 / 60); | ||
31 | 32 | ||
32 | private final Game currentGame; | 33 | private final Game currentGame; |
33 | 34 | ||
@@ -43,15 +44,18 @@ public final class Viewer { | |||
43 | */ | 44 | */ |
44 | public void eventLoop(ApplicationContext applicationContext) { | 45 | public void eventLoop(ApplicationContext applicationContext) { |
45 | List<Event> events = new LinkedList<>(); | 46 | List<Event> events = new LinkedList<>(); |
47 | Duration lastExecDuration = Duration.ZERO; | ||
46 | while (!currentGame.isOver()) { | 48 | while (!currentGame.isOver()) { |
49 | Duration last = lastExecDuration; | ||
47 | long timeBeforeExec = System.currentTimeMillis(); | 50 | long timeBeforeExec = System.currentTimeMillis(); |
48 | applicationContext.renderFrame(graphics2D -> { | 51 | applicationContext.renderFrame(graphics2D -> { |
49 | events.addAll(renderFrame(graphics2D, applicationContext, events)); //add the new events returned by updates | 52 | events.addAll(renderFrame(graphics2D, applicationContext, events, last)); //add the new events returned by updates |
50 | }); | 53 | }); |
51 | long timeAfterExec = System.currentTimeMillis(); | 54 | long timeAfterExec = System.currentTimeMillis(); |
52 | long delay = timeAfterExec - timeBeforeExec; | 55 | lastExecDuration = Duration.ofMillis(timeAfterExec - timeBeforeExec); |
53 | try { | 56 | try { |
54 | Thread.sleep(delay > 0 ? delay : 0); | 57 | Duration sleepDuration = FRAME_DURATION.minus(lastExecDuration); |
58 | if (!sleepDuration.isNegative()) Thread.sleep(sleepDuration.toMillis()); | ||
55 | } catch (Exception e) { | 59 | } catch (Exception e) { |
56 | applicationContext.exit(-1); | 60 | applicationContext.exit(-1); |
57 | } | 61 | } |
@@ -63,11 +67,11 @@ public final class Viewer { | |||
63 | * @param graphics2D the graphic2D from Zen 5 | 67 | * @param graphics2D the graphic2D from Zen 5 |
64 | * @param applicationContext the application context from Zen 5 | 68 | * @param applicationContext the application context from Zen 5 |
65 | */ | 69 | */ |
66 | public List<Event> renderFrame(Graphics2D graphics2D, ApplicationContext applicationContext, List<Event> events) { | 70 | public List<Event> renderFrame(Graphics2D graphics2D, ApplicationContext applicationContext, List<Event> events, Duration lastExecDuration) { |
67 | InputHandler inputHandler = new InputHandler(applicationContext); | 71 | InputHandler inputHandler = new InputHandler(applicationContext); |
68 | ScreenManager screenManager = new ScreenManager(applicationContext, graphics2D); | 72 | ScreenManager screenManager = new ScreenManager(applicationContext, graphics2D); |
69 | events.addAll(inputHandler.getEvents()); | 73 | events.addAll(inputHandler.getEvents()); |
70 | Context context = new Context(currentGame, events, screenManager.clearScreen(), Duration.ZERO); // TODO: duration | 74 | Context context = new Context(currentGame, events, screenManager.clearScreen(), lastExecDuration); |
71 | List<Event> newEvents = currentGame.update(context); //return new events created from update(); | 75 | List<Event> newEvents = currentGame.update(context); //return new events created from update(); |
72 | events.clear(); | 76 | events.clear(); |
73 | return newEvents; | 77 | return newEvents; |