littlejsengine 1.18.21 → 1.18.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/littlejs.js CHANGED
@@ -35,7 +35,7 @@ const engineName = 'LittleJS';
35
35
  * @type {string}
36
36
  * @default
37
37
  * @memberof Engine */
38
- const engineVersion = '1.18.21';
38
+ const engineVersion = '1.18.22';
39
39
 
40
40
  /** Frames per second to update
41
41
  * @type {number}
@@ -4167,8 +4167,9 @@ class TileInfo
4167
4167
  * @param {TextureInfo} [textureInfo] - Texture info to use
4168
4168
  * @param {number} [padding] - How many pixels padding around all sides of each tile (increases grid size, does not affect tile size)
4169
4169
  * @param {number} [bleed] - How many pixels smaller to shrink UVS of tiles (does not affect grid size, only UVs)
4170
+ * @param {number} [columns] - How many frames per row for frame(), 0 to keep frames on a single row
4170
4171
  */
4171
- constructor(pos=vec2(), size=tileDefaultSize, textureInfo=textureInfos[0], padding=tileDefaultPadding, bleed=tileDefaultBleed)
4172
+ constructor(pos=vec2(), size=tileDefaultSize, textureInfo=textureInfos[0], padding=tileDefaultPadding, bleed=tileDefaultBleed, columns=0)
4172
4173
  {
4173
4174
  /** @property {Vector2} - Top left corner of tile in pixels */
4174
4175
  this.pos = pos.copy();
@@ -4180,6 +4181,8 @@ class TileInfo
4180
4181
  this.textureInfo = textureInfo;
4181
4182
  /** @property {number} - Shrinks tile by this many pixels to prevent neighbors bleeding */
4182
4183
  this.bleed = bleed;
4184
+ /** @property {number} - How many frames per row for frame(), 0 to keep frames on a single row */
4185
+ this.columns = columns;
4183
4186
  }
4184
4187
 
4185
4188
  /** Returns a copy of this tile offset by a vector
@@ -4187,9 +4190,10 @@ class TileInfo
4187
4190
  * @return {TileInfo}
4188
4191
  */
4189
4192
  offset(offset)
4190
- { return new TileInfo(this.pos.add(offset), this.size, this.textureInfo, this.padding, this.bleed); }
4193
+ { return new TileInfo(this.pos.add(offset), this.size, this.textureInfo, this.padding, this.bleed, this.columns); }
4191
4194
 
4192
4195
  /** Returns a copy of this tile offset by a number of animation frames
4196
+ * Frames wrap down to the next row if columns is set
4193
4197
  * @param {number} frame - Offset to apply in animation frames
4194
4198
  * @return {TileInfo}
4195
4199
  */
@@ -4197,9 +4201,23 @@ class TileInfo
4197
4201
  {
4198
4202
  ASSERT(typeof frame === 'number');
4199
4203
  const w = this.size.x + this.padding*2;
4200
- const x = frame*w;
4201
- ASSERT(x + this.size.x <= this.textureInfo.size.x, 'frame extends beyond texture width!');
4202
- return this.offset(new Vector2(x));
4204
+ const h = this.size.y + this.padding*2;
4205
+ const x = (this.columns ? frame % this.columns : frame) * w;
4206
+ const y = (this.columns ? frame / this.columns | 0 : 0) * h;
4207
+ ASSERT(this.pos.x + x + this.size.x <= this.textureInfo.size.x, 'frame extends beyond texture width!');
4208
+ ASSERT(this.pos.y + y + this.size.y <= this.textureInfo.size.y, 'frame extends beyond texture height!');
4209
+ return this.offset(new Vector2(x, y));
4210
+ }
4211
+
4212
+ /** Set how many frames per row this tile uses, so frame() can wrap
4213
+ * @param {number} [columns] - Frames per row, 0 to keep frames on a single row
4214
+ * @return {TileInfo}
4215
+ */
4216
+ setColumns(columns=0)
4217
+ {
4218
+ ASSERT(isNumber(columns) && columns >= 0, 'columns must be a number >= 0');
4219
+ this.columns = columns;
4220
+ return this;
4203
4221
  }
4204
4222
 
4205
4223
  /**
@@ -4208,7 +4226,7 @@ class TileInfo
4208
4226
  * @return {TileInfo}
4209
4227
  */
4210
4228
  index(index)
4211
- { return tile(index, this.size, this.textureInfo, this.padding, this.bleed); }
4229
+ { return tile(index, this.size, this.textureInfo, this.padding, this.bleed).setColumns(this.columns); }
4212
4230
 
4213
4231
  /**
4214
4232
  * Set this tile to use a full image in a texture info
@@ -4220,7 +4238,7 @@ class TileInfo
4220
4238
  this.textureInfo = textureInfo;
4221
4239
  this.pos = new Vector2;
4222
4240
  this.size = textureInfo.size.copy();
4223
- this.bleed = this.padding = 0;
4241
+ this.bleed = this.padding = this.columns = 0;
4224
4242
  return this;
4225
4243
  }
4226
4244
  }
@@ -14907,6 +14925,444 @@ function getCrescentPoints(pos, size=1, percent=0, angle=0, invert=false, sides=
14907
14925
  }
14908
14926
  return points;
14909
14927
  }
14928
+ /**
14929
+ * LittleJS Texture Sheet Plugin
14930
+ * - Packs images into texture sheets as they are loaded
14931
+ * - Sprites are placed automatically, callers get a TileInfo
14932
+ * - Sheets are created and filled as needed
14933
+ * - Sheets fill in call order, images decode in parallel
14934
+ * - Animation frames keep layout and wrap across rows as needed
14935
+ * - WebGL textures upload once per batch of loads
14936
+ * - loadAtlas imports pre-packed atlases (TexturePacker and Aseprite json)
14937
+ * @namespace TextureSheets
14938
+ */
14939
+
14940
+ /** Width and height in pixels of texture sheets created by loadSprite
14941
+ * @type {number}
14942
+ * @default
14943
+ * @memberof Settings */
14944
+ let textureSheetSize = 2048;
14945
+
14946
+ /** Default padding pixels around each frame packed by loadSprite
14947
+ * @type {number}
14948
+ * @default
14949
+ * @memberof Settings */
14950
+ let textureSheetPadding = 1;
14951
+
14952
+ /** Array of texture sheets created by loadSprite
14953
+ * @type {Array<TextureSheet>}
14954
+ * @memberof TextureSheets */
14955
+ let textureSheets = [];
14956
+
14957
+ // pending loads pack through a queue so sheets fill in call order
14958
+ let textureSheetQueue = Promise.resolve();
14959
+ let textureSheetPendingCount = 0;
14960
+
14961
+ /**
14962
+ * Texture Sheet - A texture that images are packed into as they load
14963
+ * Uses shelf packing, images are placed left to right then wrap to a new row
14964
+ * @memberof TextureSheets
14965
+ */
14966
+ class TextureSheet
14967
+ {
14968
+ /** Create a texture sheet, called automatically by loadSprite
14969
+ * @param {number} [size] - Width and height of the sheet in pixels */
14970
+ constructor(size=textureSheetSize)
14971
+ {
14972
+ ASSERT(size > 0, 'texture sheet size must be positive');
14973
+
14974
+ /** @property {number} - Width and height of the sheet in pixels */
14975
+ this.size = size;
14976
+ /** @property {OffscreenCanvas} - Canvas holding the packed images */
14977
+ this.canvas = headlessMode ? undefined : new OffscreenCanvas(size, size);
14978
+ /** @property {OffscreenCanvasRenderingContext2D} - 2d context for the canvas */
14979
+ this.context = this.canvas?.getContext('2d');
14980
+ /** @property {TextureInfo} - The texture info for this sheet */
14981
+ this.textureInfo = new TextureInfo(this.canvas);
14982
+ /** @property {Vector2} - Where the next image will be packed */
14983
+ this.cursor = vec2();
14984
+ /** @property {number} - Height of the row being packed */
14985
+ this.rowHeight = 0;
14986
+ /** @property {boolean} - Has the canvas changed since the last webgl upload? */
14987
+ this.glDirty = false;
14988
+
14989
+ if (headlessMode)
14990
+ {
14991
+ // tiles still need bounds when there is no canvas to measure
14992
+ this.textureInfo.size = vec2(size);
14993
+ this.textureInfo.sizeInverse = vec2(1/size);
14994
+ }
14995
+ }
14996
+
14997
+ /** Find a spot for an image on this sheet without drawing it
14998
+ * @param {Vector2} imageSize - Size of the source image in pixels
14999
+ * @param {Vector2} [frameSize] - Size of each frame, or the whole image if not passed
15000
+ * @param {number} [padding] - How many pixels padding around each frame
15001
+ * @return {TileInfo} Tile for the packed image, or undefined if the sheet is full */
15002
+ tryAdd(imageSize, frameSize=imageSize, padding=textureSheetPadding)
15003
+ {
15004
+ ASSERT(isVector2(imageSize) && isVector2(frameSize), 'sizes must be vec2');
15005
+ ASSERT(frameSize.x > 0 && frameSize.y > 0, 'frame size must be positive');
15006
+ ASSERT(imageSize.x % frameSize.x === 0 && imageSize.y % frameSize.y === 0,
15007
+ 'image size must be a multiple of the frame size');
15008
+
15009
+ const cellWidth = frameSize.x + padding*2;
15010
+ const cellHeight = frameSize.y + padding*2;
15011
+ const maxColumns = this.size / cellWidth | 0;
15012
+ ASSERT(maxColumns > 0, 'frame is too wide to fit on a texture sheet');
15013
+
15014
+ // keep the layout of the source image, but narrow it if a row is too wide
15015
+ // frames wrap down to the next row, which TileInfo.frame handles via columns
15016
+ const sourceColumns = imageSize.x / frameSize.x;
15017
+ const frameCount = sourceColumns * (imageSize.y / frameSize.y);
15018
+ const columns = min(sourceColumns, maxColumns);
15019
+ const blockWidth = columns * cellWidth;
15020
+ const blockHeight = ceil(frameCount / columns) * cellHeight;
15021
+
15022
+ // probe the placement using locals so a failed try leaves the sheet unchanged
15023
+ let x = this.cursor.x, y = this.cursor.y, rowHeight = this.rowHeight;
15024
+ if (x + blockWidth > this.size)
15025
+ {
15026
+ // start a new row if this one does not have enough space left
15027
+ x = 0;
15028
+ y += rowHeight;
15029
+ rowHeight = 0;
15030
+ }
15031
+
15032
+ // out of space, the caller needs to use a different sheet
15033
+ if (y + blockHeight > this.size)
15034
+ return undefined;
15035
+
15036
+ // commit the placement, tile pos points inside the padding to match how tile() works
15037
+ this.cursor.x = x + blockWidth;
15038
+ this.cursor.y = y;
15039
+ this.rowHeight = max(rowHeight, blockHeight);
15040
+ return new TileInfo(vec2(x + padding, y + padding), frameSize, this.textureInfo, padding, 0, columns);
15041
+ }
15042
+
15043
+ /** Draw an image into this sheet at a tile returned by tryAdd
15044
+ * @param {HTMLImageElement} image - Source image to copy from
15045
+ * @param {TileInfo} tileInfo - Where to put it, from tryAdd
15046
+ * @param {boolean} [update] - Upload to webgl now, pass false when batching */
15047
+ drawImage(image, tileInfo, update=true)
15048
+ {
15049
+ ASSERT(!!this.context, 'texture sheet has no canvas');
15050
+
15051
+ // copy frames in order, reading the source left to right, top to bottom
15052
+ // the destination wraps at tileInfo.columns which may be narrower than the source
15053
+ const frameSize = tileInfo.size;
15054
+ const sourceColumns = image.width / frameSize.x;
15055
+ const frameCount = sourceColumns * (image.height / frameSize.y);
15056
+ const columns = tileInfo.columns || frameCount;
15057
+ const cellWidth = frameSize.x + tileInfo.padding*2;
15058
+ const cellHeight = frameSize.y + tileInfo.padding*2;
15059
+ for (let i = frameCount; i--;)
15060
+ {
15061
+ const sourceX = (i % sourceColumns) * frameSize.x;
15062
+ const sourceY = (i / sourceColumns | 0) * frameSize.y;
15063
+ this.context.drawImage(image,
15064
+ sourceX, sourceY, frameSize.x, frameSize.y,
15065
+ tileInfo.pos.x + (i % columns) * cellWidth,
15066
+ tileInfo.pos.y + (i / columns | 0) * cellHeight,
15067
+ frameSize.x, frameSize.y);
15068
+ }
15069
+
15070
+ // upload now unless the caller is batching more images
15071
+ this.glDirty = true;
15072
+ update && this.updateTexture();
15073
+ }
15074
+
15075
+ /** Upload the canvas to webgl if it has changed since the last upload
15076
+ * Only needed after batching, drawImage uploads automatically by default */
15077
+ updateTexture()
15078
+ {
15079
+ if (!this.glDirty) return;
15080
+ this.glDirty = false;
15081
+ this.textureInfo.createWebGLTexture();
15082
+ }
15083
+ }
15084
+
15085
+ ///////////////////////////////////////////////////////////////////////////////
15086
+
15087
+ /** Load an image and pack it into a texture sheet
15088
+ * - Returns a TileInfo immediately which is filled in when the image loads
15089
+ * - Nothing is visible until it loads, use spritesReady to wait for it
15090
+ * - Pass frameSize for animations, then step through them with TileInfo.frame
15091
+ * - Grid images keep their layout and frames wrap down to the next row
15092
+ * @param {string} src - Image source path
15093
+ * @param {Vector2|number} [frameSize] - Size of each animation frame in pixels
15094
+ * @param {number} [padding] - How many pixels padding around each frame
15095
+ * @return {TileInfo}
15096
+ * @example
15097
+ * const playerTile = loadSprite('player.png'); // a single sprite
15098
+ * const runTile = loadSprite('run.png', vec2(16)); // a 16x16 frame animation
15099
+ * @memberof TextureSheets */
15100
+ function loadSprite(src, frameSize, padding=textureSheetPadding)
15101
+ {
15102
+ ASSERT(isStringLike(src), 'image src must be a string');
15103
+ ASSERT(!frameSize || isVector2(frameSize) || isNumber(frameSize), 'frameSize must be a vec2 or number');
15104
+ ASSERT(isNumber(padding), 'padding must be a number');
15105
+
15106
+ if (isNumber(frameSize))
15107
+ frameSize = vec2(frameSize);
15108
+
15109
+ // start with an empty tile that gets filled in when the image loads
15110
+ const tileInfo = new TileInfo(vec2(), vec2(), undefined, padding, 0);
15111
+ if (headlessMode) return tileInfo;
15112
+
15113
+ // point at a sheet right away so drawing before it loads picks up empty pixels
15114
+ tileInfo.textureInfo = (textureSheets[0] || textureSheetCreate()).textureInfo;
15115
+
15116
+ // start decoding right away, images decode in parallel
15117
+ const image = new Image;
15118
+ const imagePromise = new Promise(resolve =>
15119
+ {
15120
+ image.onerror = image.onload = resolve;
15121
+ image.crossOrigin = 'anonymous';
15122
+ image.src = src;
15123
+ });
15124
+
15125
+ // pack through a queue so sheets fill in call order, not decode order
15126
+ ++textureSheetPendingCount;
15127
+ textureSheetQueue = textureSheetQueue.then(async ()=>
15128
+ {
15129
+ await imagePromise;
15130
+ if (image.width)
15131
+ {
15132
+ // pack onto a sheet, then fill in the tile that was already handed out,
15133
+ // copying every field so nothing is missed if TileInfo gains more of them
15134
+ const imageSize = vec2(image.width, image.height);
15135
+ const {sheet, tile} = textureSheetAdd(imageSize, frameSize, padding);
15136
+ Object.assign(tileInfo, tile);
15137
+ sheet.drawImage(image, tileInfo, false); // upload once per batch below
15138
+ }
15139
+ else
15140
+ {
15141
+ // leave the tile empty if the image failed to load
15142
+ LOG('loadSprite failed to load image:', src);
15143
+ }
15144
+
15145
+ // upload to webgl once per batch, when the last pending load finishes
15146
+ if (!--textureSheetPendingCount)
15147
+ textureSheets.forEach(s=> s.updateTexture());
15148
+ });
15149
+
15150
+ return tileInfo;
15151
+ }
15152
+
15153
+ /** Load a pre-packed texture atlas and repack it onto texture sheets
15154
+ * - Supports TexturePacker json (hash and array) and Aseprite json
15155
+ * - Returns an empty object which is filled with TileInfos when loaded
15156
+ * - Frames are named by the json, animations are grouped automatically
15157
+ * - Aseprite frame tags become animations, so do names like run_0, run_1
15158
+ * - Trimmed frames are restored to their full source size when packed
15159
+ * - Rotated frames are rotated back upright when packed
15160
+ * @param {string} imageSrc - Atlas image path
15161
+ * @param {string|Object} jsonSrc - Atlas json path, or already parsed json data
15162
+ * @param {number} [padding] - How many pixels padding around each frame
15163
+ * @return {Object} Object mapping frame and animation names to TileInfos
15164
+ * @example
15165
+ * const atlas = loadAtlas('sprites.png', 'sprites.json');
15166
+ * await spritesReady();
15167
+ * drawTile(pos, size, atlas.player); // a single frame
15168
+ * drawTile(pos, size, atlas.run.frame(2)); // frame 2 of the run animation
15169
+ * @memberof TextureSheets */
15170
+ function loadAtlas(imageSrc, jsonSrc, padding=textureSheetPadding)
15171
+ {
15172
+ ASSERT(isStringLike(imageSrc), 'atlas image src must be a string');
15173
+ ASSERT(isStringLike(jsonSrc) || typeof jsonSrc === 'object', 'atlas json must be a path or object');
15174
+ ASSERT(isNumber(padding), 'padding must be a number');
15175
+
15176
+ const atlas = {};
15177
+ if (headlessMode) return atlas;
15178
+
15179
+ // start fetching the json and decoding the image right away, in parallel
15180
+ const jsonPromise = typeof jsonSrc === 'object' ? Promise.resolve(jsonSrc) :
15181
+ fetch(jsonSrc).then(r=> r.ok && r.json()).catch(()=> undefined);
15182
+ const image = new Image;
15183
+ const imagePromise = new Promise(resolve =>
15184
+ {
15185
+ image.onerror = image.onload = resolve;
15186
+ image.crossOrigin = 'anonymous';
15187
+ image.src = imageSrc;
15188
+ });
15189
+
15190
+ // pack through a queue so sheets fill in call order, not decode order
15191
+ ++textureSheetPendingCount;
15192
+ textureSheetQueue = textureSheetQueue.then(async ()=>
15193
+ {
15194
+ const data = await jsonPromise;
15195
+ await imagePromise;
15196
+ if (image.width && data)
15197
+ {
15198
+ for (const group of parseAtlas(data))
15199
+ {
15200
+ // reserve a block of full size cells, one per frame
15201
+ const sourceSize = group.frames[0].sourceSize;
15202
+ const blockSize = vec2(sourceSize.x*group.frames.length, sourceSize.y);
15203
+ const {sheet, tile} = textureSheetAdd(blockSize, sourceSize, padding);
15204
+
15205
+ // draw each frame untrimmed into its cell
15206
+ const context = sheet.context;
15207
+ const cellWidth = sourceSize.x + padding*2;
15208
+ const cellHeight = sourceSize.y + padding*2;
15209
+ group.frames.forEach((f, i)=>
15210
+ {
15211
+ const x = tile.pos.x + (i % tile.columns)*cellWidth + f.offset.x;
15212
+ const y = tile.pos.y + (i / tile.columns |0)*cellHeight + f.offset.y;
15213
+ if (f.rotated)
15214
+ {
15215
+ // stored rotated 90 degrees clockwise, draw it back upright
15216
+ context.save();
15217
+ context.translate(x, y);
15218
+ context.rotate(-PI/2);
15219
+ context.drawImage(image, f.pos.x, f.pos.y, f.size.y, f.size.x,
15220
+ -f.size.y, 0, f.size.y, f.size.x);
15221
+ context.restore();
15222
+ }
15223
+ else
15224
+ context.drawImage(image, f.pos.x, f.pos.y, f.size.x, f.size.y,
15225
+ x, y, f.size.x, f.size.y);
15226
+ });
15227
+ sheet.glDirty = true;
15228
+ atlas[group.name] = tile;
15229
+ }
15230
+ }
15231
+ else
15232
+ {
15233
+ // leave the atlas empty if either file failed to load
15234
+ LOG('loadAtlas failed to load:', imageSrc, jsonSrc);
15235
+ }
15236
+
15237
+ // upload to webgl once per batch, when the last pending load finishes
15238
+ if (!--textureSheetPendingCount)
15239
+ textureSheets.forEach(s=> s.updateTexture());
15240
+ });
15241
+
15242
+ return atlas;
15243
+ }
15244
+
15245
+ /** Parse atlas json into a list of named frame groups, used by loadAtlas
15246
+ * - Accepts TexturePacker json (hash and array) and Aseprite json
15247
+ * - Frames tagged in Aseprite or named like run_0, run_1 group into animations
15248
+ * @param {Object} data - Parsed atlas json data
15249
+ * @return {Array<Object>} List of {name, frames} groups in atlas order
15250
+ * @memberof TextureSheets */
15251
+ function parseAtlas(data)
15252
+ {
15253
+ ASSERT(!!data?.frames, 'unrecognized atlas format, expected TexturePacker or Aseprite json');
15254
+
15255
+ // normalize both hash and array frame layouts into a single list
15256
+ const frames = (isArray(data.frames) ?
15257
+ data.frames.map(f=> [f.filename, f]) : Object.entries(data.frames))
15258
+ .map(([name, f])=> ({
15259
+ name: name.replace(/\.[^.\\/]+$/, ''), // strip file extension
15260
+ pos: vec2(f.frame.x, f.frame.y),
15261
+ size: vec2(f.frame.w, f.frame.h),
15262
+ offset: vec2(f.spriteSourceSize?.x ?? 0, f.spriteSourceSize?.y ?? 0),
15263
+ sourceSize: vec2(f.sourceSize?.w ?? f.frame.w, f.sourceSize?.h ?? f.frame.h),
15264
+ rotated: !!f.rotated,
15265
+ }));
15266
+
15267
+ const groups = [];
15268
+ const tags = data.meta?.frameTags;
15269
+ if (tags?.length)
15270
+ {
15271
+ // aseprite tags are authoritative, untagged frames stay individual
15272
+ const tagged = new Set;
15273
+ for (const tag of tags)
15274
+ {
15275
+ groups.push({name: tag.name, frames: frames.slice(tag.from, tag.to + 1)});
15276
+ for (let i = tag.from; i <= tag.to; ++i)
15277
+ tagged.add(i);
15278
+ }
15279
+ frames.forEach((f, i)=> tagged.has(i) || groups.push({name: f.name, frames: [f]}));
15280
+ return groups;
15281
+ }
15282
+
15283
+ // group frames that share a name stem with contiguous trailing numbers
15284
+ // run_0.png and run_1.png become a 2 frame animation named run
15285
+ const stems = new Map;
15286
+ for (const f of frames)
15287
+ {
15288
+ let match = f.name.match(/^(.+?)([-_ ])?(\d+)$/);
15289
+ if (match && !match[2] && /\d$/.test(match[1]))
15290
+ match = undefined; // all digit tails like 10 are a name, not frame 0 of 1
15291
+ const stem = match ? match[1] : f.name;
15292
+ f.groupIndex = match ? Number(match[3]) : undefined;
15293
+ stems.has(stem) || stems.set(stem, []);
15294
+ stems.get(stem).push(f);
15295
+ }
15296
+ for (const [stem, list] of stems)
15297
+ {
15298
+ // only group 2 or more frames with contiguous indices and matching sizes
15299
+ list.sort((a, b)=> a.groupIndex - b.groupIndex);
15300
+ const grouped = list.length > 1 &&
15301
+ list.every((f, i)=> f.groupIndex === list[0].groupIndex + i) &&
15302
+ list.every(f=> f.sourceSize.x === list[0].sourceSize.x &&
15303
+ f.sourceSize.y === list[0].sourceSize.y);
15304
+ if (grouped)
15305
+ groups.push({name: stem, frames: list});
15306
+ else
15307
+ list.forEach(f=> groups.push({name: f.name, frames: [f]}));
15308
+ }
15309
+ return groups;
15310
+ }
15311
+
15312
+ /** Wait for everything started by loadSprite and loadAtlas to finish packing
15313
+ * @return {Promise}
15314
+ * @example
15315
+ * async function gameInit()
15316
+ * {
15317
+ * playerTile = loadSprite('player.png');
15318
+ * runTile = loadSprite('run.png', vec2(16));
15319
+ * await spritesReady();
15320
+ * }
15321
+ * @memberof TextureSheets */
15322
+ async function spritesReady()
15323
+ {
15324
+ // keep waiting until the queue drains, more sprites may load while waiting
15325
+ while (textureSheetPendingCount)
15326
+ await textureSheetQueue;
15327
+ }
15328
+
15329
+ // create a new texture sheet and add it to the list
15330
+ function textureSheetCreate()
15331
+ {
15332
+ const sheet = new TextureSheet;
15333
+ textureSheets.push(sheet);
15334
+ return sheet;
15335
+ }
15336
+
15337
+ // use the first sheet with enough space, or make a new one
15338
+ function textureSheetAdd(imageSize, frameSize, padding)
15339
+ {
15340
+ let sheet, tile;
15341
+ for (sheet of textureSheets)
15342
+ if (tile = sheet.tryAdd(imageSize, frameSize, padding))
15343
+ break;
15344
+ if (!tile)
15345
+ {
15346
+ sheet = textureSheetCreate();
15347
+ tile = sheet.tryAdd(imageSize, frameSize, padding);
15348
+ ASSERT(!!tile, 'image is too large to fit on a texture sheet');
15349
+ }
15350
+ return {sheet, tile};
15351
+ }
15352
+
15353
+ ///////////////////////////////////////////////////////////////////////////////
15354
+ // Texture sheet setting setters
15355
+
15356
+ /** Set width and height in pixels of texture sheets created by loadSprite
15357
+ * @param {number} size
15358
+ * @memberof Settings */
15359
+ function setTextureSheetSize(size) { textureSheetSize = size; }
15360
+
15361
+ /** Set default padding pixels around each frame packed by loadSprite
15362
+ * @param {number} padding
15363
+ * @memberof Settings */
15364
+ function setTextureSheetPadding(padding) { textureSheetPadding = padding; }
15365
+
14910
15366
  /**
14911
15367
  * LittleJS Tween System Plugin
14912
15368
  * - Lightweight tweens for numbers, Vector2, Color, or any .lerp-able type