Commits

Anonymous committed cb32cd3

initial display implementation and demo

Comments (0)

Files changed (13)

+syntax: glob
+target/
+.idea
+*.iml
             <artifactId>gdx-backend-lwjgl</artifactId>
             <version>${gdx.version}</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <version>0.11.6</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 
 

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;
+
+public class Console {
+    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);
+//static boolean isFullscreen();
+//static void setFullscreen(boolean fullscreen);
+//static void setWindowTitle(String title);
+//static boolean isWindowClosed();
+//static void credits();
+//static boolean renderCredits(int x, int y, boolean alpha);
+//static void resetCredits();
+//void setDefaultBackground(TCODColor back);
+//void setDefaultForeground(TCODColor fore);
+//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);
+//TCOD_alignment_t getAlignment() const;
+//void print(int x, int y, String fmt, ...);
+//void printEx(int x, int y, TCOD_bkgnd_flag_t flag, TCOD_alignment_t alignment, String fmt, ...);
+//int printRect(int x, int y, int w, int h, String fmt, ...);
+//int printRectEx(int x, int y, int w, int h, TCOD_bkgnd_flag_t flag, TCOD_alignment_t alignment, String fmt, ...);
+//int getHeightRect(int x, int y, int w, int h, String fmt, ...);
+//static void setColorControl(TCOD_colctrl_t con, const TCODColor &fore, const TCODColor &back);
+//static void mapStringToFont(const wchar_t *s, int fontCharX, int fontCharY);
+//void print(int x, int y, const wchar_t *fmt, ...);
+//void printEx(int x, int y, TCOD_bkgnd_flag_t flag, TCOD_alignment_t alignment, const wchar_t *fmt, ...);
+//int printRect(int x, int y, int w, int h, const wchar_t *fmt, ...);
+//int printRectEx(int x, int y, int w, int h, TCOD_bkgnd_flag_t flag, TCOD_alignment_t alignment, const wchar_t *fmt, ...);
+//int getHeightRect(int x, int y, int w, int h, const wchar_t *fmt, ...);
+//void rect(int x, int y, int w, int h, boolean clear, TCOD_bkgnd_flag_t flag = TCOD_BKGND_DEFAULT);
+//void hline(int x,int y, int l, TCOD_bkgnd_flag_t flag = TCOD_BKGND_DEFAULT);
+//void vline(int x,int y, int l, TCOD_bkgnd_flag_t flag = TCOD_BKGND_DEFAULT);
+//void printFrame(int x,int y,int w,int h, boolean clear=true, TCOD_bkgnd_flag_t flag = TCOD_BKGND_DEFAULT, String fmt=NULL, ...);
+//int getWidth() const;
+//int getHeight() const;
+//TCODColor getDefaultBackground() const;
+//TCODColor getDefaultForeground() const;
+//TCODColor getCharBackground(int x, int y) const;
+//TCODColor getCharForeground(int x, int y) const;
+//int getChar(int x, int y) const;
+//static void setFade(uint8 fade, const TCODColor &fadingColor);
+//static uint8 getFade();
+//static TCODColor getFadingColor();
+//static void flush();
+//static TCOD_key_t waitForKeypress(boolean flush);
+//static TCOD_key_t checkForKeypress(int flags=TCOD_KEY_RELEASED);
+//static boolean isKeyPressed(TCOD_keycode_t key);
+//static void setKeyboardRepeat(int initialDelay,int interval);
+//static void disableKeyboardRepeat();
+//TCODConsole(int w, int h);
+//TCODConsole(String filename);
+//boolean loadAsc(String filename);
+//boolean loadApf(String filename);
+//boolean saveAsc(String filename) const;
+//boolean saveApf(String filename) const;
+//static void blit(const TCODConsole *src,int xSrc, int ySrc, int wSrc, int hSrc, TCODConsole *dst, int xDst, int yDst, float foreground_alpha=1.0f, float background_alpha=1.0f);
+//void setKeyColor(const TCODColor &col);
+//virtual ~TCODConsole();
+//void setDirty(int x, int y, int w, int h);
+//TCODConsole(TCOD_console_t con) : data(con) {}
+//static String getColorControlString( TCOD_colctrl_t ctrl );
+//static String getRGBColorControlString( TCOD_colctrl_t ctrl, const TCODColor & col );

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

 package net.fishbulb.jcod.demo;
 
+import com.badlogic.gdx.ApplicationListener;
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Input;
+import com.badlogic.gdx.InputAdapter;
+import com.badlogic.gdx.graphics.Color;
 import com.badlogic.gdx.graphics.GL10;
 import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
-import com.badlogic.gdx.ApplicationListener;
-import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.scenes.scene2d.Stage;
+import net.fishbulb.jcod.display.Tile;
+import net.fishbulb.jcod.display.TileDisplay;
+import net.fishbulb.jcod.display.Tileset;
 
-public class Demo implements ApplicationListener {
+public class Demo extends InputAdapter implements ApplicationListener {
 	Texture texture;
 	SpriteBatch batch;
-	
+    Stage stage;
+
+    Tileset tileset;
+    TileDisplay display;
+
 	@Override
 	public void create () {
-		texture = new Texture(Gdx.files.internal("libgdx-logo.png"));
+		texture = new Texture(Gdx.files.internal("tiles/terminal_trans.png"));
 		batch = new SpriteBatch();
-	}
+        stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false, batch);
+
+        Gdx.input.setInputProcessor(this);
+        stage.getRoot().setTransform(false);
+
+        tileset = new Tileset(texture, 8, 8, Tileset.Layout.InColumns);
+
+        display = new TileDisplay(20, 20, 8, 8);
+        stage.addActor(display);
+
+        Tile floor = tileset.get(10, 15);
+        Tile wall = tileset.get(1, 11);
+        Tile arrow = tileset.get(0, 1);
+
+        wall.setColor(Color.RED);
+        arrow.setColor(Color.MAGENTA);
+        arrow.setBgColor(Color.YELLOW);
+
+        display.setTile(0, 0, floor);
+        display.setTile(1, 1, wall);
+        display.setTile(2, 2, arrow);
+
+    }
 
 	@Override
 	public void resize (int width, int height) {
 	public void render () {
 		Gdx.gl.glClearColor(0, 0, 0, 0);
 		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
-		batch.begin();
-		batch.draw(texture, 100, 100);
-		batch.end();
+        stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
+        stage.draw();
 	}
 
 	@Override
-	public void pause () {
-	}
+	public void pause () {}
 
 	@Override
-	public void resume () {
-	}
+	public void resume () {}
 
 	@Override
-	public void dispose () {
-	}
+	public void dispose () {}
+
+    @Override
+    public boolean keyUp(int keyCode) {
+        return true;
+    }
+
+    @Override
+    public boolean keyDown(int keyCode) {
+        if (keyCode == Input.Keys.ESCAPE)
+            Gdx.app.exit();
+        return true;
+    }
+
 }

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

+package net.fishbulb.jcod.display;
+
+
+import com.badlogic.gdx.graphics.Color;
+import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NonNull;
+
+@Data
+@AllArgsConstructor
+public class Tile {
+    @NonNull
+    private Drawable glyph;
+
+    private Color color;
+
+    private Color bgColor;
+
+    public Tile(Drawable glyph) {
+        this(glyph, Color.WHITE, null);
+    }
+
+}

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.graphics.g2d.TextureRegion;
+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 lombok.Getter;
+import lombok.Setter;
+
+// class TileDisplay(val width: Int, val height: Int, var tileset: Tileset, val bgTile: Tile = TileDisplay.whiteTile) extends Widget
+
+public class TileDisplay extends Widget {
+    @Getter
+    private final int columns;
+
+    @Getter
+    private final int rows;
+
+    @Getter
+    private final int tileWidth;
+
+    @Getter
+    private final int tileHeight;
+
+    @Getter @Setter
+    private Tile bgTile;
+
+    private final Tile[][] tiles;
+
+    public TileDisplay(int columns, int rows, int tileWidth, int tileHeight, Tile bgTile) {
+        this.columns = columns;
+        this.rows = rows;
+        this.tileWidth = tileWidth;
+        this.tileHeight = tileHeight;
+        this.bgTile = bgTile;
+        tiles = new Tile[columns][rows];
+    }
+
+    public TileDisplay(int columns, int rows, int tileWidth, int tileHeight) {
+        this(columns, rows, tileWidth, tileHeight, defaultWhiteTile());
+    }
+
+
+    public Tile getTile(int x, int y) {
+        return tiles[x][y];
+    }
+
+    public void setTile(int x, int y, Tile tile) {
+        tiles[x][y] = tile;
+    }
+
+    public void clearTile(int x, int y, Tile tile) {
+        tiles[x][y] = null;
+    }
+
+    @Override
+    public void draw(SpriteBatch batch, float parentAlpha) {
+        // Two separate loops involves some redundancy, but cuts down on texture binds
+
+        // draw background
+        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;
+                    Color bg = tile.getBgColor();
+                    if (bg == null || bg == Color.CLEAR) continue;
+                    batch.setColor(bg.r, bg.g, bg.b, bg.a * parentAlpha);
+                    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();
+                Color fg = tile.getColor();
+                if (glyph == null || fg == null || fg == Color.CLEAR) continue;
+                batch.setColor(fg.r, fg.g, fg.b, fg.a * parentAlpha);
+                glyph.draw(batch, x * tileWidth, y * tileHeight, tileWidth, tileHeight);
+            }
+        }
+    }
+
+
+    @Override
+    public float getPrefWidth() {
+        return columns * tileWidth;
+    }
+
+    @Override
+    public float getPrefHeight() {
+        return rows * tileHeight;
+    }
+
+    /**
+     * Returns a tile of 1 white pixel for use in solid colors such as backgrounds.
+     */
+    public static Tile defaultWhiteTile() {
+        return LazyHolder.WHITE;
+    }
+
+    private static class LazyHolder {
+        private static final Tile WHITE;
+
+        static {
+            Pixmap pix = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
+            pix.setColor(1, 1, 1, 1);
+            pix.fill();
+            WHITE = new Tile(new TextureRegionDrawable(new TextureRegion(new Texture(pix))));
+
+        }
+    }
+
+}
+

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.*;
+
+public class Tileset {
+
+    @NonNull @Getter
+    private final Texture texture;
+
+    @Getter
+    private final int tileWidth;
+
+    @Getter
+    private final int tileHeight;
+
+    @NonNull @Getter
+    private final Layout layout;
+
+    public static enum Layout {
+        InRows, InColumns
+    }
+
+    public int columns() {
+        return tiles[0].length;
+    }
+
+    public int rows() {
+        return tiles.length;
+    }
+
+    private Tile[][] tiles;
+
+    public Tileset(Texture texture, int tileWidth, int tileHeight, Layout layout) {
+        this.texture = texture;
+        this.tileWidth = tileWidth;
+        this.tileHeight = tileHeight;
+        this.layout = layout;
+
+        int rows = texture.getHeight() / tileHeight;
+        int cols = texture.getWidth() / tileWidth;
+
+        tiles = new Tile[rows][cols];
+        TextureRegion[][] split = TextureRegion.split(texture, tileWidth, tileHeight);
+
+        assert split.length == tiles.length;
+        assert split[0].length == tiles[0].length;
+
+        for (int r = 0; r < split.length; r++) {
+            for (int c = 0; c < split[0].length; c++) {
+                tiles[r][c] = new Tile(new TextureRegionDrawable(split[r][c]), Color.WHITE, null);
+            }
+        }
+    }
+
+    public Tileset(String path, int tileWidth, int tileHeight, Layout layout) {
+        this(loadTexture(path), tileWidth, tileHeight, layout);
+    }
+
+    protected static Texture loadTexture(String path) {
+        Texture tex = new Texture(Gdx.files.internal(path), false);
+        tex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
+        return tex;
+    }
+
+    public Tile get(int row, int col) {
+        return tiles[row][col];
+    }
+
+    public Tile get(char idx) {
+        switch (layout) {
+            case InRows:
+                return get(idx / columns(), idx % columns());
+            case InColumns:
+            default:
+                return get(idx % rows(), idx / rows());
+        }
+    }
+}

src/main/resources/tiles/dg_dungeon32.png

Added
New image

src/main/resources/tiles/rogue_male.png

Added
New image

src/main/resources/tiles/terminal.png

Added
New image

src/main/resources/tiles/terminal_trans.png

Added
New image

src/main/resources/tiles/warrior_male.png

Added
New image

src/main/resources/tiles/wizard_male.png

Added
New image