How to optimize drawRectangle or drawHorizontalLine? Pong games are very slow.

classic Classic list List threaded Threaded
3 messages Options
bin
Reply | Threaded
Open this post in threaded view
|

How to optimize drawRectangle or drawHorizontalLine? Pong games are very slow.

bin
I have optimized the drawing of horizontal lines, but Pong games are still very slow.

Is there any way to optimize the drawHorizontalLine method?

drawHorizontalLine

I'll start with bits from X1 to the next word.

Reprocessing X2 to bits between the last word.

Then the BITS from the next word of X1 to the last word on the X2 is processed.

I don't know if my description is clear.

Or you can look at my code.

Thank you very much, Mark.


```
    /** Draws a filled rectangle whose top left corner is (x1, y1)
     * and bottom right corner is (x2,y2), using the current color. */
    function void drawRectangle(int x1, int y1, int x2, int y2) {
        var int temp;
        if (y1 > y2) {
            let temp = y1;
            let y1 = y2;
            let y2 = temp;
        }

        while (~(y2 < y1)) {
            do Screen.drawHorizontalLine(x1, x2, y1);
            let y1 = y1 + 1;
        }

        return;
    }
```

```
    function void drawHorizontalLine(int x1, int x2, int y) {
        var int temp, address1, address2, mask, i;

        // exchange
        if (x1 > x2) {
            let temp = x1;
            let x1 = x2;
            let x2 = temp;
        }

        // x1 address
        let address1 = (y * 32) + (x1 / 16);

        // x2 address
        let address2 = (y * 32) + (x2 / 16);

        if (address1 = address2) {
            while (~(x1 > x2)) {
                do Screen.drawPixel(x1, y);
                let x1 = x1 + 1;
            }
            return;
        }

        //      0         1        2     3    4    6     7
        //  |..x1..|16bits|32bits|... |... |... |..x2..|
       
        // index
        let i = x1 & 15;
        while (i < 16) {
            let mask = bitArray[i];

            if (screenColor) {
                let screenAddress[address1] = screenAddress[address1] | mask;
            } else {
                let screenAddress[address1] = screenAddress[address1] & ~mask;
            }
            let i = i + 1;
        }

        let i = x2 & 15;
        while (~(i < 0)) {
            let mask = bitArray[i];

            if (screenColor) {
                let screenAddress[address2] = screenAddress[address2] | mask;
            } else {
                let screenAddress[address2] = screenAddress[address2] & ~mask;
            }
            let i = i - 1;
        }


        if ((address2 - address1) > 1) {
            let address1 = address1 + 1;
            while (address2 > address1) {
                let screenAddress[address1] = screenColor;
                let address1 = address1 + 1;
            }
        }

        return;
    }
```
Reply | Threaded
Open this post in threaded view
|

Re: How to optimize drawRectangle or drawHorizontalLine? Pong games are very slow.

cadet1620
Administrator
The Pong ball is 6 pixels wide, so in most cases your code draws it in the "address1 = address2" loop by calling Screen.drawPixel() which does lots of slow math.

The same thing happens when the paddle moves because it is moved by drawing a 4 pixel wide white rectangle on one side and a 4 pixel wide black rectangle on the other.

You can improve the "address1 = address2" loop by using your bitArray the same way you do in your other loops.

All 3 loops can be changed into single masking operations if you build masks like 1111110000000000, 0000000001111000, etc. for the various loops.

My code creates a leftMask for X1 and a rightMask for X2. If address1 = address2, leftMask is modified by rightMask so that it will only contain the required middle bits.

Handy tricks for creating the masks: what is the binary value of bitArray[n]-1? Your bitArray might need an extra value, bitArray[16]=0, to make this trick work without special cases. Realize that a right mask is just an invert of a left mask with a different "n" value.
bin
Reply | Threaded
Open this post in threaded view
|

Re: How to optimize drawRectangle or drawHorizontalLine? Pong games are very slow.

bin
OK, I see. You can always point out the problem. Next I'll try to optimize the code.
thank you, mark