import pluginTarget from './pluginTarget';
import mapPremultipliedBlendModes from './mapPremultipliedBlendModes';
import earcut from 'earcut';
export * from './decide';
let nextUid = 0;
export {
/**
* @memberof Tiny
* @function pluginTarget
* @private
* @type {mixin}
*/
pluginTarget,
/**
* earcut v2.1.3
* @see {@link https://github.com/mapbox/earcut}
*
* @version 1.2.0
* @memberof Tiny
* @function earcut
* @param {number[]} vertices - A flat array of vertice coordinates
* @param {number[]} [holes] - An array of hole indices
* @param {number} [dimensions=2] - The number of coordinates per vertice in the input array
* @return {number[]} Triangulated polygon
*/
earcut,
};
/**
* Gets the next unique identifier
*
* @static
* @memberof Tiny
* @function uid
* @return {number} The next unique identifier to use.
*/
export function uid() {
return ++nextUid;
}
/**
* Helper for checking for webgl support
*
* @memberof Tiny
* @function isWebGLSupported
* @return {boolean} is webgl supported
*/
export function isWebGLSupported() {
const contextOptions = { stencil: true, failIfMajorPerformanceCaveat: true };
try {
if (!window.WebGLRenderingContext) {
return false;
}
const canvas = document.createElement('canvas');
let gl = canvas.getContext('webgl', contextOptions) || canvas.getContext('experimental-webgl', contextOptions);
const success = !!(gl && gl.getContextAttributes().stencil);
if (gl) {
const loseContext = gl.getExtension('WEBGL_lose_context');
if (loseContext) {
loseContext.loseContext();
}
}
gl = null;
return success;
} catch (e) {
return false;
}
}
/**
* texture 缓存,用于存储已加载的图片 texture
*
* @name TextureCache
* @memberof Tiny
* @type {object}
* @private
*/
export const TextureCache = Object.create(null);
/**
*
* @name BaseTextureCache
* @memberof Tiny
* @type {object}
* @private
*/
export const BaseTextureCache = Object.create(null);
/**
* CountDown 缓存,用于存储已实例化的 CountDown 对象
*
* @name CountDownCache
* @memberof Tiny
* @type {array}
* @version 1.1.7
* @private
*/
export const CountDownCache = [];
/**
* Destroys all texture in the cache
*
* @static
* @memberof Tiny
* @function destroyTextureCache
*/
export function destroyTextureCache() {
let key;
for (key in TextureCache) {
TextureCache[key].destroy();
}
for (key in BaseTextureCache) {
BaseTextureCache[key].destroy();
}
}
/**
* Removes all textures from cache, but does not destroy them
*
* @static
* @memberof Tiny
* @function clearTextureCache
*/
export function clearTextureCache() {
let key;
for (key in TextureCache) {
delete TextureCache[key];
}
for (key in BaseTextureCache) {
delete BaseTextureCache[key];
}
}
/**
* maps premultiply flag and blendMode to adjusted blendMode
*
* @version 1.2.0
* @memberof Tiny
* @const premultiplyBlendMode
* @type {array<number[]>}
*/
export const premultiplyBlendMode = mapPremultipliedBlendModes();
/**
* changes blendMode according to texture format
*
* @version 1.2.0
* @memberof Tiny
* @function correctBlendMode
* @param {number} blendMode - supposed blend mode
* @param {boolean} premultiplied - whether source is premultiplied
* @return {number} true blend mode for this texture
*/
export function correctBlendMode(blendMode, premultiplied) {
return premultiplyBlendMode[premultiplied ? 1 : 0][blendMode];
}
/**
* premultiplies tint
*
* @version 1.2.0
* @memberof Tiny
* @function premultiplyTint
* @param {number} tint - integet RGB
* @param {number} alpha - floating point alpha (0.0-1.0)
* @return {number} tint multiplied by alpha
*/
export function premultiplyTint(tint, alpha) {
if (alpha === 1.0) {
return (alpha * 255 << 24) + tint;
}
if (alpha === 0.0) {
return 0;
}
let R = ((tint >> 16) & 0xFF);
let G = ((tint >> 8) & 0xFF);
let B = (tint & 0xFF);
R = ((R * alpha) + 0.5) | 0;
G = ((G * alpha) + 0.5) | 0;
B = ((B * alpha) + 0.5) | 0;
return (alpha * 255 << 24) + (R << 16) + (G << 8) + B;
}
/**
* combines rgb and alpha to out array
*
* @version 1.2.0
* @memberof Tiny
* @function premultiplyRgba
* @param {Float32Array|number[]} rgb - input rgb
* @param {number} alpha - alpha param
* @param {Float32Array} [out] - output
* @param {boolean} [premultiply=true] - do premultiply it
* @return {Float32Array} vec4 rgba
*/
export function premultiplyRgba(rgb, alpha, out, premultiply) {
out = out || new Float32Array(4);
if (premultiply || premultiply === undefined) {
out[0] = rgb[0] * alpha;
out[1] = rgb[1] * alpha;
out[2] = rgb[2] * alpha;
} else {
out[0] = rgb[0];
out[1] = rgb[1];
out[2] = rgb[2];
}
out[3] = alpha;
return out;
}
/**
* converts integer tint and float alpha to vec4 form, premultiplies by default
*
* @version 1.2.0
* @memberof Tiny
* @function premultiplyTintToRgba
* @param {number} tint - input tint
* @param {number} alpha - alpha param
* @param {Float32Array} [out] - output
* @param {boolean} [premultiply=true] - do premultiply it
* @return {Float32Array} vec4 rgba
*/
export function premultiplyTintToRgba(tint, alpha, out, premultiply) {
out = out || new Float32Array(4);
out[0] = ((tint >> 16) & 0xFF) / 255.0;
out[1] = ((tint >> 8) & 0xFF) / 255.0;
out[2] = (tint & 0xFF) / 255.0;
if (premultiply || premultiply === undefined) {
out[0] *= alpha;
out[1] *= alpha;
out[2] *= alpha;
}
out[3] = alpha;
return out;
}
export let FrameCount = 0;
export function __frameDot() {
FrameCount++;
}
export function equalsFramCount(fps) {
switch (fps) {
case 10:
return !(FrameCount % 6);
case 20:
return !(FrameCount % 3);
case 30:
return !(FrameCount % 2);
case 40:
return FrameCount % 3;
case 50:
return FrameCount % 6;
default:
return true;
}
}