Global alpha stack added into Color.

v5stable
ondra 11 years ago
parent 225f062684
commit e58156b7ee
  1. 4
      src/mightypork/gamecore/control/BaseApp.java
  2. 31
      src/mightypork/gamecore/loading/AsyncResourceLoader.java
  3. 2
      src/mightypork/gamecore/render/fonts/impl/DeferredFont.java
  4. 4
      src/mightypork/rogue/App.java
  5. 6
      src/mightypork/rogue/Res.java
  6. 7
      src/mightypork/rogue/screens/LayerFps.java
  7. 70
      src/mightypork/utils/math/color/Color.java
  8. 2
      src/mightypork/utils/math/color/ColorHsb.java
  9. 2
      src/mightypork/utils/math/color/ColorRgb.java

@ -125,7 +125,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
* Resources should be registered to banks, and AsyncResourceLoader will load them. * Resources should be registered to banks, and AsyncResourceLoader will load them.
*/ */
Log.f1("Loading resources..."); Log.f1("Loading resources...");
AsyncResourceLoader.launch(this);
initResources(); initResources();
/* /*
@ -210,8 +209,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
/** /**
* Initialize resource banks; {@link AsyncResourceLoader} is already * Initialize resource banks.
* started.
*/ */
@DefaultImpl @DefaultImpl
protected void initResources() protected void initResources()

@ -6,6 +6,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import mightypork.gamecore.control.bus.BusAccess; import mightypork.gamecore.control.bus.BusAccess;
import mightypork.gamecore.control.bus.events.MainLoopTaskRequest;
import mightypork.gamecore.control.bus.events.ResourceLoadRequest; import mightypork.gamecore.control.bus.events.ResourceLoadRequest;
import mightypork.gamecore.control.interf.Destroyable; import mightypork.gamecore.control.interf.Destroyable;
import mightypork.utils.annotations.FactoryMethod; import mightypork.utils.annotations.FactoryMethod;
@ -38,8 +39,14 @@ public class AsyncResourceLoader extends Thread implements ResourceLoadRequest.L
private final LinkedBlockingQueue<Deferred> toLoad = new LinkedBlockingQueue<>(); private final LinkedBlockingQueue<Deferred> toLoad = new LinkedBlockingQueue<>();
private volatile boolean stopped; private volatile boolean stopped;
@SuppressWarnings("unused")
private final BusAccess app; 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 // textures & fonts needs to be loaded in main thread
if (resource.getClass().isAnnotationPresent(MustLoadInMainThread.class)) { if (resource.getClass().isAnnotationPresent(MustLoadInMainThread.class)) {
if (!mainLoopQueuing) {
Log.f3("<LOADER> Cannot load async: " + Log.str(resource)); Log.f3("<LOADER> Cannot load async: " + Log.str(resource));
} else {
Log.f3("<LOADER> Delegating to main thread:\n " + Log.str(resource));
// Log.f3("<LOADER> Delegating to main thread:\n " + Log.str(resource)); app.getEventBus().send(new MainLoopTaskRequest(new Runnable() {
//
// app.getEventBus().send(new MainLoopTaskRequest(new Runnable() { @Override
// public void run()
// @Override {
// public void run() resource.load();
// { }
// resource.load(); }));
// } }
// }));
return; return;
} }

@ -54,7 +54,7 @@ public class DeferredFont extends DeferredResource implements GLFont {
* @param size size (px) * @param size size (px)
*/ */
public DeferredFont(String resourcePath, String chars, double size) { 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);
} }

@ -11,6 +11,7 @@ import mightypork.gamecore.gui.screens.ScreenRegistry;
import mightypork.gamecore.input.InputSystem; import mightypork.gamecore.input.InputSystem;
import mightypork.gamecore.input.KeyStroke; import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.input.Keys; import mightypork.gamecore.input.Keys;
import mightypork.gamecore.loading.AsyncResourceLoader;
import mightypork.gamecore.render.DisplaySystem; import mightypork.gamecore.render.DisplaySystem;
import mightypork.rogue.events.ActionRequest; import mightypork.rogue.events.ActionRequest;
import mightypork.rogue.events.ActionRequest.RequestType; import mightypork.rogue.events.ActionRequest.RequestType;
@ -85,6 +86,9 @@ public class App extends BaseApp {
@Override @Override
protected void initResources() protected void initResources()
{ {
AsyncResourceLoader thread = AsyncResourceLoader.launch(this);
thread.enableMainLoopQueuing(true);
Res.load(this); Res.load(this);
} }

@ -48,8 +48,8 @@ public class Res {
fonts = new FontBank(app); fonts = new FontBank(app);
loadSounds(); loadSounds();
loadTextures();
loadFonts(); loadFonts();
loadTextures();
} }
@ -58,13 +58,9 @@ public class Res {
DeferredFont font; DeferredFont font;
font = new DeferredFont("/res/font/PolygonPixel5x7Standard.ttf", Glyphs.basic, 16); font = new DeferredFont("/res/font/PolygonPixel5x7Standard.ttf", Glyphs.basic, 16);
font.setAntialias(true);
font.setFilter(FilterMode.NEAREST);
fonts.loadFont("polygon_pixel", font); fonts.loadFont("polygon_pixel", font);
font = new DeferredFont("/res/font/PressStart2P.ttf", Glyphs.basic, 16); font = new DeferredFont("/res/font/PressStart2P.ttf", Glyphs.basic, 16);
font.setAntialias(true);
font.setFilter(FilterMode.NEAREST);
fonts.loadFont("press_start", font); fonts.loadFont("press_start", font);
fonts.addAlias("default", "polygon_pixel"); fonts.addAlias("default", "polygon_pixel");

@ -11,6 +11,7 @@ import mightypork.gamecore.input.Keys;
import mightypork.gamecore.render.fonts.GLFont; import mightypork.gamecore.render.fonts.GLFont;
import mightypork.rogue.Res; import mightypork.rogue.Res;
import mightypork.utils.math.color.Color; 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.rect.proxy.RectBound;
import mightypork.utils.math.constraints.vect.Vect; import mightypork.utils.math.constraints.vect.Vect;
import mightypork.utils.string.StringProvider; import mightypork.utils.string.StringProvider;
@ -55,6 +56,12 @@ public class LayerFps extends ScreenLayer {
root.add(tp); root.add(tp);
} }
@Override
protected void renderLayer()
{
super.renderLayer();
}
@Override @Override
public int getPriority() public int getPriority()

@ -1,6 +1,8 @@
package mightypork.utils.math.color; package mightypork.utils.math.color;
import java.util.Stack;
import mightypork.utils.annotations.FactoryMethod; import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.Calc; import mightypork.utils.math.Calc;
import mightypork.utils.math.constraints.num.Num; 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 ORANGE = rgb(1, 0.78, 0);
public static final Color PINK = rgb(1, 0.68, 0.68); public static final Color PINK = rgb(1, 0.68, 0.68);
private static final Stack<Num> globalAlphaStack = new Stack<>();
@FactoryMethod @FactoryMethod
public static final Color rgb(double r, double g, double b) public static final Color rgb(double r, double g, double b)
{ {
return rgba(Num.make(r), Num.make(g), Num.make(b), Num.ONE); return rgba(Num.make(r), Num.make(g), Num.make(b), Num.ONE);
} }
@FactoryMethod @FactoryMethod
public static final Color rgba(double r, double g, double b, double a) 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)); return rgba(Num.make(r), Num.make(g), Num.make(b), Num.make(a));
} }
@FactoryMethod @FactoryMethod
public static final Color rgba(Num r, Num g, Num b) public static final Color rgba(Num r, Num g, Num b)
{ {
return rgba(r, g, b, Num.ONE); return rgba(r, g, b, Num.ONE);
} }
@FactoryMethod @FactoryMethod
public static final Color rgba(Num r, Num g, Num b, Num a) public static final Color rgba(Num r, Num g, Num b, Num a)
{ {
return new ColorRgb(r, g, b, a); return new ColorRgb(r, g, b, a);
} }
@FactoryMethod @FactoryMethod
public static final Color hsb(double h, double s, double b) public static final Color hsb(double h, double s, double b)
{ {
return hsba(Num.make(h), Num.make(s), Num.make(b), Num.ONE); return hsba(Num.make(h), Num.make(s), Num.make(b), Num.ONE);
} }
@FactoryMethod @FactoryMethod
public static final Color hsba(double h, double s, double b, double a) 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)); return hsba(Num.make(h), Num.make(s), Num.make(b), Num.make(a));
} }
@FactoryMethod @FactoryMethod
public static final Color hsb(Num h, Num s, Num b) public static final Color hsb(Num h, Num s, Num b)
{ {
return hsba(h, s, b, Num.ONE); return hsba(h, s, b, Num.ONE);
} }
@FactoryMethod @FactoryMethod
public static final Color hsba(Num h, Num s, Num b, Num a) public static final Color hsba(Num h, Num s, Num b, Num a)
{ {
return new ColorHsb(h, s, b, a); return new ColorHsb(h, s, b, a);
} }
@FactoryMethod @FactoryMethod
public static final Color light(double a) public static final Color light(double a)
{ {
return light(Num.make(a)); return light(Num.make(a));
} }
@FactoryMethod @FactoryMethod
public static final Color light(Num a) public static final Color light(Num a)
{ {
return rgba(Num.ONE, Num.ONE, Num.ONE, a); return rgba(Num.ONE, Num.ONE, Num.ONE, a);
} }
@FactoryMethod @FactoryMethod
public static final Color dark(double a) public static final Color dark(double a)
{ {
return dark(Num.make(a)); return dark(Num.make(a));
} }
@FactoryMethod @FactoryMethod
public static final Color dark(Num a) public static final Color dark(Num a)
{ {
@ -140,5 +156,57 @@ public abstract class Color {
/** /**
* @return alpha 0-1 * @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();
/**
* <p>
* 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).
* </p>
* <p>
* multiplier value should not exceed the range 0..1, otherwise it will be
* clamped to it.
* </p>
*
* @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();
}
} }

@ -52,7 +52,7 @@ public class ColorHsb extends Color {
@Override @Override
public double alpha() public double rawAlpha()
{ {
return asRgb()[3]; return asRgb()[3];
} }

@ -41,7 +41,7 @@ public class ColorRgb extends Color {
@Override @Override
public double alpha() public double rawAlpha()
{ {
return clamp(a); return clamp(a);
} }

Loading…
Cancel
Save