Commits

Anonymous committed 1a0896d

Added incomplete (probably forever so) Noise demo

Comments (0)

Files changed (2)

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

         add(Colors.class);
         add(Offscreen.class);
         add(Lines.class);
+        add(Noise.class);
     }};
 
     Iterator<Class<? extends DemoApplet>> applets = Iterators.cycle(allApplets);

src/main/java/net/fishbulb/jcod/demo/Noise.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 toxi.math.noise.PerlinNoise;
+
+import java.util.Iterator;
+
+import static java.lang.Math.*;
+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 Noise extends DemoApplet {
+
+    Color noiseColor = new Color(0.5f, 0.5f, 1.0f, 1.0f); // light blue
+    Color currentColor = new Color(1, 1, 1, 1);
+    private long lastUpdate;
+
+    float dx = 0;
+    float dy = 0;
+
+    float zoom = 1;
+
+    private PerlinNoise noise;
+
+    public Noise(Console parent) {
+        super(parent);
+        console.setDefaultForeground(Color.WHITE);
+        console.setBlendFunction(Set);
+        noise = new PerlinNoise();
+    }
+
+    @Override public void update() {
+        long now = System.currentTimeMillis();
+        long updateMillis = 30;
+        dx += 0.0003f;
+        dy += 0.0003f;
+
+        if ((now - lastUpdate) < updateMillis) return;
+        lastUpdate = now;
+        console.setBlendFunction(Set);
+        for (int y = 0; y < height; y++) {
+            for (int x = 0; x < width; x++) {
+                float xx = (float) x*zoom / width + dx;
+                float yy = (float) y*zoom / height + dy;
+                float val = noise.noise(xx, yy);
+                currentColor.set(noiseColor);
+                currentColor.mul(val);
+                console.setCharBackground(x,y,currentColor);
+            }
+        }
+        console.setBlendFunction(None);
+        console.setDefaultForeground(Color.WHITE);
+        console.print(0, 0, String.format("zoom: %.1f    ", zoom));
+    }
+
+
+    @Override public boolean keyDown(int keyCode) {
+        switch (keyCode) {
+            case Input.Keys.PLUS:
+                zoom += 0.1f;
+                zoom = min(10, zoom);
+                return true;
+            case Input.Keys.MINUS:
+                zoom -= 0.1f;
+                zoom = max(0.1f, zoom);
+                return true;
+            default:
+                return false;
+        }
+    }
+}