diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-17 13:51:38 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-17 13:51:38 +0100 |
commit | 38e90fc2e309d394832eee471d4cb9877ae908e2 (patch) | |
tree | d0c162d8c4935e05396ac804e323cc8188565a38 | |
parent | 49699eeb453b3ea8b934a6a69795e9db26d523c3 (diff) | |
download | rpgedit-38e90fc2e309d394832eee471d4cb9877ae908e2.tar rpgedit-38e90fc2e309d394832eee471d4cb9877ae908e2.zip |
GameContext: async render loop
-rw-r--r-- | src/controller/gamecontext.ts | 18 | ||||
-rw-r--r-- | src/util.ts | 4 |
2 files changed, 14 insertions, 8 deletions
diff --git a/src/controller/gamecontext.ts b/src/controller/gamecontext.ts index 4fcc883..1a94e3e 100644 --- a/src/controller/gamecontext.ts +++ b/src/controller/gamecontext.ts @@ -9,7 +9,7 @@ import { Renderer } from '../view/renderer/renderer'; import { Collidable } from '../math/collision'; import { Movement } from '../math/line'; -import { getJSON } from '../util'; +import { getJSON, nextAnimationFrame } from '../util'; import { vec2 } from 'gl-matrix'; @@ -84,7 +84,7 @@ export class GameContext implements CollidableGroup { } }); - window.requestAnimationFrame(this.render); + this.renderLoop(); } public getTranslation(): null { @@ -160,16 +160,18 @@ export class GameContext implements CollidableGroup { this.updateStep(); } - private render = (curTime: number) => { - const time = curTime - this.initTime; - this.update(time); - + private render(): void { this.renderer.setCenter(this.player.pos); this.renderer.clear(); for (const r of [this.mapView, ...this.entities, this.player]) - r.render(time); + r.render(this.time); + } - window.requestAnimationFrame(this.render); + private async renderLoop(): Promise<void> { + while (true) { + this.update(await nextAnimationFrame() - this.initTime); + this.render(); + } } } diff --git a/src/util.ts b/src/util.ts index 73e30d0..cea404b 100644 --- a/src/util.ts +++ b/src/util.ts @@ -78,3 +78,7 @@ export function get(url: string): Promise<XMLHttpRequest> { export async function getJSON(url: string): Promise<any> { return JSON.parse((await get(url)).responseText); } + +export function nextAnimationFrame(): Promise<DOMHighResTimeStamp> { + return new Promise((resolve) => window.requestAnimationFrame(resolve)); +} |