Skip to content

Resource Management

Nicolas Cannasse edited this page Dec 2, 2015 · 16 revisions

Heaps provides a complete asset/resource management framework that can be customized with your own asset packaging system.

Getting Started

You can directly access your resources in a typed manner in Heaps by using the hxd.Res class, for instance by doing:

var tile = hxd.Res.myDirectory.myBitmap.toTile();

Please note that this is strictly typed: hxd.Res will look into the res directory of your project (or the directory specified by -D resourcePath=... haxe compilation parameter). It will then list all directories and files, and depending on the file extension, it will provide you access to the following resources:

Resources loader

The hxd.Res provides your strictly typed shortcuts to access your resources, but it does not take care of the resource loading. For this, you need to initialize a resource loader before accessing your first resource, for instance:

hxd.Res.initEmbed();

This will be the same as writing:

hxd.Res.loader = new hxd.res.Loader(hxd.fs.EmbedFileSystem.create());

A loader will cache the resources instances after they have been fetched from the underlying file system. There are several ways to store your resources.

File Systems

Resources files are accessed through a virtual file system which implements the FileSystem interface.

Heaps provides already several file systems, such as:

  • EmbedFileSystem will gives access to the resources which are embedded with your code (using haxe -resource compilation flag). On platforms such as JavaScript, this allows you to have both your code and assets stored in a single file.
  • LocalFileSystem which gives access to a local file system directory where your resources are stored. This require hard drive access so it is not available in the browser for example.
  • hxd.fmt.pak.FileSystem will read a .pak file which contains all resources packaged into a single binary. The PAK file can be loaded at runtime and several PAK files can be used, the latest loaded being able to override the resources declared in previously loaded PAK files.

You can initialize the resource loader and filesystem by yourself, or use one of the following shortcuts:

  • hxd.Res.initEmbed() for EmbedFileSystem - this will also trigger the embedding of all files present in your resource directory into your code
  • hxd.Res.initLocal() for LocalFileSystem
  • hxd.Res.initPak() for PAK FileSystem - this will load res.pak, res1.pak, res2.pak, etc. from the local filesystem - until no file is found.
Clone this wiki locally