Skip to content

Commit bbf8128

Browse files
committed
MASK fixed; sound and other events during waitKey and waitInput; polyfill for Math.trunc
1 parent 0134ae8 commit bbf8128

12 files changed

+194
-215
lines changed

Canvas.js

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Canvas.prototype = {
114114
this.bClipped = false;
115115

116116
this.iMask = 255;
117-
this.iMaskBitPos = 1;
117+
this.iMaskBit = 128;
118118
this.iMaskFirst = 1;
119119

120120
canvas = document.getElementById("cpcCanvas");
@@ -750,12 +750,11 @@ Canvas.prototype = {
750750
iGPen = this.iGPen,
751751
iGPaper = this.iGPaper,
752752
iMask = this.iMask,
753-
iMaskBitPos = this.iMaskBitPos,
753+
iMaskBit = this.iMaskBit,
754754
iMaskFirst = this.iMaskFirst,
755755
iGColMode = this.iGColMode,
756756
x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, deltaslowdirection, deltafastdirection, err, iBit;
757757

758-
759758
// we have to add origin before modifying coordinates to match CPC pixel
760759
xstart += this.xOrig;
761760
ystart = this.iHeight - 1 - (ystart + this.yOrig);
@@ -808,10 +807,37 @@ Canvas.prototype = {
808807
y = ystart;
809808
err = deltafastdirection >> 1; // eslint-disable-line no-bitwise
810809

811-
iBit = iMask & iMaskBitPos; // eslint-disable-line no-bitwise
812-
// rotate bitpos left
813-
iMaskBitPos = ((iMaskBitPos << 1) & 0xff) | (iMaskBitPos >> (8 - 1)); // eslint-disable-line no-bitwise
810+
/*
811+
if (iMaskFirst) { // draw first pixel?
812+
// rotate bitpos left
813+
iMaskBit = ((iMaskBit << 1) & 0xff) | (iMaskBit >> (8 - 1)); // eslint-disable-line no-bitwise
814+
}
815+
iBit = iMask & iMaskBit; // eslint-disable-line no-bitwise
816+
this.setPixelOriginIncluded(x, y, iBit ? iGPen : iGPaper, iGColMode); // we expect integers
817+
*/
818+
819+
/*
820+
if (iMaskFirst) { // draw first pixel?
821+
iBit = iMask & iMaskBit; // eslint-disable-line no-bitwise
822+
this.setPixelOriginIncluded(x, y, iMaskFirst && iBit ? iGPen : iGPaper, iGColMode); // we expect integers
823+
// rotate bitpos right
824+
iMaskBit = (iMaskBit >> 1) | ((iMaskBit << 7) & 0xff); // eslint-disable-line no-bitwise
825+
}
826+
*/
827+
828+
/*
829+
iBit = iMask & iMaskBit; // eslint-disable-line no-bitwise
814830
this.setPixelOriginIncluded(x, y, iMaskFirst && iBit ? iGPen : iGPaper, iGColMode); // we expect integers
831+
// rotate bitpos right
832+
iMaskBit = (iMaskBit >> 1) | ((iMaskBit << 7) & 0xff); // eslint-disable-line no-bitwise
833+
*/
834+
835+
if (iMaskFirst) { // draw first pixel?
836+
iBit = iMask & iMaskBit; // eslint-disable-line no-bitwise
837+
this.setPixelOriginIncluded(x, y, iMaskFirst && iBit ? iGPen : iGPaper, iGColMode); // we expect integers
838+
// rotate bitpos right
839+
iMaskBit = (iMaskBit >> 1) | ((iMaskBit << 7) & 0xff); // eslint-disable-line no-bitwise
840+
}
815841

816842
for (t = 0; t < deltafastdirection; t += 1) {
817843
err -= deltaslowdirection;
@@ -824,17 +850,12 @@ Canvas.prototype = {
824850
y += pdy;
825851
}
826852

827-
iBit = iMask & iMaskBitPos; // eslint-disable-line no-bitwise
828-
iMaskBitPos = ((iMaskBitPos << 1) & 0xff) | (iMaskBitPos >> (8 - 1)); // eslint-disable-line no-bitwise
829-
/*
830-
iMaskBitPos *= 2;
831-
if (iMaskBitPos > 256) {
832-
iMaskBitPos = 1;
833-
}
834-
*/
835-
853+
iBit = iMask & iMaskBit; // eslint-disable-line no-bitwise
836854
this.setPixelOriginIncluded(x, y, iBit ? iGPen : iGPaper, iGColMode); // we expect integers
855+
// rotate bitpos right
856+
iMaskBit = (iMaskBit >> 1) | ((iMaskBit << 7) & 0xff); // eslint-disable-line no-bitwise
837857
}
858+
this.iMaskBit = iMaskBit;
838859
},
839860

840861
draw: function (x, y) {
@@ -1099,7 +1120,7 @@ Canvas.prototype = {
10991120

11001121

11011122
// idea from: https://simpledevcode.wordpress.com/2015/12/29/flood-fill-algorithm-using-c-net/
1102-
fill: function (iFill) {
1123+
fill: function (iFillPen) {
11031124
var that = this,
11041125
xPos = this.xPos,
11051126
yPos = this.yPos,
@@ -1109,13 +1130,13 @@ Canvas.prototype = {
11091130
aPixels = [],
11101131
oPixel, x1, y1, bSpanLeft, bSpanRight, p1, p2, p3,
11111132
fnIsStopPen = function (p) {
1112-
return p === iFill || p === iGPen;
1133+
return p === iFillPen || p === iGPen;
11131134
},
11141135
fnIsNotInWindow = function (x, y) {
11151136
return (x < that.xLeft || x > that.xRight || y < (that.iHeight - 1 - that.yTop) || y > (that.iHeight - 1 - that.yBottom));
11161137
};
11171138

1118-
iFill %= this.oModeData.iPens; // limit pens
1139+
iFillPen %= this.oModeData.iPens; // limit pens
11191140

11201141
// apply origin
11211142
xPos += this.xOrig;
@@ -1144,7 +1165,7 @@ Canvas.prototype = {
11441165
bSpanRight = false;
11451166
p1 = this.testSubPixel(oPixel.x, y1);
11461167
while (y1 <= (that.iHeight - 1 - that.yBottom) && !fnIsStopPen(p1)) {
1147-
this.setSubPixels(oPixel.x, y1, iFill, 0);
1168+
this.setSubPixels(oPixel.x, y1, iFillPen, 0);
11481169

11491170
x1 = oPixel.x - iPixelWidth;
11501171
p2 = this.testSubPixel(x1, y1);
@@ -1244,8 +1265,10 @@ Canvas.prototype = {
12441265

12451266
clearTextWindow: function (iLeft, iRight, iTop, iBottom, iPaper) { // clear current text window
12461267
var iWidth = iRight + 1 - iLeft,
1247-
iHeight = iBottom + 1 - iTop;
1268+
iHeight = iBottom + 1 - iTop,
1269+
iPens = this.oModeData.iPens;
12481270

1271+
iPaper %= iPens; // limit papers
12491272
this.fillTextBox(iLeft, iTop, iWidth, iHeight, iPaper);
12501273
},
12511274

@@ -1305,7 +1328,7 @@ Canvas.prototype = {
13051328

13061329
setMask: function (iMask) { // set line mask
13071330
this.iMask = iMask;
1308-
this.iMaskBitPos = 1;
1331+
this.iMaskBit = 128;
13091332
},
13101333

13111334
setMaskFirst: function (iMaskFirst) { // set first dot for line mask

CodeGeneratorJs.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -683,25 +683,6 @@ CodeGeneratorJs.prototype = {
683683
node.pv = this.fnParseDefIntRealStr(node);
684684
return node.pv;
685685
},
686-
/*
687-
dim_ttt: function (node) {
688-
var aNodeArgs = fnParseArgs(node.args),
689-
i, sName, aName, sStringType;
690-
691-
for (i = 0; i < aNodeArgs.length; i += 1) {
692-
sName = aNodeArgs[i];
693-
sName += " "; // for buggy IE8 split: Otherwise it won't return last empty element in split
694-
aName = sName.split(/\[|\]\[|\]/); // split in variable and dimension(s)
695-
aName.pop(); // remove empty last element
696-
sName = aName.shift();
697-
sStringType = (sName.indexOf("$") > -1) ? "$" : "";
698-
aNodeArgs[i] = sName + " = o.dim(\"" + sStringType + "\", " + aName.join(", ") + ")";
699-
// TTT not needed to assign to variable here!
700-
}
701-
node.pv = aNodeArgs.join("; ");
702-
return node.pv;
703-
},
704-
*/
705686
dim: function (node) {
706687
var aNodeArgs = fnParseArgs(node.args),
707688
i, sName, sName2, aName;
@@ -713,9 +694,7 @@ CodeGeneratorJs.prototype = {
713694
aName.pop(); // remove empty last element
714695
sName = aName.shift();
715696
sName2 = sName.substr(2); // remove preceding "v."
716-
//sStringType = (sName.indexOf("$") > -1) ? "$" : "";
717697
aNodeArgs[i] = "/* " + sName + " = */ o.dim(\"" + sName2 + "\", " + aName.join(", ") + ")";
718-
// TTT not needed to assign to variable here!
719698
}
720699
node.pv = aNodeArgs.join("; ");
721700
return node.pv;

Controller.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Controller.prototype = {
4444
this.fnDirectInputHandler = this.fnDirectInput.bind(this);
4545
this.fnPutKeyInBufferHandler = this.fnPutKeyInBuffer.bind(this);
4646

47-
this.sMetaIdent = "CPCBasic"; //TTT
47+
this.sMetaIdent = "CPCBasic";
4848

4949
this.fnScript = null;
5050

@@ -98,8 +98,7 @@ Controller.prototype = {
9898
keyboard: this.oKeyboard,
9999
sound: this.oSound,
100100
variables: this.oVariables,
101-
tron: oModel.getProperty("tron"),
102-
onCharReturn: this.fnPutKeyInBufferHandler
101+
tron: oModel.getProperty("tron")
103102
});
104103
this.oVm.vmReset();
105104

@@ -372,18 +371,39 @@ Controller.prototype = {
372371
this.startMainLoop();
373372
},
374373

374+
fnWaitSound: function () { // rather fnEvent
375+
var oStop = this.oVm.vmGetStopObject(),
376+
aSoundData;
377+
378+
this.oVm.vmLoopCondition(); // update iNextFrameTime, timers, inks; schedule sound: free queue
379+
if (this.oSound.isActivatedByUser()) { // only if activated
380+
aSoundData = this.oVm.vmGetSoundData();
381+
while (aSoundData.length && this.oSound.testCanQueue(aSoundData[0].iState)) {
382+
this.oSound.sound(aSoundData.shift());
383+
}
384+
if (!aSoundData.length) {
385+
if (oStop.sReason === "waitSound") { // only for this reason
386+
this.oVm.vmStop("", 0, true); // no more wait
387+
}
388+
}
389+
}
390+
this.iNextLoopTimeOut = this.oVm.vmGetTimeUntilFrame(); // wait until next frame
391+
},
392+
375393
fnWaitKey: function () {
376394
var sKey;
377395

378396
sKey = this.oKeyboard.getKeyFromBuffer();
379-
if (sKey !== "") {
397+
if (sKey !== "") { // do we have a key from the buffer already?
380398
Utils.console.log("Wait for key:", sKey);
381399
this.oVm.vmStop("", 0, true);
382400
this.oKeyboard.setKeyDownHandler(null);
383-
this.startMainLoop();
401+
//this.startMainLoop();
384402
} else {
403+
this.fnWaitSound(); // sound and blinking events
385404
this.oKeyboard.setKeyDownHandler(this.fnWaitKeyHandler); // wait until keypress handler (for call &bb18)
386405
}
406+
return sKey;
387407
},
388408

389409
fnWaitInput: function () { // eslint-disable-line complexity
@@ -460,7 +480,7 @@ Controller.prototype = {
460480

461481
oInput.sInput = sInput;
462482
if (sKey === "\r") {
463-
Utils.console.log("fnWaitInput:", sInput);
483+
Utils.console.log("fnWaitInput:", sInput, "reason", oStop.sReason);
464484
if (!oInput.sNoCRLF) {
465485
this.oVm.print(iStream, "\r\n");
466486
}
@@ -471,29 +491,20 @@ Controller.prototype = {
471491
}
472492
if (bInputOk) {
473493
this.oKeyboard.setKeyDownHandler(null);
474-
this.startContinue();
494+
if (oStop.sReason === "waitInput") { // only for this reason
495+
this.oVm.vmStop("", 0, true); // no more wait
496+
} else {
497+
this.startContinue(); //TTT
498+
}
475499
}
476500
}
477501

478502
if (!bInputOk) {
479-
this.oKeyboard.setKeyDownHandler(this.fnWaitInputHandler); // make sure it is set
480-
}
481-
},
482-
483-
fnWaitSound: function () {
484-
var aSoundData;
485-
486-
this.oVm.vmLoopCondition(); // update iNextFrameTime, timers, inks; schedule sound: free queue
487-
if (this.oSound.isActivatedByUser()) { // only if activated
488-
aSoundData = this.oVm.vmGetSoundData();
489-
while (aSoundData.length && this.oSound.testCanQueue(aSoundData[0].iState)) {
490-
this.oSound.sound(aSoundData.shift());
491-
}
492-
if (!aSoundData.length) {
493-
this.oVm.vmStop("", 0, true); // no more wait
503+
if (oStop.sReason === "waitInput") { // only for this reason
504+
this.fnWaitSound(); // sound and blinking events
494505
}
506+
this.oKeyboard.setKeyDownHandler(this.fnWaitInputHandler); // make sure it is set
495507
}
496-
this.iNextLoopTimeOut = this.oVm.vmGetTimeUntilFrame(); // wait until next frame
497508
},
498509

499510
// merge two scripts with sorted line numbers, lines from script2 overwrite lines from script1
@@ -757,8 +768,6 @@ Controller.prototype = {
757768
oData,
758769
sType;
759770

760-
//this.oVm.vmStop("", 0, true);
761-
762771
if (sInput !== null && sInput !== undefined) {
763772
oData = this.splitMeta(sInput);
764773
sInput = oData.sData; // maybe changed
@@ -814,7 +823,7 @@ Controller.prototype = {
814823
this.fnParseRun();
815824
break;
816825
case "load":
817-
if (!bPutInMemory) { //sType !== "B") { // not for binary files
826+
if (!bPutInMemory) {
818827
this.view.setAreaValue("inputText", sInput);
819828
this.view.setAreaValue("resultText", "");
820829
this.invalidateScript();
@@ -830,14 +839,14 @@ Controller.prototype = {
830839
break;
831840
case "chain": // TODO: run through... : if we have a line number, make sure it is not optimized away when compiling!
832841
case "run":
833-
if (!bPutInMemory) { //if (sType !== "B") { // not for binary files
842+
if (!bPutInMemory) {
834843
this.view.setAreaValue("inputText", sInput);
835844
this.view.setAreaValue("resultText", "");
836845
iStartLine = oInFile.iLine || 0;
837846
this.fnReset();
838847
this.fnParseRun();
839848
} else {
840-
this.fnReset(); //TTT
849+
this.fnReset();
841850
}
842851
break;
843852
default:
@@ -1012,7 +1021,6 @@ Controller.prototype = {
10121021
aMeta = sMeta.split(";");
10131022

10141023
oMeta = {
1015-
//sIdent: aMeta[0],
10161024
sType: aMeta[1],
10171025
iStart: aMeta[2],
10181026
iLength: aMeta[3],
@@ -1115,7 +1123,7 @@ Controller.prototype = {
11151123
var sInput = this.view.getAreaValue("inputText"),
11161124
iStream = oParas.iStream,
11171125
aLines = this.fnGetLinesInRange(sInput, oParas.iFirst, oParas.iLast),
1118-
oRegExp = new RegExp(/([\x00-\x1f])/g), //eslint-disable-line no-control-regex
1126+
oRegExp = new RegExp(/([\x00-\x1f])/g), // eslint-disable-line no-control-regex
11191127
i, sLine;
11201128

11211129
for (i = 0; i < aLines.length; i += 1) {
@@ -1470,8 +1478,8 @@ Controller.prototype = {
14701478
this.updateResultText();
14711479

14721480
this.view.setDisabled("runButton", sReason === "reset");
1473-
this.view.setDisabled("stopButton", sReason !== "waitInput" && sReason !== "waitKey" && sReason !== "fileLoad" && sReason !== "fileSave");
1474-
this.view.setDisabled("continueButton", sReason === "end" || sReason === "waitInput" || sReason === "waitKey" || sReason === "fileLoad" || sReason === "fileSave" || sReason === "parse" || sReason === "renumLines" || sReason === "reset");
1481+
this.view.setDisabled("stopButton", sReason !== "fileLoad" && sReason !== "fileSave");
1482+
this.view.setDisabled("continueButton", sReason === "end" || sReason === "fileLoad" || sReason === "fileSave" || sReason === "parse" || sReason === "renumLines" || sReason === "reset");
14751483

14761484
this.setVarSelectOptions("varSelect", this.oVariables);
14771485
this.commonEventHandler.onVarSelectChange();
@@ -1535,7 +1543,7 @@ Controller.prototype = {
15351543
this.oVm.vmStop("error", 55); //TTT
15361544
}
15371545

1538-
if (oStop.sReason && oStop.sReason !== "waitSound") {
1546+
if (oStop.sReason && oStop.sReason !== "waitSound" && oStop.sReason !== "waitKey" && oStop.sReason !== "waitInput") {
15391547
this.bTimeoutHandlerActive = false; // not running any more
15401548
this.exitLoop();
15411549
} else {
@@ -1615,7 +1623,7 @@ Controller.prototype = {
16151623
this.view.setDisabled("runButton", true);
16161624
this.view.setDisabled("stopButton", false);
16171625
this.view.setDisabled("continueButton", true);
1618-
if (oStop.sReason === "break" || oStop.sReason === "escape" || oStop.sReason === "stop" || oStop.sReason === "direct" || oStop.sReason === "waitInput") {
1626+
if (oStop.sReason === "break" || oStop.sReason === "escape" || oStop.sReason === "stop" || oStop.sReason === "direct" /*TTT || oStop.sReason === "waitInput" */) {
16191627
if (!oSavedStop.fnInputCallback) { // no keyboard callback? make sure no handler is set (especially for direct->continue)
16201628
this.oKeyboard.setKeyDownHandler(null);
16211629
}

0 commit comments

Comments
 (0)