From e58156b7eea98dc4b536e8f47c3b4fe63af29a21 Mon Sep 17 00:00:00 2001 From: ondra Date: Wed, 16 Apr 2014 13:47:08 +0200 Subject: [PATCH] Global alpha stack added into Color. --- src/mightypork/gamecore/control/BaseApp.java | 4 +- .../gamecore/loading/AsyncResourceLoader.java | 35 +++++---- .../render/fonts/impl/DeferredFont.java | 2 +- src/mightypork/rogue/App.java | 4 ++ src/mightypork/rogue/Res.java | 6 +- src/mightypork/rogue/screens/LayerFps.java | 7 ++ src/mightypork/utils/math/color/Color.java | 72 ++++++++++++++++++- src/mightypork/utils/math/color/ColorHsb.java | 2 +- src/mightypork/utils/math/color/ColorRgb.java | 2 +- 9 files changed, 108 insertions(+), 26 deletions(-) diff --git a/src/mightypork/gamecore/control/BaseApp.java b/src/mightypork/gamecore/control/BaseApp.java index ff03377..e14d34b 100644 --- a/src/mightypork/gamecore/control/BaseApp.java +++ b/src/mightypork/gamecore/control/BaseApp.java @@ -125,7 +125,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler { * Resources should be registered to banks, and AsyncResourceLoader will load them. */ Log.f1("Loading resources..."); - AsyncResourceLoader.launch(this); initResources(); /* @@ -210,8 +209,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler { /** - * Initialize resource banks; {@link AsyncResourceLoader} is already - * started. + * Initialize resource banks. */ @DefaultImpl protected void initResources() diff --git a/src/mightypork/gamecore/loading/AsyncResourceLoader.java b/src/mightypork/gamecore/loading/AsyncResourceLoader.java index 2cb8d95..2acc5ab 100644 --- a/src/mightypork/gamecore/loading/AsyncResourceLoader.java +++ b/src/mightypork/gamecore/loading/AsyncResourceLoader.java @@ -6,6 +6,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import mightypork.gamecore.control.bus.BusAccess; +import mightypork.gamecore.control.bus.events.MainLoopTaskRequest; import mightypork.gamecore.control.bus.events.ResourceLoadRequest; import mightypork.gamecore.control.interf.Destroyable; import mightypork.utils.annotations.FactoryMethod; @@ -38,8 +39,14 @@ public class AsyncResourceLoader extends Thread implements ResourceLoadRequest.L private final LinkedBlockingQueue toLoad = new LinkedBlockingQueue<>(); private volatile boolean stopped; - @SuppressWarnings("unused") private final BusAccess app; + private volatile boolean mainLoopQueuing = false; + + + public void enableMainLoopQueuing(boolean yes) + { + mainLoopQueuing = yes; + } /** @@ -61,18 +68,20 @@ public class AsyncResourceLoader extends Thread implements ResourceLoadRequest.L // textures & fonts needs to be loaded in main thread if (resource.getClass().isAnnotationPresent(MustLoadInMainThread.class)) { - Log.f3(" Cannot load async: " + Log.str(resource)); - -// Log.f3(" Delegating to main thread:\n " + Log.str(resource)); -// -// app.getEventBus().send(new MainLoopTaskRequest(new Runnable() { -// -// @Override -// public void run() -// { -// resource.load(); -// } -// })); + if (!mainLoopQueuing) { + Log.f3(" Cannot load async: " + Log.str(resource)); + } else { + Log.f3(" Delegating to main thread:\n " + Log.str(resource)); + + app.getEventBus().send(new MainLoopTaskRequest(new Runnable() { + + @Override + public void run() + { + resource.load(); + } + })); + } return; } diff --git a/src/mightypork/gamecore/render/fonts/impl/DeferredFont.java b/src/mightypork/gamecore/render/fonts/impl/DeferredFont.java index defd9f3..ed4bc2b 100644 --- a/src/mightypork/gamecore/render/fonts/impl/DeferredFont.java +++ b/src/mightypork/gamecore/render/fonts/impl/DeferredFont.java @@ -54,7 +54,7 @@ public class DeferredFont extends DeferredResource implements GLFont { * @param size size (px) */ public DeferredFont(String resourcePath, String chars, double size) { - this(resourcePath, chars, size, FontStyle.PLAIN, true, FilterMode.LINEAR); + this(resourcePath, chars, size, FontStyle.PLAIN, false, FilterMode.NEAREST); } diff --git a/src/mightypork/rogue/App.java b/src/mightypork/rogue/App.java index 90fc2b2..e6ee79c 100644 --- a/src/mightypork/rogue/App.java +++ b/src/mightypork/rogue/App.java @@ -11,6 +11,7 @@ import mightypork.gamecore.gui.screens.ScreenRegistry; import mightypork.gamecore.input.InputSystem; import mightypork.gamecore.input.KeyStroke; import mightypork.gamecore.input.Keys; +import mightypork.gamecore.loading.AsyncResourceLoader; import mightypork.gamecore.render.DisplaySystem; import mightypork.rogue.events.ActionRequest; import mightypork.rogue.events.ActionRequest.RequestType; @@ -85,6 +86,9 @@ public class App extends BaseApp { @Override protected void initResources() { + AsyncResourceLoader thread = AsyncResourceLoader.launch(this); + thread.enableMainLoopQueuing(true); + Res.load(this); } diff --git a/src/mightypork/rogue/Res.java b/src/mightypork/rogue/Res.java index 768a066..ec8500b 100644 --- a/src/mightypork/rogue/Res.java +++ b/src/mightypork/rogue/Res.java @@ -48,8 +48,8 @@ public class Res { fonts = new FontBank(app); loadSounds(); - loadTextures(); loadFonts(); + loadTextures(); } @@ -58,13 +58,9 @@ public class Res { DeferredFont font; font = new DeferredFont("/res/font/PolygonPixel5x7Standard.ttf", Glyphs.basic, 16); - font.setAntialias(true); - font.setFilter(FilterMode.NEAREST); fonts.loadFont("polygon_pixel", font); font = new DeferredFont("/res/font/PressStart2P.ttf", Glyphs.basic, 16); - font.setAntialias(true); - font.setFilter(FilterMode.NEAREST); fonts.loadFont("press_start", font); fonts.addAlias("default", "polygon_pixel"); diff --git a/src/mightypork/rogue/screens/LayerFps.java b/src/mightypork/rogue/screens/LayerFps.java index 556b399..068f718 100644 --- a/src/mightypork/rogue/screens/LayerFps.java +++ b/src/mightypork/rogue/screens/LayerFps.java @@ -11,6 +11,7 @@ import mightypork.gamecore.input.Keys; import mightypork.gamecore.render.fonts.GLFont; import mightypork.rogue.Res; import mightypork.utils.math.color.Color; +import mightypork.utils.math.constraints.num.Num; import mightypork.utils.math.constraints.rect.proxy.RectBound; import mightypork.utils.math.constraints.vect.Vect; import mightypork.utils.string.StringProvider; @@ -55,6 +56,12 @@ public class LayerFps extends ScreenLayer { root.add(tp); } + @Override + protected void renderLayer() + { + super.renderLayer(); + } + @Override public int getPriority() diff --git a/src/mightypork/utils/math/color/Color.java b/src/mightypork/utils/math/color/Color.java index 692991b..76ba2e2 100644 --- a/src/mightypork/utils/math/color/Color.java +++ b/src/mightypork/utils/math/color/Color.java @@ -1,6 +1,8 @@ package mightypork.utils.math.color; +import java.util.Stack; + import mightypork.utils.annotations.FactoryMethod; import mightypork.utils.math.Calc; import mightypork.utils.math.constraints.num.Num; @@ -34,72 +36,86 @@ public abstract class Color { public static final Color ORANGE = rgb(1, 0.78, 0); public static final Color PINK = rgb(1, 0.68, 0.68); + private static final Stack globalAlphaStack = new Stack<>(); + + @FactoryMethod public static final Color rgb(double r, double g, double b) { return rgba(Num.make(r), Num.make(g), Num.make(b), Num.ONE); } + @FactoryMethod public static final Color rgba(double r, double g, double b, double a) { return rgba(Num.make(r), Num.make(g), Num.make(b), Num.make(a)); } + @FactoryMethod public static final Color rgba(Num r, Num g, Num b) { return rgba(r, g, b, Num.ONE); } + @FactoryMethod public static final Color rgba(Num r, Num g, Num b, Num a) { return new ColorRgb(r, g, b, a); } + @FactoryMethod public static final Color hsb(double h, double s, double b) { return hsba(Num.make(h), Num.make(s), Num.make(b), Num.ONE); } + @FactoryMethod public static final Color hsba(double h, double s, double b, double a) { return hsba(Num.make(h), Num.make(s), Num.make(b), Num.make(a)); } + @FactoryMethod public static final Color hsb(Num h, Num s, Num b) { return hsba(h, s, b, Num.ONE); } + @FactoryMethod public static final Color hsba(Num h, Num s, Num b, Num a) { return new ColorHsb(h, s, b, a); } + @FactoryMethod public static final Color light(double a) { return light(Num.make(a)); } + @FactoryMethod public static final Color light(Num a) { return rgba(Num.ONE, Num.ONE, Num.ONE, a); } - @FactoryMethod + + @FactoryMethod public static final Color dark(double a) { return dark(Num.make(a)); } + @FactoryMethod public static final Color dark(Num a) { @@ -140,5 +156,57 @@ public abstract class Color { /** * @return alpha 0-1 */ - public abstract double alpha(); + public final double alpha() + { + double alpha = rawAlpha(); + + for (Num n : globalAlphaStack) { + alpha *= clamp(n.value()); + } + + return clamp(alpha); + } + + + /** + * @return alpha 0-1, before multiplying with the global alpha value. + */ + protected abstract double rawAlpha(); + + + /** + *

+ * Push alpha multiplier on the stack (can be animated or whatever you + * like). Once done with rendering, the popAlpha() method should be called, + * otherwise you may experience unexpected glitches (such as all going + * transparent). + *

+ *

+ * multiplier value should not exceed the range 0..1, otherwise it will be + * clamped to it. + *

+ * + * @param alpha alpha multiplier + */ + public static void pushAlpha(Num alpha) + { + globalAlphaStack.push(alpha); + } + + + /** + * Remove a pushed alpha multiplier from the stack. If there's no remaining + * multiplier on the stack, an exception is raised. + * + * @return the popped multiplier + * @throws IllegalStateException if the stack is empty + */ + public static Num popAlpha() + { + if (globalAlphaStack.isEmpty()) { + throw new IllegalStateException("Global alpha stack underflow."); + } + + return globalAlphaStack.pop(); + } } diff --git a/src/mightypork/utils/math/color/ColorHsb.java b/src/mightypork/utils/math/color/ColorHsb.java index 1e74283..bdffd50 100644 --- a/src/mightypork/utils/math/color/ColorHsb.java +++ b/src/mightypork/utils/math/color/ColorHsb.java @@ -52,7 +52,7 @@ public class ColorHsb extends Color { @Override - public double alpha() + public double rawAlpha() { return asRgb()[3]; } diff --git a/src/mightypork/utils/math/color/ColorRgb.java b/src/mightypork/utils/math/color/ColorRgb.java index af4b567..279f819 100644 --- a/src/mightypork/utils/math/color/ColorRgb.java +++ b/src/mightypork/utils/math/color/ColorRgb.java @@ -41,7 +41,7 @@ public class ColorRgb extends Color { @Override - public double alpha() + public double rawAlpha() { return clamp(a); }