Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/opengl/CompositeImageData.java
1461 views
1
package org.newdawn.slick.opengl;
2
3
import java.io.BufferedInputStream;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.nio.ByteBuffer;
7
import java.util.ArrayList;
8
9
import org.newdawn.slick.util.Log;
10
11
/**
12
* A composite data source that checks multiple loaders in order of
13
* preference
14
*
15
* @author kevin
16
*/
17
public class CompositeImageData implements LoadableImageData {
18
/** The list of images sources in order of preference to try loading the data with */
19
private ArrayList sources = new ArrayList();
20
/** The data source that worked and was used - or null if no luck */
21
private LoadableImageData picked;
22
23
/**
24
* Add a potentional source of image data
25
*
26
* @param data The data source to try
27
*/
28
public void add(LoadableImageData data) {
29
sources.add(data);
30
}
31
32
/**
33
* @see org.newdawn.slick.opengl.LoadableImageData#loadImage(java.io.InputStream)
34
*/
35
public ByteBuffer loadImage(InputStream fis) throws IOException {
36
return loadImage(fis, false, null);
37
}
38
39
/**
40
* @see org.newdawn.slick.opengl.LoadableImageData#loadImage(java.io.InputStream, boolean, int[])
41
*/
42
public ByteBuffer loadImage(InputStream fis, boolean flipped, int[] transparent) throws IOException {
43
return loadImage(fis, flipped, false, transparent);
44
}
45
46
/**
47
* @see org.newdawn.slick.opengl.LoadableImageData#loadImage(java.io.InputStream, boolean, boolean, int[])
48
*/
49
public ByteBuffer loadImage(InputStream is, boolean flipped, boolean forceAlpha, int[] transparent) throws IOException {
50
CompositeIOException exception = new CompositeIOException();
51
ByteBuffer buffer = null;
52
53
BufferedInputStream in = new BufferedInputStream(is, is.available());
54
in.mark(is.available());
55
56
// cycle through our source until one of them works
57
for (int i=0;i<sources.size();i++) {
58
in.reset();
59
try {
60
LoadableImageData data = (LoadableImageData) sources.get(i);
61
62
buffer = data.loadImage(in, flipped, forceAlpha, transparent);
63
picked = data;
64
break;
65
} catch (Exception e) {
66
Log.warn(sources.get(i).getClass()+" failed to read the data", e);
67
exception.addException(e);
68
}
69
}
70
71
if (picked == null) {
72
throw exception;
73
}
74
75
return buffer;
76
}
77
78
/**
79
* Check the state of the image data and throw a
80
* runtime exception if theres a problem
81
*/
82
private void checkPicked() {
83
if (picked == null) {
84
throw new RuntimeException("Attempt to make use of uninitialised or invalid composite image data");
85
}
86
}
87
88
/**
89
* @see org.newdawn.slick.opengl.ImageData#getDepth()
90
*/
91
public int getDepth() {
92
checkPicked();
93
94
return picked.getDepth();
95
}
96
97
/**
98
* @see org.newdawn.slick.opengl.ImageData#getHeight()
99
*/
100
public int getHeight() {
101
checkPicked();
102
103
return picked.getHeight();
104
}
105
106
/**
107
* @see org.newdawn.slick.opengl.ImageData#getImageBufferData()
108
*/
109
public ByteBuffer getImageBufferData() {
110
checkPicked();
111
112
return picked.getImageBufferData();
113
}
114
115
/**
116
* @see org.newdawn.slick.opengl.ImageData#getTexHeight()
117
*/
118
public int getTexHeight() {
119
checkPicked();
120
121
return picked.getTexHeight();
122
}
123
124
/**
125
* @see org.newdawn.slick.opengl.ImageData#getTexWidth()
126
*/
127
public int getTexWidth() {
128
checkPicked();
129
130
return picked.getTexWidth();
131
}
132
133
/**
134
* @see org.newdawn.slick.opengl.ImageData#getWidth()
135
*/
136
public int getWidth() {
137
checkPicked();
138
139
return picked.getWidth();
140
}
141
142
/**
143
* @see org.newdawn.slick.opengl.LoadableImageData#configureEdging(boolean)
144
*/
145
public void configureEdging(boolean edging) {
146
for (int i=0;i<sources.size();i++) {
147
((LoadableImageData) sources.get(i)).configureEdging(edging);
148
}
149
}
150
151
}
152
153