blaze-2d
Version:
A fast and simple WebGL 2 2D game engine written in TypeScript
54 lines • 1.72 kB
JavaScript
import Color from "../utils/color";
/**
* Represents a texture with a color and optionally an image that can be applied to {@link Shape}s
*/
export default class Texture {
/**
* Creates a {@link Texture} instance with a color.
*
* @param color The color of the texture
*/
constructor(color = new Color("gray")) {
this.isOld = false;
this.unit = -1;
this.color = color;
}
/**
* Loads an image from a path and stores it in the {@link Texture}.
*
* @param imagePath A path to an image
* @returns A promise that resolves once the image has loaded or rejects if there is an error while loading the image.
*/
loadImage(imagePath) {
this.imagePath = imagePath;
this.image = new Image();
const promise = new Promise((resolve, reject) => {
this.image.onload = () => {
this.isOld = true;
resolve();
};
this.image.onerror = () => reject();
});
this.image.src = this.imagePath;
return promise;
}
/**
* Set the texture unit to be read from when rendering the texture.
*
* **Should only be used if you know what you are doing.**
*
* @param unit The texture unit the texture is stored in on the GPU
*/
setTextureUnit(unit) {
this.unit = unit;
}
/**
* Gets the texture unit the texture is stored in on the GPU, only applies to texture's which use an image.
*
* @returns The texture unit the texture is stored in on the GPU, -1 if it is not currently loaded.
*/
getTextureUnit() {
return this.unit;
}
}
//# sourceMappingURL=texture.js.map