diff --git a/src/core/core.js b/src/core/core.js index b508b815e6..391ac9d638 100644 --- a/src/core/core.js +++ b/src/core/core.js @@ -357,7 +357,6 @@ var p5 = function(sketch, node, sync) { //mandatory update values(matrixs and stack) - this._setProperty('frameCount', this.frameCount + 1); this.redraw(); this._frameRate = 1000.0/(now - this._lastFrameTime); this._lastFrameTime = now; diff --git a/src/core/structure.js b/src/core/structure.js index e420be36cd..d9ab771035 100644 --- a/src/core/structure.js +++ b/src/core/structure.js @@ -353,6 +353,7 @@ p5.prototype.redraw = function () { f.call(self); }; for (var idxRedraw = 0; idxRedraw < numberOfRedraws; idxRedraw++) { + this._setProperty('frameCount', this.frameCount + 1); this._registeredMethods.pre.forEach(callMethod); userDraw(); this._registeredMethods.post.forEach(callMethod); diff --git a/test/unit/core/structure.js b/test/unit/core/structure.js index 4103afe581..a5189f7c83 100644 --- a/test/unit/core/structure.js +++ b/test/unit/core/structure.js @@ -14,6 +14,59 @@ suite('Structure', function() { myp5.remove(); }); + suite('p5.frameCount', function() { + test('starts at zero', function() { + return new Promise(function(resolve, reject) { + // Has to use a custom p5 to hook setup correctly + new p5(function(p) { + p.setup = function() { + if(p.frameCount !== 0) { + reject('frameCount is not 0 in setup'); + } + }; + p.draw = function() { + if(p.frameCount === 1) { + resolve(); + } + }; + }); + }); + }); + test('matches draw calls', function() { + return new Promise(function(resolve, reject) { + var frames = myp5.frameCount; + var start = myp5.frameCount; + myp5.draw = function() { + try { + frames += 1; + assert.equal(myp5.frameCount, frames); + if(frames === start + 5) { + // Test 5 seperate redraws + myp5.noLoop(); + setTimeout(myp5.redraw.bind(myp5), 10); + setTimeout(myp5.redraw.bind(myp5), 20); + setTimeout(myp5.redraw.bind(myp5), 30); + setTimeout(myp5.redraw.bind(myp5), 40); + setTimeout(myp5.redraw.bind(myp5), 50); + } else if(frames === start + 10) { + // Test loop resuming + myp5.loop(); + } else if(frames === start + 15) { + // Test queuing multiple redraws + myp5.noLoop(); + setTimeout(myp5.redraw.bind(myp5, 5), 10); + } else if(frames === start + 20) { + resolve(); + } + assert.equal(myp5.frameCount, frames); + } catch(err) { + reject(err); + } + }; + }); + }); + }); + suite('p5.prototype.loop and p5.prototype.noLoop', function() { test('noLoop should stop', function() { return new Promise(function(resolve, reject) {