improved canvas render to context

This commit is contained in:
Djeeberjr 2023-02-04 20:41:10 +01:00
parent 8934d40c0b
commit d56b871554

View File

@ -73,21 +73,54 @@ abstract Canvas(Array<Array<Pixel>>) to Array<Array<Pixel>> from Array<Array<Pix
Renders the canvas directly to the context
**/
public function renderToContext(ctx: WindowContext){
var lastBgColor: Color = null;
var lastTextColor: Color = null;
for (lineIndex => line in this) {
if (line == null || line.length == 0) {
// Line is empty
continue;
}
ctx.setCursorPos(0, lineIndex);
var pritableLine = "";
for (pixelIndex => pixel in line) {
if (pixel == null) {
// If the pixel is null we need to skip it with setCurosrPos.
// Otherwise we will print an empty space with bg color
ctx.write(pritableLine);
pritableLine = "";
ctx.setCursorPos(pixelIndex + 1, lineIndex);
continue;
}
ctx.setCursorPos(pixelIndex, lineIndex);
if (pixel.bg != lastBgColor) {
// The background color has changed, we need to print the current line and set the new background color
ctx.write(pritableLine);
pritableLine = "";
// Set the new background color
ctx.setBackgroundColor(pixel.bg);
ctx.setTextColor(pixel.textColor);
ctx.write(pixel.char);
lastBgColor = pixel.bg;
}
// Same as above but for the text color
if (pixel.textColor != lastTextColor) {
// The text color has changed, we need to print the current line and set the new text color
ctx.write(pritableLine);
pritableLine = "";
// Set the new text color
ctx.setTextColor(pixel.textColor);
lastTextColor = pixel.textColor;
}
pritableLine += pixel.char;
}
ctx.write(pritableLine);
}
}
}