1
1
Fork 0
mirror of https://github.com/boxgaming/qbjs.git synced 2024-09-20 04:24:45 +00:00
This commit is contained in:
boxgaming 2022-06-10 15:43:26 -05:00
commit a528e50b81

110
qb.js
View file

@ -6,40 +6,41 @@ var QB = new function() {
this.SQUAREPIXELS = Symbol("SQUAREPIXELS");
this.OFF = Symbol("OFF");
var _strokeLineThickness = 2;
var _windowDef = [];
var _windowAspect = [];
var _strokeDrawLength = null;
var _strokeDrawAngle = null;
var _strokeDrawColor = null;
var _fgColor = null;
var _activeImage = 0;
var _bgColor = null;
var _colormap = [];
var _locX = 0;
var _locY = 0;
var _lastKey = null;
var _currScreenImage = null;
var _dataBulk = [];
var _dataLabelMap;
var _fgColor = null;
var _haltedFlag = false;
var _images = {};
var _inkeyBuffer = [];
var _keyHitBuffer = [];
var _keyDownMap = {};
var _inkeymap = {};
var _inkeynp = {};
var _keyhitmap = {};
var _inputMode = false;
var _haltedFlag = false;
var _runningFlag = false;
var _images = {};
var _activeImage = 0;
var _sourceImage = 0;
var _keyDownMap = {};
var _keyHitBuffer = [];
var _keyhitmap = {};
var _lastLimitTime;
var _lastKey = null;
var _locX = 0;
var _locY = 0;
var _nextImageId = 1000;
var _lastLimitTime = new Date();
var _readCursorPosition;
var _resize = false;
var _resizeWidth = 0;
var _resizeHeight = 0;
var _currScreenImage = null;
var _dataBulk = [];
var _readCursorPosition;
var _dataLabelMap = {};
var _rndSeed = 327680;
var _rndSeed;
var _runningFlag = false;
var _screenDiagInv;
var _sourceImage = 0;
var _strokeDrawLength = null;
var _strokeDrawAngle = null;
var _strokeDrawColor = null;
var _strokeLineThickness = 2;
var _windowAspect = [];
var _windowDef = [];
// Array handling methods
// ----------------------------------------------------
@ -116,12 +117,16 @@ var QB = new function() {
};
this.start = function() {
_runningFlag = true;
_haltedFlag = false;
_nextImageId = 1000;
_activeImage = 0;
_sourceImage = 0;
_currScreenImage = null;
_dataLabelMap = {};
_haltedFlag = false;
_lastLimitTime = new Date();
_nextImageId = 1000;
_readCursorPosition = 0;
_rndSeed = 327680;
_runningFlag = true;
_sourceImage = 0;
GX._enableTouchMouse(true);
GX.registerGameEvents(function(e){});
QB.sub_Screen(0);
@ -149,12 +154,12 @@ var QB = new function() {
// --------------------------------------------
this.func__Alpha = function(rgb, imageHandle) {
// 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) {
// 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) {
@ -942,16 +947,14 @@ var QB = new function() {
vy = ux * Math.sin(ang) + uy * Math.cos(ang);
vx *= (_strokeDrawLength/4);
vy *= (_strokeDrawLength/4);
wx = vx;
wy = vy;
} else {
vx = ux;
vy = uy;
}
wx = vx;
wy = vy;
}
}
}
cursXt = ux0 + wx;
cursYt = uy0 + wy;
if (cursSkipdraw == false) {
@ -1191,6 +1194,7 @@ var QB = new function() {
screen.lastY = y;
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.beginPath();
if (aspect == undefined) {
@ -1264,9 +1268,9 @@ var QB = new function() {
}
}
if (pattern == undefined || style == "BF") {
var ctx = screen.ctx;
ctx.lineWidth = _strokeLineThickness;
if (pattern == undefined || pattern == "BF") {
if (style == "B") {
ctx.strokeStyle = color.rgba();
ctx.beginPath();
@ -1285,30 +1289,27 @@ var QB = new function() {
ctx.stroke();
}
} else { // Stylized line.
var ctx = screen.ctx;
ctx.lineWidth = _strokeLineThickness;
ctx.fillStyle = color.rgba();
ctx.beginPath();
var nullDummy;
if (style == "B") {
nullDummy = lineStyle(sx, sy, ex, sy, value);
nullDummy = lineStyle(ex, sy, ex, ey, value);
nullDummy = lineStyle(ex, ey, sx, ey, value);
nullDummy = lineStyle(sx, ey, sx, sy, value);
lineStyle(sx, sy, ex, sy, value);
lineStyle(ex, sy, ex, ey, value);
lineStyle(ex, ey, sx, ey, value);
lineStyle(sx, ey, sx, sy, value);
} else {
nullDummy = lineStyle(sx, sy, ex, ey, value);
lineStyle(sx, sy, ex, ey, value);
}
}
_strokeDrawColor = _color(color);
};
function bitTread(num, cnt) { // Bitwise treadmill left.
function bitTreadLeft(num, cnt) { // Bitwise treadmill left.
var val = (num << cnt) | (num >>> (32 - cnt));
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)
}
@ -1329,7 +1330,7 @@ var QB = new function() {
lx += 1;
while (lx -= 1) {
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); }
xtmp += mi;
y1f -= slope;
@ -1341,7 +1342,7 @@ var QB = new function() {
ly += 1;
while (ly -= 1) {
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); }
ytmp += mi;
x1f += slope;
@ -1398,6 +1399,7 @@ var QB = new function() {
};
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;
var screen = _images[_activeImage];
var ctx = screen.ctx;
@ -1473,7 +1475,7 @@ var QB = new function() {
var r = dat[p];
var g = dat[p+1];
var b = dat[p+2];
//var a = dat[p+3];
//var a = dat[p+3]; // 0 < a < 255
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 - 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) {
if (_windowAspect[0] != false) {
ret = windowUnContendY(screen.lastY, screen.canvas.height)
ret = windowUnContendY(screen.lastY, screen.canvas.height);
} else {
ret = screen.lastY;
}
@ -1777,6 +1779,8 @@ var QB = new function() {
_images[0].lastX = _images[0].canvas.width/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
_fgColor = _color(7);
_bgColor = _color(0);
@ -1794,8 +1798,6 @@ var QB = new function() {
_keyHitBuffer = [];
_keyDownMap = {};
_readCursorPosition = 0;
};
this.func_Sgn = function(value) {
@ -1934,17 +1936,17 @@ var QB = new function() {
function windowContendY(v, h) {
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 {
return h * (v - _windowDef[1]) / (_windowDef[3] - _windowDef[1]);
return 0 + h * (v - _windowDef[1]) / (_windowDef[3] - _windowDef[1]);
}
}
function windowUnContendY(v, h) {
if (_windowAspect[2] < 0) {
return _windowDef[1] - (v - h) * (_windowDef[3] - _windowDef[1]) / h;
return _windowDef[3] - (v/h) * (_windowDef[3] - _windowDef[1]);
} else {
return _windowDef[1] + v * (_windowDef[3] - _windowDef[1]) / h;
return _windowDef[1] + (v/h) * (_windowDef[3] - _windowDef[1]);
}
}