mirror of
https://github.com/boxgaming/qbjs.git
synced 2024-09-20 04:24:45 +00:00
Merge branch 'main' of https://github.com/boxgaming/qbjs
This commit is contained in:
commit
a528e50b81
1 changed files with 60 additions and 58 deletions
116
qb.js
116
qb.js
|
@ -6,40 +6,41 @@ var QB = new function() {
|
||||||
this.SQUAREPIXELS = Symbol("SQUAREPIXELS");
|
this.SQUAREPIXELS = Symbol("SQUAREPIXELS");
|
||||||
this.OFF = Symbol("OFF");
|
this.OFF = Symbol("OFF");
|
||||||
|
|
||||||
var _strokeLineThickness = 2;
|
var _activeImage = 0;
|
||||||
var _windowDef = [];
|
|
||||||
var _windowAspect = [];
|
|
||||||
var _strokeDrawLength = null;
|
|
||||||
var _strokeDrawAngle = null;
|
|
||||||
var _strokeDrawColor = null;
|
|
||||||
var _fgColor = null;
|
|
||||||
var _bgColor = null;
|
var _bgColor = null;
|
||||||
var _colormap = [];
|
var _colormap = [];
|
||||||
var _locX = 0;
|
var _currScreenImage = null;
|
||||||
var _locY = 0;
|
var _dataBulk = [];
|
||||||
var _lastKey = null;
|
var _dataLabelMap;
|
||||||
|
var _fgColor = null;
|
||||||
|
var _haltedFlag = false;
|
||||||
|
var _images = {};
|
||||||
var _inkeyBuffer = [];
|
var _inkeyBuffer = [];
|
||||||
var _keyHitBuffer = [];
|
|
||||||
var _keyDownMap = {};
|
|
||||||
var _inkeymap = {};
|
var _inkeymap = {};
|
||||||
var _inkeynp = {};
|
var _inkeynp = {};
|
||||||
var _keyhitmap = {};
|
|
||||||
var _inputMode = false;
|
var _inputMode = false;
|
||||||
var _haltedFlag = false;
|
var _keyDownMap = {};
|
||||||
var _runningFlag = false;
|
var _keyHitBuffer = [];
|
||||||
var _images = {};
|
var _keyhitmap = {};
|
||||||
var _activeImage = 0;
|
var _lastLimitTime;
|
||||||
var _sourceImage = 0;
|
var _lastKey = null;
|
||||||
|
var _locX = 0;
|
||||||
|
var _locY = 0;
|
||||||
var _nextImageId = 1000;
|
var _nextImageId = 1000;
|
||||||
var _lastLimitTime = new Date();
|
var _readCursorPosition;
|
||||||
var _resize = false;
|
var _resize = false;
|
||||||
var _resizeWidth = 0;
|
var _resizeWidth = 0;
|
||||||
var _resizeHeight = 0;
|
var _resizeHeight = 0;
|
||||||
var _currScreenImage = null;
|
var _rndSeed;
|
||||||
var _dataBulk = [];
|
var _runningFlag = false;
|
||||||
var _readCursorPosition;
|
var _screenDiagInv;
|
||||||
var _dataLabelMap = {};
|
var _sourceImage = 0;
|
||||||
var _rndSeed = 327680;
|
var _strokeDrawLength = null;
|
||||||
|
var _strokeDrawAngle = null;
|
||||||
|
var _strokeDrawColor = null;
|
||||||
|
var _strokeLineThickness = 2;
|
||||||
|
var _windowAspect = [];
|
||||||
|
var _windowDef = [];
|
||||||
|
|
||||||
// Array handling methods
|
// Array handling methods
|
||||||
// ----------------------------------------------------
|
// ----------------------------------------------------
|
||||||
|
@ -116,12 +117,16 @@ var QB = new function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.start = function() {
|
this.start = function() {
|
||||||
_runningFlag = true;
|
|
||||||
_haltedFlag = false;
|
|
||||||
_nextImageId = 1000;
|
|
||||||
_activeImage = 0;
|
_activeImage = 0;
|
||||||
_sourceImage = 0;
|
|
||||||
_currScreenImage = null;
|
_currScreenImage = null;
|
||||||
|
_dataLabelMap = {};
|
||||||
|
_haltedFlag = false;
|
||||||
|
_lastLimitTime = new Date();
|
||||||
|
_nextImageId = 1000;
|
||||||
|
_readCursorPosition = 0;
|
||||||
|
_rndSeed = 327680;
|
||||||
|
_runningFlag = true;
|
||||||
|
_sourceImage = 0;
|
||||||
GX._enableTouchMouse(true);
|
GX._enableTouchMouse(true);
|
||||||
GX.registerGameEvents(function(e){});
|
GX.registerGameEvents(function(e){});
|
||||||
QB.sub_Screen(0);
|
QB.sub_Screen(0);
|
||||||
|
@ -149,12 +154,12 @@ var QB = new function() {
|
||||||
// --------------------------------------------
|
// --------------------------------------------
|
||||||
this.func__Alpha = function(rgb, imageHandle) {
|
this.func__Alpha = function(rgb, imageHandle) {
|
||||||
// TODO: implement corresponding logic when an image handle is supplied (maybe)
|
// TODO: implement corresponding logic when an image handle is supplied (maybe)
|
||||||
return _color(rgb).a * 255;
|
return _color(rgb).a;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.func__Alpha32 = function(rgb) {
|
this.func__Alpha32 = function(rgb) {
|
||||||
// TODO: implement corresponding logic when an image handle is supplied (maybe)
|
// TODO: implement corresponding logic when an image handle is supplied (maybe)
|
||||||
return _color(rgb).a * 255;
|
return _color(rgb).a;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.func__Atan2 = function(y, x) {
|
this.func__Atan2 = function(y, x) {
|
||||||
|
@ -942,14 +947,12 @@ var QB = new function() {
|
||||||
vy = ux * Math.sin(ang) + uy * Math.cos(ang);
|
vy = ux * Math.sin(ang) + uy * Math.cos(ang);
|
||||||
vx *= (_strokeDrawLength/4);
|
vx *= (_strokeDrawLength/4);
|
||||||
vy *= (_strokeDrawLength/4);
|
vy *= (_strokeDrawLength/4);
|
||||||
wx = vx;
|
|
||||||
wy = vy;
|
|
||||||
} else {
|
} else {
|
||||||
vx = ux;
|
vx = ux;
|
||||||
vy = uy;
|
vy = uy;
|
||||||
wx = vx;
|
|
||||||
wy = vy;
|
|
||||||
}
|
}
|
||||||
|
wx = vx;
|
||||||
|
wy = vy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cursXt = ux0 + wx;
|
cursXt = ux0 + wx;
|
||||||
|
@ -1191,6 +1194,7 @@ var QB = new function() {
|
||||||
screen.lastY = y;
|
screen.lastY = y;
|
||||||
|
|
||||||
ctx.lineWidth = _strokeLineThickness;
|
ctx.lineWidth = _strokeLineThickness;
|
||||||
|
ctx.lineWidth += Math.tanh(8*radius*_screenDiagInv); // Adds some radius to compensate for antialiasing. The prefactor is arbitrary. //
|
||||||
ctx.strokeStyle = color.rgba();
|
ctx.strokeStyle = color.rgba();
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
if (aspect == undefined) {
|
if (aspect == undefined) {
|
||||||
|
@ -1264,9 +1268,9 @@ var QB = new function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pattern == undefined || style == "BF") {
|
var ctx = screen.ctx;
|
||||||
var ctx = screen.ctx;
|
ctx.lineWidth = _strokeLineThickness;
|
||||||
ctx.lineWidth = _strokeLineThickness;
|
if (pattern == undefined || pattern == "BF") {
|
||||||
if (style == "B") {
|
if (style == "B") {
|
||||||
ctx.strokeStyle = color.rgba();
|
ctx.strokeStyle = color.rgba();
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
|
@ -1285,30 +1289,27 @@ var QB = new function() {
|
||||||
ctx.stroke();
|
ctx.stroke();
|
||||||
}
|
}
|
||||||
} else { // Stylized line.
|
} else { // Stylized line.
|
||||||
var ctx = screen.ctx;
|
|
||||||
ctx.lineWidth = _strokeLineThickness;
|
|
||||||
ctx.fillStyle = color.rgba();
|
ctx.fillStyle = color.rgba();
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
var nullDummy;
|
|
||||||
if (style == "B") {
|
if (style == "B") {
|
||||||
nullDummy = lineStyle(sx, sy, ex, sy, value);
|
lineStyle(sx, sy, ex, sy, value);
|
||||||
nullDummy = lineStyle(ex, sy, ex, ey, value);
|
lineStyle(ex, sy, ex, ey, value);
|
||||||
nullDummy = lineStyle(ex, ey, sx, ey, value);
|
lineStyle(ex, ey, sx, ey, value);
|
||||||
nullDummy = lineStyle(sx, ey, sx, sy, value);
|
lineStyle(sx, ey, sx, sy, value);
|
||||||
} else {
|
} else {
|
||||||
nullDummy = lineStyle(sx, sy, ex, ey, value);
|
lineStyle(sx, sy, ex, ey, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_strokeDrawColor = _color(color);
|
_strokeDrawColor = _color(color);
|
||||||
};
|
};
|
||||||
|
|
||||||
function bitTread(num, cnt) { // Bitwise treadmill left.
|
function bitTreadLeft(num, cnt) { // Bitwise treadmill left.
|
||||||
var val = (num << cnt) | (num >>> (32 - cnt));
|
var val = (num << cnt) | (num >>> (32 - cnt));
|
||||||
return (val | (~~bitTest(num, 15))<<0);
|
return (val | (~~bitTest(num, 15))<<0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function bitTest(num, bit) { // true or false
|
function bitTest(num, bit) { // Returns true or false.
|
||||||
return ((num>>bit) % 2 != 0)
|
return ((num>>bit) % 2 != 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1329,7 +1330,7 @@ var QB = new function() {
|
||||||
lx += 1;
|
lx += 1;
|
||||||
while (lx -= 1) {
|
while (lx -= 1) {
|
||||||
if (y1f < 0) {ytmp = y1f - 0.5; } else { ytmp = y1f + 0.5; }
|
if (y1f < 0) {ytmp = y1f - 0.5; } else { ytmp = y1f + 0.5; }
|
||||||
ptn = bitTread(ptn, 1);
|
ptn = bitTreadLeft(ptn, 1);
|
||||||
if (bitTest(ptn, 0) == true) { ctx.fillRect(xtmp, ytmp, 1, 1); }
|
if (bitTest(ptn, 0) == true) { ctx.fillRect(xtmp, ytmp, 1, 1); }
|
||||||
xtmp += mi;
|
xtmp += mi;
|
||||||
y1f -= slope;
|
y1f -= slope;
|
||||||
|
@ -1341,7 +1342,7 @@ var QB = new function() {
|
||||||
ly += 1;
|
ly += 1;
|
||||||
while (ly -= 1) {
|
while (ly -= 1) {
|
||||||
if (x1f < 0) {xtmp = x1f - 0.5; } else { xtmp = x1f + 0.5; }
|
if (x1f < 0) {xtmp = x1f - 0.5; } else { xtmp = x1f + 0.5; }
|
||||||
ptn = bitTread(ptn, 1);
|
ptn = bitTreadLeft(ptn, 1);
|
||||||
if (bitTest(ptn, 0) == true) { ctx.fillRect(xtmp, ytmp, 1, 1); }
|
if (bitTest(ptn, 0) == true) { ctx.fillRect(xtmp, ytmp, 1, 1); }
|
||||||
ytmp += mi;
|
ytmp += mi;
|
||||||
x1f += slope;
|
x1f += slope;
|
||||||
|
@ -1398,6 +1399,7 @@ var QB = new function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.sub_Paint = function(sstep, startX, startY, fillColor, borderColor) {
|
this.sub_Paint = function(sstep, startX, startY, fillColor, borderColor) {
|
||||||
|
// See: http://www.williammalone.com/articles/html5-canvas-javascript-paint-bucket-tool/
|
||||||
_images[_activeImage].dirty = true;
|
_images[_activeImage].dirty = true;
|
||||||
var screen = _images[_activeImage];
|
var screen = _images[_activeImage];
|
||||||
var ctx = screen.ctx;
|
var ctx = screen.ctx;
|
||||||
|
@ -1473,7 +1475,7 @@ var QB = new function() {
|
||||||
var r = dat[p];
|
var r = dat[p];
|
||||||
var g = dat[p+1];
|
var g = dat[p+1];
|
||||||
var b = dat[p+2];
|
var b = dat[p+2];
|
||||||
//var a = dat[p+3];
|
//var a = dat[p+3]; // 0 < a < 255
|
||||||
var thresh = 2;
|
var thresh = 2;
|
||||||
if ((Math.abs(r - c1.r) < thresh) && (Math.abs(g - c1.g) < thresh) && (Math.abs(b - c1.b) < thresh)) { return false; }
|
if ((Math.abs(r - c1.r) < thresh) && (Math.abs(g - c1.g) < thresh) && (Math.abs(b - c1.b) < thresh)) { return false; }
|
||||||
if ((Math.abs(r - c2.r) < thresh) && (Math.abs(g - c2.g) < thresh) && (Math.abs(b - c2.b) < thresh)) { return false; }
|
if ((Math.abs(r - c2.r) < thresh) && (Math.abs(g - c2.g) < thresh) && (Math.abs(b - c2.b) < thresh)) { return false; }
|
||||||
|
@ -1496,7 +1498,7 @@ var QB = new function() {
|
||||||
}
|
}
|
||||||
} else if (x == 3) {
|
} else if (x == 3) {
|
||||||
if (_windowAspect[0] != false) {
|
if (_windowAspect[0] != false) {
|
||||||
ret = windowUnContendY(screen.lastY, screen.canvas.height)
|
ret = windowUnContendY(screen.lastY, screen.canvas.height);
|
||||||
} else {
|
} else {
|
||||||
ret = screen.lastY;
|
ret = screen.lastY;
|
||||||
}
|
}
|
||||||
|
@ -1777,6 +1779,8 @@ var QB = new function() {
|
||||||
_images[0].lastX = _images[0].canvas.width/2;
|
_images[0].lastX = _images[0].canvas.width/2;
|
||||||
_images[0].lastY = _images[0].canvas.height/2;
|
_images[0].lastY = _images[0].canvas.height/2;
|
||||||
|
|
||||||
|
_screenDiagInv = 1/Math.sqrt(_images[0].canvas.width*_images[0].canvas.width + _images[0].canvas.height*_images[0].canvas.height);
|
||||||
|
|
||||||
// initialize the graphics
|
// initialize the graphics
|
||||||
_fgColor = _color(7);
|
_fgColor = _color(7);
|
||||||
_bgColor = _color(0);
|
_bgColor = _color(0);
|
||||||
|
@ -1794,8 +1798,6 @@ var QB = new function() {
|
||||||
_keyHitBuffer = [];
|
_keyHitBuffer = [];
|
||||||
_keyDownMap = {};
|
_keyDownMap = {};
|
||||||
|
|
||||||
_readCursorPosition = 0;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.func_Sgn = function(value) {
|
this.func_Sgn = function(value) {
|
||||||
|
@ -1934,17 +1936,17 @@ var QB = new function() {
|
||||||
|
|
||||||
function windowContendY(v, h) {
|
function windowContendY(v, h) {
|
||||||
if (_windowAspect[2] < 0) {
|
if (_windowAspect[2] < 0) {
|
||||||
return h * (1 - (v - _windowDef[1]) / (_windowDef[3] - _windowDef[1]));
|
return h - h * (v - _windowDef[1]) / (_windowDef[3] - _windowDef[1]);
|
||||||
} else {
|
} else {
|
||||||
return h * (v - _windowDef[1]) / (_windowDef[3] - _windowDef[1]);
|
return 0 + h * (v - _windowDef[1]) / (_windowDef[3] - _windowDef[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function windowUnContendY(v, h) {
|
function windowUnContendY(v, h) {
|
||||||
if (_windowAspect[2] < 0) {
|
if (_windowAspect[2] < 0) {
|
||||||
return _windowDef[1] - (v - h) * (_windowDef[3] - _windowDef[1]) / h;
|
return _windowDef[3] - (v/h) * (_windowDef[3] - _windowDef[1]);
|
||||||
} else {
|
} else {
|
||||||
return _windowDef[1] + v * (_windowDef[3] - _windowDef[1]) / h;
|
return _windowDef[1] + (v/h) * (_windowDef[3] - _windowDef[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue