public class ResourceLoader extends Object
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 |
|---|---|
static String |
FIGURE_DECLARATION_EXT
Extension used by Figure Declarations
|
static org.slf4j.Logger |
LOGGER
The Main Logger of the ResourceLoader
|
static String |
SHADER_PROGRAM_EXT
Extension used by Shader Programs
|
| Constructor and Description |
|---|
ResourceLoader() |
| Modifier and Type | Method and Description |
|---|---|
static void |
addResourceLocation(ResourceLocation location)
Add a ResourceLocation to the search path.
|
static void |
clearResourceLocations()
Clears all ResourceLocations from the search path.
|
static boolean |
exists(String name)
Checks if a file exists in one of the search locations
|
static Figure |
getFigure(String name)
Loads a
Figure from a json
declaration file. |
static Figure |
getFigure(String name,
boolean forceLoading)
Loads a
Figure from a json
declaration file. |
static 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). |
static 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). |
static List<ResourceLocation> |
getResourceLocations()
Returns all
ResourceLocations. |
static String |
getShader(String name)
Loads a shader File and prepares it for OpenGL.
|
static String |
getShader(String name,
boolean forceLoading)
Loads a shader File and prepares it for OpenGL.
|
static String |
getShader(String name,
boolean forceLoading,
LineBasedParser parser)
Loads a shader File and prepares it for OpenGL.
|
static 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)
|
static 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. |
static InputStream |
getStream(String name)
Searches through all ResourceLocations and returns readable InputStream
NOTE: The first occurrence of an file will get used.
|
static Texture |
getTexture(String name)
Loads a image file and converts it into a
Texture. |
static Texture |
getTexture(String name,
Dimension dimension)
Loads a image file and converts it into a
Texture. |
static Texture |
getTexture(String name,
Dimension dimension,
boolean forceLoading)
Loads a image file and converts it into a
Texture. |
static URL |
getURL(String name)
Searches through all ResourceLocations and returns the full URL
NOTE: The first occurrence of an file will get used.
|
static <T> T |
load(String name,
AsciiFileLoader<T> loader,
boolean forceLoading)
Loads a Object from a text-file using a
AsciiFileLoader. |
static <T,C> T |
load(String name,
BinaryLoader<T,C> loader,
boolean forceLoading)
Loads a Object from an
InputStream using a AsciiFileLoader. |
static void |
preLoadMesh(String name)
Pre Load a Mesh, good at startup, to allow getting the Mesh at runtime.
|
static void |
preLoadShader(String name)
Pre Load a Shader source file.
|
static void |
preLoadTexture(String name)
Pre Load a Texture, good at startup, to allow getting the Texture at runtime.
|
static void |
pushResourceLocation(ResourceLocation location) |
static String |
readFile(String name)
Reads a file into a String.
|
static String |
readFile(String name,
boolean forceLoading)
Reads a file into a String.
|
static String |
readFile(String name,
boolean forceLoading,
LineBasedParser parser)
Reads a file into a String.
|
static 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
public static void addResourceLocation(ResourceLocation location)
location - This will get added to the search path.public static void pushResourceLocation(ResourceLocation location)
(Prepends)public static void removeResourceLocation(ResourceLocation location)
location - Will get removed from the search path.public static void clearResourceLocations()
public static List<ResourceLocation> getResourceLocations()
ResourceLocations.public static void preLoadMesh(String name) throws Exception
name - The relative path (to various ResourceLocations) of the filenameIOExceptionExceptiongetMesh(String)public static void preLoadTexture(String name) throws Exception
name - The relative path (to various ResourceLocations) of the filenameIOExceptionExceptiongetTexture(String)public static void preLoadShader(String name) throws Exception
name - The relative path (to various ResourceLocations) of the filenameIOExceptionExceptiongetShader(String)public static 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.name - The name of the file to loadExceptionpublic static 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.name - The name of the file to loadforceLoading - if set to true the file will get read again and not read from cacheExceptionpublic static 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.name - The relative path (to various ResourceLocations) of the filenameExceptionpublic static 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.name - The relative path (to various ResourceLocations) of the filenameforceLoading - if set to true the file will get read again and not read from cacheExceptionpublic static 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.name - The relative path (to various ResourceLocations) of the filenameIOException - if resource not foundExceptionpublic static 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.name - The relative path (to various ResourceLocations) of the filenamedimension - The dimension of the new textureIOException - if resource not foundExceptionpublic static 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.name - 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 static String getShader(String name) throws Exception
name - The relative path (to various ResourceLocations) of the filenameIOException - if resource not foundExceptiongetShader(String, boolean)public static String getShader(String name, boolean forceLoading) throws Exception
GLSLParser)name - The relative path (to various ResourceLocations) of the filenameforceLoading - if set to true the file will get read again and not read from cacheExceptiongetShader(String, boolean, org.achtern.AchternEngine.core.resource.fileparser.LineBasedParser)public static 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.name - 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.Exceptionpublic static GLSLProgram getShaderProgram(String name) throws Exception
name - Name of the program declarationIOExceptionExceptiongetShaderProgram(String, boolean)public static 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.name - Name of the program declarationforceLoading - if set to true the file will get read again and not read from cacheExceptionpublic static <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.T - 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 cacheExceptionpublic static <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.T - 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 cacheExceptionpublic static String readFile(String name) throws IOException
\n.
If a file with the same name (the same file?) a cached value will be used.name - The name of the file to loadIOExceptionpublic static 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.name - The name of the file to loadforceLoading - if set to true the file will get read again and not read from cacheIOExceptionpublic static 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.name - 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)IOExceptionpublic static boolean exists(String name)
name - The relative path (to various ResourceLocations) of the filenamepublic static InputStream getStream(String name) throws IOException
name - The relative path (to various ResourceLocations) of the filenameIOException - if resource not foundpublic static URL getURL(String name) throws IOException
name - The relative path (to various ResourceLocations) of the filenameIOException - if resource not foundCopyright © 2014. All rights reserved.