Adds shader support.
@@ -5,6 +5,7 @@ import com.github.benmanes.caffeine.cache.Caffeine;
import com.valkryst.VTerminal.AsciiCharacter;
import com.valkryst.VTerminal.AsciiTile;
import com.valkryst.VTerminal.font.Font;
+import com.valkryst.VTerminal.shader.Shader;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
@@ -15,6 +16,8 @@ import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.VolatileImage;
+import java.util.ArrayList;
+import java.util.List;
import java.util.concurrent.TimeUnit;
@ToString
@@ -25,6 +28,9 @@ public final class ImageCache {
/** The font of the character images. */
@Getter private final Font font;
+ /** The shaders to run on each image. */
+ private final List<Shader> shaders = new ArrayList<>();
+
/**
* Constructs a new ImageCache.
*
@@ -129,6 +135,10 @@ public final class ImageCache {
bufferedImage = applyColorSwap(character, font);
bufferedImage = applyFlips(character, font, bufferedImage);
+ for (final Shader shader : shaders) {
+ bufferedImage = shader.run(bufferedImage);
+ }
+
final VolatileImage result = convertToVolatileImage(bufferedImage);
cachedImages.put(character.getCacheHash(), result);
@@ -296,4 +306,34 @@ public final class ImageCache {
return destination;
}
+
+ /**
+ * Adds a shader to the set of shaders.
+ *
+ * The order in which shaders are added determines the order in which the
+ * shaders are run.
+ *
+ * Shaders cannot be removed once added.
+ *
+ * @param shader
+ * The shader to add.
+ */
+ public void addShader(final @NonNull Shader shader) {
+ shaders.add(shader);
+ }
+
+ /**
+ * Removes the first occurrence of the specified shader from this set of shaders.
+ *
+ * If a shader is removed, then the cache is cleared. This is an extremely
+ * expensive operation, so it is advised that you do not use this function under
+ * normal circumstances.
+ *
+ * @param shader
+ * The shader to remove.
+ */
+ public void removeShader(final @NonNull Shader shader) {
+ shaders.remove(shader);
+ cachedImages.invalidateAll();
+ }
}
