|
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;
}
```
|