Skip to content

Commit 4f9933f

Browse files
committed
Moved screen update code into Go thanks to tip
1 parent 3f2569e commit 4f9933f

File tree

5 files changed

+9
-14
lines changed

5 files changed

+9
-14
lines changed

static/gomeboycolor/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,7 @@ <h5 class="modal-title" id="controlsModalLabel">Controls</h5>
470470
}
471471

472472
function updateCanvas(screenData) {
473-
var decodedData = new Uint8ClampedArray(screenData);
474-
var imageData = new ImageData(decodedData, 160, 144);
473+
var imageData = new ImageData(screenData, 160, 144);
475474
context.putImageData(imageData, canvas.width / 4, 4);
476475
}
477476

static/gomeboycolor/js/worker.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,7 @@ if (!WebAssembly.instantiateStreaming) { // polyfill
66
}
77

88
importScripts('wasm_exec.js');
9-
importScripts('base64js.min.js');
109
const GOMEBOY_COLOR_WASM = "../wasm/gbc.wasm";
11-
const SCREEN_UPDATE = "screen-update";
12-
13-
// uses transferable on post message
14-
function sendScreenUpdate(bs64) {
15-
var buf = new Uint8ClampedArray(base64js.toByteArray(bs64)).buffer;
16-
postMessage([SCREEN_UPDATE, buf], [buf]);
17-
}
1810

1911
const go = new Go();
2012
let mod, inst;

static/gomeboycolor/wasm/gbc.wasm

1.45 KB
Binary file not shown.

web_io.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package main
44

55
import (
6-
"encoding/base64"
76
"log"
87

98
"syscall/js"
@@ -128,6 +127,6 @@ func (s *html5CanvasDisplay) DrawFrame(screenData *types.Screen) {
128127

129128
// TODO this is probably a performance bottleneck
130129
if !s.headless {
131-
webworker.SendScreenUpdate(base64.StdEncoding.EncodeToString(s.imageData))
130+
webworker.SendScreenUpdate(s.imageData)
132131
}
133132
}

webworker/messages.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ func SendSaveState(gameId, state string) {
3535
postMessage("save-state", []interface{}{gameId, state})
3636
}
3737

38-
func SendScreenUpdate(screenData string) {
39-
js.Global().Call("sendScreenUpdate", screenData)
38+
func SendScreenUpdate(screenData []uint8) {
39+
typedArray := js.TypedArrayOf(screenData)
40+
defer typedArray.Release()
41+
42+
clamped := js.Global().Get("Uint8ClampedArray").New(typedArray)
43+
transferable := js.Global().Get("Array").New(clamped).Get("buffer")
44+
js.Global().Call("postMessage", []interface{}{"screen-update", clamped}, transferable)
4045
}
4146

4247
func SendFrameRate(rate float32) {

0 commit comments

Comments
 (0)