From 49e9d45435e48290c54b45021216cc64da10ef3e Mon Sep 17 00:00:00 2001 From: Ben Richards Date: Fri, 2 Feb 2024 21:13:36 +1300 Subject: [PATCH] Emit LOSE_WEBGL event. Also rearrange RESTORE_WEBGL event code. --- src/renderer/events/LOSE_WEBGL_EVENT.js | 30 ++++++++++++++++++++++ src/renderer/events/RESTORE_WEBGL_EVENT.js | 8 +++--- src/renderer/events/index.js | 1 + src/renderer/webgl/WebGLRenderer.js | 6 +++-- 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 src/renderer/events/LOSE_WEBGL_EVENT.js diff --git a/src/renderer/events/LOSE_WEBGL_EVENT.js b/src/renderer/events/LOSE_WEBGL_EVENT.js new file mode 100644 index 000000000..62d1ae0b6 --- /dev/null +++ b/src/renderer/events/LOSE_WEBGL_EVENT.js @@ -0,0 +1,30 @@ +/** + * @author Benjamin D. Richards + * @copyright 2013-2023 Photon Storm Ltd. + * @license {@link https://opensource.org/licenses/MIT|MIT License} + */ + +/** + * The Lose WebGL Event. + * + * This event is dispatched by the WebGLRenderer when the WebGL context + * is lost. + * + * Context can be lost for a variety of reasons, like leaving the browser tab. + * The game canvas DOM object will dispatch `webglcontextlost`. + * All WebGL resources get wiped, and the context is reset. + * + * While WebGL is lost, the game will continue to run, but all WebGL resources + * are lost, and new ones cannot be created. + * + * Once the context is restored and the renderer has automatically restored + * the state, the renderer will emit a `RESTORE_WEBGL` event. At that point, + * it is safe to continue. + * + * @event Phaser.Renderer.Events#LOSE_WEBGL + * @type {string} + * @since 3.80.0 + * + * @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - the renderer that owns the WebGL context + */ +module.exports = 'losewebgl'; diff --git a/src/renderer/events/RESTORE_WEBGL_EVENT.js b/src/renderer/events/RESTORE_WEBGL_EVENT.js index bc8178732..2ea43fcdc 100644 --- a/src/renderer/events/RESTORE_WEBGL_EVENT.js +++ b/src/renderer/events/RESTORE_WEBGL_EVENT.js @@ -1,11 +1,11 @@ /** - * @author Richard Davey + * @author Benjamin D. Richards * @copyright 2013-2023 Photon Storm Ltd. * @license {@link https://opensource.org/licenses/MIT|MIT License} */ /** - * The Restore Event. + * The Restore WebGL Event. * * This event is dispatched by the WebGLRenderer when the WebGL context * is restored. @@ -16,10 +16,10 @@ * Listen to this event to know when you can safely do that. * * Context can be lost for a variety of reasons, like leaving the browser tab. - * The browser window will dispatch `webglcontextlost` when this happens. + * The game canvas DOM object will dispatch `webglcontextlost`. * All WebGL resources get wiped, and the context is reset. * - * Once the context is restored, the browser will dispatch + * Once the context is restored, the canvas will dispatch * `webglcontextrestored`. Phaser uses this to re-create necessary resources. * Please wait for Phaser to dispatch the `RESTORE_WEBGL` event before * re-creating any resources of your own. diff --git a/src/renderer/events/index.js b/src/renderer/events/index.js index 10dbb5017..f6f4f8e4a 100644 --- a/src/renderer/events/index.js +++ b/src/renderer/events/index.js @@ -10,6 +10,7 @@ module.exports = { + LOSE_WEBGL: require('./LOSE_WEBGL_EVENT'), POST_RENDER: require('./POST_RENDER_EVENT'), PRE_RENDER: require('./PRE_RENDER_EVENT'), RENDER: require('./RENDER_EVENT'), diff --git a/src/renderer/webgl/WebGLRenderer.js b/src/renderer/webgl/WebGLRenderer.js index eb206f136..8df1c6463 100644 --- a/src/renderer/webgl/WebGLRenderer.js +++ b/src/renderer/webgl/WebGLRenderer.js @@ -773,6 +773,8 @@ var WebGLRenderer = new Class({ console.warn('WebGL Context lost. Renderer disabled'); } + _this.emit(Events.LOSE_WEBGL, _this); + event.preventDefault(); }; @@ -838,9 +840,9 @@ var WebGLRenderer = new Class({ console.warn('WebGL Context restored. Renderer running again.'); } - event.preventDefault(); - _this.emit(Events.RESTORE_WEBGL, _this); + + event.preventDefault(); }; canvas.addEventListener('webglcontextrestored', this.contextRestoredHandler, false);