Skip to content

Resource

final class Resource
Package: dev.joid.lib.resource · Module: core · Source: Resource.java

An image, an animated image, a vector image or a video, loaded in the background and drawn by a resource node or DrawResource.

Create one from a path, a URL, a File, a stream or an Asset, and give it to a resource node (Load from the jar, the disk or the web).

A Resource is a light handle: every handle of the same input shares one ResourceData through the cache of its ResourceBuilder, so loading the same file twice decodes it once. Its own settings (interpolation, mipmaps, region, blocking) belong to the handle. The file is decoded on a background thread the first time the resource is drawn, then uploaded on the render thread; a resource that cannot be read fails and draws a checkerboard in dev mode, nothing in production.

ReturnsSignatureSummary
Resourcecreate(ResourceBuilder builder, ResourceData data) factoryCreates a handle on data with the default settings of builder; ResourceBuilder.of does that for you.
Resourceof(Object input) factoryReturns a handle on the resource of input, through the default builder and its shared memory cache.

PropertyTypeDefaultSummary
propertiesResourcePropertiesset at creationThe settings of this handle: interpolation, mipmaps and region.
uniqueIdString""The id under which the data of this resource is cached and named in the warnings.
decoderIResourceDecodernull, then the decoder of the format that JOID detectsThe IResourceDecoder that reads the data, or null until the format is detected.

ReturnsSignatureSummary
intgetWidth()Returns the width to draw at: the width of the region when textureCoords is set, otherwise the width of the image in pixels, 0 before it is loaded.
intgetHeight()Returns the height to draw at: the height of the region when textureCoords is set, otherwise the height of the image in pixels, 0 before it is loaded.
ReturnsSignatureSummary
voidbind(TextureWrap wrap, Runnable runnable)Binds the texture of the resource with wrap and the settings of this handle, runs runnable, then unbinds it, even when runnable throws.
voidunbind()Unbinds the texture from the render state.
voidbindTextureOnly(TextureWrap wrap)Binds the texture like bind and leaves it bound; call unbind() afterwards.
ReturnsSignatureSummary
intgetSourceWidth()Returns the width of the whole image in pixels, whatever the region.
intgetSourceHeight()Returns the height of the whole image in pixels, whatever the region.
int[]getData()Returns the decoded pixels of the first texture as 0xAARRGGBB values, or null once they are uploaded.
int[]getData(int index)Returns the decoded pixels of the texture at index, or null.
ITexturegetTexture()Returns the first texture of the resource, or null before it is loaded.
ITexturegetTexture(int index)Returns the texture at index, such as one plane of a video frame, or null.
booleanisMipmappable()If true, the decoder accepts mipmaps; a video does not.
ResourceDatagetResourceData()Returns the shared data behind this handle.
IResourcePlaybackgetPlayback()Returns the playback controls of an animated image or a video, or null for a still image.
booleanisLoaded()If true, the data is decoded.
booleanisUploaded()If true, the decoded pixels are on the GPU.
booleanisGenerated()If true, the decode has started.
booleanisFailed()If true, the resource could not be read; the error went to the onError listeners.
ResourceBuildergetBuilder()Returns the builder that made this handle.
booleanisBlocking()If true, this handle decodes on the thread that draws it.
ReturnsSignatureSummary
thisblocking() fluentMakes this handle decode on the thread that draws it and wait for the end, so that it shows on the first frame; a remote asset is then also detected on that thread.
thisinterpolation(TextureFilter interpolation) fluentSets how the texture is filtered when it is scaled.
thislinear() fluentFilters the texture smoothly when it is scaled, the default.
thisnearest() fluentKeeps the pixels sharp when the texture is scaled, for pixel art.
thismipmap(boolean mipmap) fluentIf true, the texture gets mipmaps, for an image drawn much smaller than its size; if false, never.
thistextureCoords(double u, double v, double width, double height) fluentDraws only the region at u, v of width × height pixels of the image, such as one sprite of a sheet; the size of the resource becomes that of the region.
thisreset() fluentSets the settings of this handle back to those of its builder.
thiscopy() fluentReturns another handle on the same data, with the same settings.
thisfork() fluentReturns a handle on a separate playback of the same file, for an animated image or a video shown twice with its own position; a still image returns this handle.
voidgenerate()Starts the decode, on a background thread unless the handle is blocking; drawing the resource does it for you, and calling it earlier has a large image ready for its first draw (Know when an image is loaded).
voidupload()Sends the decoded pixels to the GPU, on the render thread; drawing the resource does it for you.
voidrequest(int width, int height)Tells the decoder the size the resource is drawn at, in screen pixels, so that a vector image renders at that size; while the size keeps changing, the decoder waits 200 ms before it renders again.
voidprepareBind()Starts the decode, applies the mipmap setting and uploads the pixels when they are ready; bind calls it.
thisclear() fluentFrees the textures and the pixels of the data and removes it from the cache, for every handle on it; the next draw decodes the file again.
thisreload() fluentFrees the textures and the pixels of the data, for every handle on it, so that the next draw reads the file again; a failed resource gets a new try.
ReturnsSignatureSummary
voidfireLoad()Calls the onLoad listeners of this handle on the render thread; the data calls it once the decode ends.
voidfireError(Throwable error)Calls the onError listeners of this handle on the render thread with error; the data calls it when the resource fails.

EventListenerSummary
onLoadConsumer<Resource>Adds a listener that runs with this resource when its data is decoded, through runOnRenderThread of the thread bridge, or at once when it is already decoded.
onErrorBiConsumer<Resource,Throwable>Adds a listener that runs with this resource and the error when it fails, through runOnRenderThread of the thread bridge, or at once when it already failed.

Resource create(ResourceBuilder builder, ResourceData data) factory

Creates a handle on data with the default settings of builder; ResourceBuilder.of does that for you.

Resource of(Object input) factory

Returns a handle on the resource of input, through the default builder and its shared memory cache. The format comes from the first bytes of the file, not from its name. A path found nowhere gives a failed resource, which reload() reads again; an input of a type that no locator knows throws IllegalArgumentException.

  • this properties(ResourceProperties properties) fluent
  • ResourceProperties getProperties()

Default: set at creation

The settings of this handle: interpolation, mipmaps and region. getProperties() returns them; properties(ResourceProperties) replaces them with a copy.

  • this uniqueId(String uniqueId) fluent
  • String getUniqueId()

Default: ""

The id under which the data of this resource is cached and named in the warnings. uniqueId(String) renames the shared data: every handle on it sees the new id.

  • this decoder(IResourceDecoder decoder) fluent
  • IResourceDecoder getDecoder()

Default: null, then the decoder of the format that JOID detects

The IResourceDecoder that reads the data, or null until the format is detected. decoder(IResourceDecoder) waits for a running decode, clears the data and decodes it again with decoder, for every handle on the data.

int getWidth()

Returns the width to draw at: the width of the region when textureCoords is set, otherwise the width of the image in pixels, 0 before it is loaded.

int getHeight()

Returns the height to draw at: the height of the region when textureCoords is set, otherwise the height of the image in pixels, 0 before it is loaded.

void bind(TextureWrap wrap, Runnable runnable)

Binds the texture of the resource with wrap and the settings of this handle, runs runnable, then unbinds it, even when runnable throws. It starts the decode and the upload when they have not happened yet; a resource still loading binds no texture.

void unbind()

Unbinds the texture from the render state.

void bindTextureOnly(TextureWrap wrap)

Binds the texture like bind and leaves it bound; call unbind() afterwards. A failed resource binds the checkerboard in dev mode and no texture in production.

int getSourceWidth()

Returns the width of the whole image in pixels, whatever the region.

int getSourceHeight()

Returns the height of the whole image in pixels, whatever the region.

int[] getData()

Returns the decoded pixels of the first texture as 0xAARRGGBB values, or null once they are uploaded.

int[] getData(int index)

Returns the decoded pixels of the texture at index, or null.

ITexture getTexture()

Returns the first texture of the resource, or null before it is loaded.

ITexture getTexture(int index)

Returns the texture at index, such as one plane of a video frame, or null.

boolean isMipmappable()

If true, the decoder accepts mipmaps; a video does not.

ResourceData getResourceData()

Returns the shared data behind this handle.

IResourcePlayback getPlayback()

Returns the playback controls of an animated image or a video, or null for a still image.

boolean isLoaded()

If true, the data is decoded.

boolean isUploaded()

If true, the decoded pixels are on the GPU.

boolean isGenerated()

If true, the decode has started.

boolean isFailed()

If true, the resource could not be read; the error went to the onError listeners.

ResourceBuilder getBuilder()

Returns the builder that made this handle.

boolean isBlocking()

If true, this handle decodes on the thread that draws it.

this blocking() fluent

Makes this handle decode on the thread that draws it and wait for the end, so that it shows on the first frame; a remote asset is then also detected on that thread. Use it for small images that must never pop in.

this interpolation(TextureFilter interpolation) fluent

Sets how the texture is filtered when it is scaled.

this linear() fluent

Filters the texture smoothly when it is scaled, the default.

this nearest() fluent

Keeps the pixels sharp when the texture is scaled, for pixel art.

this mipmap(boolean mipmap) fluent

If true, the texture gets mipmaps, for an image drawn much smaller than its size; if false, never. Without a call, JOID turns them on by itself when the image is drawn smaller than its texture with linear filtering.

this textureCoords(double u, double v, double width, double height) fluent

Draws only the region at u, v of width × height pixels of the image, such as one sprite of a sheet; the size of the resource becomes that of the region.

this reset() fluent

Sets the settings of this handle back to those of its builder.

this copy() fluent

Returns another handle on the same data, with the same settings.

this fork() fluent

Returns a handle on a separate playback of the same file, for an animated image or a video shown twice with its own position; a still image returns this handle. It waits for the format to be detected.

void generate()

Starts the decode, on a background thread unless the handle is blocking; drawing the resource does it for you, and calling it earlier has a large image ready for its first draw (Know when an image is loaded).

void upload()

Sends the decoded pixels to the GPU, on the render thread; drawing the resource does it for you.

void request(int width, int height)

Tells the decoder the size the resource is drawn at, in screen pixels, so that a vector image renders at that size; while the size keeps changing, the decoder waits 200 ms before it renders again. DrawResource calls it on every draw.

void prepareBind()

Starts the decode, applies the mipmap setting and uploads the pixels when they are ready; bind calls it.

this clear() fluent

Frees the textures and the pixels of the data and removes it from the cache, for every handle on it; the next draw decodes the file again.

this reload() fluent

Frees the textures and the pixels of the data, for every handle on it, so that the next draw reads the file again; a failed resource gets a new try.

void fireLoad()

Thread: runs on the render thread.

Calls the onLoad listeners of this handle on the render thread; the data calls it once the decode ends.

void fireError(Throwable error)

Thread: runs on the render thread.

Calls the onError listeners of this handle on the render thread with error; the data calls it when the resource fails.

this onLoad(Consumer<Resource> callback) fluent

Thread: runs on the render thread.

Adds a listener that runs with this resource when its data is decoded, through runOnRenderThread of the thread bridge, or at once when it is already decoded. The data keeps the handle alive until the load or the failure is notified, so a handle thrown away right after onLoad still receives it. Resource.copy does not copy the listeners.

this onError(BiConsumer<Resource,Throwable> callback) fluent

Thread: runs on the render thread.

Adds a listener that runs with this resource and the error when it fails, through runOnRenderThread of the thread bridge, or at once when it already failed.


fluent returns the instance, typed for chaining · factory creates an instance · signal also accepts a value that follows signals, as a signal, an expression or a lambda (When does JOID compute this value again?) · hook called by JOID, override it · abstract you implement it · default interface method with a body · static called on the class · protected visible to subclasses

Report a problem with this page