-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSpriteParser.js
128 lines (114 loc) · 3.27 KB
/
SpriteParser.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
This file contains the code needed to parse a .spr file (Version 2)
into a JSON datastructure.
Source: http://yuraj.ucoz.com/half-life-formats.pdf
**/
define(["util/DataReader"], function(DataReader) {
var constants = {
SPR_MAGIC: 0x50534449,
SPR_VERSION: 2
};
var data;
var parseHeader = function() {
var magic = DataReader.readInteger(data, 0);
if(magic != constants.SPR_MAGIC) {
console.log("Invalid magic number for .spr file. Expected "
+ constants.SPR_MAGIC + ", but was: " + magic);
return;
}
var version = DataReader.readInteger(data, 4);
if(version != constants.SPR_VERSION) {
console.log("Invalid version number for .spr file. Expected "
+ constants.SPR_VERSION + ", but was: " + version);
return;
}
var type = DataReader.readInteger(data, 8);
var textureFormat = DataReader.readInteger(data, 12);
var boundingRadius = DataReader.readFloat(data, 16);
var maxWidth = DataReader.readInteger(data, 20);
var maxHeight = DataReader.readInteger(data, 24);
var numFrames = DataReader.readInteger(data, 28);
var beamLength = DataReader.readFloat(data, 32);
var synchType = DataReader.readInteger(data, 36);
return {
type: type,
textureFormat: textureFormat,
boundingRadius: boundingRadius,
maxWidth: maxWidth,
maxHeight: maxHeight,
numFrames: numFrames,
beamLength: beamLength,
synchType: synchType
};
};
var parsePalette = function() {
var size = DataReader.readShort(data, 40);
var end = 42 + size * 3;
var palette = Array(size);
var n = 0;
for(var i = 42; i != end; i += 3) {
var r = data[i];
var g = data[i+1];
var b = data[i+2];
palette[n++] = [r, g, b];
}
return palette;
};
var parseSingleFrame = function(palette, offset) {
var group = DataReader.readInteger(data, offset);
var originX = DataReader.readInteger(data, offset+4);
var originY = DataReader.readInteger(data, offset+8);
var width = DataReader.readInteger(data, offset+12);
var height = DataReader.readInteger(data, offset+16);
var end = width*height + offset + 20;
//The transparent colour is the last colour in the palette
var transparentColor = palette[palette.length-1];
var isTransparent = function(rgb) {
return rgb[0] === transparentColor[0] &&
rgb[1] === transparentColor[1] &&
rgb[2] === transparentColor[2];
}
var imageData = new Uint8Array(4 * width * height);
var n = 0;
for(var i = offset+20; i != end; ++i) {
var rgb = palette[data[i]];
imageData[n++] = rgb[0]
imageData[n++] = rgb[1];
imageData[n++] = rgb[2];
imageData[n++] = isTransparent(rgb) ? 0 : 255;
}
return {
group: group,
originX: originX,
originY: originY,
width: width,
height: height,
imageData: imageData
};
};
var parseFrames = function(palette) {
var end = data.length;
var i = 42 + palette.length * 3;
var frames = [];
while(i != end) {
var frame = parseSingleFrame(palette, i);
frames.push(frame);
//Advance to the next frame
i += frame.width * frame.height + 20;
}
return frames;
};
return {
parse: function(input) {
data = input;
var header = parseHeader();
if(!header) return;
var palette = parsePalette();
var frames = parseFrames(palette);
return {
header: header,
frames: frames
};
}
};
});