Browse Source

NESTile now stores the proxy object for pixel access as a class level object instead of creating it for every call to the .pixels property.

dev
Bryan Miller 5 years ago
parent
commit
8c2ff16c7d
1 changed files with 18 additions and 15 deletions
  1. +18
    -15
      app/js/models/NESTile.js

+ 18
- 15
app/js/models/NESTile.js View File

super(); super();
this.__paletteIndex = 0; this.__paletteIndex = 0;
this.__data = new Uint8Array(16); this.__data = new Uint8Array(16);
}

get width(){return 8;}
get height(){return 8;}


get pixels(){
return new Proxy(this, {
var self = this;
this.__pixels = new Proxy(this.__data, {
get: function(obj, prop){ get: function(obj, prop){
if (prop === "length") if (prop === "length")
return 64; return 64;
throw new RangeError("Index out of bounds."); throw new RangeError("Index out of bounds.");
var dindex = Math.floor(prop*0.125); var dindex = Math.floor(prop*0.125);
var bitoffset = 7 - (prop%8); var bitoffset = 7 - (prop%8);
var v = (obj.__data[dindex] & (1 << bitoffset)) >> bitoffset;
v += 2*((obj.__data[8+dindex] & (1 << bitoffset)) >> bitoffset);
var v = (obj[dindex] & (1 << bitoffset)) >> bitoffset;
v += 2*((obj[8+dindex] & (1 << bitoffset)) >> bitoffset);
return v; return v;
}, },


var dindex = Math.floor(prop*0.125); var dindex = Math.floor(prop*0.125);
var bitoffset = (prop % 8); var bitoffset = (prop % 8);
if (value == 1 || value == 3){ if (value == 1 || value == 3){
obj.__data[dindex] |= BitMask(bitoffset);
obj[dindex] |= BitMask(bitoffset);
} else { } else {
obj.__data[dindex] &= BitMask(bitoffset, true);
obj[dindex] &= BitMask(bitoffset, true);
} }
if (value == 2 || value == 3){ if (value == 2 || value == 3){
obj.__data[8+dindex] |= BitMask(bitoffset);
obj[8+dindex] |= BitMask(bitoffset);
} else { } else {
obj.__data[8+dindex] &= BitMask(bitoffset, true);
obj[8+dindex] &= BitMask(bitoffset, true);
} }
if (!BLOCK_CHANGE_EMIT) if (!BLOCK_CHANGE_EMIT)
obj.emit("data_changed");
self.emit("data_changed");
return true; return true;
} }
}); });
} }


get width(){return 8;}
get height(){return 8;}

get pixels(){
return this.__pixels;
}

get dataArray(){ get dataArray(){
var d = []; var d = [];
for (var y = 0; y < 8; y++){ for (var y = 0; y < 8; y++){
if (ci < 0 || ci >= 4){ if (ci < 0 || ci >= 4){
throw new ValueError("Color index out of bounds."); throw new ValueError("Color index out of bounds.");
} }
this.pixels[(y*8)+x] = ci;
this.__pixels[(y*8)+x] = ci;
return this; return this;
} }


if (x < 0 || x >= 8 || y < 0 || y >= 8){ if (x < 0 || x >= 8 || y < 0 || y >= 8){
throw new ValueError("Coordinates out of bounds."); throw new ValueError("Coordinates out of bounds.");
} }
return this.pixels[(8*y) + x];
return this.__pixels[(8*y) + x];
} }


flip(flag){ flip(flag){

Loading…
Cancel
Save