This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_buildGui.js
61 lines (54 loc) · 1.66 KB
/
_buildGui.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
var Jimp = require("jimp")
, Promise = require('bluebird')
, fs = Promise.promisifyAll(require("fs"))
, _ = require('underscore')
, _inJobs = 0;
var imgToBuf = function(img) {
return new Promise(function(resolve, reject) {
var c = 0;
Jimp.read("./gui/png/"+img).then(function (image) {
var buf = new Buffer((image.bitmap.width*image.bitmap.height)*2);
image.dither565();
for (var x=0;x<image.bitmap.width;x++) {
for (var y=0;y<image.bitmap.height;y++) {
var b = image.getPixelColor(x,y);
var rgba = Jimp.intToRGBA(b);
var _p = rgba.r << 8 | rgba.g << 3 | rgba.b >> 3;
buf.writeUInt16LE(_p, c, 2);
c+=2;
}
}
resolve([buf, image.bitmap.width, image.bitmap.height]);
});
});
}
var promises = []
, pngs = [];
fs.openAsync('./gui/ui', 'w')
.then(fs.openAsync('./gui/struct.h', 'w'))
.then(fs.readdirSync('./gui/png').forEach(function(file) {
if (file[0] != ".")
pngs.push(file);
}))
.then(function() {
// reduce here
Promise.reduce(pngs, function(offset, png) {
return createImg(png, offset);
}, 0);
});
var createImg = function(imgPath, offset) {
return new Promise(function(resolve, reject) {
var ps = imgToBuf(imgPath).spread(function(ibuf, w, h) {
fs.appendFileAsync('./gui/ui', ibuf)
.then(function() {
var name = imgPath.split(".")[0];
fs.appendFileAsync('./gui/struct.h', 'UIBitmap ' + name + ' = {' + (offset) + ',' + ibuf.length + ',' + w + ',' + h + '};\n')
.then(function() {
setTimeout(function() {
resolve(offset + ibuf.length);
}, 100);
})
})
})
})
}