Skip to content

Commit

Permalink
Field method toArrays, instead of using pack
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Mar 28, 2023
1 parent ff8a3e5 commit 0f22dcc
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 92 deletions.
10 changes: 6 additions & 4 deletions src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,20 @@ class Field extends component.Component {
throw new Error(`field.fill needs array or number, got ${v}`);
}

pack() {
toArrays() {
let newPitch = this.width;
let numPixels = this.height * newPitch;
let newBuff = new Uint8Array(numPixels);
let newArrays = new Array(this.height);
for (let y = 0; y < this.height; y++) {
let newRow = new Array(this.width);
for (let x = 0; x < this.width; x++) {
let k = y*this.pitch + x;
let j = y*newPitch + x;
newBuff[j] = this.data[k];
newRow[x] = this.data[k];
}
newArrays[y] = newRow;
}
return newBuff;
return newArrays;
}

xform(kind) {
Expand Down
2 changes: 1 addition & 1 deletion src/tileset_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TilesetBuilderDisplay extends baseDisplay.BaseDisplay {
}
this._tileset = new tiles.Tileset(details);
this._frames = [];
this._numFrames = info.numFrames || 64;
this._numFrames = info.numFrames || 256;
}

name() {
Expand Down
44 changes: 22 additions & 22 deletions test/cycle_palette.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ describe('Cycle palette', function() {

util.renderCompareTo(ra, 'test/testdata/green-fruit.png');

let expect = new Uint8Array([
0, 0, 0, 0, 4, 0, 0, 0,
0, 0, 0, 0, 0, 4, 0, 0,
0, 2, 8, 8, 8, 4, 2, 0,
2, 8,14, 8, 4, 8, 8, 2,
8,14,14, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
0, 8, 8, 8, 8, 8, 8, 0,
0, 0, 8, 8, 8, 8, 0, 0
]);
assert.deepEqual(expect, ra.field.pack());
let expect = [
[ 0, 0, 0, 0, 4, 0, 0, 0],
[ 0, 0, 0, 0, 0, 4, 0, 0],
[ 0, 2, 8, 8, 8, 4, 2, 0],
[ 2, 8,14, 8, 4, 8, 8, 2],
[ 8,14,14, 8, 8, 8, 8, 8],
[ 8, 8, 8, 8, 8, 8, 8, 8],
[ 0, 8, 8, 8, 8, 8, 8, 0],
[ 0, 0, 8, 8, 8, 8, 0, 0],
];
assert.deepEqual(expect, ra.field.toArrays());

// Compare the palette
palette = ra.palette;
Expand Down Expand Up @@ -171,17 +171,17 @@ describe('Cycle palette', function() {
let img = ra.loadImage('test/testdata/small-fruit.png');
ra.paste(img);

let expect = new Uint8Array([
1, 1, 1, 1, 4, 1, 1, 1,
1, 1, 1, 1, 1, 4, 1, 1,
1, 2, 0, 0, 0, 4, 2, 1,
2, 0, 3, 0, 4, 0, 0, 2,
0, 3, 3, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 1, 1
]);
assert.deepEqual(expect, ra.field.pack());
let expect = [
[1, 1, 1, 1, 4, 1, 1, 1],
[1, 1, 1, 1, 1, 4, 1, 1],
[1, 2, 0, 0, 0, 4, 2, 1],
[2, 0, 3, 0, 4, 0, 0, 2],
[0, 3, 3, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 0, 0, 1, 1],
];
assert.deepEqual(expect, ra.field.toArrays());

// Compare the palette
let palette = ra.palette;
Expand Down
42 changes: 21 additions & 21 deletions test/plane.js → test/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ describe('Field', function() {
[0,0,0,7],
[0,7,7,0],
]);
let bin = pl.pack();
let bin = pl.toArrays();

let expect = new Uint8Array([
0, 7, 7, 0,
7, 7, 0, 7,
0, 0, 0, 7,
0, 7, 7, 0,
]);
let expect = [
[0, 7, 7, 0],
[7, 7, 0, 7],
[0, 0, 0, 7],
[0, 7, 7, 0],
];
assert.deepEqual(expect, bin);
});

Expand All @@ -38,14 +38,14 @@ describe('Field', function() {
let pl = new ra.Field();
pl.setSize(4, 4);
pl.fill(5);
let bin = pl.pack();
let bin = pl.toArrays();

let expect = new Uint8Array([
5, 5, 5, 5,
5, 5, 5, 5,
5, 5, 5, 5,
5, 5, 5, 5,
]);
let expect = [
[5, 5, 5, 5],
[5, 5, 5, 5],
[5, 5, 5, 5],
[5, 5, 5, 5],
];
assert.deepEqual(expect, bin);
});

Expand All @@ -56,14 +56,14 @@ describe('Field', function() {
7,7,0,7,
0,0,0,7,
0,7,7,0]);
let bin = pl.pack();
let bin = pl.toArrays();

let expect = new Uint8Array([
0, 7, 7, 0,
7, 7, 0, 7,
0, 0, 0, 7,
0, 7, 7, 0,
]);
let expect = [
[0, 7, 7, 0],
[7, 7, 0, 7],
[0, 0, 0, 7],
[0, 7, 7, 0],
];
assert.deepEqual(expect, bin);
});

Expand Down
8 changes: 4 additions & 4 deletions test/hsv_sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ describe('HSV sort', function() {
util.ensureFilesMatch('test/testdata/pal_sphere.png', tmpout);

// Compare the field data buffer
let bin = ra.field.pack();
let bin = ra.field.toArrays();
let pitch = 16;

let actual = '';
for (let y = 0; y < 16; y++) {
for (let x = 0; x < 16; x++) {
let k = y*pitch+x;
actual += `${bin[k]} `;
actual += `${bin[y][x]} `;
}
actual += '\n';
}
Expand Down Expand Up @@ -76,15 +76,15 @@ describe('HSV sort', function() {
ra.save(pal, tmpout);
util.ensureFilesMatch('test/testdata/pal_sphere_sort.png', tmpout);

let bin = ra.field.pack();
let bin = ra.field.toArrays();
let pitch = 16;

// Compare the field data buffer
let actual = '';
for (let y = 0; y < 16; y++) {
for (let x = 0; x < 16; x++) {
let k = y*pitch+x;
actual += `${bin[k]} `;
actual += `${bin[y][x]} `;
}
actual += '\n';
}
Expand Down
66 changes: 33 additions & 33 deletions test/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ describe('Image', function() {
util.renderCompareTo(ra, 'test/testdata/small-fruit.png');

// 8-bit data array is using pico8 values
let expect = new Uint8Array([
0, 0, 0, 0, 4, 0, 0, 0,
0, 0, 0, 0, 0, 4, 0, 0,
0, 2, 8, 8, 8, 4, 2, 0,
2, 8,14, 8, 4, 8, 8, 2,
8,14,14, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
0, 8, 8, 8, 8, 8, 8, 0,
0, 0, 8, 8, 8, 8, 0, 0
]);
assert.deepEqual(expect, ra.field.pack());
let expect = [
[0, 0, 0, 0, 4, 0, 0, 0],
[0, 0, 0, 0, 0, 4, 0, 0],
[0, 2, 8, 8, 8, 4, 2, 0],
[2, 8,14, 8, 4, 8, 8, 2],
[8,14,14, 8, 8, 8, 8, 8],
[8, 8, 8, 8, 8, 8, 8, 8],
[0, 8, 8, 8, 8, 8, 8, 0],
[0, 0, 8, 8, 8, 8, 0, 0],
];
assert.deepEqual(expect, ra.field.toArrays());
});


Expand All @@ -71,17 +71,17 @@ describe('Image', function() {

util.renderCompareTo(ra, 'test/testdata/small-fruit.png');

let expect = new Uint8Array([
0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0,
0, 4, 2, 2, 2, 1, 4, 0,
4, 2, 5, 2, 1, 2, 2, 4,
2, 5, 5, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2,
0, 2, 2, 2, 2, 2, 2, 0,
0, 0, 2, 2, 2, 2, 0, 0
]);
assert.deepEqual(expect, ra.field.pack());
let expect = [
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 4, 2, 2, 2, 1, 4, 0],
[4, 2, 5, 2, 1, 2, 2, 4],
[2, 5, 5, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2],
[0, 2, 2, 2, 2, 2, 2, 0],
[0, 0, 2, 2, 2, 2, 0, 0],
];
assert.deepEqual(expect, ra.field.toArrays());

// Compare the palette
let palette = ra.palette;
Expand Down Expand Up @@ -160,17 +160,17 @@ describe('Image', function() {
util.renderCompareTo(ra, 'test/testdata/small-fruit-quant.png');

// 8-bit data array is using pico8 values
let expect = new Uint8Array([
1, 1,39, 1, 3,32,27,38,
0,30,36,28,31, 5,40,26,
37,42, 9,14, 7, 4,45,35,
43,54,58,21,24,55,19,44,
17,57,59,50,15,18,46,22,
53, 6,56,48,47, 8,13,16,
41,51,20,10,17,18,52,34,
25,29,12,11,23,49,33, 2,
]);
assert.deepEqual(expect, ra.field.pack());
let expect = [
[ 1, 1,39, 1, 3,32,27,38],
[ 0,30,36,28,31, 5,40,26],
[37,42, 9,14, 7, 4,45,35],
[43,54,58,21,24,55,19,44],
[17,57,59,50,15,18,46,22],
[53, 6,56,48,47, 8,13,16],
[41,51,20,10,17,18,52,34],
[25,29,12,11,23,49,33, 2],
];
assert.deepEqual(expect, ra.field.toArrays());
});


Expand Down
14 changes: 7 additions & 7 deletions test/tileset.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,13 @@ describe('Tileset', function() {
assert.equal(tiles.length, 8);
assert.equal(tiles.numTiles, 8);

let pattern = ra.field.pack();
let expect = new Uint8Array([
0, 1, 2, 3,
1, 4, 4, 4,
5, 5, 2, 6,
1, 7, 0, 0,
]);
let pattern = ra.field.toArrays();
let expect = [
[0, 1, 2, 3],
[1, 4, 4, 4],
[5, 5, 2, 6],
[1, 7, 0, 0],
];
assert.deepEqual(expect, pattern);
assert.deepEqual(4, ra.field.width);
assert.deepEqual(4, ra.field.height);
Expand Down

0 comments on commit 0f22dcc

Please sign in to comment.