IO topic

IO Module

ResourceManager

The ResourceManager simplifies loading asset. The assets are loaded asynchronously.

How to render a progress bar during loading?

You have to do nothing. Bullseye2D has a default loading and progress indicator.

When assets are being loaded the the App's onLoading method gets called instead of onUpdate and onRender (you can disable this behaviour by setting loader.isEnabled to false.)

The base App class has already a default implementation, but you can create your own loader by overwriting the onLoading method.

Loading assets

Function Description
loadTexture(String path) Loads an image file as a Texture. Caches textures by path.
loadImage(String path) Loads an image (and optionally slives it into frames)
loadFont(String path, double size) Loads a font file (e.g., TTF, OTF) and prepares a BitmapFont instance.
loadSound(String path) Loads an audio file.
loadString(String path) Loads the contents of the path into a String.

All load* methods return the asset handle (Texture, Images, BitmapFont, Sound) immediately. The actual loading is asynchronous. You can already use the handles even if loading is unfinished. For example if you draw an Texture/Image while it is still loading, the render call ist just ignored. If you have the loader enabled (which is the default behaviour) you can be sure in your onUpdate/onRender methods that all assets are finished loading and ready for usage.

Loading a font

var font = resources.loadFont("assets/fonts/wireone/WireOne-Regular.ttf", 196);

// Drawing text with loaded font
gfx.drawText(font, "Drawing some letters to the screen...", x: 25, y: 25);

Loading an image / spritesheet

// Loading a sheet of 16x16 sprites
var spritesheet = resources.loadImage(
  "assets/spritesheet.png", frameWidth: 16, frameHeight: 16
);

// Drawing frame 0 of spritesheet at position 16, 16
gfx.drawImage(spritesheet, 0, 16, 16)

Loading a sound

// Load a sound
var shootFX = loadSound("assets/shoot.wav", retriggerDelayInMs: 50);

// Start playing the sound
audio.playSound(shootFX);

Playing music

// Loading a music file is not required, you can directly play it
// and it will be streamed in the background as soon as possible.
audio.playMusic("assets/music.ogg", true);

Classes

Loader IO
Manages and tracks the progress of multiple asynchronous loading operations.
LoaderItem IO
An individual item being tracked by the Loader.
ResourceManager IO
Manages the loading of game resources such as textures, images, fonts, and sounds.

Functions

encodeImageToDataURL(Images images, [int frame = 0]) Future<String?> IO
Encodes a specific frame from an Images list into a base64 Data URL string (PNG format).
load<T>(String path, Loader loadingInfo, {String responseType = "text", T? defaultValue, void onError(Event event)?, dynamic onLoad(JSAny response, dynamic completer(T), void onError(Event event))?}) Future<T> IO
Asynchronously loads a file from the specified path and returns its content as type T.
loadImageData(String path, Loader loadingInfo) Future<HTMLImageElement?> IO
Asynchronously loads image data from the specified path into an HTMLImageElement.
loadStringAsync(String url, Loader loadingInfo) Future<String> IO
Asynchronously loads a string from the specified url.