public class BasicResourceLoader extends Object implements ResourceLoaderProvider
ResourceLoader is the main entry point
to the FileSystem and other Resources.
The ResourceLoader allows you to load Meshes and
Figures,
Shaders
(GLSLProgram),
Textures and
any type of String from a file.
You can easily load your own resources from any type of file.
You can get URLs and InputStreams for any filename or write your
own AsciiFileLoader to load text-based files.
The ResourceLoader looks into given ResourceLocations in
order to get URLs and InputStreams for the given name.
By default the Classpath, Bundled Textures, Meshes, Figures, Shaders and the Local File System
are added as ResourceLocation by default. You can add your own by using addResourceLocation(ResourceLocation)
or remove all clearResourceLocations().
The ResourceLoader uses a very basic ResourceCaches in order
to avoid re-reading data from disk. These caches - however - are not caching object, only the data.| Modifier and Type | Field and Description |
|---|---|
protected ResourceCache |
binaryCache |
static String |
FIGURE_DECLARATION_EXT
Extension used by Figure Declarations
|
protected ResourceCache<String> |
fileCache |
protected List<ResourceLocation> |
locations
This list contains locations to look for resources
|
static org.slf4j.Logger |
LOGGER
The Main Logger of the ResourceLoader
|
static String |
SHADER_PROGRAM_EXT
Extension used by Shader Programs
|
| Constructor and Description |
|---|
BasicResourceLoader()
Add default locations:
Classpath
Bundled Textures
Bundled Models
Bundled Figures
Bundled Shaders
Bundled Shader Programs
Local File System
|
| Modifier and Type | Method and Description |
|---|---|
void |
addResourceLocation(ResourceLocation location)
Add a ResourceLocation to the search path.
|
void |
clearResourceLocations()
Clears all ResourceLocations from the search path.
|
boolean |
exists(String name)
Checks if a file exists in one of the search locations
|
AudioSource |
getAudioSource(String name) |
AudioSource |
getAudioSource(String name,
boolean forceLoading) |
Figure |
getFigure(String name)
Loads a
Figure from a json
declaration file. |
Figure |
getFigure(String name,
boolean forceLoading)
Loads a
Figure from a json
declaration file. |
Mesh |
getMesh(String name)
Loads a .obj file, parses it, optimises it and converts it into renderable
Mesh
When loading from cache only the data will get reused, not the object itself and it
will have to use new buffers (handled automatically). |
Mesh |
getMesh(String name,
boolean forceLoading)
Loads a .obj file, parses it, optimises it and converts it into renderable
Mesh
When loading from cache only the data will get reused, not the object itself and it
will have to use new buffers (handled automatically). |
List<ResourceLocation> |
getResourceLocations()
Returns all
ResourceLocations. |
String |
getShader(String name)
Loads a shader File and prepares it for OpenGL.
|
String |
getShader(String name,
boolean forceLoading)
Loads a shader File and prepares it for OpenGL.
|
String |
getShader(String name,
boolean forceLoading,
LineBasedParser parser)
Loads a shader File and prepares it for OpenGL.
|
GLSLProgram |
getShaderProgram(String name)
Reads programm file from disk and loads the stated source files
(uses a internal cache if the shader has been loaded previously)
|
GLSLProgram |
getShaderProgram(String name,
boolean forceLoading)
Reads programm file from disk and loads the stated source files
Calls
load(String, org.achtern.AchternEngine.core.resource.loader.AsciiFileLoader, boolean)
internally and uses the GLSLProgramLoader. |
InputStream |
getStream(String name)
Searches through all ResourceLocations and returns readable InputStream
NOTE: The first occurrence of an file will get used.
|
Texture |
getTexture(String name)
Loads a image file and converts it into a
Texture. |
Texture |
getTexture(String name,
Dimension dimension)
Loads a image file and converts it into a
Texture. |
Texture |
getTexture(String name,
Dimension dimension,
boolean forceLoading)
Loads a image file and converts it into a
Texture. |
URL |
getURL(String name)
Searches through all ResourceLocations and returns the full URL
NOTE: The first occurrence of an file will get used.
|
<T> T |
load(String name,
AsciiFileLoader<T> loader,
boolean forceLoading)
Loads a Object from a text-file using a
AsciiFileLoader. |
<T,C> T |
load(String name,
BinaryLoader<T,C> loader,
boolean forceLoading)
Loads a Object from an
InputStream using a AsciiFileLoader. |
void |
preLoadMesh(String name)
Pre Load a Mesh, good at startup, to allow getting the Mesh at runtime.
|
void |
preLoadShader(String name)
Pre Load a Shader source file.
|
void |
preLoadTexture(String name)
Pre Load a Texture, good at startup, to allow getting the Texture at runtime.
|
void |
pushResourceLocation(ResourceLocation location) |
String |
readFile(String name)
Reads a file into a String.
|
String |
readFile(String name,
boolean forceLoading)
Reads a file into a String.
|
String |
readFile(String name,
boolean forceLoading,
LineBasedParser parser)
Reads a file into a String.
|
void |
removeResourceLocation(ResourceLocation location)
Removes a ResourceLocation from the search path
|
public static final org.slf4j.Logger LOGGER
public static final String SHADER_PROGRAM_EXT
public static final String FIGURE_DECLARATION_EXT
protected ResourceCache binaryCache
protected ResourceCache<String> fileCache
protected List<ResourceLocation> locations
public BasicResourceLoader()
public void addResourceLocation(ResourceLocation location)
addResourceLocation in interface ResourceLoaderProviderlocation - This will get added to the search path.public void pushResourceLocation(ResourceLocation location)
pushResourceLocation in interface ResourceLoaderProvider(Prepends)public void removeResourceLocation(ResourceLocation location)
removeResourceLocation in interface ResourceLoaderProviderlocation - Will get removed from the search path.public void clearResourceLocations()
clearResourceLocations in interface ResourceLoaderProviderpublic List<ResourceLocation> getResourceLocations()
ResourceLocations.getResourceLocations in interface ResourceLoaderProviderpublic void preLoadMesh(String name) throws Exception
preLoadMesh in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenameIOException - if loading failsExceptiongetMesh(String)public void preLoadTexture(String name) throws Exception
preLoadTexture in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenameIOException - if loading failsExceptiongetTexture(String)public void preLoadShader(String name) throws Exception
preLoadShader in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenameIOException - if loading failsExceptiongetShader(String)public Figure getFigure(String name) throws Exception
Figure from a json
declaration file. Read more about it here FigureLoader
When loading from cache, only the file itself will get reused. This returns a completely independent Figure.
An IOException is thrown on read errors and other Exceptions might be
thrown from the Figure itself.getFigure in interface ResourceLoaderProvidername - The name of the file to loadException - if loading/parsing/processing failspublic Figure getFigure(String name, boolean forceLoading) throws Exception
Figure from a json
declaration file. Read more about it here FigureLoader
When loading from cache, only the file itself will get reused. This returns a completely independent Figure.
An IOException is thrown on read errors and other Exceptions might be
thrown from the Figure itself.getFigure in interface ResourceLoaderProvidername - The name of the file to loadforceLoading - if set to true the file will get read again and not read from cacheException - if loading/parsing/processing failspublic Mesh getMesh(String name) throws Exception
Mesh
When loading from cache only the data will get reused, not the object itself and it
will have to use new buffers (handled automatically).
An IOException is thrown on read errors and other Exceptions might be
thrown from the GLSLProgramLoader itself.getMesh in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenameException - if loading/parsing/processing failspublic Mesh getMesh(String name, boolean forceLoading) throws Exception
Mesh
When loading from cache only the data will get reused, not the object itself and it
will have to use new buffers (handled automatically).
An IOException is thrown on read errors and other Exceptions might be
thrown from the GLSLProgramLoader itself.getMesh in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenameforceLoading - if set to true the file will get read again and not read from cacheException - if loading/parsing/processing failspublic Texture getTexture(String name) throws Exception
Texture.
If the same image has been loaded previously the
TexturableData will be
used to create the new Texture object.getTexture in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenameIOException - if resource not foundExceptionpublic Texture getTexture(String name, Dimension dimension) throws Exception
Texture.
If the same image has been loaded previously the
TexturableData will be
used to create the new Texture object.
The Dimension can be used to modify the width and height
of the Texture. The image will get resized.getTexture in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenamedimension - The dimension of the new textureIOException - if resource not foundExceptionpublic Texture getTexture(String name, Dimension dimension, boolean forceLoading) throws Exception
Texture.
If the same image has been loaded previously the
TexturableData will be
used to create the new Texture object.
The Dimension can be used to modify the width and height
of the Texture. The image will get resized.getTexture in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenamedimension - The dimension of the new textureforceLoading - if set to true the file will get read again and not read from cacheIOException - if resource not foundExceptionpublic String getShader(String name) throws Exception
getShader in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenameIOException - if resource not foundExceptiongetShader(String, boolean)public String getShader(String name, boolean forceLoading) throws Exception
GLSLParser)getShader in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenameforceLoading - if set to true the file will get read again and not read from cacheException - if loading/parsing/processing failsgetShader(String, boolean, org.achtern.AchternEngine.core.resource.fileparser.LineBasedParser)public String getShader(String name, boolean forceLoading, LineBasedParser parser) throws Exception
load(String, org.achtern.AchternEngine.core.resource.loader.AsciiFileLoader, boolean)
internally and uses the ShaderSourceLoader.
An IOException is thrown on read errors and other Exceptions might be
thrown from the ShaderSourceLoader itself.getShader in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenameforceLoading - if set to true the file will get read again and not read from cacheparser - The optional parser to modify the shader lines.Exception - if loading/parsing/processing failspublic GLSLProgram getShaderProgram(String name) throws Exception
getShaderProgram in interface ResourceLoaderProvidername - Name of the program declarationIOException - if loading failsExceptiongetShaderProgram(String, boolean)public GLSLProgram getShaderProgram(String name, boolean forceLoading) throws Exception
load(String, org.achtern.AchternEngine.core.resource.loader.AsciiFileLoader, boolean)
internally and uses the GLSLProgramLoader.
An IOException is thrown on read errors and other Exceptions might be
thrown from the GLSLProgramLoader itself.getShaderProgram in interface ResourceLoaderProvidername - Name of the program declarationforceLoading - if set to true the file will get read again and not read from cacheException - if loading/parsing/processing failspublic AudioSource getAudioSource(String name) throws Exception
getAudioSource in interface ResourceLoaderProviderExceptionpublic AudioSource getAudioSource(String name, boolean forceLoading) throws Exception
getAudioSource in interface ResourceLoaderProviderExceptionpublic <T> T load(String name, AsciiFileLoader<T> loader, boolean forceLoading) throws Exception
AsciiFileLoader.
Calls readFile(String) internally.
An IOException is thrown on read errors and other Exceptions might be
thrown from the Loader itself.load in interface ResourceLoaderProviderT - The type of Object to loadname - The name of the file to loadloader - The loader used to convert the file into the ObjectforceLoading - if set to true the file will get read again and not read from cacheException - if loading/parsing/processing failspublic <T,C> T load(String name, BinaryLoader<T,C> loader, boolean forceLoading) throws Exception
InputStream using a AsciiFileLoader.
Calls getStream(String) internally.
An IOException is thrown on read errors and other Exceptions might be
thrown from the Loader itself.load in interface ResourceLoaderProviderT - The type of Object to loadC - The type to cache the dataname - The name of the file to loadloader - The loader used to convert the file into the ObjectforceLoading - if set to true the file will get read again and not read from cacheException - if loading/parsing/processing failspublic String readFile(String name) throws IOException
\n.
If a file with the same name (the same file?) a cached value will be used.readFile in interface ResourceLoaderProvidername - The name of the file to loadIOException - if loading failspublic String readFile(String name, boolean forceLoading) throws IOException
\n.
If a file with the same name (the same file?) a cached value will be used.readFile in interface ResourceLoaderProvidername - The name of the file to loadforceLoading - if set to true the file will get read again and not read from cacheIOException - if loading failspublic String readFile(String name, boolean forceLoading, LineBasedParser parser) throws IOException
\n.
If a file with the same name (the same file?) a cached value will be used.
The LineBasedParser doesn't run when loaded from cache, but any modifications
made by the parser are stored into the cache.readFile in interface ResourceLoaderProvidername - The name of the file to loadforceLoading - if set to true the file will get read again and not read from cacheparser - The parser gets called on every line (can be null)IOException - if loading failspublic boolean exists(String name)
exists in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenamepublic InputStream getStream(String name) throws IOException
getStream in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenameIOException - if resource not foundpublic URL getURL(String name) throws IOException
getURL in interface ResourceLoaderProvidername - The relative path (to various ResourceLocations) of the filenameIOException - if resource not foundCopyright © 2015. All rights reserved.