-
Notifications
You must be signed in to change notification settings - Fork 4
/
nano-runtime.js
1451 lines (1159 loc) · 46.1 KB
/
nano-runtime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* By Morgan McGuire @CasualEffects http://casual-effects.com GPL 3.0 License */
// Variables named with a leading underscore are illegal in nano and will therefore not be
// visible to the program.
var _SCREEN_WIDTH_BITS, _SCREEN_WIDTH, _SCREEN_HEIGHT, _BAR_HEIGHT, _BAR_SPACING, _FRAMEBUFFER_HEIGHT;
var _Math = Math;
////////////////////////////////////////////////////////////////////
// Array
Object.defineProperty(Array.prototype, 'len',
{ configurable: true,
get: function () { return this.length; },
set: function (n) { this.length = n; }});
/** Used by code emitted from the `with` statement */
Object.prototype.assignFields = function(dst, src, fields) {
for (var i = fields.length - 1; i >= 0; ++i) {
var f = fields[i];
dst[f] = src[f];
}
return dst;
}
function _arrayKill(i) {
if (typeof i !== 'number') { throw 'Array.kill called with a value (' + i + ') that is not a number'; }
let L = this.length;
this[i] = this[L - 1];
this.length = L - 1;
}
function _arrayDel(i) {
if (typeof i !== 'number') { throw 'Array.del called with a value (' + i + ') that is not a number'; }
this.splice(i, 1);
}
function _arrayRem(x) {
let L = this.length;
for (let i = 0; i < L; ++i) {
if (this[i] === x) {
this.splice(i, 1);
return;
}
}
// fail silently
}
function _arrayFind(x, s) {
s = s || 0;
for (let i = s; i < this.length; ++i) {
if (this[i] === x) { return i; }
}
return undefined;
}
function _arrayAdd(x) {
this.push(x);
}
function _greaterThan(a, b) {
return a > b;
}
function _arraySort(k) {
var compare = undefined;
if (k === undefined) {
if (typeof this[0] === 'object') {
// find the first property, alphabetically
var entries = Object.entries(this[0]);
entries.sort(function(a, b) { return a[0]-b[0]; });
k = entries[0][0];
compare = function (a, b) { return a[k]-b[k]; };
} else {
compare = function (a, b) {return a-b};
}
} else {
compare = function (a, b) { return a[k]-b[k]; };
}
this.sort(compare);
}
Object.defineProperty(Array.prototype, 'rem',
{ configurable: true,
get: function () { return _arrayRem; }});
Object.defineProperty(Array.prototype, 'kill',
{ configurable: true,
get: function () { return _arrayKill; }});
Object.defineProperty(Array.prototype, 'del',
{ configurable: true,
get: function () { return _arrayDel; }});
Object.defineProperty(Array.prototype, 'find',
{ configurable: true,
get: function () { return _arrayFind; }});
Object.defineProperty(Array.prototype, 'add',
{ configurable: true,
get: function () { return _arrayAdd; }});
Object.defineProperty(Array.prototype, '_sort',
{ configurable: true,
get: function () { return _arraySort; }});
/////////////////////////////////////////////////////////////////////
//
// Table
Object.defineProperty(Object.prototype, 'len',
{ configurable: true,
get: function () { return Object.keys(this).length; }});
Object.defineProperty(Object.prototype, 'keys',
{ configurable: true,
get: function () { return Object.keys(this); }});
function _tableRem(key) { delete this[key]; }
Object.defineProperty(Object.prototype, 'rem',
{ configurable: true,
get: function () { return _tableRem; }});
/////////////////////////////////////////////////////////////////////
//
// String
Object.defineProperty(String.prototype, 'len',
{ configurable: true,
get: function () { return this.length; }});
function _stringFind(x, s) {
var i = this.indexOf(x, s);
return (i === -1) ? undefined : i;
}
Object.defineProperty(String.prototype, 'find',
{ configurable: true,
get: function () { return _stringFind; }});
// Override the default string's deprecated "sub", which meant <sub>x</sub> (!)
String.prototype.sub = String.prototype.substring;
//////////////////////////////////////////////////////////////////////
//
// Function
function call(f) {
return Function.call.apply(f, arguments);
}
//////////////////////////////////////////////////////////////////////
/** Set by the IDE. A view onto the imagedata */
var _updateImageDataUint32;
/** Set by reloadRuntime(). 128x32 Uint8 array */
var _spriteSheet = null;
/** Set by reloadRuntime(). 128x23 Uint8 array */
var _fontSheet = null;
/** Callback to show the screen buffer and yield to the browser. Set by
reloadRuntime() */
var _submitFrame = null;
// Scissor (clipping) region
var _clipY1 = 0, _clipY2 = _SCREEN_HEIGHT - 1, _clipX1 = 0, _clipX2 = _SCREEN_WIDTH - 1;
// Draw call offset
var _offsetX = 0, _offsetY = 0, _scaleX = 1, _scaleY = 1;
function xform(addX, addY, scaleX, scaleY) {
if (addX === undefined) {
addX = 0; addY = 0;
scaleX = 1; scaleY = 1;
}
_offsetX = addX;
_offsetY = _BAR_HEIGHT + _BAR_SPACING + addY;
_scaleX = (scaleX === -1) ? -1 : +1;
_scaleY = (scaleY === -1) ? -1 : +1;
}
function clip(x1,y1,x2,y2) {
var yShift = _BAR_SPACING + _BAR_HEIGHT;
if (x1 === undefined) {
if (y1 === undefined && x2 === undefined && y2 === undefined) {
// No arguments
x1 = 0; y1 = 0; x2 = _SCREEN_WIDTH - 1; y2 = _SCREEN_HEIGHT - 1;
} else {
x1 = _clipX1;
}
}
if (y1 === undefined) { y1 = _clipY1 - yShift; }
if (x2 === undefined) { x2 = _clipX2; }
if (y2 === undefined) { y2 = _clipY2 - yShift; }
x1 = Math.round(x1) | 0;
y1 = Math.round(y1 + yShift) | 0;
x2 = Math.round(x2) | 0;
y2 = Math.round(y2 + yShift) | 0;
_clipX1 = _clamp(Math.min(x1, x2), 0, _SCREEN_WIDTH - 1);
_clipY1 = _clamp(Math.min(y1, y2), 0, _FRAMEBUFFER_HEIGHT - 1);
_clipX2 = _clamp(Math.max(x1, x2), 0, _SCREEN_WIDTH - 1);
_clipY2 = _clamp(Math.max(y1, y2), 0, _FRAMEBUFFER_HEIGHT - 1);
}
function abs(x) {
return (x.length !== undefined) ? x.length : Math.abs(x);
}
var max = Math.max;
var min = Math.min;
var flr = Math.floor;
var ceil = Math.ceil;
var round = Math.round;
var pow = Math.pow;
var sin = Math.sin;
var cos = Math.cos;
var acos = Math.acos;
var asin = Math.asin;
var atan = Math.atan2;
var log = Math.log;
var sgn = Math.sign;
var sqrt = Math.sqrt;
var exp = Math.exp;
/** Actual colors available for nano as ARGB little-endian encoded data in a Uint32Array. Set
by reloadRuntime() */
var _screenPalette;
/** Each pixel is one value in the _screenPalette */
var _screen;
var TRANSPARENT = 32;
// The initial "default"/"previous" color is stored directly in slot [0]
var _initialPalette = [7, 7, 13, 0, 8, 10, 12, 11, 29, TRANSPARENT];
/** Current subset of the fixed _screenPalette available, by index. Set by pal().
0 = previous
1-8 = configurable
9 = transparent
*/
var _drawPalette = new Uint8Array(_initialPalette);
_drawPalette[0] = _initialPalette[0];
var _fontWidth = {'!':1, '|':1, '.':1, 'i':1, 'I':1, 'l':1, 'ⁱ':1, 'ᵢ':1,
' ':2, ',':2, ';':2, '[':2, ']':2, '`':2, '\'':2, '(':2, ')':2, 'f':2, '⌊':2, '⌋':2, '⌈':2, '⌉':2, '¹':2, 'ʲ':2, '⁽':2, '⁾':2, '₁':2, 'ⱼ':2, '₍':2, '₎':2};
/** Maps characters to x,y coordinates in the _fontSheet */
var _fontMap = (function(){
var f = `ABCDEFGHIJKLMNOPQRSTUVWXY
abcdefghijklmnopqrstuvwxy
0123456789~!@#$%^&*()_+-=
{}[]\\/|;:\`'",.?<>Zzεπτ∞∅ξ
βδΔθλμρσϕψΩ∩∪◅▻¬⌊⌋⌈⌉≟≠≤≥∊
⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻ᵃᵝⁱʲˣᵏᵘⁿ⁽⁾
₀₁₂₃₄₅₆₇₈₉₊₋ₐᵦᵢⱼₓₖᵤₙ₍₎`;
var map = {};
for (var i = 0, x = 0, y = 0; i < f.length; ++i, ++x) {
var c = f[i];
if (c === '\n') { // newline resets
x = -1; ++y;
} else if (c !== ' ') { // skip spaces
map[c] = {x:x*5, y:y*8};
}
}
map['⊕'] = map['φ'] = map['ϕ'];
map['α'] = map['a'];
map['ω'] = map['w'];
map['∈'] = map['∊'];
map['ι'] = map['i'];
map['χ'] = map['χ'];
map['η'] = map['n'];
map['ζ'] = map['C'];
map['γ'] = map['y'];
return Object.freeze(map);
})();
var clr = 0;
var joy = Object.seal({x:0, y:0, θ:0, a:0, b:0, c:0, d:0, s:0, aa:0, bb:0, cc:0, dd:0, ss:0});
var pad = [joy, Object.seal({x:0, y:0, θ:0, a:0, b:0, c:0, d:0, s:0, aa:0, bb:0, cc:0, dd:0, ss:0})];
var mouse = Object.seal({x:-1, y:-1, l:0, ll:0, r:0, rr:0, m:0, mm:0});
var hashview = new DataView(new ArrayBuffer(8));
function _hash(d) {
// 32-bit FNV-1a
var hval = 0x811c9dc5;
if (d.length) {
// String
for (var i = d.length - 1; i >= 0; --i) {
hval ^= d.charCodeAt(i);
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
}
} else {
// Number
hashview.setFloat64(0, d);
for (var i = 7; i >= 0; --i) {
hval ^= hashview.getUint8(i);
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
}
// Near integers, FNV sometimes does a bad job because it doesn't
// mix the low bits enough. XOR with some well-distributed
// bits
hval ^= (_fract(Math.sin(d * 10) * 1e6) * 0xffffffff) | 0;
}
// Force to unsigned 32-bit
return (hval >>> 0);
}
function hash(x, y) {
var h = _hash(x);
if (y !== undefined) {
var hy = _hash(y);
h ^= ((hy >> 16) & 0xffff) | ((hy & 0xffff) << 16);
}
return Math.abs(h) / 0xffffffff;
}
function _lerp(a,b,t) { return a * (1 - t) + b * t; }
var lerp = _lerp;
// Fast 1D "hash" used by noise()
function _nhash1(n) { n = Math.sin(n) * 1e4; return n - Math.floor(n); }
// bicubic fbm value noise
// from https://www.shadertoy.com/view/4dS3Wd
function noise(octaves, x, y, z) {
// Set any missing axis to zero
x = x || 0;
y = y || 0;
z = z || 0;
// Maximum value is 1/2 + 1/4 + 1/8 ... from the straight summation
// The max is always pow(2,-octaves) less than 1.
// So, divide by (1-pow(2,-octaves))
octaves = Math.max(1, (octaves || 1) | 0);
var v = 0, k = 1 / (1 - Math.pow(2, -octaves));
var stepx = 110, stepy = 241, stepz = 171;
for (; octaves > 0; --octaves) {
var ix = Math.floor(x), iy = Math.floor(y), iz = Math.floor(z);
var fx = x - ix, fy = y - iy, fz = z - iz;
// For performance, compute the base input to a 1D hash from the integer part of the argument and the
// incremental change to the 1D based on the 3D -> 1D wrapping
var n = ix * stepx + iy * stepy + iz * stepz;
var ux = fx * fx * (3 - 2 * fx),
uy = fy * fy * (3 - 2 * fy),
uz = fz * fz * (3 - 2 * fz);
v += (_lerp(_lerp(_lerp(_nhash1(n), _nhash1(n + stepx), ux),
_lerp(_nhash1(n + stepy), _nhash1(n + stepx + stepy), ux), uy),
_lerp(_lerp(_nhash1(n + stepz), _nhash1(n + stepx + stepz), ux),
_lerp(_nhash1(n + stepy + stepz), _nhash1(n + stepx + stepy + stepz), ux), uy), uz) - 0.5) * k;
// Grab successive octaves from very different parts of the space, and
// double the frequency
x = 2 * x + 109;
y = 2 * y + 31;
z = 2 * z + 57;
k *= 0.5;
}
return v;
}
function pal(p) {
// Note that slot 0 is preserved
if (p === undefined) {
// Reset to the initial palette
for (var i = 1; i < _drawPalette.length; ++i) {
_drawPalette[i] = _initialPalette[i];
}
} else {
for (var i = 1; i <= 8; ++i) {
_drawPalette[i] = (p % 100) & 31;
p /= 100;
}
}
}
function _noop() {}
function vec(x, y, z, w) {
if (w !== undefined) {
return Object.seal({x:x, y:y, z:z, w:w})
} else if (z !== undefined) {
return Object.seal({x:x, y:y, z:z})
} else {
return Object.seal({x:x, y:y})
}
}
function xy(x, y) {
return Object.seal({x:x, y:y});
}
function xyz(x, y, z) {
return Object.seal({x:x, y:y, z:z});
}
function overlap(A, B) {
return ((Math.abs(A.pos.x - B.pos.x) <= (A.ext.x + B.ext.x)) &&
(Math.abs(A.pos.y - B.pos.y) <= (A.ext.y + B.ext.y)));
}
function mid(a, b, c) {
if (a < b) {
if (b < c) {
// a < b < c
return b;
} else if (a < c) {
// a < c <= b
return c;
} else {
// c <= a < b
return a;
}
} else if (a < c) {
return a;
} else if (b < c) {
// b < c <= a
return c;
} else {
return b;
}
}
function _lerp(x, y, a) { return (1 - a) * x + a * y; }
function _clamp(x, lo, hi) { return Math.min(Math.max(x, lo), hi); }
function _fract(x) { return x - Math.floor(x); }
function _square(x) { return x * x; }
function hsv(h,s,v,x,y) {
if (v === undefined) { v = 1; }
if (s === undefined) { s = 1; }
s = _clamp(s, 0, 1); v = _clamp(v, 0, 1);
// Convert to RGB
var r = v * (1 - s + s * _clamp(Math.abs(_fract(h + 1.0) * 6 - 3) - 1, 0, 1));
var g = v * (1 - s + s * _clamp(Math.abs(_fract(h + 2/3) * 6 - 3) - 1, 0, 1));
var b = v * (1 - s + s * _clamp(Math.abs(_fract(h + 1/3) * 6 - 3) - 1, 0, 1));
return rgb(r,g,b,x,y);
}
function gray(v,x,y) {
return rgb(v,v,v,x,y);
}
// Set by reloadRuntime
var rgb;
/** Converts the first decimal digit of a colormap to a screen color and sets
the drawPalette 0 value based on it */
function colormapToColor(colormap) {
return _drawPalette[0] = _drawPalette[colormap % 10];
}
function tri(Ax, Ay, Bx, By, Cx, Cy, colormap) {
Ax = Ax * _scaleX + _offsetX; Ay = Ay * _scaleY + _offsetY;
Bx = Bx * _scaleX + _offsetX; By = By * _scaleY + _offsetY;
Cx = Cx * _scaleX + _offsetX; Cy = Cy * _scaleY + _offsetY;
colormap |= 0;
// Extract the fill color, which is not yet used in this implementation
var fill = colormapToColor(colormap); colormap = (colormap / 10) | 0;
var border = colormapToColor(colormap);
// Culling optimization
if ((Math.min(Ax, Bx, Cx) > _clipX2 + 0.5) || (Math.min(Ay, By, Cy) > _clipY2 + 0.5) ||
(Math.max(Ax, Bx, Cx) < _clipX1 - 0.5) || (Math.max(Ay, By, Cy) < _clipY1 - 0.5)) {
return;
}
// Fill
if (fill !== TRANSPARENT) {
var shift = ((border !== TRANSPARENT) && (border !== fill)) ? 0.5 : 0;
// For each non-horizontal edge, store:
//
// [startX, startY, dx/dy slope, vertical height].
//
// These are the values needed for the edge-intersection test. Add edges so that the
// start Y coordinate is less than the end one.
var edgeArray = [];
var y0 = _clipY2 + 1, y1 = _clipY1 - 1;
function addEdge(Sx, Sy, Ex, Ey) {
if (Sy < Ey) {
// Update bounding box
if (Sy < y0) { y0 = Sy; }
if (Ey > y1) { y1 = Ey; }
edgeArray.push([Sx, Sy, (Ex - Sx) / (Ey - Sy), Ey - Sy]);
} else if (Sy > Ey) {
addEdge(Ex, Ey, Sx, Sy);
}
}
addEdge(Ax, Ay, Bx, By);
addEdge(Bx, By, Cx, Cy);
addEdge(Cx, Cy, Ax, Ay);
// Intentionally left as a float to avoid float to int conversion for the inner loop
y0 = Math.max(_clipY1, Math.floor(y0));
y1 = Math.min(_clipY2, Math.floor(y1));
for (let y = y0; y <= y1; ++y) {
// For this scanline, intersect the edge lines of the triangle.
// As a convex polygon, we can simply intersect ALL edges and then
// take the min and max intersections.
let x0 = Infinity, x1 = -Infinity;
for (let i = edgeArray.length - 1; i >= 0; --i) {
let seg = edgeArray[i];
let ry = y - seg[1];
if ((ry >= 0) && (ry < seg[3])) {
let x = seg[0] + ry * seg[2];
if (x < x0) { x0 = x; }
if (x > x1) { x1 = x; }
}
}
if ((x0 <= _clipX2) && (x1 >= _clipX1)) {
_screen.fill(fill,
(Math.max(Math.round(x0 + shift) << 0, _clipX1) + (y << _SCREEN_WIDTH_BITS)) | 0,
(Math.min(Math.round(x1 - shift) << 0, _clipX2) + (y << _SCREEN_WIDTH_BITS) + 1) | 0);
}
}
}
if ((border !== TRANSPARENT) && (border !== fill)) {
line(Ax, Ay, Bx, By, colormap);
line(Bx, By, Cx, Cy, colormap);
line(Cx, Cy, Ax, Ay, colormap);
}
}
function circ(x, y, radius, colormap) {
x = x * _scaleX + _offsetX; y = y * _scaleY + _offsetY;
colormap |= 0;
radius = Math.round(radius) | 0;
var fill = colormapToColor(colormap); colormap = (colormap / 10) | 0;
var border = colormapToColor(colormap);
// Culling optimization
if ((x - radius > _clipX2 + 0.5) || (y - radius > _clipY2 + 0.5) ||
(x + radius < _clipX1 - 0.5) || (y + radius < _clipY1 - 0.5)) {
return;
}
if (fill !== TRANSPARENT) {
// offset
var ox = radius - 1, oy = 0;
// step
var dx = 1, dy = 1;
var err = dx - radius * 2;
// Midpoint circle algorithm. Iterate over 1/8 of the circle,
// reflect to all sides
while (ox >= oy) {
_hline(x - ox, y + oy, x + ox, fill);
_hline(x - ox, y - oy, x + ox, fill);
_hline(x - oy, y + ox, x + oy, fill);
_hline(x - oy, y - ox, x + oy, fill);
// -4 gives better shape for small circles
if (err <= -4) {
++oy;
err += dy;
dy += 2;
}
// intentionally no "else" to allow diagonal jumps
if (err > -4) {
--ox;
dx += 2;
err += dx - radius * 2;
}
} // while
} // if fill
if ((border !== TRANSPARENT) && (border !== fill)) {
// offset
var ox = radius - 1, oy = 0;
// step
var dx = 1, dy = 1;
var err = dx - radius * 2;
while (ox >= oy) {
_pset(x - ox, y + oy, border);
_pset(x + ox, y + oy, border);
_pset(x - oy, y + ox, border);
_pset(x + oy, y + ox, border);
_pset(x - ox, y - oy, border);
_pset(x + ox, y - oy, border);
_pset(x - oy, y - ox, border);
_pset(x + oy, y - ox, border);
if (err <= -4) {
++oy;
err += dy;
dy += 2;
}
if (err > -4) {
--ox;
dx += 2;
err -= radius * 2 - dx;
}
} // while
} // if border
}
function rect(x1, y1, x2, y2, colormap) {
x1 = x1 * _scaleX + _offsetX; y1 = y1 * _scaleY + _offsetY;
x2 = x2 * _scaleX + _offsetX; y2 = y2 * _scaleY + _offsetY;
colormap |= 0;
var fill = colormapToColor(colormap); colormap = (colormap / 10) | 0;
var border = colormapToColor(colormap);
// Sort coordinates
var t1 = Math.min(x1, x2), t2 = Math.max(x1, x2);
x1 = t1; x2 = t2;
t1 = Math.min(y1, y2), t2 = Math.max(y1, y2);
y1 = t1; y2 = t2;
// Culling optimization
if ((x1 > _clipX2 + 0.5) || (x2 < _clipX1 - 0.5) ||
(y1 > _clipY2 + 0.5) || (y2 < _clipY1 - 0.5)) {
return;
}
if ((border !== fill) && (border !== TRANSPARENT)) {
_hline(x1, y1, x2, border);
_hline(x1, y2, x2, border);
_vline(x1, y1 + 1, y2 - 1, border);
_vline(x2, y1 + 1, y2 - 1, border);
x1 += 1; y1 += 1; x2 -= 1; y2 -= 1;
}
if (fill !== TRANSPARENT) {
// Snap to integer and clip to screen. We don't need to
// round because we know that the rect is visible.
x1 = Math.max((x1 + 0.5) | 0, _clipX1);
x2 = Math.min((x2 + 0.5) | 0, _clipX2);
y1 = Math.max((y1 + 0.5) | 0, _clipY1);
y2 = Math.min((y2 + 0.5) | 0, _clipY2);
// Fill spans
for (var y = y1, i = y1 << _SCREEN_WIDTH_BITS; y <= y2; ++y, i += _SCREEN_WIDTH) {
_screen.fill(fill, i + x1, i + x2 + 1);
}
}
}
/** Assumes x2 >= x1 and that color is a legal integer. Does not assume that x1 and x2 or y are
on screen */
function _hline(x1, y, x2, color) {
x1 = Math.round(x1) | 0;
x2 = Math.round(x2) | 0;
y = Math.round(y) | 0;
if ((x2 >= _clipX1) && (x1 <= _clipX2) && (y >= _clipY1) && (y <= _clipY2)) {
_screen.fill(color, Math.max(x1, _clipX1) + (y << _SCREEN_WIDTH_BITS), Math.min(x2, _clipX2) + (y << _SCREEN_WIDTH_BITS) + 1);
}
}
/** Assumes y2 >= y1 and that color is a legal integer. Does not assume that y1 and y2 or x are
on screen */
function _vline(x, y1, y2, color) {
x = Math.round(x) | 0;
y1 = Math.round(y1) | 0;
y2 = Math.round(y2) | 0;
if ((y2 >= _clipY1) && (y1 <= _clipY2) && (x >= _clipX1) && (x <= _clipX2)) {
y1 = Math.max(_clipY1, y1);
y2 = Math.min(_clipY2, y2);
for (var y = y1, i = (y1 << _SCREEN_WIDTH_BITS) + x; y <= y2; ++y, i += _SCREEN_WIDTH) {
_screen[i] = color;
}
}
}
function line(x1, y1, x2, y2, colormap) {
x1 = x1 * _scaleX + _offsetX; y1 = y1 * _scaleY + _offsetY;
x2 = x2 * _scaleX + _offsetX; y2 = y2 * _scaleY + _offsetY;
var color = colormapToColor(colormap | 0);
// Offscreen culling optimization
if ((color === undefined) || (color === TRANSPARENT) ||
(Math.min(x1, x2) > _clipX2 + 0.5) || (Math.max(x1, x2) < _clipX1 - 0.5) ||
(Math.min(y1, y2) > _clipY2 + 0.5) || (Math.max(y1, y2) < _clipY1 - 0.5)) {
return;
}
if (y1 === y2) {
// Horizontal perf optimization/avoid divide by zero
_hline(Math.min(x1, x2), y1, Math.max(x1, x2), color);
} else if (x1 === x2) {
// Vertical perf optimization
_vline(x1, Math.min(y1, y2), Math.max(y1, y2), color);
} else {
// General case via DDA
// Slope:
var dx = x2 - x1, dy = y2 - y1;
var m = dy / dx;
var moreHorizontal = abs(dx) > abs(dy);
if ((moreHorizontal && (x2 < x1)) ||
(! moreHorizontal && (y2 < y1))) {
// Swap endpoints to go in increasing direction on the dominant axis.
// Slope is unchanged because both deltas become negated.
var temp;
temp = y1; y1 = y2; y2 = temp;
temp = x1; x1 = x2; x2 = temp;
}
if (moreHorizontal) {
// Crop horizontally:
var m = dy / dx;
var dx = Math.max(_clipX1, x1) - x1;
x1 += dx; y1 += m * dx;
x2 = Math.min(x2, _clipX2);
for (var x = x1, y = y1; x <= x2; ++x, y += m) {
_pset(x, y, color);
} // for x
} else { // Vertical
// Compute the inverted slope
var m = dx / dy;
// Crop vertically:
var dy = Math.max(_clipY1, y1) - y1;
x1 += dy * m; y1 += dy;
y2 = Math.min(y2, _clipY2);
for (var y = y1, x = x1; y <= y2; ++y, x += m) {
_pset(x, y, color);
} // for y
} // if more horizontal
} // if diagonal
}
function pset(x, y, color) {
if (y === undefined) {
// Single-argument version
color = x;
x = y = -10000;
}
if (color === undefined) {
color = _drawPalette[0];
} else {
_drawPalette[0] = color | 0;
}
if (color !== TRANSPARENT) {
x = x * _scaleX + _offsetX; y = y * _scaleY + _offsetY;
_pset(x, y, color & 31);
}
}
/** Assumes color has been mapped and range corrected */
function _pset(x, y, color) {
// nano pixels have integer centers, so we must round instead of just truncating.
// Otherwise -0.7, which is offscreen, would become 0 and be visible.
//
// The << 0 casts to integer, which should allow more efficent compilation of
// the subsequent code
var i = Math.round(x) << 0;
var j = Math.round(y) << 0;
// "i >>> 0" converts from signed to unsigned int,
// which forces negative values to be large and lets us
// early-out sooner in the tests
if (((i >>> 0) <= _clipX2) && ((j >>> 0) <= _clipY2) && (i >= _clipX1) && (y >= _clipY1)) {
_screen[(i + (j << _SCREEN_WIDTH_BITS)) << 0] = color;
}
}
function pget(x, y) {
x = x * _scaleX + _offsetX; y = y * _scaleY + _offsetY;
// See comment in _pset
var i = Math.round(x) | 0;
var j = Math.round(y) | 0;
if (((i >>> 0) <= _clipX2) && ((j >>> 0) <= _clipY2) && (i >= _clipX1) && (j >= _clipY1)) {
return _screen[i + (j << _SCREEN_WIDTH_BITS)];
} else {
return undefined;
}
}
function text(str, x, y, colormap, align) {
if (x === undefined) { x = _SCREEN_WIDTH >> 1; }
if (y === undefined) { y = 3; }
x = x * _scaleX + _offsetX; y = y * _scaleY + _offsetY;
str = '' + str;
colormap |= 0;
var textColor = colormapToColor(colormap); colormap = (colormap / 10) | 0;
let shadowColor = TRANSPARENT, borderColor = TRANSPARENT;
// Handled specially; for text only, if the second color is
// 0 it means transparent and not previous color
if (colormap !== 0) {
shadowColor = colormapToColor(colormap); colormap = (colormap / 10) | 0;
borderColor = colormapToColor(colormap);
if (borderColor !== TRANSPARENT && shadowColor == TRANSPARENT) {
shadowColor = borderColor;
}
}
// Spaces between letters
var width = str.length - 1;
// Variable-width letters
for (var c = 0; c < str.length; ++c) {
width += (_fontWidth[str[c]] || 3);
}
if (align === undefined) { align = 5; }
let xalign = align & 3, yalign = Math.min(align >> 2, 2);
let height = 4.5;
x -= width * xalign * 0.5;
if (xalign === 2) { ++x; }
y -= height * yalign * 0.5 + 1;
// Center and round. Have to call round() because values may
// be negative
x = Math.round(x - 1) | 0;
y = Math.round(y) | 0;
if ((x > _clipX2) || (y > _clipY2) || (y + 6 < _clipY1) || (x + width + 4 < _clipX1)) {
// Cull when off-screen
return;
}
const _FONT_WIDTH_BITS = 7;
if (textColor != TRANSPARENT) {
for (let c = 0; c < str.length; ++c) {
let chr = str[c];
let src = _fontMap[chr];
if (src) {
for (let j = 0, dstY = y; j <= 7; ++j, ++dstY) {
// On screen in Y?
if (((dstY >>> 0) <= _clipY2) && (dstY >= _clipY1)) {
for (let i = 0, dstX = x, dstIndex = x + (dstY << _SCREEN_WIDTH_BITS), srcIndex = src.x + ((src.y + j) << _FONT_WIDTH_BITS);
i <= 4;
++i, ++dstX, ++dstIndex, ++srcIndex) {
// In character and on screen in X?
if (((dstX >>> 0) <= _clipX2) && (dstX >= _clipX1)) {
switch (_fontSheet[srcIndex]) {
case 4:
_screen[dstIndex] = textColor;
break;
case 3:
if (shadowColor !== TRANSPARENT) {
_screen[dstIndex] = shadowColor;
}
break;
case 2:
if (borderColor !== TRANSPARENT) {
_screen[dstIndex] = borderColor;
}
break;
case 1: // border of shadow
if ((borderColor !== TRANSPARENT) && (shadowColor !== borderColor)) {
_screen[dstIndex] = borderColor;
}
break;
} // switch on color
} // on screen x
} // for i
} // on screen y
} // for j
} // character in font
x += (_fontWidth[chr] || 3) + 1;
} // for each character
} // not transparent
}
/* Returns a shallow copy */
function _clone(a) {
if (a instanceof Array) {
return a.slice();
} else if (typeof a === 'Object') {
return Object.assign({}, a);
} else {
return a;
}
}
function _clamp(x, L, H) {
return Math.max(Math.min(x, H), L);
}
/** Helper function to share code between the IDE and the runtime. Clipping region has to be
passed because it is different for those cases. */
function _draw(spr, x, y, localPalette, xform, rot, screen, clipX1, clipY1, clipX2, clipY2) {
let c = Math.cos(rot), s = Math.sin(rot);
// Compute the net 2x2 transformation matrix
let A = c, B = s, C = -s, D = c;
xform |= 0;
if (xform & 1) {
// Transpose: swap axes
let temp = A; A = C; C = temp;
temp = B; B = D; D = temp;
}
if (xform & 2) {
// Flip H: negate X
A = -A; B = -B;
}
if (xform & 4) {
// Flip V: negate Y
C = -C; D = -D;
}
const isCaptured = (spr instanceof Uint8Array);
let srcWidth = 8, srcHeight = 8;
if (isCaptured) { srcWidth = spr.width; srcHeight = spr.height; }
const centerOffsetX = (srcWidth - 1) * 0.5, centerOffsetY = (srcHeight - 1) * 0.5;