scroll tracking, coordinates, and #defines

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

Oxford comma, everyone.

I spent some time cleaning-up/rewriting the scroll tracking which was very much a complete inherited hack from past demos. It was pretty ugly; now it's a lot better. Here's the gist of it, codewise. I've moved the defines from their header(s) to this code block, for clarity.


#define _SCANLINES 198

#define _BOARD_ROWS 22
#define _BOARD_COLS 40

#define SCREEN_TRIX_X 40
#define SCREEN_TRIX_Y (_SCANLINES / 3)

#define CHAR_TRIX_X 5
#define CHAR_TRIX_Y 10

#define BOARD_TRIX_X (_BOARD_COLS * CHAR_TRIX_X)
#define BOARD_TRIX_Y (_BOARD_ROWS * CHAR_TRIX_Y)

#define CHAR_Y (3 * CHAR_TRIX_Y)

int approach(int current, int target, int speed) {

    int diff = target - current;

    if (diff > speed)
        return current + speed;

    if (diff < -speed)
        return current - speed;

    return target;
}


void scroll() {

    if (playerDead && !waitRelease && *playerAnimation == FRAME_BLANK) {

        // Manual look-around

        int dir = (swcha ^ 0xFF) >> 4;
        scrollSpeedX = xInc[dir] << 16;
        scrollSpeedY = yInc[dir] << 17;
    }

    else {

        // Auto-tracking

#define MAX_SPEED_X (1 << 15)
#define SCROLL_EDGE_X ((SCREEN_TRIX_X / 4) << 16)
#define ACCEL_X (1 << 12)
#define DECEL_X (1 << 9)

        int tX = ((playerX * CHAR_TRIX_X) << 16) + (CHAR_TRIX_X << 15);
        int hX = scrollX + (SCREEN_TRIX_X << 15);

        if (tX < (hX - SCROLL_EDGE_X)) {
            scrollSpeedX = approach(scrollSpeedX, -MAX_SPEED_X, ACCEL_X);
        } else if (tX > (hX + SCROLL_EDGE_X))
            scrollSpeedX = approach(scrollSpeedX, MAX_SPEED_X, ACCEL_X);
        else
            scrollSpeedX = approach(scrollSpeedX, 0, DECEL_X);

#define MAX_SPEED_Y (1 << 16)
#define SCROLL_EDGE_Y ((SCREEN_TRIX_Y / 4) << 16)
#define ACCEL_Y (1 << 13)
#define DECEL_Y (1 << 10)

        int tY = ((playerY * CHAR_TRIX_Y) << 16) + (CHAR_TRIX_Y << 15);
        int hY = scrollY + (SCREEN_TRIX_Y << 15);

        if (tY < (hY - SCROLL_EDGE_Y))
            scrollSpeedY = approach(scrollSpeedY, -MAX_SPEED_Y, ACCEL_Y);
        else if (tY > (hY + SCROLL_EDGE_Y))
            scrollSpeedY = approach(scrollSpeedY, MAX_SPEED_Y, ACCEL_Y);
        else
            scrollSpeedY = approach(scrollSpeedY, 0, DECEL_Y);
    }

    scrollX += scrollSpeedX;
    scrollY += scrollSpeedY;


    if (scrollX > SCROLL_MAX_X) {
        scrollX = SCROLL_MAX_X;
        scrollSpeedX = 0;
    }

    else if (scrollX < SCROLL_MIN_X) {
        scrollX = SCROLL_MIN_X;
        scrollSpeedX = 0;
    }

    if (scrollY > SCROLL_MAX_Y) {
        scrollY = SCROLL_MAX_Y;
        scrollSpeedY = 0;
    }

    else if (scrollY < SCROLL_MIN_Y) {
        scrollY = SCROLL_MIN_Y;
        scrollSpeedY = 0;
    }
}

So basically it keeps the player sprite within a constrained rectangular area inside the screen by modifying scroll acceleration/deceleration -- through calling scroll() once per frame.  It's working fine, although the accel/decel values could do with tweaking.

So, for coordinates I have a couple of concepts

Pixel coordinates are fixed-point 16.16 format.
Trixels are ChronoColour pixels -- a single PF pixel wide and 3 scanlines high.
Characters are 5 trixels wide (CHAR_TRIX_X) x 10 trixels deep (CHAR_TRIX_Y)

The #defines in the above code are now standardised. The code I "inherited" (well, from past me, actually) was very much a hodgepodge of various demos and badly needed cleaning-up.  For example, I had multiple names for the same thing.  What's now CHAR_Y now, used to be PIECE_DEPTH and CHAR_SIZE.  What's CHAR_TRIX_Y now, used to be TRILINES.  There were quite a few more. Much more logical and consistent now!

Now the sprite draw is borked still - the position of the sprites is jumping everywhere. Possibly partly an ordering issue (move/scroll/draw or scroll/draw/move type of thing), and/or possibly some incorrect calculations.

As noted, coordinates are 16.16 format, but it was previously slightly different.  Y was in trixels (16.16), but X was in characters (16.16) which each had CHAR_TRIX_X pixels in them. The OG code used CHAR_TRIX_X = 4, so I could downshift twice to go from trixels to characters. But the newer code has CHAR_TRIX+X = 5 so that all broke.  I'm still getting back up and running with all this code, but interestingly the original version of this code goes way back to my original CDFJ tests with Boulder Dash. Ages ago. I regretted the choice forever but never fixed it, of making X effectively 14.18 format.  Now it's 16.16 it's much nicer.  And having logical names... chef's kiss.

So the video is a bit wonky. Scroll is fine. Sprite draw not so fine.
Getting there. All code updated in GitHub.




Andrew Davie

Screen shaking (think: earthquakes) is working fine. A very clean implementation, costing next to nothing.