module Eagle::Assets

Overview

Loads and caches game files: images, textures, sounds, fonts, shaders and meshes.

Each loader caches by path, so asking for the same file twice returns the same object and costs nothing. The load methods on Texture, Sound, Font, Shader and Mesh all go through here.

Paths can be absolute, relative to the assets folder, or prefixed with res://. The assets folder is found in this order:

  1. the EAGLE_ASSETS environment variable
  2. Config#assets_dir
  3. an assets/ folder next to the executable
  4. an assets/ folder in the working directory
  5. the working directory itself

Files embedded with Eagle.embed_assets take priority over the disk.

tex = Assets.texture("res://sprites/hero.png")
level = Assets.read("res://levels/1.txt")
Assets.invalidate("res://levels/1.txt") # re-read on the next load, for hot reload

Defined in:

eagle/assets/assets.cr

Class Method Summary

Class Method Detail

def self.add_search_path(path : String) : Nil #

Adds another folder to search when a file isn't in the assets folder, such as a mod folder.


def self.clear : Nil #

Forgets every cached asset and frees GPU objects. Use it between levels that share nothing, or before reloading everything.


def self.embedded?(p : String) : Bool #

True when p was embedded with Eagle.embed_assets.


def self.embedded_paths : Array(String) #

Every embedded path.


def self.exists?(p : String) : Bool #

True when the asset exists, embedded or on disk.


def self.font(p : String, size : Number) : Font #

Loads and caches a TrueType Font at a pixel size.


def self.image(p : String) : Image #

Loads and caches an Image.


def self.invalidate(p : String) : Nil #

Forgets one cached entry, so the next load reads the file again.


def self.mesh(p : String) : Mesh #

Loads and caches a Mesh from an OBJ file.


def self.path(p : String) : String #

The file path p resolves to, without checking that it exists.


def self.read(p : String) : String #

Reads a text file.


def self.read_bytes(p : String) : Bytes #

Reads a binary file. Raises AssetError when it doesn't exist.


def self.root : String #

The assets folder that relative and res:// paths resolve against.


def self.root=(path : String | Nil) #

Sets the assets folder. nil goes back to auto-detection.


def self.shader(p : String) : Shader #

Loads and caches a Shader.


def self.sound(p : String) : Sound #

Loads and caches a Sound (WAV or Ogg Vorbis).


def self.texture(p : String, filter : GPU::Filter = Texture.default_filter, wrap : GPU::Wrap = GPU::Wrap::Clamp, mipmaps : Bool = false) : Texture #

Loads and caches a Texture. The cache key includes the path only, so the first load decides the filter.