Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * The MIT License (MIT)
   *
   * Copyright (c) 2014 Christian Gärtner
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in all
  * copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
 
 package org.achtern.AchternEngine.core.resource;
 
 import lombok.Getter;
 import lombok.Setter;
 
 import java.net.URL;
 import java.util.List;

The ResourceLoader is the main entry point to the FileSystem and other Resources. The ResourceLoader allows you to load org.achtern.AchternEngine.core.rendering.mesh.Meshes and org.achtern.AchternEngine.core.scenegraph.entity.Figures, org.achtern.AchternEngine.core.rendering.shader.Shaders (org.achtern.AchternEngine.core.resource.fileparser.GLSLProgram), org.achtern.AchternEngine.core.rendering.texture.Textures and any type of String from a file. You can easily load your own resources from any type of file. You can get java.net.URLs and java.io.InputStreams for any filename or write your own org.achtern.AchternEngine.core.resource.loader.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(org.achtern.AchternEngine.core.resource.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.
 
 public class ResourceLoader {
 
     @Getter @Setter private static ResourceLoaderProvider provider = new BasicResourceLoader();

    
Add a ResourceLocation to the search path. NOTE: This will insert it to the very end of the List, resulting in the lowest priority when loading files. Add it to the very front (highest priority) with io.github.achtern.AchternEngine.core.resource.ResourceLoader#pushResourceLocation(ResourceLocation)

Parameters:
location This will get added to the search path.
 
     public static void addResourceLocation(ResourceLocation location) {
         .addResourceLocation(location);
     }

    

Parameters:
location This will get added to the front of the search path
See also:
addResourceLocation(org.achtern.AchternEngine.core.resource.ResourceLocation) (Prepends)
 
     public static void pushResourceLocation(ResourceLocation location) {
         .pushResourceLocation(location);
     }

    
Removes a ResourceLocation from the search path

Parameters:
location Will get removed from the search path.
 
     public static void removeResourceLocation(ResourceLocation location) {
         .removeResourceLocation(location);
     }

    
Clears all ResourceLocations from the search path.
    public static void clearResourceLocations() {
    }

    
Returns all ResourceLocations.

Returns:
List of all ResourceLocations
    public static List<ResourceLocationgetResourceLocations() {
        return .getResourceLocations();
    }

    
Pre Load a Mesh, good at startup, to allow getting the Mesh at runtime.

Parameters:
name The relative path (to various ResourceLocations) of the filename
Throws:
java.io.IOException if loading fails
See also:
getMesh(java.lang.String)
    public static void preLoadMesh(String namethrows Exception {
        .preLoadMesh(name);
    }

    
Pre Load a Texture, good at startup, to allow getting the Texture at runtime.

Parameters:
name The relative path (to various ResourceLocations) of the filename
Throws:
java.io.IOException if loading fails
See also:
getTexture(java.lang.String)
    public static void preLoadTexture(String namethrows Exception {
        .preLoadTexture(name);
    }

    
Pre Load a Shader source file. Only parses the sourcefile, no binding or uniform adding at this stage!

Parameters:
name The relative path (to various ResourceLocations) of the filename
Throws:
java.io.IOException if loading fails
See also:
getShader(java.lang.String)
    public static void preLoadShader(String namethrows Exception {
        .preLoadShader(name);
    }

    
Loads a org.achtern.AchternEngine.core.scenegraph.entity.Figure from a json declaration file. Read more about it here org.achtern.AchternEngine.core.resource.loader.json.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.

Parameters:
name The name of the file to load
Returns:
Figure (new instance)
Throws:
java.lang.Exception if loading/parsing/processing fails
    public static Figure getFigure(String namethrows Exception {
        return .getFigure(name);
    }

    
Loads a org.achtern.AchternEngine.core.scenegraph.entity.Figure from a json declaration file. Read more about it here org.achtern.AchternEngine.core.resource.loader.json.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.

Parameters:
name The name of the file to load
forceLoading if set to true the file will get read again and not read from cache
Returns:
Figure (new instance)
Throws:
java.lang.Exception if loading/parsing/processing fails
    public static Figure getFigure(String nameboolean forceLoadingthrows Exception {
        return .getFigure(nameforceLoading);
    }

    
Loads a .obj file, parses it, optimises it and converts it into renderable org.achtern.AchternEngine.core.rendering.mesh.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.

Parameters:
name The relative path (to various ResourceLocations) of the filename
Returns:
Mesh (new instance)
Throws:
java.lang.Exception if loading/parsing/processing fails
    public static Mesh getMesh(String namethrows Exception {
        return .getMesh(name);
    }

    
Loads a .obj file, parses it, optimises it and converts it into renderable org.achtern.AchternEngine.core.rendering.mesh.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.

Parameters:
name The relative path (to various ResourceLocations) of the filename
forceLoading if set to true the file will get read again and not read from cache
Returns:
Mesh (new instance)
Throws:
java.lang.Exception if loading/parsing/processing fails
    public static Mesh getMesh(String nameboolean forceLoadingthrows Exception {
        return .getMesh(nameforceLoading);
    }

    
Loads a image file and converts it into a org.achtern.AchternEngine.core.rendering.texture.Texture. If the same image has been loaded previously the org.achtern.AchternEngine.core.rendering.texture.TexturableData will be used to create the new org.achtern.AchternEngine.core.rendering.texture.Texture object.

Parameters:
name The relative path (to various ResourceLocations) of the filename
Returns:
A new Texture Object
Throws:
java.io.IOException if resource not found
    public static Texture getTexture(String namethrows Exception {
        return .getTexture(name);
    }

    
Loads a image file and converts it into a org.achtern.AchternEngine.core.rendering.texture.Texture. If the same image has been loaded previously the org.achtern.AchternEngine.core.rendering.texture.TexturableData will be used to create the new org.achtern.AchternEngine.core.rendering.texture.Texture object. The org.achtern.AchternEngine.core.rendering.Dimension can be used to modify the width and height of the Texture. The image will get resized.

Parameters:
name The relative path (to various ResourceLocations) of the filename
dimension The dimension of the new texture
Returns:
A new Texture Object
Throws:
java.io.IOException if resource not found
    public static Texture getTexture(String nameDimension dimensionthrows Exception {
        return .getTexture(namedimension);
    }

    
Loads a image file and converts it into a org.achtern.AchternEngine.core.rendering.texture.Texture. If the same image has been loaded previously the org.achtern.AchternEngine.core.rendering.texture.TexturableData will be used to create the new org.achtern.AchternEngine.core.rendering.texture.Texture object. The org.achtern.AchternEngine.core.rendering.Dimension can be used to modify the width and height of the Texture. The image will get resized.

Parameters:
name The relative path (to various ResourceLocations) of the filename
dimension The dimension of the new texture
forceLoading if set to true the file will get read again and not read from cache
Returns:
A new Texture Object
Throws:
java.io.IOException if resource not found
    public static Texture getTexture(String nameDimension dimensionboolean forceLoadingthrows Exception {
        return .getTexture(namedimensionforceLoading);
    }

    
Loads a shader File and prepares it for OpenGL. (uses a internal cache if the shader has been loaded previously)

Parameters:
name The relative path (to various ResourceLocations) of the filename
Returns:
A readable Stream | null if not exists
Throws:
java.io.IOException if resource not found
See also:
getShader(java.lang.String,boolean)
    public static String getShader(String namethrows Exception {
        return .getShader(name);
    }

    
Loads a shader File and prepares it for OpenGL. (uses a internal cache if the shader has been loaded previously) This uses the default LineBasedParser (org.achtern.AchternEngine.core.resource.fileparser.GLSLParser)

Parameters:
name The relative path (to various ResourceLocations) of the filename
forceLoading if set to true the file will get read again and not read from cache
Returns:
ShaderSource String
Throws:
java.lang.Exception if loading/parsing/processing fails
See also:
getShader(java.lang.String,boolean,org.achtern.AchternEngine.core.resource.fileparser.LineBasedParser)
    public static String getShader(String nameboolean forceLoadingthrows Exception {
        return .getShader(nameforceLoading);
    }

    
Loads a shader File and prepares it for OpenGL. Calls load(java.lang.String,org.achtern.AchternEngine.core.resource.loader.AsciiFileLoader,boolean) internally and uses the org.achtern.AchternEngine.core.resource.loader.ShaderSourceLoader. An IOException is thrown on read errors and other Exceptions might be thrown from the ShaderSourceLoader itself.

Parameters:
name The relative path (to various ResourceLocations) of the filename
forceLoading if set to true the file will get read again and not read from cache
parser The optional parser to modify the shader lines.
Returns:
ShaderSource String
Throws:
java.lang.Exception if loading/parsing/processing fails
    public static String getShader(String nameboolean forceLoadingLineBasedParser parserthrows Exception {
        return .getShader(nameforceLoadingparser);
    }

    
Reads programm file from disk and loads the stated source files (uses a internal cache if the shader has been loaded previously)

Parameters:
name Name of the program declaration
Returns:
A GLSLProgram with loaded shader sources.
Throws:
java.io.IOException if loading fails
See also:
getShaderProgram(java.lang.String,boolean)
    public static GLSLProgram getShaderProgram(String namethrows Exception {
        return .getShaderProgram(name);
    }

    
Reads programm file from disk and loads the stated source files Calls load(java.lang.String,org.achtern.AchternEngine.core.resource.loader.AsciiFileLoader,boolean) internally and uses the org.achtern.AchternEngine.core.resource.loader.GLSLProgramLoader. An IOException is thrown on read errors and other Exceptions might be thrown from the GLSLProgramLoader itself.

Parameters:
name Name of the program declaration
forceLoading if set to true the file will get read again and not read from cache
Returns:
A GLSLProgram with loaded shader sources.
Throws:
java.lang.Exception if loading/parsing/processing fails
    public static GLSLProgram getShaderProgram(String nameboolean forceLoadingthrows Exception {
        return .getShaderProgram(nameforceLoading);
    }

    
Loads an AudioFile from disk and converts it into an OpenAL compatible format. This will not upload the data to the OpenAL Engine yet. TODO: calls which class? An IOException is thrown on read errors and other Exceptions might be thrown from the TODO: CLASS itself.

Parameters:
name Name of the audio file
Returns:
OpenAL AudioSource (not yet uploaded)
Throws:
java.lang.Exception if loading/processing fails
    public static AudioSource getAudioSource(String namethrows Exception {
        return .getAudioSource(name);
    }

    
Loads an AudioFile from disk and converts it into an OpenAL compatible format. This will not upload the data to the OpenAL Engine yet. TODO: calls which class? An IOException is thrown on read errors and other Exceptions might be thrown from the TODO: CLASS itself.

Parameters:
name Name of the audio file
forceLoading if set to true the file will get read again and not read from cache
Returns:
OpenAL AudioSource (not yet uploaded)
Throws:
java.lang.Exception if loading/processing fails
    public static AudioSource getAudioSource(String nameboolean forceLoadingthrows Exception {
        return .getAudioSource(nameforceLoading);
    }

    
Loads a Object from a text-file using a org.achtern.AchternEngine.core.resource.loader.AsciiFileLoader. Calls readFile(java.lang.String) internally. An IOException is thrown on read errors and other Exceptions might be thrown from the Loader itself.

Parameters:
name The name of the file to load
loader The loader used to convert the file into the Object
forceLoading if set to true the file will get read again and not read from cache
<T> The type of Object to load
Returns:
The loaded object
Throws:
java.lang.Exception if loading/parsing/processing fails
    public static <T> T load(String nameAsciiFileLoader<T> loaderboolean forceLoadingthrows Exception {
        return .load(nameloaderforceLoading);
    }

    
Loads a Object from an java.io.InputStream using a org.achtern.AchternEngine.core.resource.loader.AsciiFileLoader. Calls getStream(java.lang.String) internally. An IOException is thrown on read errors and other Exceptions might be thrown from the Loader itself.

Parameters:
name The name of the file to load
loader The loader used to convert the file into the Object
forceLoading if set to true the file will get read again and not read from cache
<T> The type of Object to load
<C> The type to cache the data
Returns:
The loaded object
Throws:
java.lang.Exception if loading/parsing/processing fails
    @SuppressWarnings("unchecked")
    public static <T, C> T load(String nameBinaryLoader<T, C> loaderboolean forceLoadingthrows Exception {
        return .load(nameloaderforceLoading);
    }

    
Reads a file into a String. Lines are separated by \n. If a file with the same name (the same file?) a cached value will be used.

Parameters:
name The name of the file to load
Returns:
The read file
Throws:
java.io.IOException if loading fails
    public static String readFile(String namethrows IOException {
        return .readFile(name);
    }

    
Reads a file into a String. Lines are separated by \n. If a file with the same name (the same file?) a cached value will be used.

Parameters:
name The name of the file to load
forceLoading if set to true the file will get read again and not read from cache
Returns:
The read file
Throws:
java.io.IOException if loading fails
    public static String readFile(String nameboolean forceLoadingthrows IOException {
        return .readFile(nameforceLoading);
    }

    
Reads a file into a String. Lines are separated by \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.

Parameters:
name The name of the file to load
forceLoading if set to true the file will get read again and not read from cache
parser The parser gets called on every line (can be null)
Returns:
The read file
Throws:
java.io.IOException if loading fails
    public static String readFile(String nameboolean forceLoadingLineBasedParser parserthrows IOException {
        return .readFile(nameforceLoadingparser);
    }

    
Checks if a file exists in one of the search locations

Parameters:
name The relative path (to various ResourceLocations) of the filename
Returns:
True If resource was found in search path
    public static boolean exists(String name) {
        return .exists(name);
    }

    
Searches through all ResourceLocations and returns readable InputStream NOTE: The first occurrence of an file will get used. This depends on the order of ResourceLocations of the list. By default the Classpath takes precedence over bundled stuff and after that the local filesystem getting searched.

Parameters:
name The relative path (to various ResourceLocations) of the filename
Returns:
A readable Stream | null if not exists
Throws:
java.io.IOException if resource not found
    public static InputStream getStream(String namethrows IOException {
        return .getStream(name);
    }

    
Searches through all ResourceLocations and returns the full URL NOTE: The first occurrence of an file will get used. This depends on the order of ResourceLocations of the list. By default the Classpath takes precedence over bundled stuff and after that the local filesystem getting searched.

Parameters:
name The relative path (to various ResourceLocations) of the filename
Returns:
A readable Stream | null if not exists
Throws:
java.io.IOException if resource not found
    public static URL getURL(String namethrows IOException {
        return .getURL(name);
    }
New to GrepCode? Check out our FAQ X