| 10 | |
| 11 | /** Base Application used by all examples */ |
| 12 | export class App { |
| 13 | constructor(title, width = 800, height = 600) { |
| 14 | this.title = title; |
| 15 | this.width = width; |
| 16 | this.height = height; |
| 17 | } |
| 18 | |
| 19 | // To be extended by subclasses |
| 20 | async init() {} |
| 21 | async render(_encoder, _view) {} |
| 22 | async onEvent(_event) {} |
| 23 | |
| 24 | mainLoop() { |
| 25 | this.window.requestRedraw(); |
| 26 | } |
| 27 | |
| 28 | async run() { |
| 29 | this.adapter = await navigator.gpu.requestAdapter(); |
| 30 | if (!this.adapter) { |
| 31 | throw new Error("No GPU adapter available!"); |
| 32 | } |
| 33 | |
| 34 | this.device = await this.adapter.requestDevice(); |
| 35 | enableValidationErrors(this.device, true); |
| 36 | |
| 37 | this.window = Deno.createWindow({ |
| 38 | title: this.title, |
| 39 | width: this.width, |
| 40 | height: this.height, |
| 41 | resizable: true, |
| 42 | }); |
| 43 | |
| 44 | this.surface = this.window.createSurface(this.device); |
| 45 | |
| 46 | this.format = this.surface.getPreferredFormat(); |
| 47 | this.surface.configure({ |
| 48 | format: this.format, |
| 49 | width: this.width, |
| 50 | height: this.height, |
| 51 | }); |
| 52 | |
| 53 | await this.init(); |
| 54 | |
| 55 | this.mainLoopInterval = setInterval(() => this.mainLoop(), 1000 / 60); |
| 56 | |
| 57 | for await (const event of Deno.eventLoop()) { |
| 58 | await this.onEvent(event); |
| 59 | if (event.type === "windowEvent" && event.windowID === this.window.id) { |
| 60 | if (event.event.type === "closeRequested") { |
| 61 | this.cleanup(); |
| 62 | Deno.exit(0); |
| 63 | } else if (event.event.type === "resized") { |
| 64 | this.width = event.event.width; |
| 65 | this.height = event.event.height; |
| 66 | this.surface.configure({ |
| 67 | format: this.format, |
| 68 | width: this.width, |
| 69 | height: this.height, |
nothing calls this directly
no outgoing calls
no test coverage detected