org.jagatoo.loaders.textures
Class AbstractTextureLoader

java.lang.Object
  extended by org.jagatoo.loaders.textures.AbstractTextureLoader

public abstract class AbstractTextureLoader
extends java.lang.Object

Loads Textures from various image resources.

Loading (by name) works in the following order:
If the texture was already loaded and is still in the cache then the existing texture is returned. So don't modify loaded nad cached textures!
All TextureStreamLocators are tried in the order, in which they are registered.
If a stream was found then the following is tried on the stream:
All TextureImageFormatLoaders are tried in the order, in which they are registered. A texture can then be created from this AbstractTextureImage.
All TextureFormatLoaders are tried in the order, in which they are registered.
At last the fallback TextureImageFormatLoader is used to load the Texture. It will most probably use ImageIO.
The loading stops as soon as a Texture is created.


Field Summary
protected  boolean autoDotAddedOnce
           
protected  TextureImageFormatLoader fallbackTextureImageFormatLoader
           
protected  java.util.ArrayList<TextureFormatLoader> textureFormatLoaders
           
protected  java.util.ArrayList<TextureImageFormatLoader> textureImageFormatLoaders
           
protected  java.util.ArrayList<TextureStreamLocator> textureStreamLocators
           
 
Constructor Summary
protected AbstractTextureLoader()
           
protected AbstractTextureLoader(TextureCache textureCache, TextureImageFormatLoader fallbackTextureImageFormatLoader)
           
 
Method Summary
 void addTextureFormatLoader(TextureFormatLoader tsl)
          Registers a TextureStreamLoader.
 void addTextureImageFormatLoader(TextureImageFormatLoader l)
          Registers a TextureImageFormatLoader.
 TextureStreamLocatorFile addTextureStreamLocator(java.io.File file)
          Registers a TextureStreamLocatorFile.
 TextureStreamLocatorFile addTextureStreamLocator(java.lang.String filename)
          Registers a TextureStreamLocatorFile.
 void addTextureStreamLocator(TextureStreamLocator tsl)
          Registers a TextureStreamLocator.
 TextureStreamLocatorURL addTextureStreamLocator(java.net.URL url)
          Registers a TextureStreamLocatorURL.
 TextureStreamLocatorURL addTextureStreamLocator(java.net.URL baseURL, java.lang.String resName)
          Registers a TextureStreamLocatorURL.
protected  AbstractTexture checkCache(java.lang.String name, java.lang.String key)
           
protected  AbstractTexture createTextureFromTextureImage(AbstractTextureImage ti, boolean loadMipmaps, TextureFactory texFactory)
           
protected  java.lang.String generateCacheKey(java.lang.String name, boolean acceptAlpha, boolean loadMipmaps, boolean flipVertically, boolean allowStreching)
           
 TextureCache getCache()
           
 TextureImageFormatLoader getFallbackTextureImageFormatLoader()
           
protected  java.io.BufferedInputStream getInputStream(java.lang.String name)
           
protected  void initStandardFormatLoaders()
          Adds all known standard TextureImageFormatLoaders.
protected  AbstractTexture loadOrGetTexture(java.lang.String name, boolean flipVertically, boolean acceptAlpha, boolean loadMipmaps, boolean allowStreching, TextureFactory texFactory, boolean useCache, boolean writeToCache)
          Retrives the texture with the given name.
protected  AbstractTexture loadTextureFromStream(java.io.BufferedInputStream in, boolean flipVertically, boolean acceptAlpha, boolean loadMipmaps, boolean allowStreching, TextureFactory texFactory)
          Tries to load the texture from an InputStream.
protected  AbstractTexture loadTextureFromURL(java.net.URL imageURL, boolean flipVertically, boolean acceptAlpha, boolean loadMipmaps, boolean allowStreching, TextureFactory texFactory)
          Retrives the texture with the given name.
protected  AbstractTextureImage loadTextureImage(java.lang.String name, boolean flipVertically, boolean acceptAlpha, boolean allowStreching, TextureFactory texFactory)
          Retrives the texture-image with the given name.
protected  AbstractTextureImage loadTextureImageFromStream(java.io.BufferedInputStream in, boolean flipVertically, boolean acceptAlpha, boolean allowStreching, TextureFactory texFactory)
          Tries to load the texture-image from an InputStream.
protected abstract  void onTextureLoaded(AbstractTexture texture, java.lang.String resourceName)
          This method is called by the loaded after a texture haa actually been loaded, but not taken from teh cache.
 boolean removeTextureFormatLoader(TextureFormatLoader tsl)
          Removes a registered TextureStreamLoader.
 boolean removeTextureImageFormatLoader(TextureImageFormatLoader l)
          Removes a registered TextureImageFormatLoader
 boolean removeTextureStreamLocator(TextureStreamLocator tsl)
          Removes a registered TextureStreamLocator.
 TextureCache setCache(TextureCache textureCache, boolean copyCachedTextures)
          Sets the new TextureCache to be used to cache loaded Textures.
 void setFallbackTextureImageFormatLoader(TextureImageFormatLoader ftifl)
          Sets the TextureImageFormatLoader, that is used as the fallback-loader, if no other format-loader was capable of loading the requested texture resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

textureStreamLocators

protected final java.util.ArrayList<TextureStreamLocator> textureStreamLocators

autoDotAddedOnce

protected boolean autoDotAddedOnce

textureImageFormatLoaders

protected final java.util.ArrayList<TextureImageFormatLoader> textureImageFormatLoaders

textureFormatLoaders

protected final java.util.ArrayList<TextureFormatLoader> textureFormatLoaders

fallbackTextureImageFormatLoader

protected TextureImageFormatLoader fallbackTextureImageFormatLoader
Constructor Detail

AbstractTextureLoader

protected AbstractTextureLoader(TextureCache textureCache,
                                TextureImageFormatLoader fallbackTextureImageFormatLoader)

AbstractTextureLoader

protected AbstractTextureLoader()
Method Detail

setCache

public TextureCache setCache(TextureCache textureCache,
                             boolean copyCachedTextures)
Sets the new TextureCache to be used to cache loaded Textures.

Parameters:
textureCache - the new TextureCache
copyCachedTextures - if true, the currently cached Textures are copied to the new cache
Returns:
the previously used TextureCache.

getCache

public final TextureCache getCache()
Returns:
the TextureCache used to cache loaded Textures (never null).

addTextureStreamLocator

public void addTextureStreamLocator(TextureStreamLocator tsl)
Registers a TextureStreamLocator. A TextureStreamLocator returns an InputStream that holds the data for a Texture or an Image. Note: No check is performed to prevent duplicate registrations!

Parameters:
tsl - the TextureStreamLocator to add

addTextureStreamLocator

public TextureStreamLocatorURL addTextureStreamLocator(java.net.URL url)
Registers a TextureStreamLocatorURL. A TextureStreamLocator returns an InputStream that holds the data for a Texture or a Image. Note: No check is performed to prevent duplicate registrations!

Parameters:
url - the URL to create and add a TextureStreamLocatorURL from
Returns:
the created TextureStreamLocatorURL.

addTextureStreamLocator

public TextureStreamLocatorURL addTextureStreamLocator(java.net.URL baseURL,
                                                       java.lang.String resName)
                                                throws java.net.MalformedURLException
Registers a TextureStreamLocatorURL. A TextureStreamLocator returns an InputStream that holds the data for a Texture or a Image. Note: No check is performed to prevent duplicate registrations!

Parameters:
baseURL - the base-URL to create and add a TextureStreamLocatorURL from
resName - the resource-name to compose a new URL from (together with baseURL)
Returns:
the created TextureStreamLocatorURL.
Throws:
java.net.MalformedURLException

addTextureStreamLocator

public TextureStreamLocatorFile addTextureStreamLocator(java.io.File file)
Registers a TextureStreamLocatorFile. A TextureStreamLocator returns an InputStream that holds the data for a Texture or a Image. Note: No check is performed to prevent duplicate registrations!

Parameters:
file - the File to create and add a TextureStreamLocatorFile from
Returns:
the created TextureStreamLocatorFile.

addTextureStreamLocator

public TextureStreamLocatorFile addTextureStreamLocator(java.lang.String filename)
Registers a TextureStreamLocatorFile. A TextureStreamLocator returns an InputStream that holds the data for a Texture or a Image. Note: No check is performed to prevent duplicate registrations!

Parameters:
filename - the filename to create and add a TextureStreamLocatorFile from
Returns:
the created TextureStreamLocatorFile.

removeTextureStreamLocator

public boolean removeTextureStreamLocator(TextureStreamLocator tsl)
Removes a registered TextureStreamLocator.

Parameters:
tsl - the TextureStreamLocator to add
Returns:
true if a registration was removed.

addTextureImageFormatLoader

public void addTextureImageFormatLoader(TextureImageFormatLoader l)
Registers a TextureImageFormatLoader. A TextureImageFormatLoader creates a TextureImage from a given InputStream (if it has the correct format). Note: No check is performed to prevent duplicate registrations!

Parameters:
l - The TextureImageLoader.

removeTextureImageFormatLoader

public boolean removeTextureImageFormatLoader(TextureImageFormatLoader l)
Removes a registered TextureImageFormatLoader

Parameters:
l - the TextureImageLoader to be removed
Returns:
true if a registration was removed.

addTextureFormatLoader

public void addTextureFormatLoader(TextureFormatLoader tsl)
Registers a TextureStreamLoader. A TextureStreamLoader creates a Texture from a given InputStream (if it has the correct format). Note: No check is made to prevent duplicate registrations!

Parameters:
tsl - the TextureStreamLoader to add

removeTextureFormatLoader

public boolean removeTextureFormatLoader(TextureFormatLoader tsl)
Removes a registered TextureStreamLoader.

Parameters:
tsl - the TextureStreamLoader to be removed
Returns:
true if a registration was removed.

setFallbackTextureImageFormatLoader

public void setFallbackTextureImageFormatLoader(TextureImageFormatLoader ftifl)
Sets the TextureImageFormatLoader, that is used as the fallback-loader, if no other format-loader was capable of loading the requested texture resource.

Parameters:
ftifl -

getFallbackTextureImageFormatLoader

public final TextureImageFormatLoader getFallbackTextureImageFormatLoader()
Returns:
the TextureImageFormatLoader, that is used as the fallback-loader, if no other format-loader was capable of loading the requested texture resource.

createTextureFromTextureImage

protected AbstractTexture createTextureFromTextureImage(AbstractTextureImage ti,
                                                        boolean loadMipmaps,
                                                        TextureFactory texFactory)

loadTextureImageFromStream

protected AbstractTextureImage loadTextureImageFromStream(java.io.BufferedInputStream in,
                                                          boolean flipVertically,
                                                          boolean acceptAlpha,
                                                          boolean allowStreching,
                                                          TextureFactory texFactory)
Tries to load the texture-image from an InputStream.

Parameters:
in - the InputStream to load the Texture from
flipVertically - flip the image vertically or not
acceptAlpha - try to load with alpha channel or not
loadMipmaps - create mipmaps?
allowStreching - If true, the image is streched to power-of-two width and height, if necessary.
texFactory -
Returns:
the loaded TextureImage or null.

loadTextureFromStream

protected AbstractTexture loadTextureFromStream(java.io.BufferedInputStream in,
                                                boolean flipVertically,
                                                boolean acceptAlpha,
                                                boolean loadMipmaps,
                                                boolean allowStreching,
                                                TextureFactory texFactory)
Tries to load the texture from an InputStream.

Parameters:
in - the InputStream to load the Texture from
flipVertically - flip the image vertically or not
acceptAlpha - try to load with alpha channel or not
loadMipmaps - create mipmaps?
allowStreching - If true, the image is streched to power-of-two width and height, if necessary.
texFactory -
Returns:
the loaded Texture or null.

generateCacheKey

protected java.lang.String generateCacheKey(java.lang.String name,
                                            boolean acceptAlpha,
                                            boolean loadMipmaps,
                                            boolean flipVertically,
                                            boolean allowStreching)

checkCache

protected final AbstractTexture checkCache(java.lang.String name,
                                           java.lang.String key)

getInputStream

protected java.io.BufferedInputStream getInputStream(java.lang.String name)

loadTextureImage

protected AbstractTextureImage loadTextureImage(java.lang.String name,
                                                boolean flipVertically,
                                                boolean acceptAlpha,
                                                boolean allowStreching,
                                                TextureFactory texFactory)
Retrives the texture-image with the given name. Loading works in the following order:
All TextureStreamLocators are tried in the order in which they are registered.
If a stream was found then the following is tried on the stream:
All TextureImageFormatLoaders are tried in the order in which they are registered. A texture is then created with this AbstractTextureImage (mipmaps currently not implemented).
All TextureFormatLoaders are tried in the order in which they are registered.
At last the fallback TextureImageFormatLoader is used to load the Texture. It will most probably use ImageIO.
The loading stops as soon as a Texture is created.

Parameters:
name - The name of the texture.
flipVertically - flip the image vertically or not
acceptAlpha - try to load with alpha channel or not
allowStreching - If true, the image is streched to power-of-two width and height, if necessary.
texFactory -
Returns:
The AbstractTextureImage object or a null, if it was not found.

onTextureLoaded

protected abstract void onTextureLoaded(AbstractTexture texture,
                                        java.lang.String resourceName)
This method is called by the loaded after a texture haa actually been loaded, but not taken from teh cache.

Parameters:
texture -
resourceName -

loadOrGetTexture

protected AbstractTexture loadOrGetTexture(java.lang.String name,
                                           boolean flipVertically,
                                           boolean acceptAlpha,
                                           boolean loadMipmaps,
                                           boolean allowStreching,
                                           TextureFactory texFactory,
                                           boolean useCache,
                                           boolean writeToCache)
Retrives the texture with the given name. Loading works in the following order:
If the texture was already loaded and is still in the cache then the existing texture is returned. So don't modify textures returned by this method.
All TextureStreamLocators are tried in the order in which they are registered.
If a stream was found then the following is tried on the stream:
All TextureImageFormatLoaders are tried in the order in which they are registered. A texture is then created with this AbstractTextureImage (mipmaps currently not implemented).
All TextureFormatLoaders are tried in the order in which they are registered.
At last the fallback TextureImageFormatLoader is used to load the Texture. It will most probably use ImageIO.
The loading stops as soon as a Texture is created.
Mipmaps are generated, if requested.

Parameters:
name - The name of the texture.
flipVertically - flip the image vertically or not
acceptAlpha - try to load with alpha channel or not
loadMipmaps - create mipmaps?
allowStreching - If true, the image is streched to power-of-two width and height, if necessary.
texFactory -
useCache -
writeToCache - ignored, if useCache is false
Returns:
The AbstractTexture object or a null, if it was not found.

loadTextureFromURL

protected AbstractTexture loadTextureFromURL(java.net.URL imageURL,
                                             boolean flipVertically,
                                             boolean acceptAlpha,
                                             boolean loadMipmaps,
                                             boolean allowStreching,
                                             TextureFactory texFactory)
Retrives the texture with the given name. Loading works in the following order:
All TextureImageFormatLoaders are tried in the order in which they are registered. A texture is then created with this AbstractTextureImage (mipmaps currently not implemented).
All TextureFormatLoaders are tried in the order in which they are registered.
At last the fallback TextureImageFormatLoader is used to load the Texture. It will most probably use ImageIO.
The loading stops as soon as a Texture is created.
Mipmaps are generated, if requested.

Parameters:
imageURL - The name of the texture.
flipVertically - flip the image vertically or not
acceptAlpha - try to load with alpha channel or not
loadMipmaps - create mipmaps?
allowStreching - If true, the image is streched to power-of-two width and height, if necessary.
texFactory -
Returns:
The AbstractTexture object or a null, if it was not found.

initStandardFormatLoaders

protected void initStandardFormatLoaders()
Adds all known standard TextureImageFormatLoaders.