Skip to content

Commit c7d9d68

Browse files
committed
refactor: correct drawScanLine variables and add comments
1 parent 026fd52 commit c7d9d68

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

src/main/java/org/runejs/client/media/Rasterizer3D.java

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,45 +75,68 @@ public class Rasterizer3D extends Rasterizer {
7575
}
7676
}
7777

78-
public static void drawScanLine(int[] dest, int destOffset, int loops, int startX, int endX) {
78+
public static void drawScanLine(int[] dest, int destOffset, int color, int startX, int endX) {
79+
// restrict_edges indicates if there's a need to restrict the drawing operation within certain boundaries (viewport)
7980
if(restrict_edges) {
81+
// If the ending point is beyond the viewport's right boundary, clip it to the boundary
8082
if(endX > viewportRx) {
8183
endX = viewportRx;
8284
}
85+
86+
// If the starting point is before the viewport's left boundary, clip it to the boundary
8387
if(startX < 0) {
8488
startX = 0;
8589
}
8690
}
91+
92+
// If the start point is after or at the end point, there's no need to draw, return
8793
if(startX >= endX) {
8894
return;
8995
}
96+
97+
// Update the destination array offset with the start point
9098
destOffset += startX;
91-
int rgba = endX - startX >> 2;
99+
100+
// Calculate the number of times the loop will run for 4-pixel chunks
101+
int loops = endX - startX >> 2;
102+
103+
// If the alpha is 0 (no transparency), the loop simply draws each pixel with the provided color.
92104
if(alpha == 0) {
93-
while(--rgba >= 0) {
94-
dest[destOffset++] = loops;
95-
dest[destOffset++] = loops;
96-
dest[destOffset++] = loops;
97-
dest[destOffset++] = loops;
105+
// Draw the color 4 times, for each pixel in the 4-pixel chunk
106+
while(--loops >= 0) {
107+
dest[destOffset++] = color;
108+
dest[destOffset++] = color;
109+
dest[destOffset++] = color;
110+
dest[destOffset++] = color;
98111
}
99-
rgba = endX - startX & 0x3;
100-
while(--rgba >= 0) {
101-
dest[destOffset++] = loops;
112+
113+
// For the remaining pixels, draw them one by one
114+
loops = endX - startX & 0x3;
115+
while(--loops >= 0) {
116+
dest[destOffset++] = color;
102117
}
103118
return;
104119
}
120+
121+
// If alpha is not 0 (there is some transparency), the pixels need to be blended with the destination.
105122
int destAlpha = alpha;
106123
int sourceAlpha = 256 - alpha;
107-
loops = ((loops & 0xff00ff) * sourceAlpha >> 8 & 0xff00ff) + ((loops & 0xff00) * sourceAlpha >> 8 & 0xff00);
108-
while(--rgba >= 0) {
109-
dest[destOffset++] = loops + ((dest[destOffset] & 0xff00ff) * destAlpha >> 8 & 0xff00ff) + ((dest[destOffset] & 0xff00) * destAlpha >> 8 & 0xff00);
110-
dest[destOffset++] = loops + ((dest[destOffset] & 0xff00ff) * destAlpha >> 8 & 0xff00ff) + ((dest[destOffset] & 0xff00) * destAlpha >> 8 & 0xff00);
111-
dest[destOffset++] = loops + ((dest[destOffset] & 0xff00ff) * destAlpha >> 8 & 0xff00ff) + ((dest[destOffset] & 0xff00) * destAlpha >> 8 & 0xff00);
112-
dest[destOffset++] = loops + ((dest[destOffset] & 0xff00ff) * destAlpha >> 8 & 0xff00ff) + ((dest[destOffset] & 0xff00) * destAlpha >> 8 & 0xff00);
124+
125+
// Pre-calculate the source color multiplied by its alpha
126+
color = ((color & 0xff00ff) * sourceAlpha >> 8 & 0xff00ff) + ((color & 0xff00) * sourceAlpha >> 8 & 0xff00);
127+
128+
// For each 4-pixel chunk, blend the source color with the destination color
129+
while(--loops >= 0) {
130+
dest[destOffset++] = color + ((dest[destOffset] & 0xff00ff) * destAlpha >> 8 & 0xff00ff) + ((dest[destOffset] & 0xff00) * destAlpha >> 8 & 0xff00);
131+
dest[destOffset++] = color + ((dest[destOffset] & 0xff00ff) * destAlpha >> 8 & 0xff00ff) + ((dest[destOffset] & 0xff00) * destAlpha >> 8 & 0xff00);
132+
dest[destOffset++] = color + ((dest[destOffset] & 0xff00ff) * destAlpha >> 8 & 0xff00ff) + ((dest[destOffset] & 0xff00) * destAlpha >> 8 & 0xff00);
133+
dest[destOffset++] = color + ((dest[destOffset] & 0xff00ff) * destAlpha >> 8 & 0xff00ff) + ((dest[destOffset] & 0xff00) * destAlpha >> 8 & 0xff00);
113134
}
114-
rgba = endX - startX & 0x3;
115-
while(--rgba >= 0) {
116-
dest[destOffset++] = loops + ((dest[destOffset] & 0xff00ff) * destAlpha >> 8 & 0xff00ff) + ((dest[destOffset] & 0xff00) * destAlpha >> 8 & 0xff00);
135+
136+
// For the remaining pixels, blend them one by one
137+
loops = endX - startX & 0x3;
138+
while(--loops >= 0) {
139+
dest[destOffset++] = color + ((dest[destOffset] & 0xff00ff) * destAlpha >> 8 & 0xff00ff) + ((dest[destOffset] & 0xff00) * destAlpha >> 8 & 0xff00);
117140
}
118141
}
119142

0 commit comments

Comments
 (0)