UNPKG

blaze-2d

Version:

A fast and simple WebGL 2 2D game engine written in TypeScript

82 lines (81 loc) 3.06 kB
import Texture, { TextureUnit } from "./texture"; /** * Handles loading and unloading of textures on the GPU as needed. */ export default abstract class TextureLoader { private static ready; private static gl; private static loaded; private static textureUnits; /** * Sets up the texture loader for use, should only be called once. * * @param gl The WebGL context to use for texture loading */ static init(gl: WebGL2RenderingContext): void; /** * Checks if the loader is ready to be used for texture loading/unloading. * * @throws When TextureLoader.init has not yet been called */ private static checkReady; /** * Loads a texture into any available texture unit on the GPU, if no unit is available then the unit with the longest time since it was last used is overwritten. * * @param texture The texture to load * @returns Wether or not the texture was loaded */ static loadTexture(texture: Texture): boolean; /** * Unloads the texture from the GPU. * * @param texture The texture to unload * @param unit The texture unit the `texture` belongs to, improves performance as the unit does not need to be searched for. * @returns Wether or not the texture was unloaded */ static unloadTexture(texture: Texture, unit?: TextureUnit): boolean; /** * Unloads and then reloads the provided texture. * * @param texture The texture to reload * @returns Wether or not the texture was reloaded */ static reloadTexture(texture: Texture): boolean; /** * Checks if a texture is loaded, ie the texture's image is loaded in a texture unit on the GPU. * * @param texture The texture to check * @returns Wether or not the texture is loaded */ static isLoaded(texture: Texture): boolean; /** * Finds the texture unit the given texture is loaded in. * * @param texture The texture to find * @returns The unit the texture is loaded in or undefined */ static getUnitOfTexture(texture: Texture): TextureUnit | undefined; /** * Finds a free texture unit. * * @returns A free texture unit or undefined when no units are free */ static getFreeTextureUnit(): TextureUnit | undefined; /** * Finds the best texture unit to replace. * * First looks for free texture units and if none are available then it will return the texture unit with the oldest last used time. * * Texture units that have a {@link TextureAtlas} in them will be ignored. * * @returns The most replaceable texture unit */ static findReplaceableTextureUnit(): TextureUnit; /** * Finds a {@link TextureUnit} by its WebGL texture unit number and updates its lastUsed time to the current time from `performance.now()` * * @param unit The WebGL texture unit number * @returns Wether or not the TextureUnit was updated */ static updateLastUsed(unit: number): boolean; }