2012-09-20 02:43:39 +04:00
|
|
|
(function () {
|
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var ns = $.namespace("pskl.rendering");
|
|
|
|
|
2013-09-22 23:02:43 +04:00
|
|
|
ns.SpritesheetRenderer = function (piskelController) {
|
|
|
|
this.piskelController = piskelController;
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
|
|
|
|
2013-10-10 02:06:11 +04:00
|
|
|
ns.SpritesheetRenderer.prototype.render = function () {
|
2013-08-10 14:11:16 +04:00
|
|
|
var canvas = this.createCanvas_();
|
2013-09-22 23:02:43 +04:00
|
|
|
for (var i = 0 ; i < this.piskelController.getFrameCount() ; i++) {
|
|
|
|
var frame = this.piskelController.getFrameAt(i);
|
|
|
|
this.drawFrameInCanvas_(frame, canvas, i * this.piskelController.getWidth(), 0);
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
2013-10-10 02:06:11 +04:00
|
|
|
return canvas;
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO(juliandescottes): Mutualize with code already present in FrameRenderer
|
|
|
|
*/
|
|
|
|
ns.SpritesheetRenderer.prototype.drawFrameInCanvas_ = function (frame, canvas, offsetWidth, offsetHeight) {
|
|
|
|
var context = canvas.getContext('2d');
|
|
|
|
for(var col = 0, width = frame.getWidth(); col < width; col++) {
|
|
|
|
for(var row = 0, height = frame.getHeight(); row < height; row++) {
|
|
|
|
var color = frame.getPixel(col, row);
|
|
|
|
if(color != Constants.TRANSPARENT_COLOR) {
|
|
|
|
context.fillStyle = color;
|
|
|
|
context.fillRect(col + offsetWidth, row + offsetHeight, 1, 1);
|
2013-06-13 19:56:44 +04:00
|
|
|
}
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.SpritesheetRenderer.prototype.createCanvas_ = function () {
|
2013-09-22 23:02:43 +04:00
|
|
|
var frameCount = this.piskelController.getFrameCount();
|
2013-08-10 14:11:16 +04:00
|
|
|
if (frameCount > 0){
|
2013-09-22 23:02:43 +04:00
|
|
|
var width = frameCount * this.piskelController.getWidth();
|
|
|
|
var height = this.piskelController.getHeight();
|
2013-08-10 14:11:16 +04:00
|
|
|
return pskl.CanvasUtils.createCanvas(width, height);
|
|
|
|
} else {
|
|
|
|
throw "Cannot render empty Spritesheet";
|
|
|
|
}
|
|
|
|
};
|
2012-09-20 02:43:39 +04:00
|
|
|
})();
|