Commits

Chuck Adams  committed 0e3e64a

fixed colors in blit2x

  • Participants
  • Parent commits fce3b27

Comments (0)

Files changed (2)

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

 
         star = new Pixmap(Gdx.files.internal("star.png"));
         skull = new Pixmap(Gdx.files.internal("skull.png"));
-
     }
 
     @Override public void update() {
         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);
+        ImageUtils.imageBlit2x(skull, console, 25, 0, 0, 0, 30, 40);
 
     }
 }

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

 
 
     public static void imageBlit2x(Pixmap image, Console con, int dx, int dy, int sx, int sy, int w, int h) {
-        Color[] grid = {new Color(), new Color(), new Color(), new Color()};
-        Color[] cols = {new Color(), new Color()};
-        int ascii, cx, cy;
-
-        int maxx, maxy;
-
-        int width = image.getWidth();
-        int height = image.getHeight();
-
+        // clamp source coordinates to image dimensions
         sx = max(0, sx);
         sy = max(0, sy);
-        w = min(w, width - sx);
-        h = min(h, height - sy);
+        w = min(w, image.getWidth() - sx);
+        h = min(h, image.getHeight() - sy);
 
-        maxx = dx + w / 2 <= con.getWidth() ? w : (con.getWidth() - dx) * 2;
-        maxy = dy + h / 2 <= con.getHeight() ? h : (con.getHeight() - dy) * 2;
+        int maxx = ((dx + (w / 2)) <= con.getWidth()) ? w : ((con.getWidth() - dx) * 2);
+        int maxy = ((dy + (h / 2)) <= con.getHeight()) ? h : ((con.getHeight() - dy) * 2);
         // check that the image is not blitted outside the console
         if (!(dx + maxx / 2 >= 0 && dy + maxy / 2 >= 0 && dx < con.getWidth() && dy < con.getHeight()))
             return;
         maxy += sy;
 
         int pixel;
-        for (cx = sx; cx < maxx; cx += 2) {
-            for (cy = sy; cy < maxy; cy += 2) {
+
+        Color[] grid = {new Color(), new Color(), new Color(), new Color()};
+        Color[] cols = {new Color(), new Color()};
+
+        Color oldBg = con.getDefaultBackground();
+        Color oldFg = con.getDefaultForeground();
+
+        for (int cx = sx; cx < maxx; cx += 2) {
+            for (int cy = sy; cy < maxy; cy += 2) {
             /* get the 2x2 super pixel colors from the image */
                 int conx = dx + (cx - sx) / 2;
                 int cony = dy + (cy - sy) / 2;
                 if (cy < maxy - 1 && cy < maxy - 1 && (((pixel = image.getPixel(cx + 1, cy + 1)) & 0xff) != 0))
                     Color.rgba8888ToColor(grid[3], pixel);
 
-                ascii = getPattern(grid, cols);
+                int ascii = getPattern(grid, cols);
                 if (ascii == ' ') {
                     // single color
                     con.setCharBackground(conx, cony, cols[0], BlendMode.Set);
                 }
             }
         }
+        con.setDefaultBackground(oldBg);
+        con.setDefaultForeground(oldFg);
     }
 }