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.
Description
Section titled “Description”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.
Learn more
Section titled “Learn more”Creation
Section titled “Creation”| Returns | Signature | Summary |
|---|---|---|
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. |
Properties
Section titled “Properties”| Property | Type | Default | Summary |
|---|---|---|---|
| properties | ResourceProperties | set at creation | The settings of this handle: interpolation, mipmaps and region. |
| uniqueId | String | "" | The id under which the data of this resource is cached and named in the warnings. |
| decoder | IResourceDecoder | null, then the decoder of the format that JOID detects | The IResourceDecoder that reads the data, or null until the format is detected. |
Methods
Section titled “Methods”Position and size
Section titled “Position and size”| Returns | Signature | Summary |
|---|---|---|
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. |
Reactivity
Section titled “Reactivity”| Returns | Signature | Summary |
|---|---|---|
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. |
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. |
Queries
Section titled “Queries”| Returns | Signature | Summary |
|---|---|---|
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. |
Actions
Section titled “Actions”| Returns | Signature | Summary |
|---|---|---|
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. |
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. |
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. |
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. |
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. |
For subclasses
Section titled “For subclasses”| Returns | Signature | Summary |
|---|---|---|
void | fireLoad() | Calls the onLoad listeners of this handle on the render thread; the data calls it once the decode ends. |
void | fireError(Throwable error) | Calls the onError listeners of this handle on the render thread with error; the data calls it when the resource fails. |
Events
Section titled “Events”| Event | Listener | Summary |
|---|---|---|
| onLoad | Consumer<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. |
| onError | BiConsumer<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. |
Creation details
Section titled “Creation details”create
Section titled “create”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.
Property details
Section titled “Property details”properties
Section titled “properties”this properties(ResourceProperties properties)fluentResourceProperties getProperties()
Default: set at creation
The settings of this handle: interpolation, mipmaps and region.
getProperties() returns them; properties(ResourceProperties) replaces them with a copy.
uniqueId
Section titled “uniqueId”this uniqueId(String uniqueId)fluentString 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.
decoder
Section titled “decoder”this decoder(IResourceDecoder decoder)fluentIResourceDecoder 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.
Method details
Section titled “Method details”getWidth
Section titled “getWidth”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.
getHeight
Section titled “getHeight”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.
unbind
Section titled “unbind”void unbind()
Unbinds the texture from the render state.
bindTextureOnly
Section titled “bindTextureOnly”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.
getSourceWidth
Section titled “getSourceWidth”int getSourceWidth()
Returns the width of the whole image in pixels, whatever the region.
getSourceHeight
Section titled “getSourceHeight”int getSourceHeight()
Returns the height of the whole image in pixels, whatever the region.
getData
Section titled “getData”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.
getTexture
Section titled “getTexture”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.
isMipmappable
Section titled “isMipmappable”boolean isMipmappable()
If true, the decoder accepts mipmaps; a video does not.
getResourceData
Section titled “getResourceData”ResourceData getResourceData()
Returns the shared data behind this handle.
getPlayback
Section titled “getPlayback”IResourcePlayback getPlayback()
Returns the playback controls of an animated image or a video, or null for a still image.
isLoaded
Section titled “isLoaded”boolean isLoaded()
If true, the data is decoded.
isUploaded
Section titled “isUploaded”boolean isUploaded()
If true, the decoded pixels are on the GPU.
isGenerated
Section titled “isGenerated”boolean isGenerated()
If true, the decode has started.
isFailed
Section titled “isFailed”boolean isFailed()
If true, the resource could not be read; the error went to the onError listeners.
getBuilder
Section titled “getBuilder”ResourceBuilder getBuilder()
Returns the builder that made this handle.
isBlocking
Section titled “isBlocking”boolean isBlocking()
If true, this handle decodes on the thread that draws it.
blocking
Section titled “blocking”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.
interpolation
Section titled “interpolation”this interpolation(TextureFilter interpolation) fluent
Sets how the texture is filtered when it is scaled.
linear
Section titled “linear”this linear() fluent
Filters the texture smoothly when it is scaled, the default.
nearest
Section titled “nearest”this nearest() fluent
Keeps the pixels sharp when the texture is scaled, for pixel art.
mipmap
Section titled “mipmap”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.
textureCoords
Section titled “textureCoords”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.
generate
Section titled “generate”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).
upload
Section titled “upload”void upload()
Sends the decoded pixels to the GPU, on the render thread; drawing the resource does it for you.
request
Section titled “request”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.
prepareBind
Section titled “prepareBind”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.
reload
Section titled “reload”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.
fireLoad
Section titled “fireLoad”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.
fireError
Section titled “fireError”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.
Event details
Section titled “Event details”onLoad
Section titled “onLoad”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.
onError
Section titled “onError”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
