()
| 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, |
| 70 | }); |
| 71 | } |
| 72 | } else if (event.type === "redrawRequested" && event.windowID === this.window.id) { |
| 73 | const texture = this.surface.getCurrentTexture(); |
| 74 | const view = texture.createView(); |
| 75 | const encoder = this.device.createCommandEncoder(); |
| 76 | this.render(encoder, view); |
| 77 | this.device.queue.submit([encoder.finish()]); |
| 78 | this.surface.present(); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | cleanup() { |
| 84 | if (this.mainLoopInterval) { |
no test coverage detected