Browse Source

Added getRegion() and setRegion() to NESBank which will return color information within a defined region.

dev
Bryan Miller 5 years ago
parent
commit
4954ea9592
1 changed files with 48 additions and 0 deletions
  1. +48
    -0
      app/js/models/NESBank.js

+ 48
- 0
app/js/models/NESBank.js View File

@@ -528,6 +528,29 @@ export default class NESBank extends ISurface{
};
}

getRegion(x, y, w, h){
if (w <= 0 || h <= 0)
throw new RangeError("Width and/or Height must be greater than zero.");
var region = [];

for (let j=y; j<y+h; j++){
if (j === this.height){
h = j - y;
break;
}
for (let i=x; i<x+w; i++){
if (i === this.width){
w = i - x;
break;
}
region.push(this.getColorIndex(i, j));
}
}

return {w:w, h:h, r:region};

}

setColorIndex(x, y, ci, pi){
if (x < 0 || x >= this.width || y < 0 || y > this.height)
throw new RangeError("Coordinates out of bounds.");
@@ -550,6 +573,31 @@ export default class NESBank extends ISurface{
}


setRegion(x, y, w, h, r){
if (w <= 0 || h <= 0)
throw new RangeError("Width and/or Height must be greater than zero.");
if (!(r instanceof Array)){
throw new TypeError("Region expected to be an array.");
}
if (r.length !== w*h)
throw new RangeError("Region length does not match given width/height values.");

for (let j=0; j < h; j++){
for (let i=0; i < w; i++){
var index = (j*w) + i;
if ("pi" in r[index] && "ci" in r[index] && r[index].ci >= 0){
var _x = i+x;
var _y = j+y;
if (_x >= 0 && _x < this.width && _y >= 0 && _y < this.height)
this.setColorIndex(_x, _y, r[index].ci, r[index].pi);
}
}
}

return this;
}


snapshot(){
if (this.__redos.length > 0) // Remove the redo history. We're adding a new snapshot.
this.__redos = [];

Loading…
Cancel
Save