Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/rendering/sprite/Sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,12 @@ export class Sprite extends Drawable {
return; // failed load shows Texture.missing; nothing to heal
}

// Guard against a texture swap or destroy between scheduling and resolution.
if (this._texture === texture && !this.destroyed) {
// Guard against a texture swap or destroy between scheduling and resolution —
// and against an explicit frame set in the meantime (a Spritesheet slicing
// frames out of a still-loading atlas). The schedule-time reset above left a
// 0×0 frame, so a non-empty frame here means someone chose one deliberately;
// only heal the untouched case.
if (this._texture === texture && !this.destroyed && this._textureFrame.width === 0 && this._textureFrame.height === 0) {
this.resetTextureFrame();
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/rendering/sprite/sprite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ describe('Sprite', () => {
expect(Number.isNaN(sprite.scale.x)).toBe(false);
expect(sprite.width).toBe(40);
});

test('an explicit frame set while the texture is loading survives hydration', async () => {
const { texture, finishLoad } = makeDeferredTexture();
const sprite = new Sprite(texture);

sprite.setTextureFrame(new Rectangle(64, 0, 32, 32));

finishLoad(128, 128);
await texture.loaded;
await Promise.resolve(); // flush the .then microtask

expect(sprite.textureFrame.x).toBe(64);
expect(sprite.textureFrame.y).toBe(0);
expect(sprite.textureFrame.width).toBe(32);
expect(sprite.textureFrame.height).toBe(32);
});

test('heals to the full texture frame when no explicit frame was set before hydration', async () => {
const { texture, finishLoad } = makeDeferredTexture();
const sprite = new Sprite(texture);

finishLoad(128, 128);
await texture.loaded;
await Promise.resolve(); // flush the .then microtask

expect(sprite.textureFrame.x).toBe(0);
expect(sprite.textureFrame.y).toBe(0);
expect(sprite.textureFrame.width).toBe(128);
expect(sprite.textureFrame.height).toBe(128);
});
});

// Binding a destroyed texture is otherwise silent — warn once (dev) at
Expand Down
Loading