From f5929f23b1f007d65aae478725f95ce2753b9100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 5 Apr 2014 02:14:35 +0200 Subject: [PATCH] FInal fields, tweaks to screen system, deferred loading. --- src/mightypork/rogue/App.java | 64 ++++++++--- src/mightypork/rogue/AppAccess.java | 2 +- src/mightypork/rogue/AppAdapter.java | 2 +- src/mightypork/rogue/MainLoop.java | 24 +++-- src/mightypork/rogue/Res.java | 34 +++--- src/mightypork/rogue/bus/ChildClient.java | 2 +- .../rogue/bus/events/ActionRequest.java | 16 ++- .../rogue/bus/events/KeyboardEvent.java | 6 +- .../rogue/bus/events/MainLoopTaskRequest.java | 38 +++++++ .../rogue/bus/events/MouseButtonEvent.java | 8 +- .../rogue/bus/events/MouseMotionEvent.java | 4 +- .../rogue/bus/events/RequestType.java | 7 -- .../rogue/bus/events/ResourceLoadRequest.java | 39 +++++++ .../rogue/bus/events/ScreenChangeEvent.java | 6 +- .../rogue/bus/events/ScreenRequestEvent.java | 5 +- .../gui/{screens => }/LayeredScreen.java | 26 +---- .../rogue/gui/{screens => }/Screen.java | 29 +---- .../rogue/gui/{screens => }/ScreenLayer.java | 23 ++-- .../gui/{screens => }/ScreenRegistry.java | 11 +- .../rogue/gui/constraints/ColumnHolder.java | 45 ++++++++ .../rogue/gui/constraints/ElementHolder.java | 27 +++-- .../rogue/gui/constraints/RowHolder.java | 45 ++++++++ .../screens/screenBouncy/ScreenTestAnim.java | 70 ------------ .../BouncyBox.java | 2 +- .../LayerBouncyBoxes.java | 25 ++--- .../test_bouncyboxes/ScreenTestBouncy.java | 80 ++++++++++++++ .../LayerFlyingCat.java} | 60 ++++------- .../screens/test_cat_sound/ScreenTestCat.java | 73 +++++++++++++ src/mightypork/rogue/input/InputSystem.java | 17 ++- src/mightypork/rogue/input/KeyBinding.java | 2 +- .../rogue/input/KeyBindingPool.java | 2 +- src/mightypork/rogue/input/KeyStroke.java | 25 ++--- .../rogue/render/DisplaySystem.java | 14 ++- src/mightypork/rogue/render/Render.java | 10 +- .../rogue/{audio => sound}/DeferredAudio.java | 15 ++- .../rogue/{audio => sound}/JointVolume.java | 4 +- .../rogue/{audio => sound}/NullAudio.java | 2 +- .../rogue/{audio => sound}/SoundBank.java | 8 +- .../rogue/{audio => sound}/SoundSystem.java | 17 +-- .../players}/BaseAudioPlayer.java | 16 +-- .../players}/EffectPlayer.java | 3 +- .../{audio => sound/players}/LoopPlayer.java | 5 +- .../rogue/texture/DeferredLoader.java | 102 ++++++++++++++++++ .../DeferredTexture.java} | 17 ++- .../textures => texture}/TextureBank.java | 17 +-- .../{render/textures => texture}/TxQuad.java | 6 +- .../utils/control/bus/EventBus.java | 38 +++++-- .../utils/control/bus/EventChannel.java | 34 ++++-- .../utils/control/bus/SingularEvent.java | 11 ++ .../control/bus/events/DestroyEvent.java | 5 + .../utils/control/timing/TimerFps.java | 1 - src/mightypork/utils/logging/Log.java | 28 +++-- .../math/constraints/ConstraintFactory.java | 2 +- src/mightypork/utils/math/coord/Rect.java | 8 +- 54 files changed, 818 insertions(+), 364 deletions(-) create mode 100644 src/mightypork/rogue/bus/events/MainLoopTaskRequest.java delete mode 100644 src/mightypork/rogue/bus/events/RequestType.java create mode 100644 src/mightypork/rogue/bus/events/ResourceLoadRequest.java rename src/mightypork/rogue/gui/{screens => }/LayeredScreen.java (58%) rename src/mightypork/rogue/gui/{screens => }/Screen.java (86%) rename src/mightypork/rogue/gui/{screens => }/ScreenLayer.java (52%) rename src/mightypork/rogue/gui/{screens => }/ScreenRegistry.java (74%) create mode 100644 src/mightypork/rogue/gui/constraints/ColumnHolder.java create mode 100644 src/mightypork/rogue/gui/constraints/RowHolder.java delete mode 100644 src/mightypork/rogue/gui/screens/screenBouncy/ScreenTestAnim.java rename src/mightypork/rogue/gui/screens/{screenBouncy => test_bouncyboxes}/BouncyBox.java (96%) rename src/mightypork/rogue/gui/screens/{screenBouncy => test_bouncyboxes}/LayerBouncyBoxes.java (62%) create mode 100644 src/mightypork/rogue/gui/screens/test_bouncyboxes/ScreenTestBouncy.java rename src/mightypork/rogue/gui/screens/{screenTextures/ScreenTextureTest.java => test_cat_sound/LayerFlyingCat.java} (59%) create mode 100644 src/mightypork/rogue/gui/screens/test_cat_sound/ScreenTestCat.java rename src/mightypork/rogue/{audio => sound}/DeferredAudio.java (96%) rename src/mightypork/rogue/{audio => sound}/JointVolume.java (90%) rename src/mightypork/rogue/{audio => sound}/NullAudio.java (89%) rename src/mightypork/rogue/{audio => sound}/SoundBank.java (86%) rename src/mightypork/rogue/{audio => sound}/SoundSystem.java (86%) rename src/mightypork/rogue/{audio => sound/players}/BaseAudioPlayer.java (75%) rename src/mightypork/rogue/{audio => sound/players}/EffectPlayer.java (89%) rename src/mightypork/rogue/{audio => sound/players}/LoopPlayer.java (93%) create mode 100644 src/mightypork/rogue/texture/DeferredLoader.java rename src/mightypork/rogue/{render/textures/MultiTexture.java => texture/DeferredTexture.java} (85%) rename src/mightypork/rogue/{render/textures => texture}/TextureBank.java (80%) rename src/mightypork/rogue/{render/textures => texture}/TxQuad.java (95%) create mode 100644 src/mightypork/utils/control/bus/SingularEvent.java diff --git a/src/mightypork/rogue/App.java b/src/mightypork/rogue/App.java index 8be6a90..acd6917 100644 --- a/src/mightypork/rogue/App.java +++ b/src/mightypork/rogue/App.java @@ -8,12 +8,16 @@ import java.util.ArrayList; import javax.swing.JOptionPane; -import mightypork.rogue.audio.SoundSystem; import mightypork.rogue.bus.events.*; -import mightypork.rogue.gui.screens.ScreenRegistry; +import mightypork.rogue.bus.events.ActionRequest.RequestType; +import mightypork.rogue.gui.ScreenRegistry; +import mightypork.rogue.gui.screens.test_bouncyboxes.ScreenTestBouncy; +import mightypork.rogue.gui.screens.test_cat_sound.ScreenTestCat; import mightypork.rogue.input.InputSystem; import mightypork.rogue.input.KeyStroke; import mightypork.rogue.render.DisplaySystem; +import mightypork.rogue.sound.SoundSystem; +import mightypork.rogue.texture.DeferredLoader; import mightypork.utils.control.bus.EventBus; import mightypork.utils.control.bus.events.DestroyEvent; import mightypork.utils.control.bus.events.UpdateEvent; @@ -41,8 +45,7 @@ public class App implements AppAccess { private static SoundSystem soundSystem; private EventBus eventBus; private MainLoop mainLoop; - - public ScreenRegistry screens; + private ScreenRegistry screens; /** @@ -73,6 +76,8 @@ public class App implements AppAccess { initialize(); Log.i("Starting main loop..."); + + // open first screen mainLoop.start(); } @@ -84,7 +89,12 @@ public class App implements AppAccess { */ public static void onCrash(Throwable error) { - Log.e("The game has crashed!", error); + if (Log.ready()) { + Log.e("The game has crashed!", error); + } else { + System.err.println("The game has crashed!"); + error.printStackTrace(); + } if (inst != null) inst.shutdown(); } @@ -93,12 +103,12 @@ public class App implements AppAccess { @Override public void shutdown() { - bus().send(new DestroyEvent()); + Log.i("Shutting down subsystems..."); + bus().send(new DestroyEvent()); bus().destroy(); - Log.i("Shutting down..."); - + Log.i("Terminating..."); System.exit(0); } @@ -142,6 +152,14 @@ public class App implements AppAccess { soundSystem = new SoundSystem(this); soundSystem.setMasterVolume(1); + /* + * Load resources + */ + Log.f1("Registering resources..."); + bus().subscribe(new DeferredLoader(this)); + + Res.load(this); + /* * Input */ @@ -154,18 +172,14 @@ public class App implements AppAccess { */ Log.f2("Initializing screen registry..."); screens = new ScreenRegistry(this); - - /* - * Load resources - */ - Log.f1("Registering resources..."); - Res.load(this); + initScreens(); /* * Prepare main loop */ Log.f1("Preparing main loop..."); ArrayList loopTasks = new ArrayList(); + loopTasks.add(new Runnable() { @Override @@ -179,24 +193,44 @@ public class App implements AppAccess { } + private void initScreens() + { + Log.f3("Registering game screens..."); + + screens.add(new ScreenTestBouncy(this)); + screens.add(new ScreenTestCat(this)); + + screens.showScreen("test.cat"); + } + + private void initChannels() { Log.f3("Registering channels..."); + // framework events bus().addChannel(DestroyEvent.class, Destroyable.class); bus().addChannel(UpdateEvent.class, Updateable.class); + // input events bus().addChannel(ScreenChangeEvent.class, ScreenChangeEvent.Listener.class); bus().addChannel(KeyboardEvent.class, KeyboardEvent.Listener.class); bus().addChannel(MouseMotionEvent.class, MouseMotionEvent.Listener.class); bus().addChannel(MouseButtonEvent.class, MouseButtonEvent.Listener.class); + + // control events bus().addChannel(ScreenRequestEvent.class, ScreenRequestEvent.Listener.class); + bus().addChannel(ResourceLoadRequest.class, ResourceLoadRequest.Listener.class); bus().addChannel(ActionRequest.class, ActionRequest.Listener.class); + bus().addChannel(MainLoopTaskRequest.class, MainLoopTaskRequest.Listener.class); } private void setupGlobalKeystrokes() { + Log.f3("Setting up hot keys..."); + + // Go fullscreen input().bindKeyStroke(new KeyStroke(Keyboard.KEY_F11), new Runnable() { @Override @@ -206,6 +240,7 @@ public class App implements AppAccess { } }); + // Take screenshot input().bindKeyStroke(new KeyStroke(Keyboard.KEY_F2), new Runnable() { @Override @@ -215,6 +250,7 @@ public class App implements AppAccess { } }); + // Exit input().bindKeyStroke(new KeyStroke(Keyboard.KEY_LCONTROL, Keyboard.KEY_Q), new Runnable() { @Override diff --git a/src/mightypork/rogue/AppAccess.java b/src/mightypork/rogue/AppAccess.java index 48af191..e9fc2aa 100644 --- a/src/mightypork/rogue/AppAccess.java +++ b/src/mightypork/rogue/AppAccess.java @@ -1,9 +1,9 @@ package mightypork.rogue; -import mightypork.rogue.audio.SoundSystem; import mightypork.rogue.input.InputSystem; import mightypork.rogue.render.DisplaySystem; +import mightypork.rogue.sound.SoundSystem; import mightypork.utils.control.bus.EventBus; diff --git a/src/mightypork/rogue/AppAdapter.java b/src/mightypork/rogue/AppAdapter.java index 6c5867e..f2593bb 100644 --- a/src/mightypork/rogue/AppAdapter.java +++ b/src/mightypork/rogue/AppAdapter.java @@ -1,9 +1,9 @@ package mightypork.rogue; -import mightypork.rogue.audio.SoundSystem; import mightypork.rogue.input.InputSystem; import mightypork.rogue.render.DisplaySystem; +import mightypork.rogue.sound.SoundSystem; import mightypork.utils.control.bus.EventBus; diff --git a/src/mightypork/rogue/MainLoop.java b/src/mightypork/rogue/MainLoop.java index 81ec236..49b8758 100644 --- a/src/mightypork/rogue/MainLoop.java +++ b/src/mightypork/rogue/MainLoop.java @@ -8,15 +8,15 @@ import java.util.concurrent.ConcurrentLinkedQueue; import mightypork.rogue.bus.Subsystem; import mightypork.rogue.bus.events.ActionRequest; -import mightypork.rogue.bus.events.RequestType; -import mightypork.rogue.bus.events.ScreenRequestEvent; +import mightypork.rogue.bus.events.ActionRequest.RequestType; +import mightypork.rogue.bus.events.MainLoopTaskRequest; import mightypork.rogue.tasks.TaskTakeScreenshot; import mightypork.rogue.util.Utils; import mightypork.utils.control.bus.events.UpdateEvent; import mightypork.utils.control.timing.TimerDelta; -public class MainLoop extends Subsystem implements ActionRequest.Listener { +public class MainLoop extends Subsystem implements ActionRequest.Listener, MainLoopTaskRequest.Listener { private Queue taskQueue = new ConcurrentLinkedQueue(); private List regularTasks; @@ -35,8 +35,6 @@ public class MainLoop extends Subsystem implements ActionRequest.Listener { public void start() { - bus().queue(new ScreenRequestEvent("test.texture")); - timer = new TimerDelta(); while (running) { @@ -70,18 +68,19 @@ public class MainLoop extends Subsystem implements ActionRequest.Listener { { switch (request) { case FULLSCREEN: - taskQueue.add(taskFullscreen); + queueTask(taskFullscreen); break; case SCREENSHOT: - taskQueue.add(taskScreenshot); + queueTask(taskScreenshot); break; case SHUTDOWN: - taskQueue.add(taskShutdown); + queueTask(taskShutdown); } } + /** Take a screenshot */ private final Runnable taskScreenshot = new Runnable() { @Override @@ -92,6 +91,7 @@ public class MainLoop extends Subsystem implements ActionRequest.Listener { } }; + /** Shutdown the application */ private final Runnable taskShutdown = new Runnable() { @Override @@ -101,6 +101,7 @@ public class MainLoop extends Subsystem implements ActionRequest.Listener { } }; + /** Toggle fullscreen */ private final Runnable taskFullscreen = new Runnable() { @Override @@ -109,4 +110,11 @@ public class MainLoop extends Subsystem implements ActionRequest.Listener { disp().switchFullscreen(); } }; + + + @Override + public void queueTask(Runnable request) + { + taskQueue.add(request); + } } diff --git a/src/mightypork/rogue/Res.java b/src/mightypork/rogue/Res.java index 4e8bce6..7bc6cac 100644 --- a/src/mightypork/rogue/Res.java +++ b/src/mightypork/rogue/Res.java @@ -1,13 +1,11 @@ package mightypork.rogue; -import mightypork.rogue.audio.EffectPlayer; -import mightypork.rogue.audio.LoopPlayer; -import mightypork.rogue.audio.SoundBank; -import mightypork.rogue.gui.screens.screenBouncy.ScreenTestAnim; -import mightypork.rogue.gui.screens.screenTextures.ScreenTextureTest; -import mightypork.rogue.render.textures.TextureBank; -import mightypork.rogue.render.textures.TxQuad; +import mightypork.rogue.sound.SoundBank; +import mightypork.rogue.sound.players.EffectPlayer; +import mightypork.rogue.sound.players.LoopPlayer; +import mightypork.rogue.texture.TextureBank; +import mightypork.rogue.texture.TxQuad; import org.newdawn.slick.opengl.Texture; @@ -27,26 +25,25 @@ public class Res { textures = new TextureBank(app); sounds = new SoundBank(app); - loadSounds(app); - loadTextures(app); - loadFonts(app); - loadScreens(app); + loadSounds(); + loadTextures(); + loadFonts(); } - private static void loadFonts(App app) + private static void loadFonts() { - + // } - private static void loadTextures(App app) + private static void loadTextures() { textures.loadTexture("test.kitten", "/res/img/kitten.png"); } - private static void loadSounds(App app) + private static void loadSounds() { sounds.addEffect("gui.shutter", "/res/audio/shutter.ogg", 1, 1); @@ -54,13 +51,6 @@ public class Res { } - private static void loadScreens(App app) - { - app.screens.add("test.anim", new ScreenTestAnim(app)); - app.screens.add("test.texture", new ScreenTextureTest(app)); - } - - public static TxQuad getTxQuad(String key) { return textures.getTxQuad(key); diff --git a/src/mightypork/rogue/bus/ChildClient.java b/src/mightypork/rogue/bus/ChildClient.java index f7119e5..df89645 100644 --- a/src/mightypork/rogue/bus/ChildClient.java +++ b/src/mightypork/rogue/bus/ChildClient.java @@ -25,7 +25,7 @@ public class ChildClient extends AppAdapter implements DelegatingClient, Togglea super(app); } - private Set clients = new LinkedHashSet(); + private final Set clients = new LinkedHashSet(); private boolean listening = true; private boolean delegating = true; diff --git a/src/mightypork/rogue/bus/events/ActionRequest.java b/src/mightypork/rogue/bus/events/ActionRequest.java index 24ea336..330e08e 100644 --- a/src/mightypork/rogue/bus/events/ActionRequest.java +++ b/src/mightypork/rogue/bus/events/ActionRequest.java @@ -2,16 +2,17 @@ package mightypork.rogue.bus.events; import mightypork.utils.control.bus.Event; +import mightypork.utils.control.bus.SingularEvent; /** - * Request for action that should be performed in the main thread. + * Request for a global sction to be done in the main loop. * * @author MightyPork */ -public class ActionRequest implements Event { +public class ActionRequest implements Event, SingularEvent { - private RequestType type; + private final RequestType type; public ActionRequest(RequestType request) { @@ -27,7 +28,16 @@ public class ActionRequest implements Event { public interface Listener { + /** + * Perform the requested action + * + * @param request + */ public void requestAction(RequestType request); } + public static enum RequestType + { + FULLSCREEN, SCREENSHOT, SHUTDOWN; + } } diff --git a/src/mightypork/rogue/bus/events/KeyboardEvent.java b/src/mightypork/rogue/bus/events/KeyboardEvent.java index 971be9a..f1d00ce 100644 --- a/src/mightypork/rogue/bus/events/KeyboardEvent.java +++ b/src/mightypork/rogue/bus/events/KeyboardEvent.java @@ -13,9 +13,9 @@ import org.lwjgl.input.Keyboard; */ public class KeyboardEvent implements Event { - private int key; - private boolean down; - private char c; + private final int key; + private final boolean down; + private final char c; public KeyboardEvent(int key, char c, boolean down) { diff --git a/src/mightypork/rogue/bus/events/MainLoopTaskRequest.java b/src/mightypork/rogue/bus/events/MainLoopTaskRequest.java new file mode 100644 index 0000000..badfe30 --- /dev/null +++ b/src/mightypork/rogue/bus/events/MainLoopTaskRequest.java @@ -0,0 +1,38 @@ +package mightypork.rogue.bus.events; + + +import mightypork.utils.control.bus.Event; +import mightypork.utils.control.bus.SingularEvent; + + +/** + * Request to execute given {@link Runnable} in main loop. + * + * @author MightyPork + */ +public class MainLoopTaskRequest implements Event, SingularEvent { + + private final Runnable task; + + + public MainLoopTaskRequest(Runnable task) { + this.task = task; + } + + + @Override + public void handleBy(Listener handler) + { + handler.queueTask(task); + } + + public interface Listener { + + /** + * Perform the requested action + * + * @param request + */ + public void queueTask(Runnable request); + } +} diff --git a/src/mightypork/rogue/bus/events/MouseButtonEvent.java b/src/mightypork/rogue/bus/events/MouseButtonEvent.java index 6e583b4..62c6a06 100644 --- a/src/mightypork/rogue/bus/events/MouseButtonEvent.java +++ b/src/mightypork/rogue/bus/events/MouseButtonEvent.java @@ -16,10 +16,10 @@ public class MouseButtonEvent implements Event { public static final int BUTTON_MIDDLE = 1; public static final int BUTTON_RIGHT = 2; - private int button; - private int wheeld; - private Coord pos; - private boolean down; + private final int button; + private final int wheeld; + private final Coord pos; + private final boolean down; /** diff --git a/src/mightypork/rogue/bus/events/MouseMotionEvent.java b/src/mightypork/rogue/bus/events/MouseMotionEvent.java index 3c0112f..81248f7 100644 --- a/src/mightypork/rogue/bus/events/MouseMotionEvent.java +++ b/src/mightypork/rogue/bus/events/MouseMotionEvent.java @@ -7,8 +7,8 @@ import mightypork.utils.math.coord.Coord; public class MouseMotionEvent implements Event { - private Coord move; - private Coord pos; + private final Coord move; + private final Coord pos; public MouseMotionEvent(Coord pos, Coord move) { diff --git a/src/mightypork/rogue/bus/events/RequestType.java b/src/mightypork/rogue/bus/events/RequestType.java deleted file mode 100644 index 3609a82..0000000 --- a/src/mightypork/rogue/bus/events/RequestType.java +++ /dev/null @@ -1,7 +0,0 @@ -package mightypork.rogue.bus.events; - - -public enum RequestType -{ - FULLSCREEN, SCREENSHOT, SHUTDOWN; -} diff --git a/src/mightypork/rogue/bus/events/ResourceLoadRequest.java b/src/mightypork/rogue/bus/events/ResourceLoadRequest.java new file mode 100644 index 0000000..36dbfb5 --- /dev/null +++ b/src/mightypork/rogue/bus/events/ResourceLoadRequest.java @@ -0,0 +1,39 @@ +package mightypork.rogue.bus.events; + + +import mightypork.rogue.Deferred; +import mightypork.utils.control.bus.Event; +import mightypork.utils.control.bus.SingularEvent; + + +/** + * Request to schedule loading a deferred resource. + * + * @author MightyPork + */ +public class ResourceLoadRequest implements Event, SingularEvent { + + private final Deferred resource; + + + public ResourceLoadRequest(Deferred resource) { + this.resource = resource; + } + + + @Override + public void handleBy(Listener handler) + { + handler.loadResource(resource); + } + + public interface Listener { + + /** + * Load a resource + * + * @param resource + */ + public void loadResource(Deferred resource); + } +} diff --git a/src/mightypork/rogue/bus/events/ScreenChangeEvent.java b/src/mightypork/rogue/bus/events/ScreenChangeEvent.java index b54cbc1..b18600d 100644 --- a/src/mightypork/rogue/bus/events/ScreenChangeEvent.java +++ b/src/mightypork/rogue/bus/events/ScreenChangeEvent.java @@ -7,9 +7,9 @@ import mightypork.utils.math.coord.Coord; public class ScreenChangeEvent implements Event { - private boolean fullscreen; - private Coord screenSize; - private boolean fsChanged; + private final boolean fullscreen; + private final Coord screenSize; + private final boolean fsChanged; public ScreenChangeEvent(boolean fsChanged, boolean fullscreen, Coord size) { diff --git a/src/mightypork/rogue/bus/events/ScreenRequestEvent.java b/src/mightypork/rogue/bus/events/ScreenRequestEvent.java index b88836c..2183dc8 100644 --- a/src/mightypork/rogue/bus/events/ScreenRequestEvent.java +++ b/src/mightypork/rogue/bus/events/ScreenRequestEvent.java @@ -2,11 +2,12 @@ package mightypork.rogue.bus.events; import mightypork.utils.control.bus.Event; +import mightypork.utils.control.bus.SingularEvent; -public class ScreenRequestEvent implements Event { +public class ScreenRequestEvent implements Event, SingularEvent { - private String scrName; + private final String scrName; public ScreenRequestEvent(String screenKey) { diff --git a/src/mightypork/rogue/gui/screens/LayeredScreen.java b/src/mightypork/rogue/gui/LayeredScreen.java similarity index 58% rename from src/mightypork/rogue/gui/screens/LayeredScreen.java rename to src/mightypork/rogue/gui/LayeredScreen.java index 615d741..e38e97f 100644 --- a/src/mightypork/rogue/gui/screens/LayeredScreen.java +++ b/src/mightypork/rogue/gui/LayeredScreen.java @@ -1,15 +1,15 @@ -package mightypork.rogue.gui.screens; +package mightypork.rogue.gui; +import java.util.Collection; import java.util.LinkedList; import mightypork.rogue.AppAccess; -import mightypork.utils.control.interf.Updateable; public abstract class LayeredScreen extends Screen { - private final LinkedList layers = new LinkedList(); + private final Collection layers = new LinkedList(); public LayeredScreen(AppAccess app) { @@ -17,18 +17,6 @@ public abstract class LayeredScreen extends Screen { } - @Override - protected abstract void deinitScreen(); - - - @Override - protected abstract void onScreenEnter(); - - - @Override - protected abstract void onScreenLeave(); - - @Override protected final void renderScreen() { @@ -38,14 +26,6 @@ public abstract class LayeredScreen extends Screen { } - /** - * Update screen. Layers should implement {@link Updateable} to receive - * updates directly from bus. - */ - @Override - protected abstract void updateScreen(double delta); - - /** * Add a layer to the screen. * diff --git a/src/mightypork/rogue/gui/screens/Screen.java b/src/mightypork/rogue/gui/Screen.java similarity index 86% rename from src/mightypork/rogue/gui/screens/Screen.java rename to src/mightypork/rogue/gui/Screen.java index aa3a157..2f82c01 100644 --- a/src/mightypork/rogue/gui/screens/Screen.java +++ b/src/mightypork/rogue/gui/Screen.java @@ -1,4 +1,4 @@ -package mightypork.rogue.gui.screens; +package mightypork.rogue.gui; import static org.lwjgl.opengl.GL11.*; @@ -10,20 +10,17 @@ import mightypork.rogue.input.KeyBindingPool; import mightypork.rogue.input.KeyStroke; import mightypork.rogue.render.Render; import mightypork.utils.control.interf.Destroyable; -import mightypork.utils.control.interf.Updateable; import mightypork.utils.math.constraints.ConstraintContext; import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Rect; /** - * Screen class.
- * Screen animates 3D world, while contained panels render 2D overlays, process - * inputs and run the game logic. + * Screen class. * * @author MightyPork */ -public abstract class Screen extends ChildClient implements Destroyable, Updateable, KeyBinder, ConstraintContext, ScreenChangeEvent.Listener { +public abstract class Screen extends ChildClient implements Destroyable, KeyBinder, ConstraintContext, ScreenChangeEvent.Listener { private final KeyBindingPool keybindings = new KeyBindingPool(); @@ -127,14 +124,6 @@ public abstract class Screen extends ChildClient implements Destroyable, Updatea protected abstract void renderScreen(); - /** - * Update animations and timing.
- * - * @param delta time elapsed - */ - protected abstract void updateScreen(double delta); - - /** * Render screen */ @@ -173,16 +162,6 @@ public abstract class Screen extends ChildClient implements Destroyable, Updatea } - /** - * Update and render the screen - */ - @Override - public final void update(double delta) - { - updateScreen(delta); - } - - @Override public final Rect getRect() { @@ -241,4 +220,6 @@ public abstract class Screen extends ChildClient implements Destroyable, Updatea glMatrixMode(GL_MODELVIEW); } + + public abstract String getId(); } diff --git a/src/mightypork/rogue/gui/screens/ScreenLayer.java b/src/mightypork/rogue/gui/ScreenLayer.java similarity index 52% rename from src/mightypork/rogue/gui/screens/ScreenLayer.java rename to src/mightypork/rogue/gui/ScreenLayer.java index 29f7f2a..1f64684 100644 --- a/src/mightypork/rogue/gui/screens/ScreenLayer.java +++ b/src/mightypork/rogue/gui/ScreenLayer.java @@ -1,9 +1,11 @@ -package mightypork.rogue.gui.screens; +package mightypork.rogue.gui; import mightypork.rogue.bus.ChildClient; import mightypork.rogue.gui.constraints.Renderable; -import mightypork.utils.control.interf.Updateable; +import mightypork.rogue.input.KeyBinder; +import mightypork.rogue.input.KeyBindingPool; +import mightypork.rogue.input.KeyStroke; import mightypork.utils.math.constraints.ConstraintContext; import mightypork.utils.math.coord.Rect; @@ -13,24 +15,33 @@ import mightypork.utils.math.coord.Rect; * * @author MightyPork */ -public abstract class ScreenLayer extends ChildClient implements Renderable, Updateable, ConstraintContext { +public abstract class ScreenLayer extends ChildClient implements Renderable, ConstraintContext, KeyBinder { - private Screen screen; + private final Screen screen; + + private final KeyBindingPool keybindings = new KeyBindingPool(); public ScreenLayer(Screen screen) { super(screen); // screen as AppAccess this.screen = screen; + addChildClient(keybindings); } @Override - public abstract void render(); + public final void bindKeyStroke(KeyStroke stroke, Runnable task) + { + keybindings.bindKeyStroke(stroke, task); + } @Override - public abstract void update(double delta); + public final void unbindKeyStroke(KeyStroke stroke) + { + keybindings.unbindKeyStroke(stroke); + } protected Screen screen() diff --git a/src/mightypork/rogue/gui/screens/ScreenRegistry.java b/src/mightypork/rogue/gui/ScreenRegistry.java similarity index 74% rename from src/mightypork/rogue/gui/screens/ScreenRegistry.java rename to src/mightypork/rogue/gui/ScreenRegistry.java index dccaa3b..ff492d6 100644 --- a/src/mightypork/rogue/gui/screens/ScreenRegistry.java +++ b/src/mightypork/rogue/gui/ScreenRegistry.java @@ -1,4 +1,4 @@ -package mightypork.rogue.gui.screens; +package mightypork.rogue.gui; import java.util.HashMap; @@ -6,11 +6,12 @@ import java.util.HashMap; import mightypork.rogue.AppAccess; import mightypork.rogue.bus.Subsystem; import mightypork.rogue.bus.events.ScreenRequestEvent; +import mightypork.utils.logging.Log; public class ScreenRegistry extends Subsystem implements ScreenRequestEvent.Listener { - private HashMap screens = new HashMap(); + private final HashMap screens = new HashMap(); private Screen active = null; @@ -19,9 +20,9 @@ public class ScreenRegistry extends Subsystem implements ScreenRequestEvent.List } - public void add(String key, Screen screen) + public void add(Screen screen) { - screens.put(key, screen); + screens.put(screen.getId(), screen); addChildClient(screen); } @@ -29,6 +30,8 @@ public class ScreenRegistry extends Subsystem implements ScreenRequestEvent.List @Override public void showScreen(String key) { + Log.f3("Request to show screen \"" + key + "\""); + Screen toshow = screens.get(key); if (toshow == null) throw new RuntimeException("Screen " + key + " not defined."); diff --git a/src/mightypork/rogue/gui/constraints/ColumnHolder.java b/src/mightypork/rogue/gui/constraints/ColumnHolder.java new file mode 100644 index 0000000..3347bac --- /dev/null +++ b/src/mightypork/rogue/gui/constraints/ColumnHolder.java @@ -0,0 +1,45 @@ +package mightypork.rogue.gui.constraints; + + +import static mightypork.utils.math.constraints.ConstraintFactory.*; +import mightypork.rogue.AppAccess; +import mightypork.utils.math.constraints.ConstraintContext; + + +public class ColumnHolder extends ElementHolder { + + private final int cols; + private int col = 0; + + + public ColumnHolder(AppAccess app, ConstraintContext context, int rows) { + super(app, context); + this.cols = rows; + } + + + public ColumnHolder(AppAccess app, int rows) { + super(app); + this.cols = rows; + } + + + /** + * Add a row to the holder. + * + * @param elem + */ + public void addRow(RenderableWithContext elem) + { + if (elem == null) return; + add(elem, c_column(null, cols, col++)); + } + + + @Override + public void remove(RenderableWithContext elem) + { + throw new UnsupportedOperationException("Can't remove from ColumnHolder."); + } + +} diff --git a/src/mightypork/rogue/gui/constraints/ElementHolder.java b/src/mightypork/rogue/gui/constraints/ElementHolder.java index 6e5f7f6..4c27050 100644 --- a/src/mightypork/rogue/gui/constraints/ElementHolder.java +++ b/src/mightypork/rogue/gui/constraints/ElementHolder.java @@ -19,7 +19,7 @@ import mightypork.utils.math.coord.Rect; */ public class ElementHolder extends ChildClient implements ConstraintContext, RenderableWithContext { - private LinkedList elements = new LinkedList(); + private final LinkedList elements = new LinkedList(); private ConstraintContext context; @@ -57,19 +57,18 @@ public class ElementHolder extends ChildClient implements ConstraintContext, Ren } - /** - * Add element to the holder. - * - * @param elem - */ - public void add(RenderableWithContext elem) - { - if (elem == null) return; - elem.setContext(this); - elements.add(elem); - addChildClient(elem); - } - +// /** +// * Add element to the holder. +// * +// * @param elem +// */ +// public void add(RenderableWithContext elem) +// { +// if (elem == null) return; +// elem.setContext(this); +// elements.add(elem); +// addChildClient(elem); +// } /** * Add element to the holder. diff --git a/src/mightypork/rogue/gui/constraints/RowHolder.java b/src/mightypork/rogue/gui/constraints/RowHolder.java new file mode 100644 index 0000000..fb92f68 --- /dev/null +++ b/src/mightypork/rogue/gui/constraints/RowHolder.java @@ -0,0 +1,45 @@ +package mightypork.rogue.gui.constraints; + + +import static mightypork.utils.math.constraints.ConstraintFactory.*; +import mightypork.rogue.AppAccess; +import mightypork.utils.math.constraints.ConstraintContext; + + +public class RowHolder extends ElementHolder { + + private final int rows; + private int row = 0; + + + public RowHolder(AppAccess app, ConstraintContext context, int rows) { + super(app, context); + this.rows = rows; + } + + + public RowHolder(AppAccess app, int rows) { + super(app); + this.rows = rows; + } + + + /** + * Add a row to the holder. + * + * @param elem + */ + public void addRow(RenderableWithContext elem) + { + if (elem == null) return; + add(elem, c_row(null, rows, row++)); + } + + + @Override + public void remove(RenderableWithContext elem) + { + throw new UnsupportedOperationException("Can't remove from RowHolder."); + } + +} diff --git a/src/mightypork/rogue/gui/screens/screenBouncy/ScreenTestAnim.java b/src/mightypork/rogue/gui/screens/screenBouncy/ScreenTestAnim.java deleted file mode 100644 index d776520..0000000 --- a/src/mightypork/rogue/gui/screens/screenBouncy/ScreenTestAnim.java +++ /dev/null @@ -1,70 +0,0 @@ -package mightypork.rogue.gui.screens.screenBouncy; - - -import mightypork.rogue.AppAccess; -import mightypork.rogue.gui.screens.LayeredScreen; -import mightypork.rogue.input.KeyStroke; - -import org.lwjgl.input.Keyboard; - - -public class ScreenTestAnim extends LayeredScreen { - - private LayerBouncyBoxes layer; - - - public ScreenTestAnim(AppAccess app) { - super(app); - - layer = new LayerBouncyBoxes(this); - - addLayer(layer); - - bindKeyStroke(new KeyStroke(Keyboard.KEY_RIGHT), new Runnable() { - - @Override - public void run() - { - layer.goRight(); - } - }); - - bindKeyStroke(new KeyStroke(Keyboard.KEY_LEFT), new Runnable() { - - @Override - public void run() - { - layer.goLeft(); - } - }); - } - - - @Override - protected void deinitScreen() - { - // no impl - } - - - @Override - protected void onScreenEnter() - { - // no impl - } - - - @Override - protected void onScreenLeave() - { - // no impl - } - - - @Override - protected void updateScreen(double delta) - { - // no impl - } - -} diff --git a/src/mightypork/rogue/gui/screens/screenBouncy/BouncyBox.java b/src/mightypork/rogue/gui/screens/test_bouncyboxes/BouncyBox.java similarity index 96% rename from src/mightypork/rogue/gui/screens/screenBouncy/BouncyBox.java rename to src/mightypork/rogue/gui/screens/test_bouncyboxes/BouncyBox.java index e95da53..df1509b 100644 --- a/src/mightypork/rogue/gui/screens/screenBouncy/BouncyBox.java +++ b/src/mightypork/rogue/gui/screens/test_bouncyboxes/BouncyBox.java @@ -1,4 +1,4 @@ -package mightypork.rogue.gui.screens.screenBouncy; +package mightypork.rogue.gui.screens.test_bouncyboxes; import static mightypork.utils.math.constraints.ConstraintFactory.*; diff --git a/src/mightypork/rogue/gui/screens/screenBouncy/LayerBouncyBoxes.java b/src/mightypork/rogue/gui/screens/test_bouncyboxes/LayerBouncyBoxes.java similarity index 62% rename from src/mightypork/rogue/gui/screens/screenBouncy/LayerBouncyBoxes.java rename to src/mightypork/rogue/gui/screens/test_bouncyboxes/LayerBouncyBoxes.java index fcc61f4..dfed278 100644 --- a/src/mightypork/rogue/gui/screens/screenBouncy/LayerBouncyBoxes.java +++ b/src/mightypork/rogue/gui/screens/test_bouncyboxes/LayerBouncyBoxes.java @@ -1,4 +1,4 @@ -package mightypork.rogue.gui.screens.screenBouncy; +package mightypork.rogue.gui.screens.test_bouncyboxes; import static mightypork.utils.math.constraints.ConstraintFactory.*; @@ -6,29 +6,29 @@ import static mightypork.utils.math.constraints.ConstraintFactory.*; import java.util.ArrayList; import java.util.List; -import mightypork.rogue.gui.constraints.ElementHolder; -import mightypork.rogue.gui.screens.Screen; -import mightypork.rogue.gui.screens.ScreenLayer; +import mightypork.rogue.gui.Screen; +import mightypork.rogue.gui.ScreenLayer; +import mightypork.rogue.gui.constraints.RowHolder; import mightypork.utils.math.constraints.RectConstraint; public class LayerBouncyBoxes extends ScreenLayer { List boxes = new ArrayList(); - private ElementHolder layout; + private RowHolder layout; public LayerBouncyBoxes(Screen screen) { super(screen); + // shrink screen rect by 8% on all sides RectConstraint holder_rect = c_shrink(this, c_percent(c_height(this), c_n(8))); - layout = new ElementHolder(screen, holder_rect); - addChildClient(layout); + addChildClient(layout = new RowHolder(screen, holder_rect, 16)); - for (int i = 0; i < 32; i++) { + for (int i = 0; i < 16; i++) { BouncyBox bbr = new BouncyBox(); - layout.add(bbr, c_row(null, 32, i)); + layout.addRow(bbr); boxes.add(bbr); } @@ -42,13 +42,6 @@ public class LayerBouncyBoxes extends ScreenLayer { } - @Override - public void update(double delta) - { - // no impl - } - - public void goLeft() { for (BouncyBox bbr : boxes) { diff --git a/src/mightypork/rogue/gui/screens/test_bouncyboxes/ScreenTestBouncy.java b/src/mightypork/rogue/gui/screens/test_bouncyboxes/ScreenTestBouncy.java new file mode 100644 index 0000000..1f16042 --- /dev/null +++ b/src/mightypork/rogue/gui/screens/test_bouncyboxes/ScreenTestBouncy.java @@ -0,0 +1,80 @@ +package mightypork.rogue.gui.screens.test_bouncyboxes; + + +import mightypork.rogue.AppAccess; +import mightypork.rogue.bus.events.ScreenRequestEvent; +import mightypork.rogue.gui.LayeredScreen; +import mightypork.rogue.input.KeyStroke; + +import org.lwjgl.input.Keyboard; + + +public class ScreenTestBouncy extends LayeredScreen { + + private LayerBouncyBoxes layer; + + + public ScreenTestBouncy(AppAccess app) { + super(app); + + layer = new LayerBouncyBoxes(this); + + addLayer(layer); + + bindKeyStroke(new KeyStroke(true, Keyboard.KEY_RIGHT), new Runnable() { + + @Override + public void run() + { + layer.goRight(); + } + }); + + bindKeyStroke(new KeyStroke(true, Keyboard.KEY_LEFT), new Runnable() { + + @Override + public void run() + { + layer.goLeft(); + } + }); + + bindKeyStroke(new KeyStroke(Keyboard.KEY_C), new Runnable() { + + @Override + public void run() + { + bus().queue(new ScreenRequestEvent("test.cat")); + } + }); + } + + + @Override + protected void deinitScreen() + { + // no impl + } + + + @Override + protected void onScreenEnter() + { + // no impl + } + + + @Override + protected void onScreenLeave() + { + // no impl + } + + + @Override + public String getId() + { + return "test.bouncy"; + } + +} diff --git a/src/mightypork/rogue/gui/screens/screenTextures/ScreenTextureTest.java b/src/mightypork/rogue/gui/screens/test_cat_sound/LayerFlyingCat.java similarity index 59% rename from src/mightypork/rogue/gui/screens/screenTextures/ScreenTextureTest.java rename to src/mightypork/rogue/gui/screens/test_cat_sound/LayerFlyingCat.java index 9370b03..0708279 100644 --- a/src/mightypork/rogue/gui/screens/screenTextures/ScreenTextureTest.java +++ b/src/mightypork/rogue/gui/screens/test_cat_sound/LayerFlyingCat.java @@ -1,27 +1,27 @@ -package mightypork.rogue.gui.screens.screenTextures; +package mightypork.rogue.gui.screens.test_cat_sound; import static mightypork.utils.math.constraints.ConstraintFactory.*; import java.util.Random; -import mightypork.rogue.AppAccess; import mightypork.rogue.Res; -import mightypork.rogue.bus.events.ActionRequest; import mightypork.rogue.bus.events.MouseButtonEvent; -import mightypork.rogue.bus.events.RequestType; -import mightypork.rogue.gui.screens.Screen; +import mightypork.rogue.gui.Screen; +import mightypork.rogue.gui.ScreenLayer; import mightypork.rogue.input.KeyStroke; import mightypork.rogue.render.Render; +import mightypork.utils.control.interf.Updateable; import mightypork.utils.math.animation.AnimDouble; import mightypork.utils.math.animation.Easing; import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.coord.Coord; import org.lwjgl.input.Keyboard; +import org.newdawn.slick.opengl.Texture; -public class ScreenTextureTest extends Screen implements MouseButtonEvent.Listener { +public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButtonEvent.Listener { private RectConstraint kittenbox; @@ -31,53 +31,28 @@ public class ScreenTextureTest extends Screen implements MouseButtonEvent.Listen private Random rand = new Random(); + private Texture cat_tx = Res.getTexture("test.kitten"); - public ScreenTextureTest(AppAccess app) { - super(app); + + public LayerFlyingCat(Screen screen) { + super(screen); kittenbox = c_move(c_box_sized(this, c_n(s), c_n(s)), c_n(x), c_n(y)); - bindKeyStroke(new KeyStroke(Keyboard.KEY_ESCAPE), new Runnable() { + bindKeyStroke(new KeyStroke(Keyboard.KEY_RETURN), new Runnable() { @Override public void run() { - snd().fadeOutAllLoops(); - bus().schedule(new ActionRequest(RequestType.SHUTDOWN), 3); + x.fadeTo(disp().getWidth() / 2 - s.getTo() / 2, 2); + y.fadeTo(disp().getHeight() / 2 - s.getTo() / 2, 2); } }); } @Override - protected void deinitScreen() - { - } - - - @Override - protected void onScreenEnter() - { - System.out.println("YOLO"); - Res.getLoop("test.wilderness").fadeIn(); - } - - - @Override - protected void onScreenLeave() - { - } - - - @Override - protected void renderScreen() - { - Render.quadTextured(kittenbox.getRect(), Res.getTexture("test.kitten")); - } - - - @Override - protected void updateScreen(double delta) + public void update(double delta) { s.update(delta); x.update(delta); @@ -101,4 +76,11 @@ public class ScreenTextureTest extends Screen implements MouseButtonEvent.Listen y.fadeTo(pos.y - newSize / 2D, t); } + + @Override + public void render() + { + Render.quadTextured(kittenbox.getRect(), cat_tx); + } + } diff --git a/src/mightypork/rogue/gui/screens/test_cat_sound/ScreenTestCat.java b/src/mightypork/rogue/gui/screens/test_cat_sound/ScreenTestCat.java new file mode 100644 index 0000000..322bf00 --- /dev/null +++ b/src/mightypork/rogue/gui/screens/test_cat_sound/ScreenTestCat.java @@ -0,0 +1,73 @@ +package mightypork.rogue.gui.screens.test_cat_sound; + + +import mightypork.rogue.AppAccess; +import mightypork.rogue.Res; +import mightypork.rogue.bus.events.ActionRequest; +import mightypork.rogue.bus.events.ActionRequest.RequestType; +import mightypork.rogue.bus.events.ScreenRequestEvent; +import mightypork.rogue.gui.LayeredScreen; +import mightypork.rogue.input.KeyStroke; + +import org.lwjgl.input.Keyboard; + + +public class ScreenTestCat extends LayeredScreen { + + LayerFlyingCat layer; + + + public ScreenTestCat(AppAccess app) { + super(app); + + addLayer(layer = new LayerFlyingCat(this)); + + bindKeyStroke(new KeyStroke(Keyboard.KEY_ESCAPE), new Runnable() { + + @Override + public void run() + { + snd().fadeOutAllLoops(); + bus().schedule(new ActionRequest(RequestType.SHUTDOWN), 3); + } + }); + + bindKeyStroke(new KeyStroke(Keyboard.KEY_B), new Runnable() { + + @Override + public void run() + { + bus().queue(new ScreenRequestEvent("test.bouncy")); + } + }); + } + + + @Override + protected void deinitScreen() + { + } + + + @Override + protected void onScreenEnter() + { + snd().fadeOutAllLoops(); + Res.getLoop("test.wilderness").fadeIn(); + } + + + @Override + protected void onScreenLeave() + { + Res.getLoop("test.wilderness").fadeOut(); + } + + + @Override + public String getId() + { + return "test.cat"; + } + +} diff --git a/src/mightypork/rogue/input/InputSystem.java b/src/mightypork/rogue/input/InputSystem.java index 0f4814f..b732755 100644 --- a/src/mightypork/rogue/input/InputSystem.java +++ b/src/mightypork/rogue/input/InputSystem.java @@ -4,10 +4,10 @@ package mightypork.rogue.input; import mightypork.rogue.AppAccess; import mightypork.rogue.bus.Subsystem; import mightypork.rogue.bus.events.ActionRequest; +import mightypork.rogue.bus.events.ActionRequest.RequestType; import mightypork.rogue.bus.events.KeyboardEvent; import mightypork.rogue.bus.events.MouseButtonEvent; import mightypork.rogue.bus.events.MouseMotionEvent; -import mightypork.rogue.bus.events.RequestType; import mightypork.utils.control.interf.Updateable; import mightypork.utils.math.coord.Coord; @@ -20,7 +20,7 @@ import org.lwjgl.opengl.Display; public class InputSystem extends Subsystem implements Updateable, KeyBinder { // listeners - private KeyBindingPool keybindings; + private final KeyBindingPool keybindings; private boolean yAxisDown = true; @@ -127,6 +127,7 @@ public class InputSystem extends Subsystem implements Updateable, KeyBinder { int key = Keyboard.getEventKey(); boolean down = Keyboard.getEventKeyState(); char c = Keyboard.getEventCharacter(); + bus().queue(new KeyboardEvent(key, c, down)); } @@ -137,4 +138,16 @@ public class InputSystem extends Subsystem implements Updateable, KeyBinder { c.setY_ip(disp().getSize().y - c.y); } } + + + /** + * Set whether Y axis should go top-down instead of LWJGL default bottom-up.
+ * Default = true. + * + * @param yAxisDown + */ + public void setYDown(boolean yAxisDown) + { + this.yAxisDown = yAxisDown; + } } diff --git a/src/mightypork/rogue/input/KeyBinding.java b/src/mightypork/rogue/input/KeyBinding.java index fcf8202..4c56159 100644 --- a/src/mightypork/rogue/input/KeyBinding.java +++ b/src/mightypork/rogue/input/KeyBinding.java @@ -6,7 +6,7 @@ import mightypork.rogue.bus.events.KeyboardEvent; public class KeyBinding implements KeyboardEvent.Listener { - private KeyStroke keystroke; + private final KeyStroke keystroke; private Runnable handler; private boolean wasActive = false; diff --git a/src/mightypork/rogue/input/KeyBindingPool.java b/src/mightypork/rogue/input/KeyBindingPool.java index 9e4d3cd..2d7252a 100644 --- a/src/mightypork/rogue/input/KeyBindingPool.java +++ b/src/mightypork/rogue/input/KeyBindingPool.java @@ -16,7 +16,7 @@ import mightypork.utils.logging.Log; */ public class KeyBindingPool implements KeyBinder, KeyboardEvent.Listener { - private Set bindings = new HashSet(); + private final Set bindings = new HashSet(); /** diff --git a/src/mightypork/rogue/input/KeyStroke.java b/src/mightypork/rogue/input/KeyStroke.java index 7f47c79..aee83f1 100644 --- a/src/mightypork/rogue/input/KeyStroke.java +++ b/src/mightypork/rogue/input/KeyStroke.java @@ -10,18 +10,18 @@ import org.lwjgl.input.Keyboard; public class KeyStroke { - private Set keys = new LinkedHashSet(); - private boolean down = true; + private final Set keys = new LinkedHashSet(); + private final boolean fallingEdge; /** * KeyStroke * - * @param down true for falling edge, up for rising edge + * @param fallingEdge true for falling edge, up for rising edge * @param keys keys that must be pressed */ - public KeyStroke(boolean down, int... keys) { - this.down = down; + public KeyStroke(boolean fallingEdge, int... keys) { + this.fallingEdge = fallingEdge; for (int k : keys) { this.keys.add(k); } @@ -29,11 +29,12 @@ public class KeyStroke { /** - * Falling edge keystroke + * Rising edge keystroke * * @param keys */ public KeyStroke(int... keys) { + fallingEdge = false; for (int k : keys) { this.keys.add(k); } @@ -47,13 +48,7 @@ public class KeyStroke { st &= Keyboard.isKeyDown(k); } - return down ? st : !st; - } - - - public void setKeys(Set keys) - { - this.keys = keys; + return fallingEdge ? st : !st; } @@ -81,7 +76,7 @@ public class KeyStroke { return false; } - if (down != other.down) return false; + if (fallingEdge != other.fallingEdge) return false; return true; } @@ -99,7 +94,7 @@ public class KeyStroke { s += Keyboard.getKeyName(i.next()); } - s += down ? ",DOWN" : "UP"; + s += fallingEdge ? ",DOWN" : ",UP"; s += ")"; diff --git a/src/mightypork/rogue/render/DisplaySystem.java b/src/mightypork/rogue/render/DisplaySystem.java index f5bd4be..e72a286 100644 --- a/src/mightypork/rogue/render/DisplaySystem.java +++ b/src/mightypork/rogue/render/DisplaySystem.java @@ -140,7 +140,19 @@ public class DisplaySystem extends Subsystem implements ConstraintContext { */ public Coord getSize() { - return new Coord(Display.getWidth(), Display.getHeight()); + return new Coord(getWidth(), getHeight()); + } + + + public int getWidth() + { + return Display.getWidth(); + } + + + public int getHeight() + { + return Display.getHeight(); } diff --git a/src/mightypork/rogue/render/Render.java b/src/mightypork/rogue/render/Render.java index 5a3bd8f..3456325 100644 --- a/src/mightypork/rogue/render/Render.java +++ b/src/mightypork/rogue/render/Render.java @@ -5,7 +5,7 @@ import static org.lwjgl.opengl.GL11.*; import java.io.IOException; -import mightypork.rogue.render.textures.TxQuad; +import mightypork.rogue.texture.TxQuad; import mightypork.utils.files.FileUtils; import mightypork.utils.logging.Log; import mightypork.utils.math.color.RGB; @@ -110,14 +110,16 @@ public class Render { * @param resourcePath * @return the loaded texture */ - public static Texture loadTexture(String resourcePath) + public synchronized static Texture loadTexture(String resourcePath) { + if (!inited) { + inited = true; + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - inited = true; } try { @@ -187,7 +189,7 @@ public class Render { { double left = quad.xMin(); double bottom = quad.yMin(); - double right = quad.yMax(); + double right = quad.xMax(); double top = quad.yMax(); // draw with color diff --git a/src/mightypork/rogue/audio/DeferredAudio.java b/src/mightypork/rogue/sound/DeferredAudio.java similarity index 96% rename from src/mightypork/rogue/audio/DeferredAudio.java rename to src/mightypork/rogue/sound/DeferredAudio.java index 4a4ac89..239ddc2 100644 --- a/src/mightypork/rogue/audio/DeferredAudio.java +++ b/src/mightypork/rogue/sound/DeferredAudio.java @@ -1,4 +1,4 @@ -package mightypork.rogue.audio; +package mightypork.rogue.sound; import mightypork.rogue.Deferred; @@ -90,14 +90,14 @@ public class DeferredAudio implements Destroyable, Deferred { * @return resource is loaded */ @Override - public boolean isLoaded() + public synchronized boolean isLoaded() { return audio != null; } @Override - public void load() + public synchronized void load() { ensureLoaded(); } @@ -108,7 +108,7 @@ public class DeferredAudio implements Destroyable, Deferred { * * @return is loaded */ - protected boolean ensureLoaded() + protected synchronized boolean ensureLoaded() { if (isLoaded()) return true; // already loaded if (loadFailed || resourcePath == null) return false; @@ -295,4 +295,11 @@ public class DeferredAudio implements Destroyable, Deferred { return true; } + + @Override + public String toString() + { + return "Audio(\"" + resourcePath + "\")"; + } + } diff --git a/src/mightypork/rogue/audio/JointVolume.java b/src/mightypork/rogue/sound/JointVolume.java similarity index 90% rename from src/mightypork/rogue/audio/JointVolume.java rename to src/mightypork/rogue/sound/JointVolume.java index 87ec20e..7fda073 100644 --- a/src/mightypork/rogue/audio/JointVolume.java +++ b/src/mightypork/rogue/sound/JointVolume.java @@ -1,4 +1,4 @@ -package mightypork.rogue.audio; +package mightypork.rogue.sound; import mightypork.utils.math.Calc; @@ -12,7 +12,7 @@ import mightypork.utils.objects.Mutable; */ public class JointVolume extends Mutable { - private Mutable[] volumes; + private final Mutable[] volumes; /** diff --git a/src/mightypork/rogue/audio/NullAudio.java b/src/mightypork/rogue/sound/NullAudio.java similarity index 89% rename from src/mightypork/rogue/audio/NullAudio.java rename to src/mightypork/rogue/sound/NullAudio.java index a3d21b8..a5df14b 100644 --- a/src/mightypork/rogue/audio/NullAudio.java +++ b/src/mightypork/rogue/sound/NullAudio.java @@ -1,4 +1,4 @@ -package mightypork.rogue.audio; +package mightypork.rogue.sound; public class NullAudio extends DeferredAudio { diff --git a/src/mightypork/rogue/audio/SoundBank.java b/src/mightypork/rogue/sound/SoundBank.java similarity index 86% rename from src/mightypork/rogue/audio/SoundBank.java rename to src/mightypork/rogue/sound/SoundBank.java index 0b9c85c..49cc8b6 100644 --- a/src/mightypork/rogue/audio/SoundBank.java +++ b/src/mightypork/rogue/sound/SoundBank.java @@ -1,4 +1,4 @@ -package mightypork.rogue.audio; +package mightypork.rogue.sound; import java.util.HashMap; @@ -6,6 +6,8 @@ import java.util.Map; import mightypork.rogue.AppAccess; import mightypork.rogue.AppAdapter; +import mightypork.rogue.sound.players.EffectPlayer; +import mightypork.rogue.sound.players.LoopPlayer; import mightypork.utils.logging.Log; @@ -15,8 +17,8 @@ public class SoundBank extends AppAdapter { private static final LoopPlayer NULL_LOOP = new LoopPlayer(NO_SOUND, 0, 0, null); private static final EffectPlayer NULL_EFFECT = new EffectPlayer(NO_SOUND, 0, 0, null); - private Map effects = new HashMap(); - private Map loops = new HashMap(); + private final Map effects = new HashMap(); + private final Map loops = new HashMap(); public SoundBank(AppAccess app) { diff --git a/src/mightypork/rogue/audio/SoundSystem.java b/src/mightypork/rogue/sound/SoundSystem.java similarity index 86% rename from src/mightypork/rogue/audio/SoundSystem.java rename to src/mightypork/rogue/sound/SoundSystem.java index e0f4b80..54c3aab 100644 --- a/src/mightypork/rogue/audio/SoundSystem.java +++ b/src/mightypork/rogue/sound/SoundSystem.java @@ -1,4 +1,4 @@ -package mightypork.rogue.audio; +package mightypork.rogue.sound; import java.nio.FloatBuffer; @@ -7,6 +7,9 @@ import java.util.Set; import mightypork.rogue.AppAccess; import mightypork.rogue.bus.Subsystem; +import mightypork.rogue.bus.events.ResourceLoadRequest; +import mightypork.rogue.sound.players.EffectPlayer; +import mightypork.rogue.sound.players.LoopPlayer; import mightypork.utils.control.interf.Updateable; import mightypork.utils.math.Calc.Buffers; import mightypork.utils.math.coord.Coord; @@ -69,12 +72,12 @@ public class SoundSystem extends Subsystem implements Updateable { // -- instance -- - public Mutable masterVolume = new Mutable(1D); - public Mutable effectsVolume = new JointVolume(masterVolume); - public Mutable loopsVolume = new JointVolume(masterVolume); + public final Mutable masterVolume = new Mutable(1D); + public final Mutable effectsVolume = new JointVolume(masterVolume); + public final Mutable loopsVolume = new JointVolume(masterVolume); - private Set loopPlayers = new HashSet(); - private Set resources = new HashSet(); + private final Set loopPlayers = new HashSet(); + private final Set resources = new HashSet(); public SoundSystem(AppAccess app) { @@ -146,6 +149,8 @@ public class SoundSystem extends Subsystem implements Updateable { private DeferredAudio getResource(String res) { DeferredAudio a = new DeferredAudio(res); + bus().queue(new ResourceLoadRequest(a)); + if (resources.contains(a)) throw new IllegalArgumentException("Sound resource " + res + " is already registered."); resources.add(a); return a; diff --git a/src/mightypork/rogue/audio/BaseAudioPlayer.java b/src/mightypork/rogue/sound/players/BaseAudioPlayer.java similarity index 75% rename from src/mightypork/rogue/audio/BaseAudioPlayer.java rename to src/mightypork/rogue/sound/players/BaseAudioPlayer.java index 3c7555a..d3ccf72 100644 --- a/src/mightypork/rogue/audio/BaseAudioPlayer.java +++ b/src/mightypork/rogue/sound/players/BaseAudioPlayer.java @@ -1,22 +1,24 @@ -package mightypork.rogue.audio; +package mightypork.rogue.sound.players; +import mightypork.rogue.sound.DeferredAudio; +import mightypork.utils.control.interf.Destroyable; import mightypork.utils.objects.Mutable; -public abstract class BaseAudioPlayer { +public abstract class BaseAudioPlayer implements Destroyable { /** the track */ - private DeferredAudio audio; + private final DeferredAudio audio; /** base gain for sfx */ - private double baseGain = 1; + private final double baseGain; /** base pitch for sfx */ - private double basePitch = 1; + private final double basePitch; /** dedicated volume control */ - private Mutable gainMultiplier = null; + private final Mutable gainMultiplier; public BaseAudioPlayer(DeferredAudio track, double baseGain, Mutable gainMultiplier) { @@ -36,10 +38,10 @@ public abstract class BaseAudioPlayer { } + @Override public void destroy() { audio.destroy(); - audio = null; } diff --git a/src/mightypork/rogue/audio/EffectPlayer.java b/src/mightypork/rogue/sound/players/EffectPlayer.java similarity index 89% rename from src/mightypork/rogue/audio/EffectPlayer.java rename to src/mightypork/rogue/sound/players/EffectPlayer.java index 6297446..a288d77 100644 --- a/src/mightypork/rogue/audio/EffectPlayer.java +++ b/src/mightypork/rogue/sound/players/EffectPlayer.java @@ -1,6 +1,7 @@ -package mightypork.rogue.audio; +package mightypork.rogue.sound.players; +import mightypork.rogue.sound.DeferredAudio; import mightypork.utils.math.coord.Coord; import mightypork.utils.objects.Mutable; diff --git a/src/mightypork/rogue/audio/LoopPlayer.java b/src/mightypork/rogue/sound/players/LoopPlayer.java similarity index 93% rename from src/mightypork/rogue/audio/LoopPlayer.java rename to src/mightypork/rogue/sound/players/LoopPlayer.java index fd672fc..20428d1 100644 --- a/src/mightypork/rogue/audio/LoopPlayer.java +++ b/src/mightypork/rogue/sound/players/LoopPlayer.java @@ -1,6 +1,7 @@ -package mightypork.rogue.audio; +package mightypork.rogue.sound.players; +import mightypork.rogue.sound.DeferredAudio; import mightypork.utils.control.interf.Updateable; import mightypork.utils.control.timing.Pauseable; import mightypork.utils.math.animation.AnimDouble; @@ -14,7 +15,7 @@ public class LoopPlayer extends BaseAudioPlayer implements Updateable, Pauseable private int sourceID = -1; /** animator for fade in and fade out */ - private AnimDouble fadeAnim = new AnimDouble(0); + private final AnimDouble fadeAnim = new AnimDouble(0); private double lastUpdateGain = 0; diff --git a/src/mightypork/rogue/texture/DeferredLoader.java b/src/mightypork/rogue/texture/DeferredLoader.java new file mode 100644 index 0000000..2ede4e0 --- /dev/null +++ b/src/mightypork/rogue/texture/DeferredLoader.java @@ -0,0 +1,102 @@ +package mightypork.rogue.texture; + + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; + +import mightypork.rogue.AppAccess; +import mightypork.rogue.Deferred; +import mightypork.rogue.bus.events.MainLoopTaskRequest; +import mightypork.rogue.bus.events.ResourceLoadRequest; +import mightypork.utils.control.interf.Destroyable; +import mightypork.utils.logging.Log; + + +/** + * Asynchronous resource loading thread. + * + * @author MightyPork + */ +public class DeferredLoader extends Thread implements ResourceLoadRequest.Listener, Destroyable { + + public static void launch(AppAccess app) + { + (new DeferredLoader(app)).start(); + } + + private ExecutorService exs = Executors.newCachedThreadPool(); + + private LinkedBlockingQueue toLoad = new LinkedBlockingQueue(); + private boolean stopped; + private AppAccess app; + + + public DeferredLoader(AppAccess app) { + super("Deferred loader"); + this.app = app; + } + + + @Override + public void loadResource(Deferred resource) + { + toLoad.add(resource); + } + + + @Override + public void run() + { + while (!stopped) { + + try { + final Deferred def = toLoad.take(); + if (def == null) continue; + + if (!def.isLoaded()) { + + // texture needs to be loaded in main thread, unfortunately. + // -> delegate to MainLoop + if (def instanceof DeferredTexture) { + app.bus().queue(new MainLoopTaskRequest(new Runnable() { + + @Override + public void run() + { + Log.f3(" Loading \"" + Log.str(def) + "\""); + def.load(); + } + })); + + continue; + } + + Log.f3(" Loading \"" + Log.str(def) + "\""); + + exs.submit(new Runnable() { + + @Override + public void run() + { + def.load(); + } + }); + } + + } catch (InterruptedException ignored) { + // + } + + } + } + + + @Override + public void destroy() + { + stopped = true; + exs.shutdownNow(); + } + +} diff --git a/src/mightypork/rogue/render/textures/MultiTexture.java b/src/mightypork/rogue/texture/DeferredTexture.java similarity index 85% rename from src/mightypork/rogue/render/textures/MultiTexture.java rename to src/mightypork/rogue/texture/DeferredTexture.java index 4425d81..4f68634 100644 --- a/src/mightypork/rogue/render/textures/MultiTexture.java +++ b/src/mightypork/rogue/texture/DeferredTexture.java @@ -1,4 +1,4 @@ -package mightypork.rogue.render.textures; +package mightypork.rogue.texture; import mightypork.rogue.Deferred; @@ -8,13 +8,13 @@ import mightypork.utils.math.coord.Rect; import org.newdawn.slick.opengl.Texture; -public class MultiTexture implements Texture, Deferred { +public class DeferredTexture implements Texture, Deferred { private Texture backingTexture; - private String resourcePath; + private final String resourcePath; - public MultiTexture(String resourcePath) { + public DeferredTexture(String resourcePath) { this.resourcePath = resourcePath; } @@ -26,7 +26,7 @@ public class MultiTexture implements Texture, Deferred { @Override - public void load() + public synchronized void load() { if (!isLoaded()) { backingTexture = Render.loadTexture(resourcePath); @@ -144,4 +144,11 @@ public class MultiTexture implements Texture, Deferred { backingTexture.setTextureFilter(textureFilter); } + + @Override + public String toString() + { + return "Texture(\"" + resourcePath + "\")"; + } + } diff --git a/src/mightypork/rogue/render/textures/TextureBank.java b/src/mightypork/rogue/texture/TextureBank.java similarity index 80% rename from src/mightypork/rogue/render/textures/TextureBank.java rename to src/mightypork/rogue/texture/TextureBank.java index b3c2f47..8fa5539 100644 --- a/src/mightypork/rogue/render/textures/TextureBank.java +++ b/src/mightypork/rogue/texture/TextureBank.java @@ -1,10 +1,11 @@ -package mightypork.rogue.render.textures; +package mightypork.rogue.texture; import java.util.HashMap; import mightypork.rogue.AppAccess; import mightypork.rogue.AppAdapter; +import mightypork.rogue.bus.events.ResourceLoadRequest; import mightypork.utils.math.coord.Rect; import org.newdawn.slick.opengl.Texture; @@ -21,11 +22,11 @@ public class TextureBank extends AppAdapter { super(app); } - private HashMap textures = new HashMap(); + private final HashMap textures = new HashMap(); - private HashMap quads = new HashMap(); + private final HashMap quads = new HashMap(); - private MultiTexture lastTx; + private DeferredTexture lastTx; /** @@ -36,7 +37,9 @@ public class TextureBank extends AppAdapter { */ public void loadTexture(String key, String resourcePath) { - MultiTexture tx = new MultiTexture(resourcePath); + DeferredTexture tx = new DeferredTexture(resourcePath); + bus().queue(new ResourceLoadRequest(tx)); + textures.put(key, tx); lastTx = tx; } @@ -51,7 +54,7 @@ public class TextureBank extends AppAdapter { */ public void makeQuad(String quadKey, String textureKey, Rect quad) { - MultiTexture tx = textures.get(textureKey); + DeferredTexture tx = textures.get(textureKey); if (tx == null) throw new RuntimeException("Texture with key " + textureKey + " not defined!"); TxQuad txquad = tx.getQuad(quad); @@ -68,7 +71,7 @@ public class TextureBank extends AppAdapter { */ public void makeQuad(String quadKey, Rect quad) { - MultiTexture tx = lastTx; + DeferredTexture tx = lastTx; if (tx == null) throw new RuntimeException("There's no texture loaded yet, can't define quads!"); TxQuad txquad = tx.getQuad(quad); diff --git a/src/mightypork/rogue/render/textures/TxQuad.java b/src/mightypork/rogue/texture/TxQuad.java similarity index 95% rename from src/mightypork/rogue/render/textures/TxQuad.java rename to src/mightypork/rogue/texture/TxQuad.java index 9f4be4a..5463c88 100644 --- a/src/mightypork/rogue/render/textures/TxQuad.java +++ b/src/mightypork/rogue/texture/TxQuad.java @@ -1,4 +1,4 @@ -package mightypork.rogue.render.textures; +package mightypork.rogue.texture; import mightypork.utils.math.coord.Rect; @@ -14,9 +14,9 @@ import org.newdawn.slick.opengl.Texture; public class TxQuad { /** The texture */ - public Texture tx; + public final Texture tx; /** Coords in texture (0-1) */ - public Rect uvs; + public final Rect uvs; /** diff --git a/src/mightypork/utils/control/bus/EventBus.java b/src/mightypork/utils/control/bus/EventBus.java index 461c72d..a646210 100644 --- a/src/mightypork/utils/control/bus/EventBus.java +++ b/src/mightypork/utils/control/bus/EventBus.java @@ -16,17 +16,30 @@ import mightypork.utils.logging.Log; */ final public class EventBus implements Destroyable { + /** Message channels */ private BufferedHashSet> channels = new BufferedHashSet>(); + + /** Registered clients */ private BufferedHashSet clients = new BufferedHashSet(); + + /** Messages queued for delivery */ private DelayQueue sendQueue = new DelayQueue(); - private BusThread busThread; + /** Queue polling thread */ + private QueuePollingThread busThread; + + /** Log all */ private boolean logging = false; + + /** Whether the bus was destroyed */ private boolean dead = false; + /** + * Make a new bus and start it's queue thread. + */ public EventBus() { - busThread = new BusThread(); + busThread = new QueuePollingThread(); busThread.start(); } @@ -153,12 +166,18 @@ final public class EventBus implements Destroyable { if (logging) Log.f3(" - [ Sending: " + Log.str(message) + " ]"); boolean sent = false; + boolean channelAccepted = false; for (EventChannel b : channels) { + if (b.canBroadcast(message)) channelAccepted = true; sent |= b.broadcast(message, clients); } - if (!sent) Log.w(" Not accepted by any channel: " + Log.str(message)); + // more severe + if (!channelAccepted) Log.w(" Not accepted by any channel: " + Log.str(message)); + + // less severe + if (logging && !sent) Log.w(" Not delivered to any client: " + Log.str(message)); channels.setBuffering(false); clients.setBuffering(false); @@ -248,16 +267,23 @@ final public class EventBus implements Destroyable { } - private class BusThread extends Thread { + private class QueuePollingThread extends Thread { + + public boolean stopped = false; - public boolean stopped; + + public QueuePollingThread() { + super("Queue Polling Thread"); + } @Override public void run() { + DelayedMessage dm; + while (!stopped) { - DelayedMessage dm = null; + dm = null; try { dm = sendQueue.take(); diff --git a/src/mightypork/utils/control/bus/EventChannel.java b/src/mightypork/utils/control/bus/EventChannel.java index b41a3b4..517d92c 100644 --- a/src/mightypork/utils/control/bus/EventChannel.java +++ b/src/mightypork/utils/control/bus/EventChannel.java @@ -55,7 +55,7 @@ final public class EventChannel, CLIENT> { * * @param message a message to be sent * @param clients collection of clients - * @return true if message was accepted by this channel + * @return true if message was sent */ public boolean broadcast(Event message, Collection clients) { @@ -63,9 +63,7 @@ final public class EventChannel, CLIENT> { EVENT evt = messageClass.cast(message); - doBroadcast(evt, clients, new HashSet()); - - return true; + return doBroadcast(evt, clients, new HashSet()); } @@ -75,14 +73,20 @@ final public class EventChannel, CLIENT> { * @param message sent message * @param clients subscribing clients * @param processed clients already processed + * @return success */ - private void doBroadcast(final EVENT message, final Collection clients, final Collection processed) + private boolean doBroadcast(final EVENT message, final Collection clients, final Collection processed) { + boolean sent = false; + for (Object client : clients) { + + // exclude obvious non-clients if (!isClientValid(client)) { continue; } + // avoid executing more times if (processed.contains(client)) { Log.w(" Client already served: " + Log.str(client)); continue; @@ -97,17 +101,28 @@ final public class EventChannel, CLIENT> { } } - sendTo(client, message); + sent |= sendTo(client, message); + + // singular event ain't no whore, handled once only. + if (sent && message instanceof SingularEvent) return true; + // pass on to delegated clients if (client instanceof DelegatingClient) { if (((DelegatingClient) client).doesDelegate()) { + Collection children = ((DelegatingClient) client).getChildClients(); - if (children != null && children.size() > 0) doBroadcast(message, children, processed); + + if (children != null && children.size() > 0) { + sent |= doBroadcast(message, children, processed); + } + } else { if (logging) Log.f3(" Client not delegating: " + Log.str(client)); } } } + + return sent; } @@ -116,14 +131,17 @@ final public class EventChannel, CLIENT> { * * @param client target client * @param message message to send + * @return success */ @SuppressWarnings("unchecked") - private void sendTo(Object client, EVENT message) + private boolean sendTo(Object client, EVENT message) { if (isClientOfType(client)) { if (logging) Log.f3(" Delivered " + Log.str(message) + " to " + Log.str(client)); ((Event) message).handleBy((CLIENT) client); + return true; } + return false; } diff --git a/src/mightypork/utils/control/bus/SingularEvent.java b/src/mightypork/utils/control/bus/SingularEvent.java new file mode 100644 index 0000000..4226763 --- /dev/null +++ b/src/mightypork/utils/control/bus/SingularEvent.java @@ -0,0 +1,11 @@ +package mightypork.utils.control.bus; + + +/** + * Event handled by only single client. + * + * @author MightyPork + */ +public interface SingularEvent { + +} diff --git a/src/mightypork/utils/control/bus/events/DestroyEvent.java b/src/mightypork/utils/control/bus/events/DestroyEvent.java index 28c6b00..80b45fa 100644 --- a/src/mightypork/utils/control/bus/events/DestroyEvent.java +++ b/src/mightypork/utils/control/bus/events/DestroyEvent.java @@ -5,6 +5,11 @@ import mightypork.utils.control.bus.Event; import mightypork.utils.control.interf.Destroyable; +/** + * Invoke destroy() method of all subscribers. Used to deinit a system. + * + * @author MightyPork + */ public class DestroyEvent implements Event { @Override diff --git a/src/mightypork/utils/control/timing/TimerFps.java b/src/mightypork/utils/control/timing/TimerFps.java index d94e841..83438e3 100644 --- a/src/mightypork/utils/control/timing/TimerFps.java +++ b/src/mightypork/utils/control/timing/TimerFps.java @@ -95,7 +95,6 @@ public class TimerFps { */ public void startNewFrame() { - // System.out.println("! start new frame !"); long time = getTime(); lastFrame = time; nextFrame = time + FRAME; diff --git a/src/mightypork/utils/logging/Log.java b/src/mightypork/utils/logging/Log.java index 115a1d0..9da5c90 100644 --- a/src/mightypork/utils/logging/Log.java +++ b/src/mightypork/utils/logging/Log.java @@ -18,7 +18,7 @@ public class Log { */ public static void f1(String msg) { - if (esl && main != null) main.f1(msg); + if (esl && ready()) main.f1(msg); } @@ -29,7 +29,7 @@ public class Log { */ public static void f2(String msg) { - if (esl && main != null) main.f2(msg); + if (esl && ready()) main.f2(msg); } @@ -40,7 +40,7 @@ public class Log { */ public static void f3(String msg) { - if (esl && main != null) main.f3(msg); + if (esl && ready()) main.f3(msg); } @@ -51,7 +51,7 @@ public class Log { */ public static void i(String msg) { - if (esl && main != null) main.i(msg); + if (esl && ready()) main.i(msg); } @@ -62,7 +62,7 @@ public class Log { */ public static void w(String msg) { - if (esl && main != null) main.w(msg); + if (esl && ready()) main.w(msg); } @@ -73,7 +73,7 @@ public class Log { */ public static void e(String msg) { - if (esl && main != null) main.e(msg); + if (esl && ready()) main.e(msg); } @@ -85,7 +85,7 @@ public class Log { */ public static void e(String msg, Throwable thrown) { - if (esl && main != null) main.e(msg, thrown); + if (esl && ready()) main.e(msg, thrown); } @@ -96,13 +96,13 @@ public class Log { */ public static void e(Throwable thrown) { - if (esl && main != null) main.e(thrown); + if (esl && ready()) main.e(thrown); } public static void enable(boolean flag) { - if (esl && main != null) main.enable(flag); + if (esl && ready()) main.enable(flag); } @@ -163,7 +163,7 @@ public class Log { public int addMonitor(LogMonitor mon) { - if (main == null) throw new IllegalStateException("Main logger not initialized."); + if (!ready()) throw new IllegalStateException("Main logger not initialized."); return main.addMonitor(mon); } @@ -171,7 +171,7 @@ public class Log { public void removeMonitor(int id) { - if (main == null) throw new IllegalStateException("Main logger not initialized."); + if (!ready()) throw new IllegalStateException("Main logger not initialized."); main.removeMonitor(id); } @@ -207,4 +207,10 @@ public class Log { return (enclosing == null ? "" : enclosing.getSimpleName() + ".") + cls.getSimpleName(); } + + + public static boolean ready() + { + return main != null; + } } diff --git a/src/mightypork/utils/math/constraints/ConstraintFactory.java b/src/mightypork/utils/math/constraints/ConstraintFactory.java index 561ed6a..73773e6 100644 --- a/src/mightypork/utils/math/constraints/ConstraintFactory.java +++ b/src/mightypork/utils/math/constraints/ConstraintFactory.java @@ -245,7 +245,7 @@ public class ConstraintFactory { double height = getContext().getRect().getSize().y; double perRow = height / rows; - return Rect.fromSize(getOrigin().add(0, perRow * (rows - index - 1)), getSize().setY(perRow)); + return Rect.fromSize(getOrigin().add(0, perRow * index), getSize().setY(perRow)); } }; } diff --git a/src/mightypork/utils/math/coord/Rect.java b/src/mightypork/utils/math/coord/Rect.java index a59b1fb..9199cff 100644 --- a/src/mightypork/utils/math/coord/Rect.java +++ b/src/mightypork/utils/math/coord/Rect.java @@ -239,7 +239,7 @@ public class Rect { */ public Coord getCenterBottom() { - return new Coord((max.x + min.x) / 2, min.y); + return new Coord((max.x + min.x) / 2D, min.y); } @@ -250,7 +250,7 @@ public class Rect { */ public Coord getCenterLeft() { - return new Coord(min.x, (max.y + min.y) / 2); + return new Coord(min.x, (max.y + min.y) / 2D); } @@ -261,7 +261,7 @@ public class Rect { */ public Coord getCenterRight() { - return new Coord(max.x, (max.y + min.y) / 2); + return new Coord(max.x, (max.y + min.y) / 2D); } @@ -272,7 +272,7 @@ public class Rect { */ public Coord getCenterTop() { - return new Coord((max.x + min.x) / 2, max.y); + return new Coord((max.x + min.x) / 2D, max.y); }