Commits

Anonymous committed 6b83781

TileDisplay.draw properly blends parentAlpha and Color.
Tile color representation switched to int

Comments (0)

Files changed (5)

src/main/java/net/fishbulb/jcod/Console.java

 package net.fishbulb.jcod;
 
-import com.badlogic.gdx.graphics.Color;
-import com.badlogic.gdx.scenes.scene2d.utils.BaseDrawable;
-import net.fishbulb.jcod.display.Tile;
+import lombok.Getter;
+import lombok.Setter;
+
+/** ==Incompatibilities==
+ *
+ * Methods not supported:
+ * <ul>
+ * <li>initRoot (create and set root directly)</li>
+ * <li>setCustomFont (set tileset instead)</li>
+ * <li>isFullScreen</li>
+ * <li>setFullScreen</li>
+ * <li>setWindowTitle</li>
+ * <li>isWindowClosed</li>
+ * <li></li>
+ * <li></li>
+ * </ul>
+ * */
 
 public class Console {
+
+    @Getter @Setter
     private Console root = null;
 
-    static void initRoot(int w, int h) {
-        
-    }
+
 
 
 }
 
-
-//static void initRoot(int w, int h, String  title, boolean fullscreen = false, TCOD_renderer_t renderer=TCOD_RENDERER_SDL);
-//static void setCustomFont(String fontFile, int flags=TCOD_FONT_LAYOUT_ASCII_INCOL,int nbCharHoriz=0, int nbCharVertic=0);
 //static void mapAsciiCodeToFont(int asciiCode, int fontCharX, int fontCharY);
 //static void mapAsciiCodesToFont(int firstAsciiCode, int nbCodes, int fontCharX, int fontCharY);
 //static void mapStringToFont(String s, int fontCharX, int fontCharY);

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

 import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.scenes.scene2d.Stage;
+import com.badlogic.gdx.scenes.scene2d.actions.Actions;
+import com.badlogic.gdx.scenes.scene2d.ui.Image;
 import net.fishbulb.jcod.display.Tile;
 import net.fishbulb.jcod.display.TileDisplay;
 import net.fishbulb.jcod.display.Tileset;
 
     Tileset tileset;
     TileDisplay display;
+    Image bgImage;
 
 	@Override
 	public void create () {
         stage.getRoot().setTransform(false);
 
         tileset = new Tileset(texture, 8, 8, Tileset.Layout.InColumns);
-
-        display = new TileDisplay(20, 20, 8, 8);
+        display = new TileDisplay(20, 20, 32, 32);
         stage.addActor(display);
 
         Tile floor = tileset.get(10, 15);
         display.setTile(1, 1, wall);
         display.setTile(2, 2, arrow);
 
+        display.addAction(Actions.alpha(0,5));
+
     }
 
 	@Override

src/main/java/net/fishbulb/jcod/display/Tile.java

     @NonNull
     private Drawable glyph;
 
-    private float fg;
+    private int fg;
 
-    private float bg;
+    private int bg;
 
     public void setColor(Color fgColor) {
-        fg = fgColor.toFloatBits();
+        fg = fgColor.toIntBits();
     }
 
     public void setBackground(Color bgColor) {
-        bg = bgColor.toFloatBits();
+        bg = bgColor.toIntBits();
     }
 
     public Tile(Drawable glyph) {
     }
 
     public Tile(Drawable glyph, Color fgColor, Color bgColor) {
-        this(glyph, fgColor.toFloatBits(), bgColor.toFloatBits());
+        this(glyph, fgColor.toIntBits(), bgColor.toIntBits());
     }
 }
 

src/main/java/net/fishbulb/jcod/display/TileDisplay.java

 package net.fishbulb.jcod.display;
 
+import com.badlogic.gdx.graphics.Color;
 import com.badlogic.gdx.graphics.Pixmap;
 import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.scenes.scene2d.ui.Widget;
 import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
 import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
+import com.badlogic.gdx.utils.NumberUtils;
 import lombok.Getter;
 import lombok.Setter;
 
+import static com.badlogic.gdx.utils.NumberUtils.intToFloatColor;
+import static java.lang.Math.abs;
+
 public class TileDisplay extends Widget {
     @Getter
     private final int columns;
         this(columns, rows, tileWidth, tileHeight, defaultWhiteTile());
     }
 
-
     public Tile getTile(int x, int y) {
         return tiles[x][y];
     }
 
     @Override
     public void draw(SpriteBatch batch, float parentAlpha) {
-        // Two separate loops involves some redundancy, but cuts down on texture binds
-        // XXX TODO support parentAlpha
+        // Color.equals compares int components and doesn't need an epsilon
+        if ((abs(1.0 - parentAlpha) < 0.0001) && getColor().equals(Color.WHITE)) {
+            drawSimple(batch);
+        } else {
+            drawHairy(batch, parentAlpha);
+        }
+    }
 
+    // Draws tiles without any adjustment to sprite batch color
+    private void drawSimple(SpriteBatch batch) {
         // draw background
         if (!(bgTile == null || bgTile.getGlyph() == null)) {
             Drawable bgGlyph = bgTile.getGlyph();
                 for (int y = 0; y < rows; y++) {
                     Tile tile = tiles[x][y];
                     if (tile == null) continue;
-                    batch.setColor(tile.getBg());
+                    batch.setColor(intToFloatColor(tile.getBg()));
                     bgGlyph.draw(batch, x * tileWidth, y * tileHeight, tileWidth, tileHeight);
                 }
             }
                 Tile tile = tiles[x][y];
                 if (tile == null) continue;
                 Drawable glyph = tile.getGlyph();
-                batch.setColor(tile.getFg());
+                batch.setColor(intToFloatColor(tile.getFg()));
                 glyph.draw(batch, x * tileWidth, y * tileHeight, tileWidth, tileHeight);
             }
         }
     }
 
+    // drawSimple specialized to adjust colors
+    private void drawHairy(SpriteBatch batch, float parentAlpha) {
+        // draw background
+        Color base = getColor();
+        base.a *= parentAlpha;
+        Color cur = new Color();
+
+        if (!(bgTile == null || bgTile.getGlyph() == null)) {
+            Drawable bgGlyph = bgTile.getGlyph();
+            for (int x = 0; x < columns; x++) {
+                for (int y = 0; y < rows; y++) {
+                    Tile tile = tiles[x][y];
+                    if (tile == null) continue;
+                    setColorFromIntBits(tile.getBg(), cur);
+                    batch.setColor(cur.mul(base));
+                    bgGlyph.draw(batch, x * tileWidth, y * tileHeight, tileWidth, tileHeight);
+                }
+            }
+        }
+
+        // draw foregrounds
+        for (int x = 0; x < columns; x++) {
+            for (int y = 0; y < rows; y++) {
+                Tile tile = tiles[x][y];
+                if (tile == null) continue;
+                Drawable glyph = tile.getGlyph();
+                setColorFromIntBits(tile.getFg(), cur);
+                batch.setColor(cur.mul(base));
+                glyph.draw(batch, x * tileWidth, y * tileHeight, tileWidth, tileHeight);
+            }
+        }
+    }
+
+    private void setColorFromIntBits(int bits, Color color) {
+        color.r = (bits & 0xff) / 255f;
+        color.g = ((bits >>> 8) & 0xff) / 255f;
+        color.b = ((bits >>> 16) & 0xff) / 255f;
+        color.a = ((bits >>> 24) & 0xff) / 255f;
+    }
 
     @Override
     public float getPrefWidth() {

src/main/java/net/fishbulb/jcod/display/Tileset.java

 package net.fishbulb.jcod.display;
 
 import com.badlogic.gdx.Gdx;
-import com.badlogic.gdx.graphics.Color;
 import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.g2d.TextureRegion;
 import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
-import lombok.*;
+import lombok.Getter;
+import lombok.NonNull;
 
 public class Tileset {