-
Notifications
You must be signed in to change notification settings - Fork 3
/
RenderManager.js
106 lines (87 loc) · 2.92 KB
/
RenderManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright 2011 Brandon Lockaby
This file is part of JSPDP.
https://github.com/brandon-lockaby/JSPDP
JSPDP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JSPDP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JSPDP. If not, see <http://www.gnu.org/licenses/>.
*/
JSPDP.RenderManager = function() {
};
var proto = (JSPDP.RenderManager.prototype = new JSPDP.TableauUI());
proto.init = function(settings, renderer_classes) {
JSPDP.TableauUI.prototype.init.call(this, settings);
this.canvas = this.createCanvas();
if(this.tableau instanceof JSPDP.RisingTableau) {
this.canvas.height -= this.panelDimensions.height;
}
this.ctx = this.canvas.getContext('2d');
settings.element = this.canvas;
this.renderers = [];
if(typeof(renderer_classes) != 'undefined') {
for(var i = 0; i < renderer_classes.length; i++) {
this.renderers.push(new renderer_classes[i]().init(settings));
}
}
this.lastTick = this.tableau.tickCount;
return this;
};
proto.get = function(t) {
for(var i in this.renderers) {
if(this.renderers[i] instanceof t) {
return this.renderers[i];
}
}
};
proto.start = function() {
this.running = true;
this.refresh();
var requestAnimationFrame = (function(){
//Check for each browser
//@paul_irish function
//Globalises this function to work on any browser as each browser has a different namespace for this
return window.requestAnimationFrame || //Chromium
window.webkitRequestAnimationFrame || //Webkit
window.mozRequestAnimationFrame || //Mozilla Geko
window.oRequestAnimationFrame || //Opera Presto
window.msRequestAnimationFrame || //IE Trident?
function(callback, element){ //Fallback function
window.setTimeout(callback, 1000 / 45);
}
})();
var self = this;
var run = function() {
self.draw();
if(self.running) requestAnimationFrame(run);
};
requestAnimationFrame(run);
};
proto.stop = function() {
this.running = false;
};
proto.refresh = function() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
for(var i = 0; i < this.renderers.length; i++) {
var renderer = this.renderers[i];
renderer.refresh();
renderer.draw(this.ctx);
}
};
proto.draw = function() {
if(this.tableau.tickCount != this.lastTick) {
this.lastTick = this.tableau.tickCount;
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
for(var i = 0; i < this.renderers.length; i++) {
var renderer = this.renderers[i];
renderer.update()
renderer.draw(this.ctx);
}
}
};