Commits

Anonymous committed 205dc18

added normal blit

Comments (0)

Files changed (4)

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

 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.graphics.Pixmap;
 import net.fishbulb.jcod.Console;
+import net.fishbulb.jcod.util.BlendMode;
 import net.fishbulb.jcod.util.ImageUtils;
 
 public class Image extends DemoApplet {
     private long lastUpdate;
 
     Pixmap star;
+    Pixmap skull;
+
+    float rot = 0.0f;
+
     public Image(Console parent) {
         super(parent);
 
         star = new Pixmap(Gdx.files.internal("star.png"));
+        skull = new Pixmap(Gdx.files.internal("skull.png"));
+
     }
 
     @Override public void update() {
         lastUpdate = now;
         console.clear();
 
+        rot = rot + 0.1f;
+
         ImageUtils.imageBlit2x(star,console,0,0,0,0,48,48);
 
+        ImageUtils.imageBlit(skull,console,0,0, BlendMode.Add, 1.0f, 1.0f, rot);
+
     }
 }

src/main/java/net/fishbulb/jcod/util/ImageUtils.java

 import java.nio.ByteBuffer;
 import java.nio.IntBuffer;
 
+import static java.lang.Math.cos;
 import static java.lang.Math.max;
 import static java.lang.Math.min;
+import static java.lang.Math.sin;
 import static net.fishbulb.jcod.util.ColorUtils.lerp;
 import static net.fishbulb.jcod.util.ColorUtils.rgbdist;
 
         return grayScaleToAlpha(pixmap, false);     // default blend mode in SpriteBatch assumes non-premultiplied
     }
 
+
+    public static void imageBlit(Pixmap image, Console console, float x, float y,
+                                 BlendFunction bkgnd_flag, float scalex, float scaley, float angle) {
+
+        Color tmp = new Color();
+
+        if (scalex == 0.0f || scaley == 0.0f || bkgnd_flag == BlendMode.None) return;
+        int width = image.getWidth();
+        int height = image.getHeight();
+        if (scalex == 1.0f && scaley == 1.0f && angle == 0.0f && x - ((int) x) == 0.0f && y - ((int) y) == 0.0f) {
+            // clip the image
+            int ix = (int) (x - width * 0.5f);
+            int iy = (int) (y - height * 0.5f);
+            int minx = max(ix, 0);
+            int miny = max(iy, 0);
+            int maxx = min(ix + width, console.getWidth());
+            int maxy = min(iy + height, console.getHeight());
+            int offx = 0, offy = 0;
+            int cx, cy;
+            if (ix < 0) offx = -ix;
+            if (iy < 0) offy = -iy;
+            for (cx = minx; cx < maxx; cx++) {
+                for (cy = miny; cy < maxy; cy++) {
+                    int pixel = image.getPixel(cx - minx + offx, cy - miny + offy);
+                    Color.rgba8888ToColor(tmp, pixel);
+                    console.setCharBackground(cx, cy, tmp, bkgnd_flag);
+                }
+            }
+        } else {
+            float iw = width / 2 * scalex;
+            float ih = height / 2 * scaley;
+            // get the coordinates of the image corners in the console
+            float newx_x = (float) cos(angle);
+            float newx_y = -(float) sin(angle);
+            float newy_x = newx_y;
+            float newy_y = -newx_x;
+            float x0, y0, x1, y1, x2, y2, x3, y3; /* image corners coordinates */
+            int rx, ry, rw, rh; /* rectangular area in the console */
+            int cx, cy;
+            int minx, miny, maxx, maxy;
+            float invscalex, invscaley;
+            // 0 = P - w/2 x' +h/2 y'
+            x0 = x - iw * newx_x + ih * newy_x;
+            y0 = y - iw * newx_y + ih * newy_y;
+            // 1 = P + w/2 x' + h/2 y'
+            x1 = x + iw * newx_x + ih * newy_x;
+            y1 = y + iw * newx_y + ih * newy_y;
+            // 2 = P + w/2 x' - h/2 y'
+            x2 = x + iw * newx_x - ih * newy_x;
+            y2 = y + iw * newx_y - ih * newy_y;
+            // 3 = P - w/2 x' - h/2 y'
+            x3 = x - iw * newx_x - ih * newy_x;
+            y3 = y - iw * newx_y - ih * newy_y;
+            // get the affected rectangular area in the console
+            rx = (int) (min(min(x0, x1), min(x2, x3)));
+            ry = (int) (min(min(y0, y1), min(y2, y3)));
+            rw = (int) (max(max(x0, x1), max(x2, x3))) - rx;
+            rh = (int) (max(max(y0, y1), max(y2, y3))) - ry;
+            // clip it
+            minx = max(rx, 0);
+            miny = max(ry, 0);
+            maxx = min(rx + rw, console.getWidth());
+            maxy = min(ry + rh, console.getHeight());
+            invscalex = 1.0f / scalex;
+            invscaley = 1.0f / scaley;
+            for (cx = minx; cx < maxx; cx++) {
+                for (cy = miny; cy < maxy; cy++) {
+                    float ix, iy;
+                    // map the console pixel to the image world
+                    ix = (iw + (cx - x) * newx_x + (cy - y) * (-newy_x)) * invscalex;
+                    iy = (ih + (cx - x) * (newx_y) - (cy - y) * newy_y) * invscaley;
+                    int pixel = image.getPixel((int) (ix), (int) (iy));
+                    Color.rgba8888ToColor(tmp, pixel);
+                    console.setCharBackground(cx, cy, tmp, bkgnd_flag);
+                }
+            }
+        }
+    }
+
+
     private static int getPattern(Color[] desired, Color[] palette) {
         // adapted from Jeff Lait's code posted on r.g.r.d
         int flag = 0;

src/main/resources/circle.png

Added
New image

src/main/resources/skull.png

Added
New image