View Single Post
Old 20 December 2019, 15:38   #18
deimos
Registered User

 
Join Date: Jul 2018
Location: France
Posts: 551
Quote:
Originally Posted by a/b View Post
Code:
...
fci = reverse = !topleft;
etc...
Yeah, it's the same code, but 8*6 lines shorter. It's 'easier' to map directly to Scc in asm (I'm sure the produced code is the same, but it's about the idea/hint to give to compiler how to optimize w/o using branches).
And I don't find it any harder to understand, but that's subjective.
Thank you, I've gone and made that change, but I've kept it as two assignment lines for now as I want to keep the order of the two assignments consistent while I'm still testing.

Edit:

I've also changed the switch back to a nested if statement, as I wasn't 100% sure that it was impossible for the horizon to run down an edge, ending up intersecting 3 edges. This way I can check opposing edges first.

Code:
void ArtificialHorizon_Draw(const Camera * camera) {
    BlitterClear();

    LONG a = ((Entity *) camera)->transformation.rotation.value[0][1];
    LONG b = ((Entity *) camera)->transformation.rotation.value[1][1];

    LONG cz = ((Entity *) camera)->transformation.rotation.value[2][1] << 4; // 4, decided experimentally

    LONG leftX = -WIDTH / 2;
    LONG rightX = WIDTH / 2;
    LONG topY = HEIGHT / 2;
    LONG bottomY = -HEIGHT / 2;

    BOOL topLeft = a * leftX + b * topY + cz < 0;
    BOOL topRight = a * rightX + b * topY + cz < 0;
    BOOL bottomLeft = a * leftX + b * bottomY + cz < 0;
    BOOL bottomRight = a * rightX + b * bottomY + cz < 0;

    Point2D upperPoint, lowerPoint;
    BOOL reverse;
    BOOL fci;
    Kludge kludge;

    if (topLeft != bottomLeft && topRight != bottomRight) {
        WORD y0 = topY + (cz - a * rightX) / b;
        WORD y1 = topY + (cz + a * rightX) / b;

        if (y0 > y1) {
            upperPoint = (Point2D) {{ WIDTH - 1, y1 }};
            lowerPoint = (Point2D) {{ 0, y0 }};

            reverse = !topLeft;
            fci = !topLeft;
            kludge = (Kludge) { TRUE, 0, y1 - 1 };
        } else {
            upperPoint = (Point2D) {{ 0, y0 }};
            lowerPoint = (Point2D) {{ WIDTH - 1, y1 }};

            reverse = !topRight;
            fci = topRight;
            kludge = (Kludge) { TRUE, y1 + 1, HEIGHT - 1 };
        }
    } else if (topLeft != topRight && bottomLeft != bottomRight) {
        WORD x0 = rightX - (cz + b * topY) / a;
        WORD x1 = rightX - (cz - b * topY) / a;

        upperPoint = (Point2D) {{ x0, 0 }};
        lowerPoint = (Point2D) {{ x1, HEIGHT - 1 }};

        if (x0 > x1) {
            reverse = !topLeft;
            fci = !topLeft;
            kludge = (Kludge) { FALSE };
        } else {
            reverse = bottomLeft;
            fci = !bottomLeft;
            kludge = (Kludge) { FALSE };
        }
    } else if (topLeft != bottomLeft) {
        if (topLeft != topRight) {
            upperPoint = (Point2D) {{ rightX - (cz + b * topY) / a, 0 }};
            lowerPoint = (Point2D) {{ 0, topY + (cz - a * rightX) / b }};

            reverse = bottomRight;
            fci = bottomRight;
            kludge = (Kludge) { FALSE };
        } else /* bottomLeft != bottomRight */ {
            upperPoint = (Point2D) {{ 0, topY + (cz - a * rightX) / b }};
            lowerPoint = (Point2D) {{ rightX - (cz - b * topY) / a, HEIGHT - 1 }};

            reverse = !topRight;
            fci = topRight;
            kludge = (Kludge) { FALSE };
        }
    } else if (topRight != bottomRight) {
        if (topLeft != topRight) {
            WORD y1 = topY + (cz + a * rightX) / b;

            upperPoint = (Point2D) {{ rightX - (cz + b * topY) / a, 0 }};
            lowerPoint = (Point2D) {{ WIDTH - 1, y1 }};

            reverse = bottomLeft;
            fci = !bottomLeft;
            kludge = (Kludge) { TRUE, y1 + 1, HEIGHT - 1 };
        } else /* bottomLeft != bottomRight */ {
            WORD y1 = topY + (cz + a * rightX) / b;

            upperPoint = (Point2D) {{ WIDTH - 1, y1 }};
            lowerPoint = (Point2D) {{ rightX - (cz - b * topY) / a, HEIGHT - 1 }};

            reverse = !topLeft;
            fci = !topLeft;
            kludge = (Kludge) { TRUE, 0, y1 - 1 };
        }
    } else {
        BlitterFill(topLeft);
        BlitterCopy();
        return;
    }

    Line2D line = reverse ? (Line2D) {{ lowerPoint, upperPoint }} : (Line2D) {{ upperPoint, lowerPoint }};

    KPrintF("Line (%ld, %ld) - (%ld, %ld)\n", line.value[0].value[X], line.value[0].value[Y], line.value[1].value[X], line.value[1].value[Y]);

    BlitterLine(line, TRUE);
    if (kludge.required)
        DoKludgeLine(kludge.start, kludge.end, 0x0001);

    BlitterFill(fci);
    
    BlitterLine(line, FALSE);
    if (kludge.required) {
        DoKludgeLine(kludge.start, kludge.end, 0x0000);
    }

    BlitterWhiteLine(line);

    BlitterCopy();
}

Last edited by deimos; 20 December 2019 at 16:49.
deimos is offline  
 
Page generated in 0.04247 seconds with 11 queries