-
Notifications
You must be signed in to change notification settings - Fork 4
/
transformSprites.js
81 lines (71 loc) · 1.88 KB
/
transformSprites.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
var fs = require('fs');
//assuming sprites with 16X16 dimmension
var cols = 16;
var rows = 16;
/* byte version..
var createSprite = function(name, byteArray){
var wstream = fs.createWriteStream('./app/sprites/'+name);
//var buffer = new Buffer(byteArray);
//var encoded = encodeByteArray(byteArray);
//console.log(encoded);
wstream.write(buffer);
wstream.end();
};
*/
var createSprite = function(name, byteArray){
var encoded = '';
// for (var i = 0; i < byteArray.length; i++) {
// var code=byteArray[i].toString(16);
// encoded+=code.length==1?'0'+code:code;
// };
for (var i = 0; i < byteArray.length; i++) {
var b = byteArray[i];
var code = '';
if(b<0X55){
code = '{';
}else if(b<0XAA){
code = '';
}else{
code = '~';
}
var byte = b%0X55+0X21;
code += byte==92?'\\':'';
code += String.fromCharCode(byte);
encoded+=code;
}
fs.writeFile('./sprites/'+name, encoded, function(err){
if(err){
console.log('exception:', err);
return;
}
console.log('file created');
});
}
var createImage = function(name, byteArray){
var png = new Png(new Buffer(byteArray), ~~(byteArray.length/3), 1);
png.encode(function(encodedPng){
fs.writeFileSync('./sprites/'+name+'.png', encodedPng.toString('binary'), 'binary');
});
};
var transformSprite = function(file){
var spritestream = fs.readFile('./sprites/' + file + '.sprite', 'binary', function(err, data){
if(err){
console.log(err);
return;
}
var byteArray = [];
data = data.replace(/\n/g, '');
for (var i = 0; i < data.length; i++) {
var character = data[i];
if(character==='M'){
byteArray.push(i);
}
if(character==='/'){
break;
}
};
createSprite(file.replace('.sprite', ''), byteArray);
//createImage(file[0], byteArray);
});
};
transformSprite('water01');