diff --git a/src/js/controller/settings/exportimage/CExportController.js b/src/js/controller/settings/exportimage/CExportController.js index b46f61a3..e2ee07b2 100644 --- a/src/js/controller/settings/exportimage/CExportController.js +++ b/src/js/controller/settings/exportimage/CExportController.js @@ -17,11 +17,12 @@ ns.CExportController.prototype.onCDownloadButtonClick_ = function (evt) { var fileName = this.getPiskelName_() + '.c'; - var cName = this.getPiskelName_().replace(" ","_"); + var cName = this.getPiskelName_().replace(' ','_'); var outputCanvas = this.getFramesheetAsCanvas(); var width = this.piskelController.getWidth(); var height = this.piskelController.getHeight(); - + var frameCount = this.piskelController.getFrameCount(); + // Create a background canvas that will be filled with the transparent color before each render. var background = pskl.utils.CanvasUtils.createCanvas(width, height); var context = background.getContext('2d'); @@ -29,16 +30,16 @@ // Useful defines for C routines var frameStr = '#include \n\n'; - frameStr += '#define '+ cName.toUpperCase() + '_FRAME_COUNT ' + this.piskelController.getFrameCount() + '\n'; - frameStr += '#define '+ cName.toUpperCase() + '_WIDTH ' + width + '\n'; - frameStr += '#define '+ cName.toUpperCase() + '_HEIGHT ' + height + '\n\n'; + frameStr += '#define ' + cName.toUpperCase() + '_FRAME_COUNT ' + this.piskelController.getFrameCount() + '\n'; + frameStr += '#define ' + cName.toUpperCase() + '_FRAME_WIDTH ' + width + '\n'; + frameStr += '#define ' + cName.toUpperCase() + '_FRAME_HEIGHT ' + height + '\n\n'; - frameStr += '/* Piskel \"' + this.getPiskelName_() + '\" */\n\n'; + frameStr += '/* Piskel data for \"' + this.getPiskelName_() + '\" */\n\n'; - frameStr += 'uint32_t ' + this.getPiskelName_().toLowerCase(); - frameStr += '_data[' + this.piskelController.getFrameCount() + '][' + width * height + '] = {\n'; - - for (var i = 0 ; i < this.piskelController.getFrameCount() ; i++) { + frameStr += 'uint32_t ' + cName.toLowerCase(); + frameStr += '_data[' + frameCount + '][' + width * height + '] = {\n'; + + for (var i = 0 ; i < frameCount ; i++) { var render = this.piskelController.renderFrameAt(i, true); context.clearRect(0, 0, width, height); context.fillRect(0, 0, width, height); @@ -47,30 +48,38 @@ var pix = imgd.data; frameStr += '{\n'; - for (var i = 0; i < pix.length; i += 4) { - frameStr += this.rgbToCUint(pix[i], pix[i+1], pix[i+2]); - if (i != pix.length - 4) - frameStr += ', '; - if (i % (width * 4) == 0) - frameStr += '\n'; + for (var j = 0; j < pix.length; j += 4) { + frameStr += this.rgbToCHex(pix[j], pix[j + 1], pix[j + 2]); + if (j != pix.length - 4) { + frameStr += ', '; + } + if (((j + 4) % (width * 4)) === 0) { + frameStr += '\n'; + } + } + if (i != (frameCount - 1)) { + frameStr += '},\n'; + } else { + frameStr += '}\n'; } - frameStr += '\n}'; - } - - frameStr += '};' + + frameStr += '};\n'; pskl.utils.BlobUtils.stringToBlob(frameStr, function(blob) { pskl.utils.FileUtils.downloadAsFile(blob, fileName); }.bind(this), 'application/text'); - }; ns.CExportController.prototype.getPiskelName_ = function () { return this.piskelController.getPiskel().getDescriptor().name; }; - ns.CExportController.prototype.rgbToCUint = function (r, g, b) { - return '0x' + r.toString(16).slice(-2) + g.toString(16).slice(-2) + b.toString(16).slice(-2); + ns.CExportController.prototype.rgbToCHex = function (r, g, b) { + var hexStr = '0x'; + hexStr += ('00' + r.toString(16)).substr(-2); + hexStr += ('00' + g.toString(16)).substr(-2); + hexStr += ('00' + b.toString(16)).substr(-2); + return hexStr; }; ns.CExportController.prototype.getFramesheetAsCanvas = function () {