The complex, but capable, character graphics system

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

The software implements a "character graphics" mode on the '2600.

Resolution is painfully low, of course - being made out of playfield pixels. With just 40 pixels on a horizontal line, and 66 'trixels' (groups of 3 scanlines forming the ChronoColour pixel), things are extremely limited. Nonetheless, usable graphics can be done.

In my system, the size of characters can be defined through constants - so each game can use an 'arbitrary' character size. In "Wen Hop?" the characters are 5 pixels wide x 10 trixels deep.

Here's a typical character shape...

const unsigned char _CHAR_INSULATOR_TOP[CHAR_Y] = {

    0b01110, 0b00010, 0b01110,    // 0 | OO@ |
    0b11111, 0b00001, 0b11111,    // 1 |OOOO@|
    0b11011, 0b00000, 0b00000,    // 2 |oo oo|
    0b10110, 0b00000, 0b00000,    // 3 |o oo |
    0b01110, 0b00010, 0b01110,    // 4 | OO@ |
    0b01110, 0b00010, 0b01110,    // 5 | OO@ |
    0b00000, 0b00000, 0b00000,    // 6 |     |
    0b00100, 0b00000, 0b00000,    // 7 |  o  |
    0b00100, 0b00000, 0b00100,    // 8 |  O  |
    0b00000, 0b00000, 0b00000,    // 9 |     |
};

That's 3 colour planes, 5 pixels in each, by 10 trixel lines deep.  As an aside, that ASCII-art representation on the side is done by a tool that modifies the source code to interpret the data. It's really useful to help visualise a character, but I do wish ASCII had a decent range of shaded blocks that were also fixed-width. But, I digress.

So the whole system relies on a "BOARD", which is of arbitrary width and depth. The BOARD contains an array of character numbers. The view (what we see on the screen) is a scrollable window into the board.  So, character numbers range from 0 to 127.  The top bit is a "flag" bit which is used to indicate that the character has JUST been placed on the board and should not be involved in interactions this frame. It gets the sequencing of some things correct, so it's important.

Each character number is used to index into multiple tables.  For the purposes of screen draw (a highly efficient obscure piece of code), characters can "auto animate".  Firstly, though, let's look at the character set pointer table...

const unsigned char *const charSet[CH_MAX] = {

    // see ChName @ attribute.h

    char_parallaxBlank,              // 000 CH_BLANK,
    _CHAR_DIRT,                      // 001 CH_DIRT,
    _CHAR_BRICKWALL,                 // 002 CH_BRICKWALL,
    _CHAR_DOOR,                      // 003 CH_DOORCLOSED,
    _CHAR_DOOR,                      // 004 CH_DOOROPEN_0,
    _CHAR_BLANK,                     // 005 CH_EXITBLANK,
    // etc...

So, given a character "number" - actually referenced through an enum value in the comments at right-side - we get a pointer to the character "shape".  Simple enough.  As becomes obvious here, I have up to 128 character names. These are defined simply in an enum...

enum ChName {
    CH_BLANK,                     // 000
    CH_DIRT,                      // 001
    CH_BRICKWALL,                 // 002
    CH_DOORCLOSED,                // 003
    CH_DOOROPEN_0,                // 004
    // etc...

So, these values are what are placed in the BOARD, and what are used to lookup the shape in the charSet table.  Good so far, and with just these systems we have enough to draw a screen.  But the system is actually a lot more versatile than this.

The screen draw has an auto-animation feature for character TYPES.  Consider, for example, the spinning DOGE coin... it has 6 animation frames (=characters) as it spins.  How do I do the animation?  Well, I could go through the whole board and everytime I see frame #1 for doge, I could replace it with frame #2, etc. This would work.  Only, the game loop runs at something like 10 fps (by design). It could go as fast as maybe 20 fps - but the board being big and everything on it having to be processed every frame... time gets prohibitive.  So, the system is designed to allow a slower game loop. But this would mean that animations can happen at a maximum of that many frames per second.

Let's say right now, the doge coin animation is spinning at 60 fps. How is that achieved?  Well, it's through the character animation system - an automatic animation loop for each character TYPE that is processed every frame.  And the screen draw, rather than drawing the character on the BOARD, draws the current animation character FOR that character.

So, here's a little animation loop for the DOGE character type...

const unsigned char AnimPulseDoge[] = {

    CH_DOGE_00, 12,
    CH_DOGE_01, 8,
    CH_DOGE_02, 5,
    CH_DOGE_03, 4,
    CH_DOGE_04, 5,
    CH_DOGE_05, 8,

    CH_DOGE_04, 5,
    CH_DOGE_03, 4,
    CH_DOGE_02, 5,
    CH_DOGE_01, 8,

    ANIM_LOOP
};

The first number is of course the character to display. The second is how many frames to display it for.    It works by first translating a character number to an object TYPE.  So all of the 6 frames of the spinning DOGE characters are of type TYPE_DOGE.  It's this type number that is used to index into the animation table, to get the correct animation frame for that character being drawn.  The upshot of this, though, is that ALL characters of a particular TYPE on the screen always show the same animation frame (=character).  I get free animation at 60Hz at the cost of having all similar things display the same frame.  I can bypass this, of course, by just using non-animation for particular characters.  Let's look at the TYPE system.

Each character has an entry in the 'CharToType' table. That entry converts the character number to an object type number...

enum ObjectType {

    // update Attribute[] in attribute.c
    // update AnimateBase[] in animations.c


    TYPE_BLANK,                    // 00      ASSUMED == 0 in code
    TYPE_DIRT,                     // 01
    TYPE_BRICKWALL,                // 02
    TYPE_OUTBOX_PRE,               // 03
    TYPE_OUTBOX,                   // 04
    // etc...

The type system becomes VERY powerful later on, when doing collisions and game logic.  But first, the above enum is just a list of different types that we have to do things with.  As noted, each character is associated with a type, in the CharToType table...

const enum ObjectType CharToType[CH_MAX] = {

    TYPE_BLANK,                    // 000 CH_BLANK,
    TYPE_DIRT,                     // 001 CH_DIRT,
    // 2 to 13 culled for brevity in this writeup
    TYPE_LAVA,                     // 014 CH_LAVA_BLANK,
    TYPE_LAVA,                     // 015 CH_LAVA_SMALL,
    TYPE_LAVA,                     // 016 CH_LAVA_MEDIUM,
    TYPE_LAVA,                     // 017 CH_LAVA_LARGE,
    TYPE_MELLON_HUSK,              // 018 CH_MELLON_HUSK,
    TYPE_DOGE,                     // 019 CH_DOGE_01,
    TYPE_DOGE,                     // 020 CH_DOGE_02,
    TYPE_DOGE,                     // 021 CH_DOGE_03,
    TYPE_DOGE,                     // 022 CH_DOGE_04,
    TYPE_DOGE,                     // 023 CH_DOGE_05,
    TYPE_DOGE,                     // 024 CH_DOGE_STATIC,
    TYPE_PEBBLE_ROCK,              // 025 CH_PEBBLE_ROCK,
    // etc...

So here we see for example, the 6 DOGE characters (19 to 23 and 11 (not shown)) all convert to TYPE_DOGE. They all do the "same thing". The character animation table is...

const unsigned char *const AnimateBase[TYPE_MAX] = {

    0,                    // 00 TYPE_BLANK
    0,                    // 01 TYPE_DIRT
    0,                    // 02 TYPE_BRICKWALL
    0,                    // 03 TYPE_OUTBOX_PRE
    AnimFlashOut,         // 04 TYPE_OUTBOX
    0,                    // 05 TYPE_STEELWALL
    0,                    // 06 TYPE_ROCK
    AnimPulseDoge,        // 07 TYPE_DOGE
    0,                    // 08 TYPE_MELLON_HUSK_PRE
    AnimMellonHusk,       // 09 TYPE_MELLON_HUSK
    0,                    // 10 TYPE_PEBBLE1
    0,                    // 11 TYPE_DUST_0
    // etc...

So we see many have '0' which means that type does not auto-animate. But the others point to a little animation 'routine' as shown above. The TYPE_DOGE points to AnimPulseDoge.  We have a small array 'Animate' (one entry per type) pointing to the current entry for each type in its animation program. So when the screen is drawn, we get '*Animate[CharToType[character]] to figure out what character to ACTUALLY draw. 

Seems complex, but it gives really cheap/effective full-screen animation of many characters with next to no effort or processing time.

But where it becomes REALLY useful is in deciding 'what to do' when the player is moving about the BOARD. Given a character we're about to hit - we convert to the type (CharToType[character]) and use that to lookup an 'Attribute' table.  That table contains a list of flags for that type, indicating how it behaves. Might be simpler to just dump an example here...

Firstly, for code readability, I define aliases for the attributes. Attributes are single-bits in the 32 bit attribute byte for each type.

enum AttributeAlias {

    ROL = ATT_ROLL,                    // doge should roll off this object
    CVT = ATT_CONVERT,                 // iff below surface, object converts to lava or water
    XPD = ATT_EXPLODABLE,              // object is destroyed in explosions
    PER = ATT_PERMEABLE,               // square is 'permeable' (not solid/not blank)
    SPC = ATT_BLANK,                   // object is blank
    DRT = ATT_DIRT,                    // object is dirt
    GRB = ATT_GRAB,                    // doge
    PSH = ATT_PUSH,                    //
    SQB = ATT_SQUASHABLE_TO_BLANKS,    //
    HRD = ATT_HARD,                    // Hard. rocks create dust when falling onto
    XIT = ATT_EXIT,                    //
    QUI = ATT_NOROCKNOISE,             //
    RKF = ATT_BLANK,                   //
    MIN = ATT_MINE,                    //
    WTF = ATT_WATERFLOW,               //
    CVY = ATT_CONVERT,                 //
    GND = ATT_GRIND,                   //
    PIP = ATT_PIPE,                    //
    PH4 = ATT_PHASE4,                  // processed every 4 frames
    PH2 = ATT_PHASE2,                  // processed every 2 frames
    PH1 = ATT_PHASE1,                  // processed every 1 frame
    DIS = ATT_DISSOLVES,               //
    MLT = ATT_MELTS,                   //
    DGE = ATT_GEODOGE,                 // a geodoge
    PAD = ATT_PAD,                     // add dirt corners to this object square
    CRU = ATT_CRUSHES,                 // object above crushes player
    CNR = ATT_CORNER,                  // this object contributes to making a dirt corner
};

OK, so we have a number of evident behaviours basically. For example any TYPE marked with ATT_ROLL indicates an object that a DOGE will roll off. All I do is check that bit.  Something like 'if the type of the character the doge is on top of has ATT_ROLL bit set, I roll off it'. This is used all throughout the code. But first the TYPE/attribute table...

const unsigned int Attribute[TYPE_MAX] = {
#define _ 0

// CNR PAD DGE MLT DIS PH* PIP GND CVY WTF MIN RKF QUI XIT HRD SQB PSH GRB DRT SPC PER XPD CVT CRU ROL
// ---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+----+
    _ |PAD| _ | _ | _ | _ | _ | _ | _ | _ | _ |RKF|QUI| _ | _ | _ | _ | _ | _ |SPC|PER|XPD|CVT| _ | _  , // 00 TYPE_BLANK,
   CNR| _ | _ | _ |DIS| _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ |DRT| _ |PER|XPD|CVT| _ | _  , // 01 TYPE_DIRT,
    _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ |HRD| _ | _ | _ | _ | _ | _ | _ | _ | _ |ROL , // 02 TYPE_BRICKWALL,
    _ | _ | _ | _ | _ |PH1| _ | _ | _ | _ | _ | _ | _ | _ |HRD| _ | _ | _ | _ | _ | _ | _ | _ | _ | _  , // 03 TYPE_OUTBOX_PRE,
    _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ |XIT| _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _  , // 04 TYPE_OUTBOX,
    _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ |HRD| _ | _ | _ | _ | _ | _ | _ | _ | _ | _  , // 05 TYPE_STEELWALL,
    _ |PAD| _ |MLT| _ |PH1| _ |GND|CVY| _ |MIN| _ | _ | _ |HRD| _ |PSH| _ | _ | _ | _ |XPD| _ | _ |ROL , // 06 TYPE_ROCK,
    _ |PAD| _ |MLT| _ |PH4| _ |GND|CVY| _ | _ | _ | _ | _ | _ | _ |PSH|GRB| _ | _ | _ |XPD| _ | _ |ROL , // 07 TYPE_DOGE,
    _ |PAD| _ | _ | _ |PH1| _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _  , // 08 TYPE_MELLON_HUSK_PRE,
    _ |PAD| _ | _ | _ |PH1| _ | _ | _ | _ | _ |RKF|QUI| _ | _ |SQB|PSH| _ | _ | _ | _ |XPD| _ | _ | _  , // 09 TYPE_MELLON_HUSK,
   CNR|PAD| _ | _ |DIS|PH4| _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ |DRT| _ |PER|XPD| _ | _ | _  , // etc...

So, that's basically it. I use the char->type->attribute->flag stuff all over the code, to check various things. For a recent example, here's the code that processes the electric fence. Basically the insulator at the top does all the processing - when we find it on the board we execute the following code. The code just steps through the characters below the insulator until it finds the matching lower insulator. For each character it has to decide if it should replace the characer with an electric character or leave it alone/stop.

        int x = boardCol;
        int y = boardRow + 1;

        while (CharToType[GET(*p)] != TYPE_INSULATOR) {
            unsigned int att = Attribute[CharToType[GET(*p)]];
            if (att & (ATT_EXPLODABLE | ATT_GEODOGE | ATT_DISSOLVES | ATT_BLANK)) {

                if (att & ATT_EXPLODABLE && !(att & ATT_BLANK))
                    FLASH(0x28, 10);

                *p = FLAG(CH_ELECTRIC_0 + rangeRandom(4));
                nDots(20, x, y, PT_SPIRAL, 40 + rangeRandom(20), CHAR_CENTER_X, 0, 80, 7);
                break;
            }
            p += dir;
            y++;
        }


Hopefully that will be fairly self-explanatory. The type-attribute system makes it very simple and powerful to do conditional stuff based on what character is on the baord.

It's a really cool system that I like quite a lot.

Andrew Davie

Here's the guts of the screen draw - grabbing appropriate characters from the board.

void grabCharacters() {

    enum ChName p2;
    enum ObjectType type;
    unsigned char udlr;

    for (int col = 0; col < 5; col++) {

        p2 = GET(p[col]);
        type = CharToType[p2];

        if (Animate[type])
            img[col] = charSet[revectorChar[*Animate[type]]];
        else
            img[col] = charSet[revectorChar[p2]];
    }
    p += 4;
}

In the inner loop we see

1. get character from the board
2. convert to a type
3. if that type animates, get the current animation character instead
4. get the pointer to the character definition

The 'revector' part is currently 'unused'. It's designed to get around the 128 character limitation by allowing me to have a section of the character set be dynamic (changing for each level).  The gist of the code shown, though, shows how a character number on board is convered to a pointer to character shape data, with 'automatic/inbuilt' animation for the character type.

There's also another bit inside that loop which I cut out to make the explanation simpler.  Here it is, though...

        if (Attribute[type] & ATT_PAD) {

            udlr = ((Attribute[CharToType[GET(p[col - _1ROW])]] & ATT_CORNER) >> POS_CORNER |
                    ((Attribute[CharToType[GET(p[col + 1])]] & ATT_CORNER) >> (POS_CORNER - 1)) |
                    ((Attribute[CharToType[GET(p[col + _1ROW])]] & ATT_CORNER) >> (POS_CORNER - 2)) |
                    ((Attribute[CharToType[GET(p[col - 1])]] & ATT_CORNER) >> (POS_CORNER - 3)));

            corner[col] = roundedCorner[udlr];
        }

        else
            corner[col] = _CHAR_BLANK;


You may or may not have noticed the super-cool 'rounded corners' of the dirt whenever you're digging. This is an automated system that operates during draw time. The board just has CH_DIRT, but we actually overlay a character on top of whatever the board has - a 'corner' character. That's determined in the above code by looking at the attributes of characters above, below, and to the sides. If those characters are of certain types, we add a rounded corner there. It's a super-cool realtime system that produces a great effect on the screen.

There are 16 possible corner types (4 corners = 2^4 combinations). So I have 16 characters defined for corner overlays, thus...

const unsigned char *const roundedCorner[] = {
    _CHAR_BLANK,              // 00
    _CHAR_BLANK,              // 01 U
    _CHAR_BLANK,              // 02 R
    _CHAR_INNER_CORNER_RU,    // 03 RU
    _CHAR_BLANK,              // 04 D
    _CHAR_BLANK,              // 05 DU
    _CHAR_INNER_CORNER_6,     // 06 RD
    _CHAR_INNER_CORNER_7,     // 07 URD
    _CHAR_BLANK,              // 08 L
    _CHAR_INNER_CORNER_9,     // 09 LU
    _CHAR_BLANK,              // 10 LR
    _CHAR_INNER_CORNER_11,    // 11 LUR
    _CHAR_INNER_CORNER_12,    // 12 LD
    _CHAR_INNER_CORNER_13,    // 13 LDU
    _CHAR_INNER_CORNER_14,    // 14 LRD
    _CHAR_INNER_CORNER_15,    // 15 LURD
};

It makes a world of difference to the visuals, and effectively operates 'behind the scenes'. I never think about drawing corners on stuff; they are just 'added' automatically at draw time.  Not super-cheap in terms of draw time, but well worth it.  Again, it shows the value of the type/attribute system - I just look at the corner attribute of the surrounding characters...

                    ((Attribute[CharToType[GET(p[col + 1])]] & ATT_CORNER) >> (POS_CORNER - 1)) |

That's directly taking the ATT_CORNER bit (which happens to be bit 26) from the attribute table and shifting it to bit 1. Various shifts for various corner bits, we end up with 4 bits set, giving us a character # index to the corner table. Complex, but elegant.




Andrew Davie

Quote from: Andrew Davie on 11 Jun 2026, 01:16 AMYou may or may not have noticed the super-cool 'rounded corners' of the dirt whenever you're digging. This is an automated system that operates during draw time. The board just has CH_DIRT, but we actually overlay a character on top of whatever the board has - a 'corner' character. That's determined in the above code by looking at the attributes of characters above, below, and to the sides. If those characters are of certain types, we add a rounded corner there. It's a super-cool realtime system that produces a great effect on the screen.

I'm uaing @SpiceWare's emoji commenting all over the place now. The image shows a couple of the corner characters, which makes things immediately obvious as to what I was trying to describe...