Matrix Sprite Draw

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

I currently have a software sprite system which can draw arbitrary-height, 8-PF pixel wide sprites consisting of ChronoColour "trixels". It currently draws on 32-pixel wide mirrored playfields (PF1/PF2/PF2/PF1), and used to be able to draw on 40-pixel wide non-mirrored playfields. I say "used to" because I recently updated it for the former and haven't tested it on the latter. I think I probably broke a few things, so we'll leave it at "used to" until I fix that.

So, 8-PF pixel wide fits into a single byte width - one bit per pixel, 3 planes = 3 bits per trixel giving 8 virtual colours.  Effectively a 12 trixel high sprite = 12 x 3 scanlines high = 36 lines high = 36 bytes.  I also added a couple of extra bytes in the data so you can specify the center point (or in other words the origin) of the sprite. That means if you draw at (x,y) then that center pixel will always be onscreen at (x,y) regardless of the sprite size/shape.

Anwyay, I digress...

That's all working (at least on the 32-wide title screens).  Now I want to extend the system to be able to draw *matrices* of 8-pixel-wide sprites.  That is, join strips of 8-pixel wide, arbitrary height, sprites together so that they display as a whole. This is leading towards drawing *really big* sprites onscreen, but keeping at least some efficiency in the data format.

So the first step will be designing a new data format. It's going to be just a slight modification of the previous format.  When we think of a "frame" for a sprite, this used to consist of the center point data, followed by the graphics data.  That will be replaced by a center point, and then a list of "strips", each strip having an x,y offset from the center point. I think that will work.

This will require modification of the fantastic tool 'sp' I have, which currently culls individual sprites from a "sprite sheet" and then processes each of those sprites to produce the C-structures necessary. It needs to be extended to handle more-than-8-pixel-wide sprites. It should be quite straightforward. Then I'll need to modify the sprite draw routine to draw the strips of sprites, rather than just one strip. It's a bit of an incremental change rather than a wholesale new system.  It should take me a couple of days, depending on how lazy I am.

Here's the current 'blitter' code which draws on the 32-wide mirrored PF.  It's quite complex, really... but does have some quite neat stuff in there. To draw a software sprite, I call 'blitter' with parameters frame number, x position, y position, buffer pointer for right half of screen, buffer pointer for left half of screen. The buffer pointers are required to make this generic code for both 32-wide and 40-wide playfields.

// COMPILE-TIME REVERSE BITS IN BYTE
#define RVS(a)                                                                                     \
    (((((a) >> 0) & 1) << 7) | ((((a) >> 1) & 1) << 6) | ((((a) >> 2) & 1) << 5) |                 \
     ((((a) >> 3) & 1) << 4) | ((((a) >> 4) & 1) << 3) | ((((a) >> 5) & 1) << 2) |                 \
     ((((a) >> 6) & 1) << 1) | ((((a) >> 7) & 1) << 0))

#define P0(a) RVS(a)
#define P1(a) P0(a), P0(a + 1)
#define P2(a) P1(a), P1(a + 2)
#define P3(a) P2(a), P2(a + 4)
#define P4(a) P3(a), P3(a + 8)
#define P5(a) P4(a), P4(a + 16)
#define P6(a) P5(a), P5(a + 32)
#define P7(a) P6(a), P6(a + 64)
#define P8(a) P7(a), P7(a + 128)

// Want to call RVS(n) for 0-255 values. The weird #defines above allows a
// single-call It's effectively a recursive power-of-two call of the base RVS
// macro

const unsigned char reverseBits[] = {
    P8(0),
};

void blitShape(int ch, int shift, int y, int offset, bool mirror, int colour) {

    int height = frameHeight[ch] * 4;

    unsigned char *p1 = RAM + offset + y;
    unsigned char *p2 = p1 + _ARENA_SCANLINES;
    unsigned char *p3 = p2 + _ARENA_SCANLINES;

    int modifier = y + colour;

    if (y >= 0)
        while (modifier > 2)
            modifier -= 3;

    else
        while (modifier < 0)
            modifier += 3;

    int bitsh = 19 - shift;

    const char nextRoller[] = {2, 3, 1, 2, 3, 1, 2, 3};

    if (!mirror)

        for (int trix = 0; trix < height && y < _ARENA_SCANLINES; trix += 4) {

            if (y >= 0) {

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
                unsigned char *fp = (unsigned char *)(frameLowTable[ch]);
#pragma GCC diagnostic pop
                unsigned int mask = fp[trix];
                mask <<= bitsh;
                mask = ~mask;

                unsigned char m1 = mask >> (16 + 7);
                unsigned char m1Mirrored = BitRev[m1];
                unsigned char m2 = mask >> (8 + 7);
                unsigned char m2Mirrored = mask >> (8 + 7);
                unsigned char m3 = mask >> 7;
                unsigned char m3Mirrored = BitRev[m3];

#define INNER(r)                                                                                   \
    if (y + r < _ARENA_SCANLINES) {                                                                \
        unsigned int shiftCh = fp[trix + nextRoller[roller + r + modifier]];                       \
        shiftCh <<= bitsh;                                                                         \
        if (KERNEL == KERNEL_GAME)                                                                 \
            *(p1 + r) = (*(p1 + r) & m1Mirrored) | BitRev[(unsigned char)(shiftCh >> (16 + 7))];   \
        *(p2 + r) = (*(p2 + r) & m2) | shiftCh >> (8 + 7);                                         \
        *(p3 + r) = (*(p3 + r) & m3Mirrored) | BitRev[(char)(shiftCh >> 7)];                       \
    }

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"

                INNER(0)
                INNER(1)
                INNER(2)

#pragma GCC diagnostic pop
            }

            p1 += 3;
            p2 += 3;
            p3 += 3;

            y += 3;
        }

    else // mirror

        for (int trix = 0; trix < height && y < _ARENA_SCANLINES; trix += 4) {

            if (y > 0) {

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
                unsigned char *fp = (unsigned char *)(frameLowTable[ch]);
#pragma GCC diagnostic pop

                unsigned int mask = BitRev[fp[trix]];
                mask <<= bitsh;
                mask = ~mask;

                unsigned char m1 = mask >> (16 + 7);
                unsigned char m1Mirrored = BitRev[m1];
                unsigned char m2 = mask >> (8 + 7);
                unsigned char m2Mirrored = mask >> (8 + 7);
                unsigned char m3 = mask >> 7;
                unsigned char m3Mirrored = BitRev[m3];

#define INNER_MIRROR(r)                                                                            \
    if (y + r < _ARENA_SCANLINES) {                                                                \
        unsigned char *fp = (unsigned char *)(frameLowTable[ch]);                                  \
        unsigned int shiftCh = BitRev[fp[trix + nextRoller[roller + r + modifier]]];               \
        shiftCh <<= bitsh;                                                                         \
                                                                                                   \
        if (KERNEL == KERNEL_GAME)                                                                 \
            *(p1 + r) = (*(p1 + r) & m1Mirrored) | BitRev[(char)(shiftCh >> (16 + 7))];            \
        *(p2 + r) = (*(p2 + r) & m2) | shiftCh >> (8 + 7);                                         \
        *(p3 + r) = (*(p3 + r) & m3Mirrored) | BitRev[(char)(shiftCh >> 7)];                       \
    }

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
                INNER_MIRROR(0)
                INNER_MIRROR(1)
                INNER_MIRROR(2)
#pragma GCC diagnostic pop
            }

            p1 += 3;
            p2 += 3;
            p3 += 3;

            y += 3;
        }
}

void blitter(int f, int x, int y, int bufferRHS, int bufferLHS) {

    if (x > -8 && x < 20)
        blitShape(f, x, y, bufferLHS, false, 1);

    if (x > 12 && x < 40)
        blitShape(f, x - 16, y, bufferRHS, false, 1);
}

#pragma GCC diagnostic pop

Andrew Davie

This is a bit of a diversion - I'm working on improving the sprite workflow, including a matrix-based sprite system for large sprites. In this video I've used fighting sprites as my testbed, and I'm just showing a full-screen grab here so you can see my worflow - extremely quick to make a change in the graphics editor (GIMP) and build in VSCodium. A matter of seconds.

Right now theere are two large fighter sprites (software sprites drawn in PF). I can handle about 4 at the same time, give or take. The graphics are pretty crap but remember these are straight auto-converted from 24bpp colour sprite sheet into 3bpp iCC format. Any actual game would require hand-tweaking of colours and shapes. But, for all that, it gives some idea of what it might look like.

Shown here, the sprites are at max 8 pixels wide. That's what the matrix system will do - when working - allow wider sprites by drawing multiple strips of 8-pixel sprites as required.


Andrew Davie

With regard to a fighting game - one pretty cool idea/possibility would be to overlay the PF low-resolution frame with high-resolution (double-wide sprite) heads. That might be worth trying - I may get to that sometime in the future if I ever do a fighting game.


Bomberman94

Uhhh - i maybe see a version of Karateka on the horizon 😃

Andrew Davie

#4
With a bit of tweaking, I can change the colour of frames. It's pretty basic in functionality, as frame data is "hardwired" to the 8 colour palette's index numbers (so, red belt is ALWAYS colour 2 which is ALWAYS red).  But as I said, a tweak and it's possible to get more colours. So to speak.


Andrew Davie

#5
Here's a binary. Don't expect much; just experimenting and developing systems at this stage!
Just want to emphasise; these are only auto-converted rough mockups of animation frames -- no human hands were involved. I expect final graphics would be far superior.


Andrew Davie

It's looking better. I'm done for the day.  There are about 100 animation frames installed; this will reduce when I get rid of the ones I don't like/need. The players are different colours (at least their belts/hair are). I'll work on improving that, but really all this is a diversion so I don't have to work on the sprite matrices today. Too late now (3am). Maybe tomorrow.


Andrew Davie

The SaveKey is really useful for enabling a "completely" random startup for demo modes - and in this case sprite colours. Initialising a random number seed is usually done via plonking the current date/time in your random seed variables. With a cart you don't have time available, although you could wait for user interaction and using some sort of time delay there to initialise the seed.

In the case where there's no user interaction before you start using your random generator, things get a bit difficult. Up until now, every time I pressed reset, I got exactly the same behaviour from my program. Which is kinda good, except when you want random things to happen.

In my SaveKey code, I have a counter that I increment on the SK every time the game is run. I now also use this same counter to mangle/init the random number seed. What that basically means is that i have a "real" random number on startup, not the same sequence every time.

So in this video, I'm generating a random "palette" for the fighters. In actuality it's just three random ChronoColour colours which are mixed to produce the 8 colour set. The video shows a number of resets, just testing out how the various colour combinations affect the visuals.  There are some OK, even nice, ones in there.


Andrew Davie

The matrix sprite draw is quite capable now. I need to add sprite mirroring. I need a new name for the PF sprite system... something snazzy. Names are important.

Anwyay, with this latest video I am cycling through a simple animation.


There's a lot happening here in the background. The man isn't changing x,y position at all - the animations are simply cyling through a number of frames and because they have 'correct' center points, the player does his somersault. Rough, but roughly correct.

So it's quite interesting how it all works. I'll try and describe.

Firstly we start with a sprite sheet. That's just a bunch of frames drawn on the same image with black borders around all.  I wrote a tool 'sp.py' to separate out all of these to individual images. The tool also handles centerpoints for the frames. These are drawn as a new layer in the sprite sheet, and exported to the filename '*_centerpoints.png'.  Basically just a non-zero pixel at the location of the center point of each frame.  So when the tool goes through to process each individual frame, it looks for the presence of a centerpoint file - and if so, it will include that in the sprite c data.  If not, then the midpoint of the base of the frame is used.

Anwyay, I have a manifest file. That file contains an 'alias' for each frame and other data. Frames are generated with generic names 'FRAME_0' etc. But you can modify the manifest file and give meaningful names to each frame. For example, you could change 'FRAME_0' to 'WALK_0'.  From the source code, you can then reference the frames by their alias names. The nice thing about this is that you can change the frame graphics somewhat and the naming you give it will be persistent. Needs work, but basically functional.

So i went through and found the somersault frames, and named them... "FLIP0", "FLIP1" etc.  Now I can tell the sprite system to draw the flip frames in order. I just have a simple array with frame names to display, and a delay loop. This system will be improved, of course.... but here's the array for the animation shown in the video.

const unsigned char frameSequence[] = {

    WALK1, WALK1, WALK1, WALK1, WALK1, WALK1, WALK2, WALK3, WALK4, FLIP0, FLIP7,
    FLIP1, FLIP2, FLIP3, FLIP4, FLIP5, FLIP6, FLIP8, WALK4,

    WALK1, WALK1, WALK1, WALK1, WALK1, WALK1, WALK2, WALK3, WALK4, FLIP0, FLIP7,
    FLIP1, FLIP2, FLIP3, FLIP4, FLIP5, FLIP6, FLIP8, FLIP0, FLIP7, FLIP1, FLIP2,
    FLIP3, FLIP4, FLIP5, FLIP6, FLIP8, WALK4, WALK4, WALK4, WALK4, WALK4,

};

So, the whole point of this was to get an early peek at how the whole system looks with decent-speed animations. I mean, we all can see that the resolution of the frames is painfully low. I know that; obviously games will have to be designed around the limitations and graphics drawn to take care of this limitation. But anyway, even with the extreme placeholder nature of the graphics (no hand editing/tweaking at all, just down-converted from an sprite-sheet 400% bigger and run directly through the tools - Well ok, I did colour-fill the shirt but no actual changes to the shapes ... well, it shows potential. The flip isn't a total loss.