Skip to content

Commit

Permalink
feat(core): add webgl2 and offscreencanvas check
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaisthorpe committed Aug 26, 2024
1 parent 8761597 commit 46136d6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-walls-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tedengine/ted': minor
---

Check for WebGL2 and OffscreenCanvas support on bootstrap
27 changes: 27 additions & 0 deletions packages/ted/src/fred/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export type TBrowserFeature = 'webgl2' | 'offscreencanvas';

export default class TBrowser {
public supports(feature: TBrowserFeature): boolean {
switch (feature) {
case 'webgl2':
return this.supportsWebGL2();
case 'offscreencanvas':
return this.supportsOffscreenCanvas();
default:
return false;
}
}

private supportsWebGL2(): boolean {
try {
const canvas = document.createElement('canvas');
return !!canvas.getContext('webgl2');
} catch (e) {
return false;
}
}

private supportsOffscreenCanvas(): boolean {
return typeof OffscreenCanvas !== 'undefined';
}
}
11 changes: 11 additions & 0 deletions packages/ted/src/fred/fred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { TMessageTypesJobs } from '../jobs/messages';
import type { TFrameParams } from '../renderer/frame-params';
import TRenderer from '../renderer/renderer';
import type { TEngineContextData, TGameContextData } from '../ui/context';
import TBrowser from './browser';
import type { TWindowBlurEvent, TWindowFocusEvent } from './events';
import { TEventTypesWindow } from './events';
import type { TFredMessageReady, TFredMessageShutdown } from './messages';
Expand Down Expand Up @@ -132,11 +133,21 @@ export default class TFred {

/**
* Bootstrap gets the main thread ready for loading by:
* - Checking browser features
* - Creating the canvas
* - Starting to prepare graphics
* - Setting up events, input, audio
*/
private async bootstrap() {
const browser = new TBrowser();
if (!browser.supports('webgl2') || !browser.supports('offscreencanvas')) {
// @todo show error message
this.setErrorMessage(
'Browser does not support WebGL2 or OffscreenCanvas',
);
return;
}

// Create the canvas
this.container.classList.add('t-game-container');
this.canvas = document.createElement('canvas');
Expand Down
2 changes: 2 additions & 0 deletions packages/ted/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export * from './engine/engine';

export * from './fred/events';
export { default as TFred } from './fred/fred';
export * from './fred/browser';
export { default as TBrowser } from './fred/browser';

export * from './graphics';
export { default as TCanvas } from './graphics/canvas';
Expand Down

0 comments on commit 46136d6

Please sign in to comment.