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.
*/
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()

@ -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<Deferred> 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)) {
if (!mainLoopQueuing) {
Log.f3("<LOADER> Cannot load async: " + Log.str(resource));
} else {
Log.f3("<LOADER> Delegating to main thread:\n " + Log.str(resource));
app.getEventBus().send(new MainLoopTaskRequest(new Runnable() {
// Log.f3("<LOADER> Delegating to main thread:\n " + Log.str(resource));
//
// app.getEventBus().send(new MainLoopTaskRequest(new Runnable() {
//
// @Override
// public void run()
// {
// resource.load();
// }
// }));
@Override
public void run()
{
resource.load();
}
}));
}
return;
}

@ -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);
}

@ -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);
}

@ -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");

@ -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()

@ -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<Num> 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
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();
/**
* <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
public double alpha()
public double rawAlpha()
{
return asRgb()[3];
}

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

Loading…
Cancel
Save