mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 12:40:46 +00:00
69d5f33665
## Description: This PR introduces handling for the canvas `contextlost` and `contextrestored` events to make the game's rendering more robust. Previously, if the graphics context was lost (which can happen due to browser memory management, GPU driver resets, etc.), the render loop would continue to run, but most drawing operations would silently fail. This resulted in a broken visual state where terrain, structures, and other graphics would disappear, leading to what players have referred to as the "black screen bug". These changes implement the following: 1. In `GameRenderer`, event listeners are added to the main canvas. 2. On `contextlost`, the `requestAnimationFrame` loop is cancelled, pausing rendering. 3. On `contextrestored`, a full redraw of all layers is triggered, and the render loop is restarted, allowing the game to gracefully recover. Additionally, a related bug in the `StructureLayer` is fixed. During a redraw/restoration, the layer could attempt to render unit icons to its canvas before the images were fully (re)decoded. The `init` method now explicitly waits for all icon images to be decoded before drawing them, ensuring the layer is restored correctly. **Important Note:** This PR represents a partial fix for the context loss issue. Specifically, the `StructureIconsLayer` remains in a broken state after context restoration. This layer is rendered using Pixi.js, which has its own specific process for handling renderer recovery. Due to my lack of experience with the Pixi.js API, I was unable to implement the fix for this layer. This would be an excellent follow-up contribution for someone familiar with Pixi. ## Please complete the following: - [X] I have added screenshots for all UI updates - [X] I process any text displayed to the user through translateText() and I've added it to the en.json file - [X] I have added relevant tests to the test directory - [X] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [X] I have read and accepted the CLA agreement (only required once). ## Please put your Discord username so you can be contacted if a bug or regression is found: aaa4xu