-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loader.pde
105 lines (83 loc) · 2.55 KB
/
Loader.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
//Loader class thanks to jmcouillard.com
public class Loader extends Thread {
private PApplet p;
private ConcurrentHashMap<Integer, PImage> imgs;
private int threshold;
private int framesTotal;
private boolean stop = false;
private int currentFrame = 0;
private int updateDelay = 20;
private float health = 0;
private float loops = 0;
private String source;
public Loader(PApplet p, ConcurrentHashMap<Integer, PImage> imgs, int threshold, int framesTotal, String source) {
this.p = p;
this.imgs = imgs;
this.threshold = threshold;
this.framesTotal = framesTotal;
this.source = source;
}
public void run() {
while (!stop) {
int found = 0;
for (int i=0; i<threshold; i++) {
int frame = (i + currentFrame - (threshold/2)) % framesTotal;
if (frame <0) frame = framesTotal + frame;
if (!imgs.containsKey(frame)) {
String src = source+fixedDigits(frame+1)+".png";
imgs.put(frame, p.loadImage(src));
//System.out.println("Loaded " + frame);
} else {
found++;
//System.out.println("Already exists " + frame);
}
}
health = (float)found / threshold;
loops++;
if(loops >= 25){
clean();
loops = 0;
}
// Sleep
try {
sleep(updateDelay);
}
catch (InterruptedException e) {
}
}
}
public void clean() {
for (Integer key : imgs.keySet ()) {
if (key < currentFrame - threshold) {
PImage img = imgs.get(key);
img = null;
imgs.remove(key);
}
}
}
public void setFrame(int frame) {
this.currentFrame = frame;
}
public boolean ready() {
return imgs.size() > threshold/2;
}
public float getHealth() {
return health;
}
public void setUpdateDelay(int val) {
updateDelay = val;
}
public String fixedDigits(int value) {
if (value < 10) {
return "000" + value;
} else if (value < 100) {
return "00" + value;
} else if (value < 1000) {
return "0" + value;
} else if (value < 10000) {
return "" + value;
} else {
return Integer.toString(value);
}
}
}