1

I'm writing the drawing system for a roguelike game based on ascii characters (graphics similar to dwarf fortress). I'm using the AsciiPanel from here. My problem is that when I draw entities on my map, they seem to blink, when they should be solid.

In this gif, the r characters in the top row are the entities.blinking entities

This is the map's draw method that is called every frame.

public void draw(final Display display) {
        for (int x = getViewportX(); x < getViewportX() + viewportWidthInTiles; x++) {
            for (int y = viewportY; y < viewportY + viewportHeightInTiles; y++) {
                final char character = background[x][y].getCharacter();
                final Color foreground = background[x][y].getForeground();
                final Color backgroundColor = background[x][y].getBackground();
                final AsciiCharacterData data = new AsciiCharacterData(
                        character, foreground, backgroundColor);
                display.setCharacterAt(x - getViewportX(), y - viewportY,
                        background[x][y].getDrawingLayer(), data);
            }
        }
        display.clearLayer(DrawingLayer.PRIMARY);
        for (int i = 0; i < entities.size(); i++) {
            final Entity e = entities.get(i);

            final char character = e.getCharacter();
            final Color foreground = e.getForeground();
            final Color backgroundColor = e.getBackground();
            final AsciiCharacterData data = new AsciiCharacterData(character,
                    foreground, backgroundColor);
            display.setCharacterAt(e.getX() - getViewportX(), e.getY()
                    - viewportY, e.getDrawingLayer(), data);
        }
    }

I think I know what causes the problem, because if I write display.clearLayer(DrawingLayer.BACKGROUND); (the layer the tiles are drawn to) before I draw the background tiles, it creates something even more ridiculous.

crazy

This is the Display class, where I think I am making some mistake.

public class Display {
    private static final char TRANSPARENT_CHARACTER = ' ';

    private final AsciiPanel displayPanel;
    private final int widthInCharacters, heightInCharacters;

    private final static int Z_LEVELS = DrawingLayer.values().length;

    private final AsciiCharacterData[][][] characterMap;

    public Display(final AsciiPanel panel) {
        displayPanel = panel;
        widthInCharacters = panel.getWidthInCharacters();
        heightInCharacters = panel.getHeightInCharacters();

        characterMap = new AsciiCharacterData[widthInCharacters][heightInCharacters][Z_LEVELS];
        for (int x = 0; x < widthInCharacters; x++) {
            for (int y = 0; y < heightInCharacters; y++) {
                for (int z = 0; z < Z_LEVELS; z++) {
                    characterMap[x][y][z] = new AsciiCharacterData(
                            TRANSPARENT_CHARACTER,
                            displayPanel.getDefaultForegroundColor(),
                            displayPanel.getDefaultBackgroundColor());
                }
            }
        }
    }

    public void setCharacterAt(final int x, final int y, final DrawingLayer z,
            final AsciiCharacterData c) {
        if (x < 0 || x >= widthInCharacters || y < 0 || y >= heightInCharacters)
            return;
        characterMap[x][y][z.layer] = c;

        // if z is not the top level
        if (z.layer != Z_LEVELS - 1) {
            // check all levels above
            for (int i = z.layer + 1; i < Z_LEVELS; i++) {
                // if there is an opaque character
                if (characterMap[x][y][i].character != TRANSPARENT_CHARACTER)
                    // we dont need to draw anything
                    return;
            }
        }

        if (c.character == TRANSPARENT_CHARACTER) {
            // loop through all characters under the transparent character
            for (int i = z.layer - 1; i >= 0; i--) {
                // if we find a non transparent character
                if (characterMap[x][y][i].character != TRANSPARENT_CHARACTER) {
                    // display that one instead
                    displayPanel.write(characterMap[x][y][i].character, x, y,
                            characterMap[x][y][i].foregroundColor,
                            characterMap[x][y][i].backgroundColor);
                    return;
                }
            }
            // if there were no non trasparent characters
            displayPanel.write(TRANSPARENT_CHARACTER, x, y);
            // if we are a highlighter, we draw the below character and then
            // just draw on top
        } else {
            displayPanel.write(c.character, x, y, c.foregroundColor,
                    c.backgroundColor);
        }
        displayPanel.repaint();
    }

    public AsciiCharacterData getCharacterAt(final int x, final int y,
            final DrawingLayer z) {
        return characterMap[x][y][z.layer];
    }

    public int getWidth() {
        return widthInCharacters;
    }

    public int getHeight() {
        return heightInCharacters;
    }

    public void clearLayer(final DrawingLayer layer) {
        for (int x = 0; x < widthInCharacters; x++) {
            for (int y = 0; y < heightInCharacters; y++) {
                setCharacterAt(x, y, layer,
                        new AsciiCharacterData(TRANSPARENT_CHARACTER,
                                displayPanel.getDefaultForegroundColor(),
                                displayPanel.getDefaultBackgroundColor()));
            }
        }
    }
}
Kyranstar
  • 1,650
  • 2
  • 14
  • 35
  • 1
    I'm not familiar with `AsciiPanel`. I know that in old MS-DOS text mode, setting the high bit on a character's attribute could either cause it to blink, or yield a brighter color. There were two modes the display could be in. Is it possible that `AsciiPanel` is emulating this behavior? Just a guess. – David Conrad Sep 25 '14 at 00:27
  • I don't think so? Some of those in the second gif aren't really even blinking, just acting strange. – Kyranstar Sep 25 '14 at 00:30
  • I'm probably wrong then. – David Conrad Sep 25 '14 at 00:31
  • AsciiPanel is amazing! If you make something with it, put it on GitHub and shoot me a link! I can help you out more! – Anubian Noob Oct 02 '14 at 01:02

1 Answers1

1

Solved! It was one line in the setCharacterAt method. I was repainting every time I set a character which (while inefficient) also creates that flicker.

Kyranstar
  • 1,650
  • 2
  • 14
  • 35