blaze-2d
Version:
A fast and simple WebGL 2 2D game engine written in TypeScript
111 lines (110 loc) • 3.98 kB
TypeScript
import { vec2 } from "gl-matrix";
import Texture from "./texture";
/**
* Represents an image/texture stored on the atlas.
*
* @field **tl** The top left corner of the image on the atlas in pixels
* @field **br** The bottom right corner of the image on the atlas in pixels
* @field **texture** The texture stored
*/
export interface TextureAtlasImage {
tl: vec2;
br: vec2;
texture: Texture;
}
/**
* Combines {@link Texture}s into one single image using an offscreen canvas.
*
* If a {@link Texture} is added to the atlas that has no image then a 1x1 pixel is used with the color of the texture.
*
* The size of the atlas should be no larger than `MAX_TEXTURE_SIZE` to insure the atlas can be stored on the GPU.
*
* Also provides an api to get the locations of textures on the atlas.
*/
export default class TextureAtlas extends Texture {
private size;
private textures;
private canvas;
private ctx;
constructor(size: number);
/**
* Adds the given images to the atlas and then refreshes the atlas.
*
* @param images The paths to the images to add
* @returns Wether or not the images were added
*/
addImages(...images: string[]): Promise<boolean>;
/**
* Adds a texture to the atlas.
*
* @param texture The texture to add
* @param refreshAtlas Wether or not to refresh the atlas once the texture has been added
* @returns Wether or not the texture was added to the atlas
*/
addTexture(texture: Texture, refreshAtlas?: boolean): boolean;
/**
* Adds the given textures to the atlas and then refreshes the atlas.
*
* @param textures The textures to add.
* @returns The promise returned by `this.refreshAtlas()`
*/
addTextures(...textures: Texture[]): Promise<void>;
/**
* Removes a texture from the atlas.
*
* @param texture The texture to remove
* @param refreshAtlas Wether or not to refresh the atlas once the texture has been removed
* @returns Wether or not the texture was removed from the atlas
*/
removeTexture(texture: Texture, refreshAtlas?: boolean): boolean;
/**
* Gets the location ({@link TextureAtlasImage}) of the texture on the atlas.
*
* @param texture The texture to find
* @returns The {@link TextureAtlasImage} of the texture on the atlas or undefined
*/
getTexture(texture: Texture): TextureAtlasImage;
/**
* Gets all the textures on the atlas as an array.
*
* @returns An array of all textures on the atlas
*/
getAllTextures(): TextureAtlasImage[];
/**
* Packs all textures in the atlas together, draws them to the canvas and loads the canvas as a data URL to `this.image`.
*
* @returns A promise that resolves once the image has loaded or rejects if there is an error while loading the atlas image.
*/
refreshAtlas(): Promise<void>;
/**
* Loads the atlas' canvas into `this.image`.
*
* @returns A promise that resolves once the image has loaded or rejects if there is an error while loading the image.
*/
loadImage(): Promise<void>;
/**
* Packs all textures in the atlas onto it's canvas.
*
* It then loads the canvas as a data URL into `this.image` using `this.loadImage`.
*/
packTextures(): void;
/**
* Draws all textures stored in the atlas onto the canvas.
*
* If `this.packTextures` has not been called beforehand then all textures will be drawn over each other or in undesired positions.
*/
drawTexturesOnCanvas(): void;
/**
* Finds the tallest texture in the given list of textures.
*
* @param textures The list of textures to search
* @returns The tallest texture and it's index in `textures`
*/
private findTallestTexture;
/**
* Gets the size of the atlas image.
*
* @returns The size of the atlas image
*/
getSize(): number;
}