From d2838b3519d8320ffc9f32884b2c484adc96d49b Mon Sep 17 00:00:00 2001 From: wouterlucas Date: Thu, 23 Jan 2025 14:53:08 +0100 Subject: [PATCH] feat: SubTexture forward parent texture Freed events --- examples/tests/texture-reload.ts | 38 +++++++++++++++++++------------- src/core/textures/SubTexture.ts | 9 ++++++++ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/examples/tests/texture-reload.ts b/examples/tests/texture-reload.ts index 974dc410..67932e45 100644 --- a/examples/tests/texture-reload.ts +++ b/examples/tests/texture-reload.ts @@ -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 @@ -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; @@ -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({ @@ -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, @@ -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}`, @@ -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; } } diff --git a/src/core/textures/SubTexture.ts b/src/core/textures/SubTexture.ts index c28ef98d..01533301 100644 --- a/src/core/textures/SubTexture.ts +++ b/src/core/textures/SubTexture.ts @@ -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); }); } @@ -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);