forked from asalga/Horadrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpriteSheetLoader.pde
129 lines (89 loc) · 3.74 KB
/
SpriteSheetLoader.pde
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
129
/*
TODO: add rotation
*/
public class SpriteSheetLoader{
PImage test;
HashMap map;
public void load(String texturePackerMetaFile){
/* JSONObject json;
json = loadJSONObject(texturePackerMetaFile);
String imageFilename = json.getJSONObject("meta").getString("image");
// print(imageFilename);
PImage texture = loadImage(imageFilename);
JSONArray frames = json.getJSONArray("frames");
map = new HashMap<String, PImage>(frames.size());
//println(frames.size());
// Iterate over all the sprites in the json file
for(int i = 0; i < frames.size(); i++){
//
JSONObject frame = frames.getJSONObject(i);
// Regardless of whether the sprite is rotated or not, it's final dimensions
// can be extracted from sourceSize.
JSONObject sourceSize = frame.getJSONObject("sourceSize");
int dstSpriteWidth = sourceSize.getInt("w");
int dstSpriteHeight = sourceSize.getInt("h");
PImage img = createImage(dstSpriteWidth, dstSpriteHeight, ARGB);
println("Created image: (" + dstSpriteWidth + "," + dstSpriteHeight + ")");
// Pull the actual sprite from the sheet
JSONObject dimensions = frame.getJSONObject("frame");
int srcStartX = dimensions.getInt("x");
int srcStartY = dimensions.getInt("y");
int srcWidth = dimensions.getInt("w");
int srcHeight = dimensions.getInt("h");
// This is actually our destination, where we copy the pixels into the image.
JSONObject spriteSourceSize = frame.getJSONObject("spriteSourceSize");
int dstStartX = spriteSourceSize.getInt("x");
int dstStartY = spriteSourceSize.getInt("y");
int dw = spriteSourceSize.getInt("w");
int dh = spriteSourceSize.getInt("h");
//println("dw: " + dw);
//println("dh: " + dh);
// println("dstSpriteWidth: " + dstSpriteWidth);
//println("dstSpriteHeight: " + dstSpriteHeight);
// If the sprite isn't rotated, our logic is much simpler so we break up the cases.
if(frame.getBoolean("rotated")){
println("sprite is rotated");
// Going to copy the pixels starting from the sprite in the sheet
// upper left corner downwards moving left to right
// inverted for src
//int dstWidth = 400;
// int dstHeight = 324;
// We only need to copy the trimmed part
int pixelsCopied = 0;
int pixelsToCopy = srcWidth * srcHeight;
int srcX = srcStartX;
int srcY = srcStartY;
int dstX = dstStartX;
int dstY = dh - 1;
//println(dw);
int srcRotHeight = srcWidth;
//println(srcRotHeight);
for( ;pixelsCopied < pixelsToCopy; pixelsCopied++, dstX++, srcY++){
//
if(srcY == srcRotHeight + srcStartY){
srcY = srcStartY;
srcX++;
dstX = dstStartX;
dstY--;
}
// println(dstY*dw+dstX);
color c = texture.pixels[srcY * texture.width + srcX];
//
img.pixels[dstY * dstSpriteWidth + dstX] = c;
}
img.updatePixels();
map.put(frame.getString("filename"), img);
}
else{
println("sprite is not rotated");
// Copy from the sprite sheet to our individual sprite
// potentially not touching the transparent pixels that border the sprite.
img.copy(texture, srcStartX, srcStartY, srcWidth, srcHeight, dstStartX, dstStartY, dw, dh);
map.put(frame.getString("filename"), img);
}
}*/
}
public PImage get(String sprite){
return (PImage)map.get(sprite);
}
}