Skip to content

Commit

Permalink
Background caching
Browse files Browse the repository at this point in the history
  • Loading branch information
stalgiag committed Nov 5, 2019
1 parent 8686a4e commit 8debb51
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) {

this.fontInfos = {};

this._cachedBackground = undefined;

return this;
};

Expand Down Expand Up @@ -533,12 +535,19 @@ p5.RendererGL.prototype._update = function() {
*/
p5.RendererGL.prototype.background = function(...args) {
const _col = this._pInst.color(...args);
const _r = _col.levels[0] / 255;
const _g = _col.levels[1] / 255;
const _b = _col.levels[2] / 255;
const _a = _col.levels[3] / 255;
this.GL.clearColor(_r, _g, _b, _a);
this.GL.depthMask(true);
if (this._cachedBackground) {
if (!this._arraysEqual(_col._array, this._cachedBackground)) {
const _r = _col.levels[0] / 255;
const _g = _col.levels[1] / 255;
const _b = _col.levels[2] / 255;
const _a = _col.levels[3] / 255;
this.GL.clearColor(_r, _g, _b, _a);
this.GL.depthMask(true);
this._cachedBackground = _col._array.slice(0);
}
} else {
this._cachedBackground = _col._array.slice(0);
}
this.GL.clear(this.GL.COLOR_BUFFER_BIT | this.GL.DEPTH_BUFFER_BIT);
};

Expand Down Expand Up @@ -1293,6 +1302,23 @@ p5.RendererGL.prototype._bindBuffer = function(
///////////////////////////////
//// UTILITY FUNCTIONS
//////////////////////////////
p5.RendererGL.prototype._arraysEqual = function(a, b) {
if (a.length !== b.length) return false;
for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
};

p5.RendererGL.prototype._isTypedArray = function(arr) {
let res = false;
res = arr instanceof Float32Array;
res = arr instanceof Float64Array;
res = arr instanceof Int16Array;
res = arr instanceof Uint16Array;
res = arr instanceof Uint32Array;
return res;
};
/**
* turn a two dimensional array into one dimensional array
* @private
Expand Down

0 comments on commit 8debb51

Please sign in to comment.