Forked from juliandescottes/export_to_unit8_t.js
Last active
January 13, 2019 22:24
-
-
Save code0100fun/825e52f5e43a624e20a279118b54bb89 to your computer and use it in GitHub Desktop.
Piskel Feature request: Add unit8_t version to "Export as C file" for single color images #606
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var export_to_unit8_t = function () { | |
var HTML_NEW_LINE = '
'; | |
var width = pskl.app.piskelController.getWidth(); | |
var height = pskl.app.piskelController.getHeight(); | |
var frameCount = pskl.app.piskelController.getFrameCount(); | |
var str = ''; | |
str += '#include <stdint.h>' + HTML_NEW_LINE; | |
str += '#define SPRITE_SHEET_FRAME_COUNT ' + frameCount + HTML_NEW_LINE; | |
str += '#define SPRITE_SHEET_FRAME_WIDTH ' + width + HTML_NEW_LINE; | |
str += '#define SPRITE_SHEET_FRAME_HEIGHT ' + height + HTML_NEW_LINE; | |
str += 'static const uint32_t sprite_sheet_data[' + frameCount + '][' + width * height + '] = {' + HTML_NEW_LINE; | |
var lines = []; | |
for (i = 0; i < frameCount; i++) { | |
var frame = pskl.utils.LayerUtils.mergeFrameAt(pskl.app.piskelController.getLayers(), i); | |
frame.forEachPixel(function (color, col, row) { | |
if (!lines[row]) { | |
lines[row] = []; | |
} | |
var line = lines[row]; | |
var isTransparent = color === pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR); | |
line[col] = isTransparent ? '0' : '1'; | |
}); | |
str += lines.map(function (line) { | |
return ' B' + line.join(''); | |
}).join(',' + HTML_NEW_LINE); | |
str += HTML_NEW_LINE + '},' + HTML_NEW_LINE; | |
} | |
str += HTML_NEW_LINE + '};' + HTML_NEW_LINE; | |
window.open('data:text/html,<textarea style="width:100%;height:100%;">' + str + '</textarea>'); | |
}; | |
export_to_unit8_t(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment