Permalink
Browse files

Moved screen update code into Go thanks to tip

  • Loading branch information...
djhworld committed Sep 22, 2018
1 parent 3f2569e commit 4f9933fd0fab6d9f310776d89f8c296e72af1c9a
@@ -470,8 +470,7 @@ <h5 class="modal-title" id="controlsModalLabel">Controls</h5>
}
function updateCanvas(screenData) {
var decodedData = new Uint8ClampedArray(screenData);
var imageData = new ImageData(decodedData, 160, 144);
var imageData = new ImageData(screenData, 160, 144);
context.putImageData(imageData, canvas.width / 4, 4);
}
@@ -6,15 +6,7 @@ if (!WebAssembly.instantiateStreaming) { // polyfill
}

importScripts('wasm_exec.js');
importScripts('base64js.min.js');
const GOMEBOY_COLOR_WASM = "../wasm/gbc.wasm";
const SCREEN_UPDATE = "screen-update";

// uses transferable on post message
function sendScreenUpdate(bs64) {
var buf = new Uint8ClampedArray(base64js.toByteArray(bs64)).buffer;
postMessage([SCREEN_UPDATE, buf], [buf]);
}

const go = new Go();
let mod, inst;
BIN +1.45 KB (100%) static/gomeboycolor/wasm/gbc.wasm
Binary file not shown.
@@ -3,7 +3,6 @@
package main

import (
"encoding/base64"
"log"

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

// TODO this is probably a performance bottleneck
if !s.headless {
webworker.SendScreenUpdate(base64.StdEncoding.EncodeToString(s.imageData))
webworker.SendScreenUpdate(s.imageData)
}
}
@@ -35,8 +35,13 @@ func SendSaveState(gameId, state string) {
postMessage("save-state", []interface{}{gameId, state})
}

func SendScreenUpdate(screenData string) {
js.Global().Call("sendScreenUpdate", screenData)
func SendScreenUpdate(screenData []uint8) {
typedArray := js.TypedArrayOf(screenData)
defer typedArray.Release()

clamped := js.Global().Get("Uint8ClampedArray").New(typedArray)
transferable := js.Global().Get("Array").New(clamped).Get("buffer")
js.Global().Call("postMessage", []interface{}{"screen-update", clamped}, transferable)
}

func SendFrameRate(rate float32) {

0 comments on commit 4f9933f

Please sign in to comment.