-
Notifications
You must be signed in to change notification settings - Fork 905
Make structure icon recovery state-sourced after runtime renderer disruption #3480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Skigim
wants to merge
7
commits into
openfrontio:main
Choose a base branch
from
Skigim:copilot/investigate-rendering-bug
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3056abb
Initial plan
Copilot 47aedf4
debug: instrument runtime render recovery paths
Copilot 8b31624
test: finalize render recovery investigation hooks
Copilot 0c82597
refactor: remove temporary render debug API
Copilot d4840f6
feat: implement dispose method for resource cleanup in GameRenderer a…
Skigim 4b26d4e
Merge branch 'main' into copilot/investigate-rendering-bug
Skigim b5d0611
fix: ensure rebuildAllStructuresFromState is a no-op if renderer is n…
Skigim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,18 @@ export class StructureIconsLayer implements Layer { | |
| private visibilityStateDirty = true; | ||
| private pendingConfirm: MouseUpEvent | null = null; | ||
| private hasHiddenStructure = false; | ||
| private onWebGLContextLost: ((event: Event) => void) | null = null; | ||
| private onWebGLContextRestored: (() => void) | null = null; | ||
| private readonly onResize = () => this.resizeCanvas(); | ||
| private readonly onToggleStructures = (e: ToggleStructuresEvent) => | ||
| this.toggleStructures(e.structureTypes); | ||
| private readonly onMouseMove = (e: MouseMoveEvent) => this.moveGhost(e); | ||
| private readonly onMouseUp = (e: MouseUpEvent) => | ||
| this.requestConfirmStructure(e); | ||
| private readonly onConfirmGhostStructure = () => | ||
| this.requestConfirmStructure( | ||
| new MouseUpEvent(this.mousePos.x, this.mousePos.y), | ||
| ); | ||
| potentialUpgrade: StructureRenderInfo | undefined; | ||
|
|
||
| constructor( | ||
|
|
@@ -166,35 +178,77 @@ export class StructureIconsLayer implements Layer { | |
|
|
||
| this.renderer = renderer; | ||
| this.rendererInitialized = true; | ||
| this.onWebGLContextLost = (event) => { | ||
| // Prevent the browser's default context-loss handling so Pixi can restore | ||
| // the WebGL context and we can rebuild structure renders from game state. | ||
| event.preventDefault(); | ||
| }; | ||
| this.onWebGLContextRestored = () => { | ||
| this.redraw(); | ||
| }; | ||
| this.pixicanvas.addEventListener( | ||
| "webglcontextlost", | ||
| this.onWebGLContextLost, | ||
| ); | ||
| this.pixicanvas.addEventListener( | ||
| "webglcontextrestored", | ||
| this.onWebGLContextRestored, | ||
| ); | ||
| } | ||
|
|
||
| shouldTransform(): boolean { | ||
| return false; | ||
| } | ||
|
|
||
| async init() { | ||
| this.eventBus.on(ToggleStructuresEvent, (e) => | ||
| this.toggleStructures(e.structureTypes), | ||
| ); | ||
| this.eventBus.on(MouseMoveEvent, (e) => this.moveGhost(e)); | ||
| this.eventBus.on(ToggleStructuresEvent, this.onToggleStructures); | ||
| this.eventBus.on(MouseMoveEvent, this.onMouseMove); | ||
| this.eventBus.on(MouseUpEvent, this.onMouseUp); | ||
| this.eventBus.on(ConfirmGhostStructureEvent, this.onConfirmGhostStructure); | ||
|
|
||
| this.eventBus.on(MouseUpEvent, (e) => this.requestConfirmStructure(e)); | ||
| this.eventBus.on(ConfirmGhostStructureEvent, () => | ||
| this.requestConfirmStructure( | ||
| new MouseUpEvent(this.mousePos.x, this.mousePos.y), | ||
| ), | ||
| ); | ||
|
|
||
| window.addEventListener("resize", () => this.resizeCanvas()); | ||
| window.addEventListener("resize", this.onResize); | ||
| await this.setupRenderer(); | ||
| this.redraw(); | ||
| } | ||
|
|
||
| dispose() { | ||
| this.eventBus.off(ToggleStructuresEvent, this.onToggleStructures); | ||
| this.eventBus.off(MouseMoveEvent, this.onMouseMove); | ||
| this.eventBus.off(MouseUpEvent, this.onMouseUp); | ||
| this.eventBus.off(ConfirmGhostStructureEvent, this.onConfirmGhostStructure); | ||
| window.removeEventListener("resize", this.onResize); | ||
|
|
||
| if (this.onWebGLContextLost) { | ||
| this.pixicanvas?.removeEventListener( | ||
| "webglcontextlost", | ||
| this.onWebGLContextLost, | ||
| ); | ||
| } | ||
| if (this.onWebGLContextRestored) { | ||
| this.pixicanvas?.removeEventListener( | ||
| "webglcontextrestored", | ||
| this.onWebGLContextRestored, | ||
| ); | ||
| } | ||
|
|
||
| this.onWebGLContextLost = null; | ||
| this.onWebGLContextRestored = null; | ||
| } | ||
|
|
||
| resizeCanvas() { | ||
| if (this.renderer) { | ||
| this.pixicanvas.width = window.innerWidth; | ||
| this.pixicanvas.height = window.innerHeight; | ||
| this.renderer.resize(innerWidth, innerHeight, 1); | ||
| this.resizeStages(); | ||
| // Canvas size changes affect screen-space culling and icon placement, so | ||
| // recompute every tracked structure location after a resize/reset. | ||
| for (const render of this.rendersByUnitId.values()) { | ||
| this.computeNewLocation(render); | ||
| } | ||
| if (this.ghostUnit) { | ||
| this.moveGhost(new MouseMoveEvent(this.mousePos.x, this.mousePos.y)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -221,6 +275,24 @@ export class StructureIconsLayer implements Layer { | |
|
|
||
| redraw() { | ||
| this.resizeCanvas(); | ||
| this.rebuildAllStructuresFromState(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must say i do like that, as a by-effect, Alt+R will work on the structure icons with this. Whereas currently only some of the graphics are rebuild if the user tries Alt+R. |
||
| } | ||
|
|
||
| rebuildAllStructuresFromState() { | ||
| if (!this.rendererInitialized || !this.renderer) { | ||
| return; | ||
| } | ||
|
|
||
| this.clearAllStructureRenders(); | ||
|
|
||
| for (const unitView of this.game.units()) { | ||
| if ( | ||
| unitView.isActive() && | ||
| this.structures.has(unitView.type() as PlayerBuildableUnitType) | ||
| ) { | ||
| this.addNewStructure(unitView); | ||
| } | ||
| } | ||
Skigim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| renderLayer(mainContext: CanvasRenderingContext2D) { | ||
|
|
@@ -852,4 +924,36 @@ export class StructureIconsLayer implements Layer { | |
| this.potentialUpgrade = undefined; | ||
| } | ||
| } | ||
|
|
||
| private clearAllStructureRenders() { | ||
| this.clearStageChildren(this.iconsStage); | ||
| this.clearStageChildren(this.levelsStage); | ||
| this.clearStageChildren(this.dotsStage); | ||
| this.rendersByUnitId.clear(); | ||
| this.seenUnitIds.clear(); | ||
| this.potentialUpgrade = undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Deep-clears a Pixi stage after renderer disruption or before a full | ||
| * state-sourced rebuild. Callers must repopulate the stage immediately after | ||
| * this cleanup because all child display objects are destroyed recursively. | ||
| */ | ||
| private clearStageChildren(stage?: PIXI.Container) { | ||
| if (!stage) { | ||
| return; | ||
| } | ||
|
|
||
| for (const child of stage.removeChildren()) { | ||
| child.destroy({ children: true }); | ||
| } | ||
| } | ||
Skigim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private resizeStages() { | ||
| this.iconsStage?.setSize(this.pixicanvas.width, this.pixicanvas.height); | ||
| this.ghostStage?.setSize(this.pixicanvas.width, this.pixicanvas.height); | ||
| this.levelsStage?.setSize(this.pixicanvas.width, this.pixicanvas.height); | ||
| this.dotsStage?.setSize(this.pixicanvas.width, this.pixicanvas.height); | ||
| this.rootStage?.setSize(this.pixicanvas.width, this.pixicanvas.height); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this related the the canvas redraw?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could fit well with my attempt at fixing the "icons displaced on zoom" in #3453. Which is currently waiting for testers.
If this code isn't accepted as part of this PR because it is outside its scope, it may well fit within the scope of my PR 3453 or as a seperate PR. Since resize isn't covered by TransformHandler, currently icon locations are indeed not recalculated in all cases it seems.
(also @Skigim )