Commits

Anonymous committed ecc48dd

added Lines demo (alpha parts not finished)

  • Participants
  • Parent commits 7375f08

Comments (0)

Files changed (3)

src/main/java/net/fishbulb/jcod/demo/Demo.java

     List<Class<? extends DemoApplet>> allApplets = new ArrayList<Class<? extends DemoApplet>>() {{
         add(Colors.class);
         add(Offscreen.class);
+        add(Lines.class);
     }};
 
     Iterator<Class<? extends DemoApplet>> applets = Iterators.cycle(allApplets);
                 break;
 
         }
-        return true;
+
+        return active.keyDown(keyCode);
     }
 
 }

src/main/java/net/fishbulb/jcod/demo/DemoApplet.java

 
     public abstract void update();
 
+    public boolean keyDown(int keyCode) { return false; }
+
 }

src/main/java/net/fishbulb/jcod/demo/Lines.java

+package net.fishbulb.jcod.demo;
+
+import com.badlogic.gdx.Input;
+import com.badlogic.gdx.graphics.Color;
+import com.google.common.collect.Iterators;
+import net.fishbulb.jcod.Console;
+import net.fishbulb.jcod.util.BlendMode;
+
+import java.util.Iterator;
+
+import static java.lang.Math.cos;
+import static java.lang.Math.sin;
+import static net.fishbulb.jcod.util.BlendMode.*;
+import static net.fishbulb.jcod.util.PlotAlgorithms.PlotFunction;
+import static net.fishbulb.jcod.util.PlotAlgorithms.bresenham;
+
+public class Lines extends DemoApplet {
+
+    Color lineColor = new Color(0.25f, 0.25f, 1.0f, 1.0f); // light blue
+
+    float currentAlpha = 1.0f;
+
+    BlendMode currentMode;
+
+    BlendMode[] allModes = new BlendMode[]{
+            None, Set, Multiply, Lighten, Darken, Screen, ColorDodge, ColorBurn, Add,
+            AddWithSourceAlpha, Burn, Overlay, AlphaTCOD,
+    };
+
+    Iterator<BlendMode> modes = Iterators.cycle(allModes);
+
+    PlotFunction listener = new PlotFunction() {
+        @Override public boolean apply(int x, int y) {
+            getConsole().setCharBackground(x, y, lineColor, currentMode);
+            return true;
+        }
+    };
+
+    Console backdrop;
+    private long lastUpdate;
+
+    private void cycleMode() {
+        currentMode = modes.next();
+    }
+
+    public Lines(Console parent) {
+        super(parent);
+        console.setDefaultForeground(Color.WHITE);
+        console.setBlendFunction(None);
+        backdrop = new Console(width, height);
+        Color col = new Color(1, 1, 1, 1);
+        for (int x = 0; x < width; x++) {
+            for (int y = 0; y < height; y++) {
+                col.r = ((float) x / (width - 1));
+                col.g = ((float) (x + y) / (width - 1 + height - 1));
+                col.b = ((float) y / (height - 1));
+                backdrop.setCharBackground(x, y, col, Set);
+            }
+        }
+        cycleMode();
+        cycleMode();
+    }
+
+    @Override public void update() {
+        long now = System.currentTimeMillis();
+        long updateMillis = 30;
+
+        if ((now - lastUpdate) < updateMillis) return;
+        lastUpdate = now;
+
+        // blit the background
+        backdrop.blit(0, 0, width, height, console, 0, 0, 1, 1);
+
+        // render the gradient
+        int recty = (int) ((height - 2) * (1.0 + cos((double) now / 1000)) / 2.0);
+        Color col = new Color(1, 1, 1, 1);
+        for (int x = 0; x < width; x++) {
+            col.r = (float) x / width;
+            col.g = (float) x / width;
+            col.b = (float) x / width;
+            console.setCharBackground(x, recty, col, currentMode);
+            console.setCharBackground(x, recty + 1, col, currentMode);
+            console.setCharBackground(x, recty + 2, col, currentMode);
+        }
+
+        // calculate the segment ends
+        double angle = (double) now / 1000 * 2.0f;
+        double cosAngle = cos(angle);
+        double sinAngle = sin(angle);
+        int xo = (int) (width / 2 * (1 + cosAngle));
+        int yo = (int) (height / 2 + sinAngle * width / 2);
+        int xd = (int) (width / 2 * (1 - cosAngle));
+        int yd = (int) (height / 2 - sinAngle * width / 2);
+
+        // render the line
+        bresenham(xo, yo, xd, yd, listener);
+
+        // print the current flag
+        console.print(2, 2, currentMode + " (ENTER to change)");
+    }
+
+
+    @Override public boolean keyDown(int keyCode) {
+        if (keyCode == Input.Keys.ENTER) {
+            cycleMode();
+            return true;
+        }
+        return false;
+    }
+}