From 5ea8048f0cf6a1a158087f224b57db84b67f0634 Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Wed, 12 Jul 2017 12:26:14 +0300 Subject: [PATCH] Limit screen updates to once every 30ms and only if needed. --- sim/state/screen.ts | 3 ++- sim/visuals/board.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/sim/state/screen.ts b/sim/state/screen.ts index 929fd62d..8ea2a2e4 100644 --- a/sim/state/screen.ts +++ b/sim/state/screen.ts @@ -2,6 +2,7 @@ namespace pxsim { export class EV3ScreenState { + shouldUpdate: boolean; points: {[x: number]: {[y: number]: number}}; constructor() { this.points = {}; @@ -14,6 +15,7 @@ namespace pxsim { const xPoints = this.points[x] if (!xPoints) this.points[x] = {}; this.points[x][y] = v; + this.shouldUpdate = true; } } } @@ -24,7 +26,6 @@ namespace pxsim.screen { export function _setPixel(x: number, y: number, mode: Draw) { const screenState = (board() as DalBoard).screenState; screenState.setPixel(x, y, mode); - runtime.queueDisplayUpdate(); } diff --git a/sim/visuals/board.ts b/sim/visuals/board.ts index 235105ef..a86ceb4e 100644 --- a/sim/visuals/board.ts +++ b/sim/visuals/board.ts @@ -284,6 +284,11 @@ namespace pxsim.visuals { this.updateState(); this.attachEvents(); } + + let board = this; + window.setInterval(function(){ + board.updateScreen(); + }, 30); } public getView(): SVGAndSize { @@ -459,6 +464,9 @@ namespace pxsim.visuals { let state = this.board; if (!state || !state.screenState) return; + if (!state.screenState.shouldUpdate) return; + state.screenState.shouldUpdate = false; + this.screenCanvasData = this.screenCanvasCtx.getImageData(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); Object.keys(state.screenState.points).forEach(xStr => {