Commits

Anonymous committed fae4dd8

more implementation of Console

Comments (0)

Files changed (1)

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

 
 public class Console {
 
-    public class Cell {
-        public char c;
-        public int fg;
-        public int bg;
-
-        Tile tile = null;  // generated from above
-
-        public Cell(char c, int fg, int bg) {
-            this.c = c;
-            this.fg = fg;
-            this.bg = bg;
-        }
-
-        public Cell(char c, Color fg, Color bg) {
-            this(c, fg.toIntBits(), bg.toIntBits());
-        }
-
-        public Cell(char c, Color fg) {
-            this(c, fg, defaultBackground);
-        }
-
-        public Cell(char c) {
-            this(c, defaultForeground, defaultBackground);
-        }
-
-        public Cell(Cell that) {
-            this(that.c, that.fg, that.bg);
-        }
-
-        public void setFg(Color c) {
-            fg = c.toIntBits();
-        }
-
-        public void setBg(Color c) {
-            bg = c.toIntBits();
-        }
-
-        @Override
-        public boolean equals(Object o) {
-            if (this == o) return true;
-            if (o == null || getClass() != o.getClass()) return false;
-
-            Cell cell = (Cell) o;
-
-            if (bg != cell.bg) return false;
-            if (c != cell.c) return false;
-            if (fg != cell.fg) return false;
-
-            return true;
-        }
-
-        @Override
-        public int hashCode() {
-            int result = (int) c;
-            result = 31 * result + fg;
-            result = 31 * result + bg;
-            return result;
-        }
-    }
-
-
     @Getter @Setter
     private static Console root = null;
 
     @Getter @Setter
     private Tileset tileSet;
 
+    @Getter
+    private final int width;
+
+    @Getter
+    private final int height;
+
     @Getter @Setter
     private Color defaultBackground = Color.CLEAR;
 
     @Getter @Setter
     private Color defaultForeground = Color.WHITE;
 
+    @Getter @Setter
+    private char defaultChar = ' '; // JCOD extension (TCOD hardwires it to space)
+
+    private final Cell[] cells;
+
+    private int toIdx(int x, int y) {
+        return y * width + x;
+    }
+
+    private Cell cellAt(int x, int y) {
+        return cells[y * width + x];
+    }
+
+    public Console(Tileset font, int width, int height) {
+        this.width = width;
+        this.height = height;
+
+        cells = new Cell[width * height];
+        for (int i = 0; i < width * height; i++) {
+            cells[i] = new Cell(defaultChar, defaultForeground, defaultBackground);
+        }
+    }
+
     public static void mapAsciiCodeToFont(char asciiCode, int fontCharX, int fontCharY) {
         if (root != null) root.mapCharToFont(asciiCode, fontCharX, fontCharY);
     }
         }
     }
 
+    public void clear() {
+        int fg = defaultForeground.toIntBits();
+        int bg = defaultBackground.toIntBits();
+        for (Cell c : cells) {
+            c.set(defaultChar, fg, bg);
+        }
+    }
+
+    //void setCharBackground(int x, int y, const TCODColor &col, TCOD_bkgnd_flag_t flag = TCOD_BKGND_SET);
+    //TODO support blend modes
+    public void setCharBackground(int x, int y, Color col) {
+        cellAt(x, y).bg(col);
+    }
+
+    // raw color is faster if you're just setting it
+    public void setCharBackground(int x, int y, int col) {
+        cellAt(x, y).bg(col);
+    }
+
+    public void setCharForeground(int x, int y, Color col) {
+        cellAt(x, y).fg(col);
+    }
+
+    // raw color is faster if you're just setting it
+    public void setCharForeground(int x, int y, int col) {
+        cellAt(x, y).fg(col);
+    }
+
+    public void setChar(int x, int y, char c) {
+        cellAt(x, y).c(c);
+    }
+
+
+
+//void putChar(int x, int y, int c, TCOD_bkgnd_flag_t flag = TCOD_BKGND_DEFAULT);
+//void putCharEx(int x, int y, int c, const TCODColor &fore, const TCODColor &back);
+
+
+
+
+    public class Cell {
+
+        private char c;
+        private Tile tile;
+
+        public Cell(char c, int fg, int bg) {
+            this.c = c;
+            tile = new Tile(tileSet.get(c), fg, bg);
+            tile.setFg(fg);
+            tile.setBg(bg);
+        }
+
+        public Cell(char c, Color fg, Color bg) {
+            this(c, fg.toIntBits(), bg.toIntBits());
+        }
+
+        public Cell(char c, Color fg) {
+            this(c, fg, defaultBackground);
+        }
+
+        public Cell(char c) {
+            this(c, defaultForeground, defaultBackground);
+        }
+
+        public Cell(Cell that) {
+            this(that.c, that.tile.getFg(), that.tile.getBg());
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) return true;
+            if (o == null || getClass() != o.getClass()) return false;
+            Cell cell = (Cell) o;
+            return c == cell.c;
+        }
+
+        @Override
+        public int hashCode() {
+            return c;
+        }
+
+        public char c() {
+            return c;
+        }
+
+        public Cell c(char c) {
+            this.c = c;
+            tile.setGlyph(tileSet.get(c).getGlyph());
+            return this;
+        }
+
+        public int fg() {
+            return tile.getFg();
+        }
+
+        public Cell fg(int fg) {
+            tile.setFg(fg);
+            return this;
+        }
+
+        public Cell fg(Color fg) {
+            tile.setFg(fg.toIntBits());
+            return this;
+        }
+
+        public int bg() {
+            return tile.getBg();
+        }
+
+        public Cell bg(int bg) {
+            tile.setBg(bg);
+            return this;
+        }
+
+        public Cell bg(Color bg) {
+            tile.setBg(bg.toIntBits());
+            return this;
+        }
+
+        public Cell set(char c, int fg, int bg) {
+            this.c = c;
+            tile.setFg(fg);
+            tile.setBg(bg);
+            return this;
+        }
+
+        public Cell set(char c, Color fg, Color bg) {
+            this.c = c;
+            tile.setFg(fg.toIntBits());
+            tile.setBg(bg.toIntBits());
+            return this;
+        }
+
+    }
+
 
 
 }
 
 //void clear();
-//void setCharBackground(int x, int y, const TCODColor &col, TCOD_bkgnd_flag_t flag = TCOD_BKGND_SET);
-//void setCharForeground(int x, int y, const TCODColor &col);
-//void setChar(int x, int y, int c);
-//void putChar(int x, int y, int c, TCOD_bkgnd_flag_t flag = TCOD_BKGND_DEFAULT);
-//void putCharEx(int x, int y, int c, const TCODColor &fore, const TCODColor &back);
 //void setBackgroundFlag(TCOD_bkgnd_flag_t flag);
 //TCOD_bkgnd_flag_t getBackgroundFlag() const;
 //void setAlignment(TCOD_alignment_t alignment);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.