mirror of
https://github.com/boxgaming/qbjs.git
synced 2024-09-20 04:24:45 +00:00
Added Paint floodfill .
This commit is contained in:
parent
ea12bf28da
commit
db616e15db
4 changed files with 81 additions and 3 deletions
|
@ -42,7 +42,7 @@ CodeMirror.defineMode("qbjs", function(conf, parserConf) {
|
||||||
'_screenexists', '_sndclose', '_sndopen', '_sndplay', '_sndloop', '_sndpause', '_sndstop', '_sndvol',
|
'_screenexists', '_sndclose', '_sndopen', '_sndplay', '_sndloop', '_sndpause', '_sndstop', '_sndvol',
|
||||||
'_title', '_trim', '_width', 'abs', 'asc', 'atn', 'chr', 'circle', 'cls', 'color', 'command', 'cos', 'cvi', 'cvl', 'exp',
|
'_title', '_trim', '_width', 'abs', 'asc', 'atn', 'chr', 'circle', 'cls', 'color', 'command', 'cos', 'cvi', 'cvl', 'exp',
|
||||||
'fix', 'hex', 'input', 'inkey', 'instr', 'int', 'lbound', 'left', 'lcase', 'len', 'line', 'locate', 'log', 'ltrim', 'mid', 'mki', 'mkl',
|
'fix', 'hex', 'input', 'inkey', 'instr', 'int', 'lbound', 'left', 'lcase', 'len', 'line', 'locate', 'log', 'ltrim', 'mid', 'mki', 'mkl',
|
||||||
'oct', 'point', 'preset', 'print', 'pset', 'right', 'rtrim', 'rnd', 'screen', 'shared', 'sgn', 'sin', 'sleep', 'space', 'sqr', 'str', 'swap', 'tan',
|
'oct', 'paint', 'point', 'preset', 'print', 'pset', 'right', 'rtrim', 'rnd', 'screen', 'shared', 'sgn', 'sin', 'sleep', 'space', 'sqr', 'str', 'swap', 'tan',
|
||||||
'timer', 'ubound', 'ucase', 'val',
|
'timer', 'ubound', 'ucase', 'val',
|
||||||
// QBJS-specific
|
// QBJS-specific
|
||||||
'alert', 'confirm', 'domadd', 'domcontainer', 'domcreate', 'domevent','domget', 'domgetimage', 'domremove', 'export',
|
'alert', 'confirm', 'domadd', 'domcontainer', 'domcreate', 'domevent','domget', 'domgetimage', 'domremove', 'export',
|
||||||
|
|
76
qb.js
76
qb.js
|
@ -858,6 +858,82 @@ var QB = new function() {
|
||||||
return n.toString(8).toUpperCase();
|
return n.toString(8).toUpperCase();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.sub_Paint = function(sstep, startX, startY, fillColor, borderColor) {
|
||||||
|
var screen = _images[_activeImage];
|
||||||
|
var ctx = screen.ctx;
|
||||||
|
var data = ctx.getImageData(0, 0, screen.canvas.width, screen.canvas.height).data;
|
||||||
|
ctx.beginPath();
|
||||||
|
|
||||||
|
var x0 = startX;
|
||||||
|
var y0 = startY;
|
||||||
|
|
||||||
|
if (sstep) {
|
||||||
|
x0 = screen.lastX + x0;
|
||||||
|
y0 = screen.lastY + y0;
|
||||||
|
}
|
||||||
|
|
||||||
|
var pixelStack = [[Math.floor(x0), Math.floor(y0)]];
|
||||||
|
|
||||||
|
while (pixelStack.length) {
|
||||||
|
var newPos, x, y, pixelIndex, flagLeft, flagRight;
|
||||||
|
newPos = pixelStack.pop();
|
||||||
|
x = newPos[0];
|
||||||
|
y = newPos[1];
|
||||||
|
pixelIndex = (y * screen.canvas.width + x) * 4;
|
||||||
|
|
||||||
|
while ((y-- >= 0) && checkPixel(data, pixelIndex, fillColor, borderColor)) {
|
||||||
|
pixelIndex -= screen.canvas.width * 4;
|
||||||
|
}
|
||||||
|
pixelIndex += screen.canvas.width * 4;
|
||||||
|
++y;
|
||||||
|
|
||||||
|
flagLeft = false;
|
||||||
|
flagRight = false;
|
||||||
|
while ((y++ < screen.canvas.height - 1) && checkPixel(data, pixelIndex, fillColor, borderColor)) {
|
||||||
|
ctx.fillStyle = fillColor.rgba();
|
||||||
|
ctx.fillRect(x, y, 1, 1);
|
||||||
|
data[pixelIndex] = fillColor.r;
|
||||||
|
data[pixelIndex+1] = fillColor.g;
|
||||||
|
data[pixelIndex+2] = fillColor.b;
|
||||||
|
data[pixelIndex+3] = fillColor.a;
|
||||||
|
|
||||||
|
if (x > 0) {
|
||||||
|
if (checkPixel(data, pixelIndex - 4, fillColor, borderColor)) {
|
||||||
|
if (!flagLeft) {
|
||||||
|
pixelStack.push([x - 1, y]);
|
||||||
|
flagLeft = true;
|
||||||
|
}
|
||||||
|
} else if (flagLeft) {
|
||||||
|
flagLeft = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x < screen.canvas.width - 1) {
|
||||||
|
if (checkPixel(data, pixelIndex + 4, fillColor, borderColor)) {
|
||||||
|
if (!flagRight) {
|
||||||
|
pixelStack.push([x + 1, y]);
|
||||||
|
flagRight = true;
|
||||||
|
}
|
||||||
|
} else if (flagRight) {
|
||||||
|
flagRight = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pixelIndex += screen.canvas.width * 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function checkPixel(dat, p, c1, c2) {
|
||||||
|
var r = dat[p];
|
||||||
|
var g = dat[p+1];
|
||||||
|
var b = dat[p+2];
|
||||||
|
//var a = dat[p+3];
|
||||||
|
if ((r == c1.r) && (g == c1.g) && (b == c1.b)) { return false; }
|
||||||
|
if ((r == c2.r) && (g == c2.g) && (b == c2.b)) { return false; }
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
this.func_Point = function(x, y) {
|
this.func_Point = function(x, y) {
|
||||||
var screen = _images[_activeImage];
|
var screen = _images[_activeImage];
|
||||||
var ret = 0;
|
var ret = 0;
|
||||||
|
|
3
qb2js.js
3
qb2js.js
|
@ -469,7 +469,7 @@ var ConvertSub = null;
|
||||||
}
|
}
|
||||||
if ( m.name == "Line" ) {
|
if ( m.name == "Line" ) {
|
||||||
js = (await func_CallMethod( m)) +"(" +(await func_ConvertLine( args)) +");";
|
js = (await func_CallMethod( m)) +"(" +(await func_ConvertLine( args)) +");";
|
||||||
} else if ( m.name == "PSet" || m.name == "Circle" || m.name == "PReset" ) {
|
} else if ( m.name == "PSet" || m.name == "Circle" || m.name == "PReset" || m.name == "Paint" ) {
|
||||||
js = (await func_CallMethod( m)) +"(" +(await func_ConvertPSet( args)) +");";
|
js = (await func_CallMethod( m)) +"(" +(await func_ConvertPSet( args)) +");";
|
||||||
} else if ( m.name == "_PrintString" ) {
|
} else if ( m.name == "_PrintString" ) {
|
||||||
js = (await func_CallMethod( m)) +"(" +(await func_ConvertPrintString( args)) +");";
|
js = (await func_CallMethod( m)) +"(" +(await func_ConvertPrintString( args)) +");";
|
||||||
|
@ -2532,6 +2532,7 @@ if (QB.halted()) { return; }
|
||||||
await sub_AddQBMethod( "FUNCTION", "Mki$", False);
|
await sub_AddQBMethod( "FUNCTION", "Mki$", False);
|
||||||
await sub_AddQBMethod( "FUNCTION", "Mkl$", False);
|
await sub_AddQBMethod( "FUNCTION", "Mkl$", False);
|
||||||
await sub_AddQBMethod( "FUNCTION", "Oct$", False);
|
await sub_AddQBMethod( "FUNCTION", "Oct$", False);
|
||||||
|
await sub_AddQBMethod( "SUB", "Paint", False);
|
||||||
await sub_AddQBMethod( "FUNCTION", "Point", False);
|
await sub_AddQBMethod( "FUNCTION", "Point", False);
|
||||||
await sub_AddQBMethod( "SUB", "PReset", False);
|
await sub_AddQBMethod( "SUB", "PReset", False);
|
||||||
await sub_AddQBMethod( "SUB", "Print", True);
|
await sub_AddQBMethod( "SUB", "Print", True);
|
||||||
|
|
|
@ -546,7 +546,7 @@ Function ConvertSub$ (m As Method, args As String)
|
||||||
If m.name = "Line" Then
|
If m.name = "Line" Then
|
||||||
js = CallMethod(m) + "(" + ConvertLine(args) + ");"
|
js = CallMethod(m) + "(" + ConvertLine(args) + ");"
|
||||||
|
|
||||||
ElseIf m.name = "PSet" Or m.name = "Circle" Or m.name = "PReset" Then
|
ElseIf m.name = "PSet" Or m.name = "Circle" Or m.name = "PReset" Or m.name = "Paint" Then
|
||||||
js = CallMethod(m) + "(" + ConvertPSet(args) + ");"
|
js = CallMethod(m) + "(" + ConvertPSet(args) + ");"
|
||||||
|
|
||||||
ElseIf m.name = "_PrintString" Then
|
ElseIf m.name = "_PrintString" Then
|
||||||
|
@ -2674,6 +2674,7 @@ Sub InitQBMethods
|
||||||
AddQBMethod "FUNCTION", "Mki$", False
|
AddQBMethod "FUNCTION", "Mki$", False
|
||||||
AddQBMethod "FUNCTION", "Mkl$", False
|
AddQBMethod "FUNCTION", "Mkl$", False
|
||||||
AddQBMethod "FUNCTION", "Oct$", False
|
AddQBMethod "FUNCTION", "Oct$", False
|
||||||
|
AddQBMethod "SUB", "Paint", False
|
||||||
AddQBMethod "FUNCTION", "Point", False
|
AddQBMethod "FUNCTION", "Point", False
|
||||||
AddQBMethod "SUB", "PReset", False
|
AddQBMethod "SUB", "PReset", False
|
||||||
AddQBMethod "SUB", "Print", True
|
AddQBMethod "SUB", "Print", True
|
||||||
|
|
Loading…
Reference in a new issue