Path: blob/master/SLICK_HOME/src/org/newdawn/slick/opengl/CompositeImageData.java
1461 views
package org.newdawn.slick.opengl;12import java.io.BufferedInputStream;3import java.io.IOException;4import java.io.InputStream;5import java.nio.ByteBuffer;6import java.util.ArrayList;78import org.newdawn.slick.util.Log;910/**11* A composite data source that checks multiple loaders in order of12* preference13*14* @author kevin15*/16public class CompositeImageData implements LoadableImageData {17/** The list of images sources in order of preference to try loading the data with */18private ArrayList sources = new ArrayList();19/** The data source that worked and was used - or null if no luck */20private LoadableImageData picked;2122/**23* Add a potentional source of image data24*25* @param data The data source to try26*/27public void add(LoadableImageData data) {28sources.add(data);29}3031/**32* @see org.newdawn.slick.opengl.LoadableImageData#loadImage(java.io.InputStream)33*/34public ByteBuffer loadImage(InputStream fis) throws IOException {35return loadImage(fis, false, null);36}3738/**39* @see org.newdawn.slick.opengl.LoadableImageData#loadImage(java.io.InputStream, boolean, int[])40*/41public ByteBuffer loadImage(InputStream fis, boolean flipped, int[] transparent) throws IOException {42return loadImage(fis, flipped, false, transparent);43}4445/**46* @see org.newdawn.slick.opengl.LoadableImageData#loadImage(java.io.InputStream, boolean, boolean, int[])47*/48public ByteBuffer loadImage(InputStream is, boolean flipped, boolean forceAlpha, int[] transparent) throws IOException {49CompositeIOException exception = new CompositeIOException();50ByteBuffer buffer = null;5152BufferedInputStream in = new BufferedInputStream(is, is.available());53in.mark(is.available());5455// cycle through our source until one of them works56for (int i=0;i<sources.size();i++) {57in.reset();58try {59LoadableImageData data = (LoadableImageData) sources.get(i);6061buffer = data.loadImage(in, flipped, forceAlpha, transparent);62picked = data;63break;64} catch (Exception e) {65Log.warn(sources.get(i).getClass()+" failed to read the data", e);66exception.addException(e);67}68}6970if (picked == null) {71throw exception;72}7374return buffer;75}7677/**78* Check the state of the image data and throw a79* runtime exception if theres a problem80*/81private void checkPicked() {82if (picked == null) {83throw new RuntimeException("Attempt to make use of uninitialised or invalid composite image data");84}85}8687/**88* @see org.newdawn.slick.opengl.ImageData#getDepth()89*/90public int getDepth() {91checkPicked();9293return picked.getDepth();94}9596/**97* @see org.newdawn.slick.opengl.ImageData#getHeight()98*/99public int getHeight() {100checkPicked();101102return picked.getHeight();103}104105/**106* @see org.newdawn.slick.opengl.ImageData#getImageBufferData()107*/108public ByteBuffer getImageBufferData() {109checkPicked();110111return picked.getImageBufferData();112}113114/**115* @see org.newdawn.slick.opengl.ImageData#getTexHeight()116*/117public int getTexHeight() {118checkPicked();119120return picked.getTexHeight();121}122123/**124* @see org.newdawn.slick.opengl.ImageData#getTexWidth()125*/126public int getTexWidth() {127checkPicked();128129return picked.getTexWidth();130}131132/**133* @see org.newdawn.slick.opengl.ImageData#getWidth()134*/135public int getWidth() {136checkPicked();137138return picked.getWidth();139}140141/**142* @see org.newdawn.slick.opengl.LoadableImageData#configureEdging(boolean)143*/144public void configureEdging(boolean edging) {145for (int i=0;i<sources.size();i++) {146((LoadableImageData) sources.get(i)).configureEdging(edging);147}148}149150}151152153