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.
@@ -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}
@@ -3476,8 +3476,9 @@ class TileInfo
3476
3476
  * @param {TextureInfo} [textureInfo] - Texture info to use
3477
3477
  * @param {number} [padding] - How many pixels padding around all sides of each tile (increases grid size, does not affect tile size)
3478
3478
  * @param {number} [bleed] - How many pixels smaller to shrink UVS of tiles (does not affect grid size, only UVs)
3479
+ * @param {number} [columns] - How many frames per row for frame(), 0 to keep frames on a single row
3479
3480
  */
3480
- constructor(pos=vec2(), size=tileDefaultSize, textureInfo=textureInfos[0], padding=tileDefaultPadding, bleed=tileDefaultBleed)
3481
+ constructor(pos=vec2(), size=tileDefaultSize, textureInfo=textureInfos[0], padding=tileDefaultPadding, bleed=tileDefaultBleed, columns=0)
3481
3482
  {
3482
3483
  /** @property {Vector2} - Top left corner of tile in pixels */
3483
3484
  this.pos = pos.copy();
@@ -3489,6 +3490,8 @@ class TileInfo
3489
3490
  this.textureInfo = textureInfo;
3490
3491
  /** @property {number} - Shrinks tile by this many pixels to prevent neighbors bleeding */
3491
3492
  this.bleed = bleed;
3493
+ /** @property {number} - How many frames per row for frame(), 0 to keep frames on a single row */
3494
+ this.columns = columns;
3492
3495
  }
3493
3496
 
3494
3497
  /** Returns a copy of this tile offset by a vector
@@ -3496,9 +3499,10 @@ class TileInfo
3496
3499
  * @return {TileInfo}
3497
3500
  */
3498
3501
  offset(offset)
3499
- { return new TileInfo(this.pos.add(offset), this.size, this.textureInfo, this.padding, this.bleed); }
3502
+ { return new TileInfo(this.pos.add(offset), this.size, this.textureInfo, this.padding, this.bleed, this.columns); }
3500
3503
 
3501
3504
  /** Returns a copy of this tile offset by a number of animation frames
3505
+ * Frames wrap down to the next row if columns is set
3502
3506
  * @param {number} frame - Offset to apply in animation frames
3503
3507
  * @return {TileInfo}
3504
3508
  */
@@ -3506,9 +3510,23 @@ class TileInfo
3506
3510
  {
3507
3511
  ASSERT(typeof frame === 'number');
3508
3512
  const w = this.size.x + this.padding*2;
3509
- const x = frame*w;
3510
- ASSERT(x + this.size.x <= this.textureInfo.size.x, 'frame extends beyond texture width!');
3511
- return this.offset(new Vector2(x));
3513
+ const h = this.size.y + this.padding*2;
3514
+ const x = (this.columns ? frame % this.columns : frame) * w;
3515
+ const y = (this.columns ? frame / this.columns | 0 : 0) * h;
3516
+ ASSERT(this.pos.x + x + this.size.x <= this.textureInfo.size.x, 'frame extends beyond texture width!');
3517
+ ASSERT(this.pos.y + y + this.size.y <= this.textureInfo.size.y, 'frame extends beyond texture height!');
3518
+ return this.offset(new Vector2(x, y));
3519
+ }
3520
+
3521
+ /** Set how many frames per row this tile uses, so frame() can wrap
3522
+ * @param {number} [columns] - Frames per row, 0 to keep frames on a single row
3523
+ * @return {TileInfo}
3524
+ */
3525
+ setColumns(columns=0)
3526
+ {
3527
+ ASSERT(isNumber(columns) && columns >= 0, 'columns must be a number >= 0');
3528
+ this.columns = columns;
3529
+ return this;
3512
3530
  }
3513
3531
 
3514
3532
  /**
@@ -3517,7 +3535,7 @@ class TileInfo
3517
3535
  * @return {TileInfo}
3518
3536
  */
3519
3537
  index(index)
3520
- { return tile(index, this.size, this.textureInfo, this.padding, this.bleed); }
3538
+ { return tile(index, this.size, this.textureInfo, this.padding, this.bleed).setColumns(this.columns); }
3521
3539
 
3522
3540
  /**
3523
3541
  * Set this tile to use a full image in a texture info
@@ -3529,7 +3547,7 @@ class TileInfo
3529
3547
  this.textureInfo = textureInfo;
3530
3548
  this.pos = new Vector2;
3531
3549
  this.size = textureInfo.size.copy();
3532
- this.bleed = this.padding = 0;
3550
+ this.bleed = this.padding = this.columns = 0;
3533
3551
  return this;
3534
3552
  }
3535
3553
  }
@@ -14216,6 +14234,444 @@ function getCrescentPoints(pos, size=1, percent=0, angle=0, invert=false, sides=
14216
14234
  }
14217
14235
  return points;
14218
14236
  }
14237
+ /**
14238
+ * LittleJS Texture Sheet Plugin
14239
+ * - Packs images into texture sheets as they are loaded
14240
+ * - Sprites are placed automatically, callers get a TileInfo
14241
+ * - Sheets are created and filled as needed
14242
+ * - Sheets fill in call order, images decode in parallel
14243
+ * - Animation frames keep layout and wrap across rows as needed
14244
+ * - WebGL textures upload once per batch of loads
14245
+ * - loadAtlas imports pre-packed atlases (TexturePacker and Aseprite json)
14246
+ * @namespace TextureSheets
14247
+ */
14248
+
14249
+ /** Width and height in pixels of texture sheets created by loadSprite
14250
+ * @type {number}
14251
+ * @default
14252
+ * @memberof Settings */
14253
+ let textureSheetSize = 2048;
14254
+
14255
+ /** Default padding pixels around each frame packed by loadSprite
14256
+ * @type {number}
14257
+ * @default
14258
+ * @memberof Settings */
14259
+ let textureSheetPadding = 1;
14260
+
14261
+ /** Array of texture sheets created by loadSprite
14262
+ * @type {Array<TextureSheet>}
14263
+ * @memberof TextureSheets */
14264
+ let textureSheets = [];
14265
+
14266
+ // pending loads pack through a queue so sheets fill in call order
14267
+ let textureSheetQueue = Promise.resolve();
14268
+ let textureSheetPendingCount = 0;
14269
+
14270
+ /**
14271
+ * Texture Sheet - A texture that images are packed into as they load
14272
+ * Uses shelf packing, images are placed left to right then wrap to a new row
14273
+ * @memberof TextureSheets
14274
+ */
14275
+ class TextureSheet
14276
+ {
14277
+ /** Create a texture sheet, called automatically by loadSprite
14278
+ * @param {number} [size] - Width and height of the sheet in pixels */
14279
+ constructor(size=textureSheetSize)
14280
+ {
14281
+ ASSERT(size > 0, 'texture sheet size must be positive');
14282
+
14283
+ /** @property {number} - Width and height of the sheet in pixels */
14284
+ this.size = size;
14285
+ /** @property {OffscreenCanvas} - Canvas holding the packed images */
14286
+ this.canvas = headlessMode ? undefined : new OffscreenCanvas(size, size);
14287
+ /** @property {OffscreenCanvasRenderingContext2D} - 2d context for the canvas */
14288
+ this.context = this.canvas?.getContext('2d');
14289
+ /** @property {TextureInfo} - The texture info for this sheet */
14290
+ this.textureInfo = new TextureInfo(this.canvas);
14291
+ /** @property {Vector2} - Where the next image will be packed */
14292
+ this.cursor = vec2();
14293
+ /** @property {number} - Height of the row being packed */
14294
+ this.rowHeight = 0;
14295
+ /** @property {boolean} - Has the canvas changed since the last webgl upload? */
14296
+ this.glDirty = false;
14297
+
14298
+ if (headlessMode)
14299
+ {
14300
+ // tiles still need bounds when there is no canvas to measure
14301
+ this.textureInfo.size = vec2(size);
14302
+ this.textureInfo.sizeInverse = vec2(1/size);
14303
+ }
14304
+ }
14305
+
14306
+ /** Find a spot for an image on this sheet without drawing it
14307
+ * @param {Vector2} imageSize - Size of the source image in pixels
14308
+ * @param {Vector2} [frameSize] - Size of each frame, or the whole image if not passed
14309
+ * @param {number} [padding] - How many pixels padding around each frame
14310
+ * @return {TileInfo} Tile for the packed image, or undefined if the sheet is full */
14311
+ tryAdd(imageSize, frameSize=imageSize, padding=textureSheetPadding)
14312
+ {
14313
+ ASSERT(isVector2(imageSize) && isVector2(frameSize), 'sizes must be vec2');
14314
+ ASSERT(frameSize.x > 0 && frameSize.y > 0, 'frame size must be positive');
14315
+ ASSERT(imageSize.x % frameSize.x === 0 && imageSize.y % frameSize.y === 0,
14316
+ 'image size must be a multiple of the frame size');
14317
+
14318
+ const cellWidth = frameSize.x + padding*2;
14319
+ const cellHeight = frameSize.y + padding*2;
14320
+ const maxColumns = this.size / cellWidth | 0;
14321
+ ASSERT(maxColumns > 0, 'frame is too wide to fit on a texture sheet');
14322
+
14323
+ // keep the layout of the source image, but narrow it if a row is too wide
14324
+ // frames wrap down to the next row, which TileInfo.frame handles via columns
14325
+ const sourceColumns = imageSize.x / frameSize.x;
14326
+ const frameCount = sourceColumns * (imageSize.y / frameSize.y);
14327
+ const columns = min(sourceColumns, maxColumns);
14328
+ const blockWidth = columns * cellWidth;
14329
+ const blockHeight = ceil(frameCount / columns) * cellHeight;
14330
+
14331
+ // probe the placement using locals so a failed try leaves the sheet unchanged
14332
+ let x = this.cursor.x, y = this.cursor.y, rowHeight = this.rowHeight;
14333
+ if (x + blockWidth > this.size)
14334
+ {
14335
+ // start a new row if this one does not have enough space left
14336
+ x = 0;
14337
+ y += rowHeight;
14338
+ rowHeight = 0;
14339
+ }
14340
+
14341
+ // out of space, the caller needs to use a different sheet
14342
+ if (y + blockHeight > this.size)
14343
+ return undefined;
14344
+
14345
+ // commit the placement, tile pos points inside the padding to match how tile() works
14346
+ this.cursor.x = x + blockWidth;
14347
+ this.cursor.y = y;
14348
+ this.rowHeight = max(rowHeight, blockHeight);
14349
+ return new TileInfo(vec2(x + padding, y + padding), frameSize, this.textureInfo, padding, 0, columns);
14350
+ }
14351
+
14352
+ /** Draw an image into this sheet at a tile returned by tryAdd
14353
+ * @param {HTMLImageElement} image - Source image to copy from
14354
+ * @param {TileInfo} tileInfo - Where to put it, from tryAdd
14355
+ * @param {boolean} [update] - Upload to webgl now, pass false when batching */
14356
+ drawImage(image, tileInfo, update=true)
14357
+ {
14358
+ ASSERT(!!this.context, 'texture sheet has no canvas');
14359
+
14360
+ // copy frames in order, reading the source left to right, top to bottom
14361
+ // the destination wraps at tileInfo.columns which may be narrower than the source
14362
+ const frameSize = tileInfo.size;
14363
+ const sourceColumns = image.width / frameSize.x;
14364
+ const frameCount = sourceColumns * (image.height / frameSize.y);
14365
+ const columns = tileInfo.columns || frameCount;
14366
+ const cellWidth = frameSize.x + tileInfo.padding*2;
14367
+ const cellHeight = frameSize.y + tileInfo.padding*2;
14368
+ for (let i = frameCount; i--;)
14369
+ {
14370
+ const sourceX = (i % sourceColumns) * frameSize.x;
14371
+ const sourceY = (i / sourceColumns | 0) * frameSize.y;
14372
+ this.context.drawImage(image,
14373
+ sourceX, sourceY, frameSize.x, frameSize.y,
14374
+ tileInfo.pos.x + (i % columns) * cellWidth,
14375
+ tileInfo.pos.y + (i / columns | 0) * cellHeight,
14376
+ frameSize.x, frameSize.y);
14377
+ }
14378
+
14379
+ // upload now unless the caller is batching more images
14380
+ this.glDirty = true;
14381
+ update && this.updateTexture();
14382
+ }
14383
+
14384
+ /** Upload the canvas to webgl if it has changed since the last upload
14385
+ * Only needed after batching, drawImage uploads automatically by default */
14386
+ updateTexture()
14387
+ {
14388
+ if (!this.glDirty) return;
14389
+ this.glDirty = false;
14390
+ this.textureInfo.createWebGLTexture();
14391
+ }
14392
+ }
14393
+
14394
+ ///////////////////////////////////////////////////////////////////////////////
14395
+
14396
+ /** Load an image and pack it into a texture sheet
14397
+ * - Returns a TileInfo immediately which is filled in when the image loads
14398
+ * - Nothing is visible until it loads, use spritesReady to wait for it
14399
+ * - Pass frameSize for animations, then step through them with TileInfo.frame
14400
+ * - Grid images keep their layout and frames wrap down to the next row
14401
+ * @param {string} src - Image source path
14402
+ * @param {Vector2|number} [frameSize] - Size of each animation frame in pixels
14403
+ * @param {number} [padding] - How many pixels padding around each frame
14404
+ * @return {TileInfo}
14405
+ * @example
14406
+ * const playerTile = loadSprite('player.png'); // a single sprite
14407
+ * const runTile = loadSprite('run.png', vec2(16)); // a 16x16 frame animation
14408
+ * @memberof TextureSheets */
14409
+ function loadSprite(src, frameSize, padding=textureSheetPadding)
14410
+ {
14411
+ ASSERT(isStringLike(src), 'image src must be a string');
14412
+ ASSERT(!frameSize || isVector2(frameSize) || isNumber(frameSize), 'frameSize must be a vec2 or number');
14413
+ ASSERT(isNumber(padding), 'padding must be a number');
14414
+
14415
+ if (isNumber(frameSize))
14416
+ frameSize = vec2(frameSize);
14417
+
14418
+ // start with an empty tile that gets filled in when the image loads
14419
+ const tileInfo = new TileInfo(vec2(), vec2(), undefined, padding, 0);
14420
+ if (headlessMode) return tileInfo;
14421
+
14422
+ // point at a sheet right away so drawing before it loads picks up empty pixels
14423
+ tileInfo.textureInfo = (textureSheets[0] || textureSheetCreate()).textureInfo;
14424
+
14425
+ // start decoding right away, images decode in parallel
14426
+ const image = new Image;
14427
+ const imagePromise = new Promise(resolve =>
14428
+ {
14429
+ image.onerror = image.onload = resolve;
14430
+ image.crossOrigin = 'anonymous';
14431
+ image.src = src;
14432
+ });
14433
+
14434
+ // pack through a queue so sheets fill in call order, not decode order
14435
+ ++textureSheetPendingCount;
14436
+ textureSheetQueue = textureSheetQueue.then(async ()=>
14437
+ {
14438
+ await imagePromise;
14439
+ if (image.width)
14440
+ {
14441
+ // pack onto a sheet, then fill in the tile that was already handed out,
14442
+ // copying every field so nothing is missed if TileInfo gains more of them
14443
+ const imageSize = vec2(image.width, image.height);
14444
+ const {sheet, tile} = textureSheetAdd(imageSize, frameSize, padding);
14445
+ Object.assign(tileInfo, tile);
14446
+ sheet.drawImage(image, tileInfo, false); // upload once per batch below
14447
+ }
14448
+ else
14449
+ {
14450
+ // leave the tile empty if the image failed to load
14451
+ LOG('loadSprite failed to load image:', src);
14452
+ }
14453
+
14454
+ // upload to webgl once per batch, when the last pending load finishes
14455
+ if (!--textureSheetPendingCount)
14456
+ textureSheets.forEach(s=> s.updateTexture());
14457
+ });
14458
+
14459
+ return tileInfo;
14460
+ }
14461
+
14462
+ /** Load a pre-packed texture atlas and repack it onto texture sheets
14463
+ * - Supports TexturePacker json (hash and array) and Aseprite json
14464
+ * - Returns an empty object which is filled with TileInfos when loaded
14465
+ * - Frames are named by the json, animations are grouped automatically
14466
+ * - Aseprite frame tags become animations, so do names like run_0, run_1
14467
+ * - Trimmed frames are restored to their full source size when packed
14468
+ * - Rotated frames are rotated back upright when packed
14469
+ * @param {string} imageSrc - Atlas image path
14470
+ * @param {string|Object} jsonSrc - Atlas json path, or already parsed json data
14471
+ * @param {number} [padding] - How many pixels padding around each frame
14472
+ * @return {Object} Object mapping frame and animation names to TileInfos
14473
+ * @example
14474
+ * const atlas = loadAtlas('sprites.png', 'sprites.json');
14475
+ * await spritesReady();
14476
+ * drawTile(pos, size, atlas.player); // a single frame
14477
+ * drawTile(pos, size, atlas.run.frame(2)); // frame 2 of the run animation
14478
+ * @memberof TextureSheets */
14479
+ function loadAtlas(imageSrc, jsonSrc, padding=textureSheetPadding)
14480
+ {
14481
+ ASSERT(isStringLike(imageSrc), 'atlas image src must be a string');
14482
+ ASSERT(isStringLike(jsonSrc) || typeof jsonSrc === 'object', 'atlas json must be a path or object');
14483
+ ASSERT(isNumber(padding), 'padding must be a number');
14484
+
14485
+ const atlas = {};
14486
+ if (headlessMode) return atlas;
14487
+
14488
+ // start fetching the json and decoding the image right away, in parallel
14489
+ const jsonPromise = typeof jsonSrc === 'object' ? Promise.resolve(jsonSrc) :
14490
+ fetch(jsonSrc).then(r=> r.ok && r.json()).catch(()=> undefined);
14491
+ const image = new Image;
14492
+ const imagePromise = new Promise(resolve =>
14493
+ {
14494
+ image.onerror = image.onload = resolve;
14495
+ image.crossOrigin = 'anonymous';
14496
+ image.src = imageSrc;
14497
+ });
14498
+
14499
+ // pack through a queue so sheets fill in call order, not decode order
14500
+ ++textureSheetPendingCount;
14501
+ textureSheetQueue = textureSheetQueue.then(async ()=>
14502
+ {
14503
+ const data = await jsonPromise;
14504
+ await imagePromise;
14505
+ if (image.width && data)
14506
+ {
14507
+ for (const group of parseAtlas(data))
14508
+ {
14509
+ // reserve a block of full size cells, one per frame
14510
+ const sourceSize = group.frames[0].sourceSize;
14511
+ const blockSize = vec2(sourceSize.x*group.frames.length, sourceSize.y);
14512
+ const {sheet, tile} = textureSheetAdd(blockSize, sourceSize, padding);
14513
+
14514
+ // draw each frame untrimmed into its cell
14515
+ const context = sheet.context;
14516
+ const cellWidth = sourceSize.x + padding*2;
14517
+ const cellHeight = sourceSize.y + padding*2;
14518
+ group.frames.forEach((f, i)=>
14519
+ {
14520
+ const x = tile.pos.x + (i % tile.columns)*cellWidth + f.offset.x;
14521
+ const y = tile.pos.y + (i / tile.columns |0)*cellHeight + f.offset.y;
14522
+ if (f.rotated)
14523
+ {
14524
+ // stored rotated 90 degrees clockwise, draw it back upright
14525
+ context.save();
14526
+ context.translate(x, y);
14527
+ context.rotate(-PI/2);
14528
+ context.drawImage(image, f.pos.x, f.pos.y, f.size.y, f.size.x,
14529
+ -f.size.y, 0, f.size.y, f.size.x);
14530
+ context.restore();
14531
+ }
14532
+ else
14533
+ context.drawImage(image, f.pos.x, f.pos.y, f.size.x, f.size.y,
14534
+ x, y, f.size.x, f.size.y);
14535
+ });
14536
+ sheet.glDirty = true;
14537
+ atlas[group.name] = tile;
14538
+ }
14539
+ }
14540
+ else
14541
+ {
14542
+ // leave the atlas empty if either file failed to load
14543
+ LOG('loadAtlas failed to load:', imageSrc, jsonSrc);
14544
+ }
14545
+
14546
+ // upload to webgl once per batch, when the last pending load finishes
14547
+ if (!--textureSheetPendingCount)
14548
+ textureSheets.forEach(s=> s.updateTexture());
14549
+ });
14550
+
14551
+ return atlas;
14552
+ }
14553
+
14554
+ /** Parse atlas json into a list of named frame groups, used by loadAtlas
14555
+ * - Accepts TexturePacker json (hash and array) and Aseprite json
14556
+ * - Frames tagged in Aseprite or named like run_0, run_1 group into animations
14557
+ * @param {Object} data - Parsed atlas json data
14558
+ * @return {Array<Object>} List of {name, frames} groups in atlas order
14559
+ * @memberof TextureSheets */
14560
+ function parseAtlas(data)
14561
+ {
14562
+ ASSERT(!!data?.frames, 'unrecognized atlas format, expected TexturePacker or Aseprite json');
14563
+
14564
+ // normalize both hash and array frame layouts into a single list
14565
+ const frames = (isArray(data.frames) ?
14566
+ data.frames.map(f=> [f.filename, f]) : Object.entries(data.frames))
14567
+ .map(([name, f])=> ({
14568
+ name: name.replace(/\.[^.\\/]+$/, ''), // strip file extension
14569
+ pos: vec2(f.frame.x, f.frame.y),
14570
+ size: vec2(f.frame.w, f.frame.h),
14571
+ offset: vec2(f.spriteSourceSize?.x ?? 0, f.spriteSourceSize?.y ?? 0),
14572
+ sourceSize: vec2(f.sourceSize?.w ?? f.frame.w, f.sourceSize?.h ?? f.frame.h),
14573
+ rotated: !!f.rotated,
14574
+ }));
14575
+
14576
+ const groups = [];
14577
+ const tags = data.meta?.frameTags;
14578
+ if (tags?.length)
14579
+ {
14580
+ // aseprite tags are authoritative, untagged frames stay individual
14581
+ const tagged = new Set;
14582
+ for (const tag of tags)
14583
+ {
14584
+ groups.push({name: tag.name, frames: frames.slice(tag.from, tag.to + 1)});
14585
+ for (let i = tag.from; i <= tag.to; ++i)
14586
+ tagged.add(i);
14587
+ }
14588
+ frames.forEach((f, i)=> tagged.has(i) || groups.push({name: f.name, frames: [f]}));
14589
+ return groups;
14590
+ }
14591
+
14592
+ // group frames that share a name stem with contiguous trailing numbers
14593
+ // run_0.png and run_1.png become a 2 frame animation named run
14594
+ const stems = new Map;
14595
+ for (const f of frames)
14596
+ {
14597
+ let match = f.name.match(/^(.+?)([-_ ])?(\d+)$/);
14598
+ if (match && !match[2] && /\d$/.test(match[1]))
14599
+ match = undefined; // all digit tails like 10 are a name, not frame 0 of 1
14600
+ const stem = match ? match[1] : f.name;
14601
+ f.groupIndex = match ? Number(match[3]) : undefined;
14602
+ stems.has(stem) || stems.set(stem, []);
14603
+ stems.get(stem).push(f);
14604
+ }
14605
+ for (const [stem, list] of stems)
14606
+ {
14607
+ // only group 2 or more frames with contiguous indices and matching sizes
14608
+ list.sort((a, b)=> a.groupIndex - b.groupIndex);
14609
+ const grouped = list.length > 1 &&
14610
+ list.every((f, i)=> f.groupIndex === list[0].groupIndex + i) &&
14611
+ list.every(f=> f.sourceSize.x === list[0].sourceSize.x &&
14612
+ f.sourceSize.y === list[0].sourceSize.y);
14613
+ if (grouped)
14614
+ groups.push({name: stem, frames: list});
14615
+ else
14616
+ list.forEach(f=> groups.push({name: f.name, frames: [f]}));
14617
+ }
14618
+ return groups;
14619
+ }
14620
+
14621
+ /** Wait for everything started by loadSprite and loadAtlas to finish packing
14622
+ * @return {Promise}
14623
+ * @example
14624
+ * async function gameInit()
14625
+ * {
14626
+ * playerTile = loadSprite('player.png');
14627
+ * runTile = loadSprite('run.png', vec2(16));
14628
+ * await spritesReady();
14629
+ * }
14630
+ * @memberof TextureSheets */
14631
+ async function spritesReady()
14632
+ {
14633
+ // keep waiting until the queue drains, more sprites may load while waiting
14634
+ while (textureSheetPendingCount)
14635
+ await textureSheetQueue;
14636
+ }
14637
+
14638
+ // create a new texture sheet and add it to the list
14639
+ function textureSheetCreate()
14640
+ {
14641
+ const sheet = new TextureSheet;
14642
+ textureSheets.push(sheet);
14643
+ return sheet;
14644
+ }
14645
+
14646
+ // use the first sheet with enough space, or make a new one
14647
+ function textureSheetAdd(imageSize, frameSize, padding)
14648
+ {
14649
+ let sheet, tile;
14650
+ for (sheet of textureSheets)
14651
+ if (tile = sheet.tryAdd(imageSize, frameSize, padding))
14652
+ break;
14653
+ if (!tile)
14654
+ {
14655
+ sheet = textureSheetCreate();
14656
+ tile = sheet.tryAdd(imageSize, frameSize, padding);
14657
+ ASSERT(!!tile, 'image is too large to fit on a texture sheet');
14658
+ }
14659
+ return {sheet, tile};
14660
+ }
14661
+
14662
+ ///////////////////////////////////////////////////////////////////////////////
14663
+ // Texture sheet setting setters
14664
+
14665
+ /** Set width and height in pixels of texture sheets created by loadSprite
14666
+ * @param {number} size
14667
+ * @memberof Settings */
14668
+ function setTextureSheetSize(size) { textureSheetSize = size; }
14669
+
14670
+ /** Set default padding pixels around each frame packed by loadSprite
14671
+ * @param {number} padding
14672
+ * @memberof Settings */
14673
+ function setTextureSheetPadding(padding) { textureSheetPadding = padding; }
14674
+
14219
14675
  /**
14220
14676
  * LittleJS Tween System Plugin
14221
14677
  * - Lightweight tweens for numbers, Vector2, Color, or any .lerp-able type
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "littlejsengine",
3
- "version": "1.18.21",
3
+ "version": "1.18.22",
4
4
  "description": "LittleJS - Tiny and Fast HTML5 Game Engine",
5
5
  "main": "dist/littlejs.esm.js",
6
6
  "types": "dist/littlejs.d.ts",
@@ -100,4 +100,16 @@ export
100
100
  threeJS,
101
101
  ThreeJSPlugin,
102
102
  ThreeJSObject,
103
- }
103
+
104
+ // Texture Sheets
105
+ textureSheetSize,
106
+ textureSheetPadding,
107
+ setTextureSheetSize,
108
+ setTextureSheetPadding,
109
+ textureSheets,
110
+ TextureSheet,
111
+ loadSprite,
112
+ loadAtlas,
113
+ parseAtlas,
114
+ spritesReady,
115
+ }