From 4fa8fabe39df19fd1f303ea8054170a9c05eec87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mora?= Date: Mon, 4 May 2020 09:47:43 +0200 Subject: [PATCH] Clean code --- .../src/main/java/module-info.java | 2 + .../main/java/org/sheepy/vsand/Extenders.java | 8 +- .../vsand/VSandApplicationLauncher.java | 23 ++---- .../sheepy/vsand/VSandBenchmarkLauncher.java | 23 ++---- .../vsand/VSandHeadlessBenchmarkLauncher.java | 8 +- .../vsand/audio/MaterialSoundManager.java | 33 ++++++++ .../org/sheepy/vsand/draw/DrawManager.java | 78 ++++++------------ .../sheepy/vsand/input/AbstractDrawer.java | 72 ++++++++++++++++ ...andInputManager.java => InputManager.java} | 54 +++--------- .../org/sheepy/vsand/input/PrimaryDrawer.java | 41 ++++++++++ .../sheepy/vsand/input/SecondaryDrawer.java | 41 ++++++++++ .../vsand/{ => logic}/VSandMainLoop.java | 82 ++++++------------- .../java/org/sheepy/vsand/test/VSandTest.java | 6 +- 13 files changed, 277 insertions(+), 194 deletions(-) create mode 100644 org.sheepy.vsand/src/main/java/org/sheepy/vsand/audio/MaterialSoundManager.java create mode 100644 org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/AbstractDrawer.java rename org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/{VSandInputManager.java => InputManager.java} (72%) create mode 100644 org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/PrimaryDrawer.java create mode 100644 org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/SecondaryDrawer.java rename org.sheepy.vsand/src/main/java/org/sheepy/vsand/{ => logic}/VSandMainLoop.java (59%) diff --git a/org.sheepy.vsand/src/main/java/module-info.java b/org.sheepy.vsand/src/main/java/module-info.java index 1bf2fde2..f42c496e 100644 --- a/org.sheepy.vsand/src/main/java/module-info.java +++ b/org.sheepy.vsand/src/main/java/module-info.java @@ -11,6 +11,7 @@ requires org.sheepy.lily.vulkan.extra.api; exports org.sheepy.vsand; + exports org.sheepy.vsand.logic; exports org.sheepy.vsand.model; exports org.sheepy.vsand.model.impl; @@ -19,6 +20,7 @@ opens org.sheepy.vsand; opens org.sheepy.vsand.constants; opens org.sheepy.vsand.draw; + opens org.sheepy.vsand.input; opens org.sheepy.vsand.loader; opens org.sheepy.vsand.logic; opens org.sheepy.vsand.ui; diff --git a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/Extenders.java b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/Extenders.java index 3312d696..73b3e3c0 100644 --- a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/Extenders.java +++ b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/Extenders.java @@ -8,6 +8,9 @@ import org.sheepy.vsand.draw.DrawCircleAdapter; import org.sheepy.vsand.draw.DrawLineAdapter; import org.sheepy.vsand.draw.DrawSquareAdapter; +import org.sheepy.vsand.input.InputManager; +import org.sheepy.vsand.input.PrimaryDrawer; +import org.sheepy.vsand.input.SecondaryDrawer; import org.sheepy.vsand.loader.*; import org.sheepy.vsand.logic.ApplicationBehaviour; import org.sheepy.vsand.ui.MaterialSelectorInputProviderAdapter; @@ -33,6 +36,9 @@ public List> classifiers() DrawLineAdapter.class, ApplicationBehaviour.class, MaterialSelectorInputProviderAdapter.class, - MaterialCountAdapter.class); + MaterialCountAdapter.class, + InputManager.class, + PrimaryDrawer.class, + SecondaryDrawer.class); } } diff --git a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandApplicationLauncher.java b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandApplicationLauncher.java index c1dd0bf2..eb1fdc4b 100644 --- a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandApplicationLauncher.java +++ b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandApplicationLauncher.java @@ -3,6 +3,7 @@ import org.sheepy.lily.core.api.LilyLauncher; import org.sheepy.lily.core.api.resource.IResourceLoader; import org.sheepy.lily.core.api.util.DebugUtil; +import org.sheepy.vsand.logic.VSandMainLoop; import org.sheepy.vsand.model.VSandApplication; import java.io.IOException; @@ -14,7 +15,7 @@ public class VSandApplicationLauncher private static final IResourceLoader resourceLoader = IResourceLoader.INSTANCE; - public static void main(String[] args) + public static void main(String[] args) throws IOException { DebugUtil.parseMainArgs(args); final var application = loadApplication(); @@ -23,21 +24,13 @@ public static void main(String[] args) LilyLauncher.launch(application, mainLoop); } - public static VSandApplication loadApplication() + public static VSandApplication loadApplication() throws IOException { final var module = VSandApplicationLauncher.class.getModule(); - try - { - final var inputStream = module.getResourceAsStream(APPLICATION_PATH); - final var resource = resourceLoader.loadResource(inputStream); - final var res = (VSandApplication) resource.getContents().get(0); - res.setVersion(APPLICATION_VERSION); - return res; - } - catch (final IOException e) - { - e.printStackTrace(); - return null; - } + final var inputStream = module.getResourceAsStream(APPLICATION_PATH); + final var resource = resourceLoader.loadResource(inputStream); + final var res = (VSandApplication) resource.getContents().get(0); + res.setVersion(APPLICATION_VERSION); + return res; } } diff --git a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandBenchmarkLauncher.java b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandBenchmarkLauncher.java index 017014bf..8973ff4c 100644 --- a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandBenchmarkLauncher.java +++ b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandBenchmarkLauncher.java @@ -3,18 +3,20 @@ import org.eclipse.emf.common.util.EList; import org.eclipse.emf.ecore.util.EcoreUtil; import org.sheepy.lily.core.api.LilyLauncher; +import org.sheepy.vsand.logic.VSandMainLoop; import org.sheepy.vsand.model.DrawCommand; import org.sheepy.vsand.model.Material; import org.sheepy.vsand.model.VSandApplication; import org.sheepy.vsand.model.VSandFactory; +import java.io.IOException; import java.util.List; public class VSandBenchmarkLauncher { public static final int DEFAULT_ITERATION_COUNT = 3000; - public static void main(String[] args) + public static void main(String[] args) throws IOException { final var application = createTestApplication(); final var mainLoop = VSandMainLoop.createBenchmark(application, DEFAULT_ITERATION_COUNT); @@ -22,14 +24,12 @@ public static void main(String[] args) LilyLauncher.launch(application, mainLoop); } - public static VSandApplication createTestApplication() + public static VSandApplication createTestApplication() throws IOException { final var application = VSandApplicationLauncher.loadApplication(); // remove UI - EcoreUtil.delete(application.getScene() - .getCompositors() - .get(1)); + EcoreUtil.delete(application.getScene().getCompositors().get(1)); final DemoDrawer drawer = new DemoDrawer(application); drawer.drawLavaPot(200, 400); @@ -98,12 +98,7 @@ private static DrawCommand drawCircle(final Material sand, int x, int y, int siz return draw; } - private static DrawCommand drawLine(final Material sand, - int x1, - int y1, - int x2, - int y2, - int size) + private static DrawCommand drawLine(final Material sand, int x1, int y1, int x2, int y2, int size) { final var draw = VSandFactory.eINSTANCE.createDrawLine(); draw.setMaterial(sand); @@ -117,11 +112,7 @@ private static DrawCommand drawLine(final Material sand, private static Material findMaterial(List materials, String name) { - return materials.stream() - .filter(m -> m.getName() - .equals(name)) - .findFirst() - .orElse(null); + return materials.stream().filter(m -> m.getName().equals(name)).findFirst().orElse(null); } } } diff --git a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandHeadlessBenchmarkLauncher.java b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandHeadlessBenchmarkLauncher.java index 866b5aee..f779e9da 100644 --- a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandHeadlessBenchmarkLauncher.java +++ b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandHeadlessBenchmarkLauncher.java @@ -3,18 +3,20 @@ import org.eclipse.emf.ecore.util.EcoreUtil; import org.sheepy.lily.core.api.LilyLauncher; import org.sheepy.lily.vulkan.model.VulkanEngine; +import org.sheepy.vsand.logic.VSandMainLoop; + +import java.io.IOException; public class VSandHeadlessBenchmarkLauncher { - public static void main(String[] args) + public static void main(String[] args) throws IOException { final var application = VSandBenchmarkLauncher.createTestApplication(); application.setScene(null); final var vulkanEngine = (VulkanEngine) application.getEngines().get(0); EcoreUtil.delete(vulkanEngine.getProcesses().get(1)); - final var mainLoop = VSandMainLoop.createBenchmark( application, - VSandBenchmarkLauncher.DEFAULT_ITERATION_COUNT); + final var mainLoop = VSandMainLoop.createBenchmark(application, VSandBenchmarkLauncher.DEFAULT_ITERATION_COUNT); LilyLauncher.launch(application, mainLoop); } diff --git a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/audio/MaterialSoundManager.java b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/audio/MaterialSoundManager.java new file mode 100644 index 00000000..0567933e --- /dev/null +++ b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/audio/MaterialSoundManager.java @@ -0,0 +1,33 @@ +package org.sheepy.vsand.audio; + +import org.sheepy.lily.game.api.audio.AudioConfiguration; +import org.sheepy.lily.game.api.audio.IAudioAllocation; +import org.sheepy.lily.game.api.audio.IAudioHandle; +import org.sheepy.vsand.model.Material; + +public final class MaterialSoundManager +{ + private static final AudioConfiguration audioConfig = new AudioConfiguration.Builder().gain(0.85f).build(); + + private IAudioHandle audioHandle; + + public void start(Material material) + { + final var paintSound = material.getPaintSound(); + if (paintSound != null) + { + final var pitch = material.getPitch(); + final var soundAdapter = paintSound.allocationHandle(IAudioAllocation.class).get(); + audioHandle = soundAdapter.play(audioConfig.builder().pitch(pitch).build()); + } + } + + public void stop() + { + if (audioHandle != null) + { + audioHandle.end(); + audioHandle = null; + } + } +} diff --git a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/draw/DrawManager.java b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/draw/DrawManager.java index fff6eac0..0bb860de 100644 --- a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/draw/DrawManager.java +++ b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/draw/DrawManager.java @@ -1,7 +1,6 @@ package org.sheepy.vsand.draw; import org.joml.Vector2ic; -import org.sheepy.lily.core.api.input.IInputManager; import org.sheepy.lily.game.api.audio.AudioConfiguration; import org.sheepy.lily.game.api.audio.IAudioAllocation; import org.sheepy.lily.game.api.audio.IAudioHandle; @@ -9,87 +8,56 @@ import org.sheepy.vsand.model.Material; import org.sheepy.vsand.model.VSandApplication; import org.sheepy.vsand.model.VSandFactory; -import org.sheepy.vsand.util.BoardUtil; import org.sheepy.vsand.util.EShapeSize; public final class DrawManager { - private static final AudioConfiguration audioConfig = new AudioConfiguration.Builder().gain(0.85f).build(); + private Vector2ic previousPosition; + private Material material; + private boolean firstDraw = false; - private final VSandApplication application; - private final IInputManager inputManager; - private boolean wasUsed = false; - private Vector2ic previousCursor; - private IAudioHandle audioHandle; - - public DrawManager(VSandApplication application, IInputManager inputManager) + public void start(Material material) { - this.application = application; - this.inputManager = inputManager; + this.material = material; + firstDraw = true; } - public void manage(Material material, boolean needDraw) - { - if (needDraw) - { - final var firstDraw = wasUsed == false; - newcreateDrawCommand(material, firstDraw); - - if (firstDraw) - { - final var paintSound = material.getPaintSound(); - if (paintSound != null) - { - final var pitch = material.getPitch(); - final var soundAdapter = paintSound.allocationHandle(IAudioAllocation.class).get(); - audioHandle = soundAdapter.play(audioConfig.builder().pitch(pitch).build()); - } - } - wasUsed = true; - } - else - { - wasUsed = false; - if (audioHandle != null) - { - audioHandle.end(); - audioHandle = null; - } - } - } - - private void newcreateDrawCommand(Material material, boolean firstDraw) + public DrawCommand draw(final int brushSize, final Vector2ic position) { final DrawCommand newCommand; - final var size = EShapeSize.values()[application.getBrushSize() - 1]; - final var cursor = inputManager.getCursorPosition(); - final var cursorBoard = BoardUtil.toBoardPosition(cursor, application); + final var size = EShapeSize.values()[brushSize - 1]; - if (firstDraw || (previousCursor.x() == cursorBoard.x() && previousCursor.y() == cursorBoard.y())) + if (firstDraw || (previousPosition.x() == position.x() && previousPosition.y() == position.y())) { final var command = VSandFactory.eINSTANCE.createDrawCircle(); command.setMaterial(material); command.setSize(size.getSize()); - command.setX(cursorBoard.x()); - command.setY(cursorBoard.y()); + command.setX(position.x()); + command.setY(position.y()); newCommand = command; + firstDraw = false; } else { final var command = VSandFactory.eINSTANCE.createDrawLine(); command.setMaterial(material); command.setSize(size.getSize()); - command.setX1(previousCursor.x()); - command.setY1(previousCursor.y()); - command.setX2(cursorBoard.x()); - command.setY2(cursorBoard.y()); + command.setX1(previousPosition.x()); + command.setY1(previousPosition.y()); + command.setX2(position.x()); + command.setY2(position.y()); newCommand = command; } - previousCursor = cursorBoard; - application.getDrawQueue().add(newCommand); + previousPosition = position; + return newCommand; + } + + public Material getMaterial() + { + return material; } } \ No newline at end of file diff --git a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/AbstractDrawer.java b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/AbstractDrawer.java new file mode 100644 index 00000000..2b8ee152 --- /dev/null +++ b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/AbstractDrawer.java @@ -0,0 +1,72 @@ +package org.sheepy.vsand.input; + +import org.sheepy.lily.core.api.adapter.IAdapter; +import org.sheepy.lily.core.api.input.IInputManager; +import org.sheepy.vsand.audio.MaterialSoundManager; +import org.sheepy.vsand.draw.DrawManager; +import org.sheepy.vsand.model.Material; +import org.sheepy.vsand.model.VSandApplication; +import org.sheepy.vsand.util.BoardUtil; + +abstract class AbstractDrawer implements IAdapter +{ + private final MaterialSoundManager soundManager = new MaterialSoundManager(); + private final DrawManager drawManager = new DrawManager(); + private final VSandApplication application; + private final IInputManager inputManager; + + private boolean isDrawing = false; + private boolean drawRequested = false; + + public AbstractDrawer(VSandApplication application) + { + this.application = application; + inputManager = IInputManager.get(application).orElseThrow(); + inputManager.listen(this::afterPollInputs, IInputManager.Features.AfterPollInputs); + } + + private void afterPollInputs() + { + if (drawRequested) + { + final var material = getMaterial(application); + final boolean materialHasChanged = drawManager.getMaterial() != material; + if (isDrawing && materialHasChanged) + { + soundManager.stop(); + isDrawing = false; + } + + if (!isDrawing) + { + soundManager.start(material); + drawManager.start(material); + isDrawing = true; + } + + newDrawCommand(); + } + else if (isDrawing) + { + soundManager.stop(); + isDrawing = false; + } + } + + private void newDrawCommand() + { + final var cursor = inputManager.getCursorPosition(); + final var cursorBoard = BoardUtil.toBoardPosition(cursor, application); + final var brushSize = application.getBrushSize(); + final var drawCommand = drawManager.draw(brushSize, cursorBoard); + application.getDrawQueue().add(drawCommand); + } + + protected void setDrawRequested(boolean drawRequested) + { + this.drawRequested = drawRequested; + } + + protected abstract Material getMaterial(VSandApplication application); + +} diff --git a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/VSandInputManager.java b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/InputManager.java similarity index 72% rename from org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/VSandInputManager.java rename to org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/InputManager.java index 079534ba..27b641b4 100644 --- a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/VSandInputManager.java +++ b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/InputManager.java @@ -1,55 +1,39 @@ package org.sheepy.vsand.input; import org.eclipse.emf.common.util.EList; +import org.sheepy.lily.core.api.adapter.IAdapter; +import org.sheepy.lily.core.api.adapter.annotation.Adapter; +import org.sheepy.lily.core.api.extender.ModelExtender; import org.sheepy.lily.core.api.input.IInputManager; import org.sheepy.lily.core.api.input.event.KeyEvent; -import org.sheepy.lily.core.api.input.event.MouseClickEvent; import org.sheepy.lily.core.api.input.event.ScrollEvent; import org.sheepy.lily.core.model.types.EKeyState; -import org.sheepy.lily.game.api.window.IWindow; -import org.sheepy.vsand.draw.DrawManager; import org.sheepy.vsand.model.Material; import org.sheepy.vsand.model.VSandApplication; -public final class VSandInputManager +@ModelExtender(scope = VSandApplication.class) +@Adapter(lazy = false) +public final class InputManager implements IAdapter { private final VSandApplication application; - private final IWindow window; - private final DrawManager leftDrawManager; - private final DrawManager rightDrawManager; + private final IInputManager inputManager; private boolean shiftPressed = false; - private boolean leftClickPressed = false; - private boolean rightClickPressed = false; - - public VSandInputManager(final IInputManager inputManager, - IWindow window, - VSandApplication application, - DrawManager leftDrawManager, - DrawManager rightDrawManager) + public InputManager(VSandApplication application) { - this.window = window; this.application = application; - this.leftDrawManager = leftDrawManager; - this.rightDrawManager = rightDrawManager; + inputManager = IInputManager.get(application).orElseThrow(); + inputManager.showCursor(inputManager.isMouseOnUI()); inputManager.listen(this::onMouseOverUI, IInputManager.Features.MouseOverUIEvent); - inputManager.listen(this::afterPollInputs, IInputManager.Features.AfterPollInputs); inputManager.listen(this::onKeyEvent, IInputManager.Features.KeyEvent); inputManager.listen(this::onScrollEvent, IInputManager.Features.ScrollEvent); - inputManager.listen(this::onMouseClickEvent, IInputManager.Features.MouseClickEvent); } private void onMouseOverUI(Boolean overUI) { - window.hideCursor(!overUI); - } - - private void afterPollInputs() - { - leftDrawManager.manage(application.getMainMaterial(), leftClickPressed); - rightDrawManager.manage(application.getSecondaryMaterial(), rightClickPressed); + inputManager.showCursor(overUI); } private void onKeyEvent(KeyEvent event) @@ -182,20 +166,4 @@ else if (index == materials.size()) return next; } - - private void onMouseClickEvent(MouseClickEvent event) - { - switch (event.button) - { - case LEFT: - leftClickPressed = event.pressed; - rightClickPressed = false; - break; - case RIGHT: - rightClickPressed = event.pressed; - leftClickPressed = false; - break; - default: - } - } } diff --git a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/PrimaryDrawer.java b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/PrimaryDrawer.java new file mode 100644 index 00000000..d70cbe52 --- /dev/null +++ b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/PrimaryDrawer.java @@ -0,0 +1,41 @@ +package org.sheepy.vsand.input; + +import org.sheepy.lily.core.api.adapter.annotation.Adapter; +import org.sheepy.lily.core.api.extender.ModelExtender; +import org.sheepy.lily.core.api.input.IInputManager; +import org.sheepy.lily.core.api.input.event.MouseClickEvent; +import org.sheepy.vsand.model.Material; +import org.sheepy.vsand.model.VSandApplication; + +@ModelExtender(scope = VSandApplication.class) +@Adapter(lazy = false) +public final class PrimaryDrawer extends AbstractDrawer +{ + public PrimaryDrawer(final VSandApplication application) + { + super(application); + + final var inputManager = IInputManager.get(application).orElseThrow(); + inputManager.listen(this::onMouseClickEvent, IInputManager.Features.MouseClickEvent); + } + + @Override + protected Material getMaterial(VSandApplication application) + { + return application.getMainMaterial(); + } + + private void onMouseClickEvent(MouseClickEvent event) + { + switch (event.button) + { + case LEFT: + setDrawRequested(event.pressed); + break; + case RIGHT: + setDrawRequested(false); + break; + default: + } + } +} diff --git a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/SecondaryDrawer.java b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/SecondaryDrawer.java new file mode 100644 index 00000000..15436d07 --- /dev/null +++ b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/SecondaryDrawer.java @@ -0,0 +1,41 @@ +package org.sheepy.vsand.input; + +import org.sheepy.lily.core.api.adapter.annotation.Adapter; +import org.sheepy.lily.core.api.extender.ModelExtender; +import org.sheepy.lily.core.api.input.IInputManager; +import org.sheepy.lily.core.api.input.event.MouseClickEvent; +import org.sheepy.vsand.model.Material; +import org.sheepy.vsand.model.VSandApplication; + +@ModelExtender(scope = VSandApplication.class) +@Adapter(lazy = false) +public final class SecondaryDrawer extends AbstractDrawer +{ + public SecondaryDrawer(final VSandApplication application) + { + super(application); + + final var inputManager = IInputManager.get(application).orElseThrow(); + inputManager.listen(this::onMouseClickEvent, IInputManager.Features.MouseClickEvent); + } + + @Override + protected Material getMaterial(VSandApplication application) + { + return application.getSecondaryMaterial(); + } + + private void onMouseClickEvent(MouseClickEvent event) + { + switch (event.button) + { + case LEFT: + setDrawRequested(false); + break; + case RIGHT: + setDrawRequested(event.pressed); + break; + default: + } + } +} diff --git a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandMainLoop.java b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/logic/VSandMainLoop.java similarity index 59% rename from org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandMainLoop.java rename to org.sheepy.vsand/src/main/java/org/sheepy/vsand/logic/VSandMainLoop.java index d1a243c4..af4143c2 100644 --- a/org.sheepy.vsand/src/main/java/org/sheepy/vsand/VSandMainLoop.java +++ b/org.sheepy.vsand/src/main/java/org/sheepy/vsand/logic/VSandMainLoop.java @@ -1,7 +1,5 @@ -package org.sheepy.vsand; +package org.sheepy.vsand.logic; -import org.sheepy.lily.core.api.engine.IInputEngineAdapter; -import org.sheepy.lily.core.api.input.IInputManager; import org.sheepy.lily.core.api.util.DebugUtil; import org.sheepy.lily.vulkan.api.engine.IVulkanEngineAdapter; import org.sheepy.lily.vulkan.api.process.IProcessAdapter; @@ -9,9 +7,6 @@ import org.sheepy.lily.vulkan.model.process.IPipelineTask; import org.sheepy.lily.vulkan.model.process.compute.ComputePipeline; import org.sheepy.lily.vulkan.model.process.compute.ComputeProcess; -import org.sheepy.lily.vulkan.model.process.graphic.GraphicProcess; -import org.sheepy.vsand.draw.DrawManager; -import org.sheepy.vsand.input.VSandInputManager; import org.sheepy.vsand.model.VSandApplication; import org.sheepy.vsand.util.FPSCounter; @@ -21,10 +16,10 @@ public final class VSandMainLoop implements Runnable private final VSandApplication application; private final boolean benchmarkMode; private final int stopIteration; + private final IProcessAdapter boardProcessAdapter; + private final IProcessAdapter renderProcessAdapter; + private final IPipelineTask boardImageBarrier; - private IProcessAdapter boardProcessAdapter; - private IProcessAdapter renderProcessAdapter; - private IPipelineTask boardImageBarrier; private boolean loaded = false; private long currentIteration = 0; private long startNs; @@ -46,6 +41,14 @@ private VSandMainLoop(VSandApplication application, boolean benchmarkMode, int s this.application = application; this.benchmarkMode = benchmarkMode; this.stopIteration = stopIteration; + + final var vulkanEngine = (VulkanEngine) application.getEngines().get(0); + final var processes = vulkanEngine.getProcesses(); + final var boardProcess = (ComputeProcess) processes.get(0); + final var boardToPixelPipeline = (ComputePipeline) (boardProcess.getPipelinePkg().getPipelines().get(2)); + boardProcessAdapter = boardProcess.adaptNotNull(IProcessAdapter.class); + boardImageBarrier = boardToPixelPipeline.getTaskPkg().getTasks().get(2); + renderProcessAdapter = processes.get(1).adaptNotNull(IProcessAdapter.class); } @Override @@ -67,24 +70,21 @@ public void run() application.setPaused(true); } - if (renderProcessAdapter != null) + if (benchmarkMode == false) + { + renderProcessAdapter.run(); + } + else if (renderProcessAdapter != null) { - if (benchmarkMode == false) + if (nextRenderDate < System.nanoTime()) { renderProcessAdapter.run(); + nextRenderDate = System.nanoTime() + frameDurationNs; + boardImageBarrier.setEnabled(true); } else { - if (nextRenderDate < System.nanoTime()) - { - renderProcessAdapter.run(); - nextRenderDate = System.nanoTime() + frameDurationNs; - boardImageBarrier.setEnabled(true); - } - else - { - boardImageBarrier.setEnabled(false); - } + boardImageBarrier.setEnabled(false); } } @@ -98,30 +98,14 @@ public void run() private void load() { - - final var vulkanEngine = (VulkanEngine) application.getEngines().get(0); final var engineAdapter = vulkanEngine.adapt(IVulkanEngineAdapter.class); - if (application.getScene() != null) + final var window = engineAdapter.getWindow(); + if (window != null) { - final var window = engineAdapter.getWindow(); frameDurationNs = (long) ((1. / window.getRefreshRate()) * 1e9); - if (benchmarkMode == false) - { - final var inputManager = IInputManager.get(application).orElseThrow(); - final var mainDrawManager = new DrawManager(application, inputManager); - final var secondaryDrawManager = new DrawManager(application, inputManager); - new VSandInputManager(inputManager, window, application, mainDrawManager, secondaryDrawManager); - - if (inputManager.isMouseOnUI() == false) - { - window.hideCursor(true); - } - } } - gatherProcesses(vulkanEngine); - startNs = System.nanoTime(); nextRenderDate = System.nanoTime() + frameDurationNs; @@ -131,26 +115,6 @@ private void load() } } - private void gatherProcesses(VulkanEngine vulkanEngine) - { - final var processes = vulkanEngine.getProcesses(); - for (final var process : processes) - { - if (process instanceof ComputeProcess) - { - boardProcessAdapter = process.adaptNotNull(IProcessAdapter.class); - final var boardToPixelPipeline = (ComputePipeline) ((ComputeProcess) process).getPipelinePkg() - .getPipelines() - .get(2); - boardImageBarrier = boardToPixelPipeline.getTaskPkg().getTasks().get(2); - } - else if (process instanceof GraphicProcess) - { - renderProcessAdapter = process.adaptNotNull(IProcessAdapter.class); - } - } - } - private void printBenchmarkResult() { final double duration = (System.nanoTime() - startNs) / 1e9; diff --git a/org.sheepy.vsand/src/test/java/org/sheepy/vsand/test/VSandTest.java b/org.sheepy.vsand/src/test/java/org/sheepy/vsand/test/VSandTest.java index e0f464da..d9df1c7b 100644 --- a/org.sheepy.vsand/src/test/java/org/sheepy/vsand/test/VSandTest.java +++ b/org.sheepy.vsand/src/test/java/org/sheepy/vsand/test/VSandTest.java @@ -3,12 +3,14 @@ import org.junit.jupiter.api.Test; import org.sheepy.lily.core.api.LilyLauncher; import org.sheepy.vsand.VSandBenchmarkLauncher; -import org.sheepy.vsand.VSandMainLoop; +import org.sheepy.vsand.logic.VSandMainLoop; + +import java.io.IOException; public class VSandTest { @Test - public void test() + public void test() throws IOException { final var application = VSandBenchmarkLauncher.createTestApplication(); final var mainLoop = VSandMainLoop.createBenchmark(application, 120);