Skip to content

Commit

Permalink
feat: SubTexture forward parent texture Freed events
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterlucas committed Jan 23, 2025
1 parent 8a6dfdf commit d2838b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
38 changes: 23 additions & 15 deletions examples/tests/texture-reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export default async function ({ renderer, testRoot }: ExampleSettings) {
export default async function test({ renderer, testRoot }: ExampleSettings) {
const screenWidth = renderer.settings.appWidth;
const screenHeight = renderer.settings.appHeight;
const nodeSize = 128; // Each node will be 128x128 pixels
Expand Down Expand Up @@ -197,6 +197,14 @@ export default async function ({ renderer, testRoot }: ExampleSettings) {
src: rockoPng,
});

image.on('loaded', () => {
console.warn('Parent Image texture loaded.');
});

image.on('freed', () => {
console.warn('Parent Image texture freed.');
});

const nodeSpawnX = 1100;
const nodeSpawnY = 30;

Expand All @@ -214,15 +222,6 @@ export default async function ({ renderer, testRoot }: ExampleSettings) {
height: nodeSize,
parent: testRoot,
}),
'Image Texture': () =>
renderer.createNode({
x: nodeSpawnX,
y: nodeSpawnY,
width: nodeSize,
height: nodeSize,
src: rockoPng,
parent: testRoot,
}),
// No need to test color textures, they all sample from the same 1x1 pixel texture
// and are not subject to the same memory constraints as other textures
// "Color Texture": () => renderer.createNode({
Expand All @@ -248,6 +247,15 @@ export default async function ({ renderer, testRoot }: ExampleSettings) {
height: nodeSize,
parent: testRoot,
}),
'Image Texture': () =>
renderer.createNode({
x: nodeSpawnX,
y: nodeSpawnY,
width: nodeSize,
height: nodeSize,
src: rockoPng,
parent: testRoot,
}),
'RTT Node': () => {
const rtt = renderer.createNode({
rtt: true,
Expand Down Expand Up @@ -294,21 +302,19 @@ export default async function ({ renderer, testRoot }: ExampleSettings) {
const result = await testNode(testNodeInstance);

if (!result) {
console.error(`${testIdx}. Test failed for: ${name}`);
finalStatus.text = `Test failed for: ${name}`;
finalStatus.color = 0xff0000ff;
allTestsPassed = false;
}

console.log(`${testIdx}. Test passed for: ${name}`);
const status = result ? 'passed' : 'failed';
console.log(`${testIdx}. Test ${result} for: ${name}`);

testNodeInstance.x = 500;
testNodeInstance.y = lastStatusOffSet + 128;
testNodeInstance.width = 128;
testNodeInstance.height = 128;

const status = result ? 'passed' : 'failed';

renderer.createTextNode({
fontFamily: 'Ubuntu',
text: `${testIdx}. Test ${status} for: ${name}`,
Expand All @@ -327,10 +333,12 @@ export default async function ({ renderer, testRoot }: ExampleSettings) {
console.log('All tests passed successfully!');
finalStatus.text = `All tests passed successfully!`;
finalStatus.color = 0x00ff00ff;

return true;
} else {
console.error('One or more tests failed.');
finalStatus.text = `One or more tests failed.`;
finalStatus.color = 0xff0000ff;
throw new Error('Test suite failed.');
return false;
}
}
9 changes: 9 additions & 0 deletions src/core/textures/SubTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,13 @@ export class SubTexture extends Texture {
this.onParentTxLoaded(parentTx, parentTx.dimensions!);
} else if (parentTx.state === 'failed') {
this.onParentTxFailed(parentTx, parentTx.error!);
} else if (parentTx.state === 'freed') {
this.onParentTxFreed();
}

parentTx.on('loaded', this.onParentTxLoaded);
parentTx.on('failed', this.onParentTxFailed);
parentTx.on('freed', this.onParentTxFreed);
});
}

Expand All @@ -138,6 +142,11 @@ export class SubTexture extends Texture {
this.setSourceState('failed', error);
};

private onParentTxFreed = () => {
console.log('Parent texture freed, freeing sub-texture');
this.setSourceState('freed');
};

override onChangeIsRenderable(isRenderable: boolean): void {
// Propagate the renderable owner change to the parent texture
this.parentTexture.setRenderableOwner(this, isRenderable);
Expand Down

0 comments on commit d2838b3

Please sign in to comment.