mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
Add jsutil package for new API of Go 1.13
Audio part still cannot be compiled due to Oto. Fixes #878
This commit is contained in:
parent
a19bf4214b
commit
c52e043006
@ -26,6 +26,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/audio"
|
||||
"github.com/hajimehoshi/ebiten/internal/jsutil"
|
||||
)
|
||||
|
||||
// TODO: This just uses decodeAudioData, that can treat audio files other than MP3.
|
||||
@ -174,9 +175,9 @@ func init() {
|
||||
|
||||
func float32ArrayToSlice(arr js.Value) []float32 {
|
||||
f := make([]float32, arr.Length())
|
||||
a := js.TypedArrayOf(f)
|
||||
a, free := jsutil.SliceToTypedArray(f)
|
||||
a.Call("set", arr)
|
||||
a.Release()
|
||||
free()
|
||||
return f
|
||||
}
|
||||
|
||||
@ -195,7 +196,7 @@ func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
|
||||
// TODO: 1 is a correct second argument?
|
||||
oc := offlineAudioContextClass.New(2, 1, context.SampleRate())
|
||||
|
||||
u8 := js.TypedArrayOf(buf)
|
||||
u8, free := jsutil.SliceToTypedArray(buf)
|
||||
a := u8.Get("buffer").Call("slice", u8.Get("byteOffset"), u8.Get("byteOffset").Int()+u8.Get("byteLength").Int())
|
||||
|
||||
succeeded := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
@ -229,7 +230,7 @@ func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
|
||||
defer failed.Release()
|
||||
|
||||
oc.Call("decodeAudioData", a, succeeded, failed)
|
||||
u8.Release()
|
||||
free()
|
||||
|
||||
timeout := time.Duration(math.Pow(2, float64(try))) * time.Second
|
||||
t := time.NewTimer(timeout)
|
||||
|
@ -18,6 +18,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/jsutil"
|
||||
)
|
||||
|
||||
var flatten = js.Global().Get("window").Call("eval", `(function(arr) {
|
||||
@ -117,9 +119,9 @@ func DecodeVorbis(buf []byte) (*Samples, int, int, error) {
|
||||
}
|
||||
|
||||
s := make([]float32, flattened.Length())
|
||||
arr := js.TypedArrayOf(s)
|
||||
arr, free := jsutil.SliceToTypedArray(s)
|
||||
arr.Call("set", flattened)
|
||||
arr.Release()
|
||||
free()
|
||||
|
||||
samples.samples = append(samples.samples, s)
|
||||
samples.lengthInSamples += int64(len(s)) / int64(samples.channels)
|
||||
@ -127,9 +129,9 @@ func DecodeVorbis(buf []byte) (*Samples, int, int, error) {
|
||||
})
|
||||
defer f.Release()
|
||||
|
||||
arr := js.TypedArrayOf(buf)
|
||||
arr, free := jsutil.SliceToTypedArray(buf)
|
||||
js.Global().Get("stbvorbis").Call("decode", arr, f)
|
||||
arr.Release()
|
||||
free()
|
||||
|
||||
if err := <-ch; err != nil {
|
||||
return nil, 0, 0, err
|
||||
|
@ -21,6 +21,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/jsutil"
|
||||
)
|
||||
|
||||
type file struct {
|
||||
@ -63,11 +65,6 @@ func OpenFile(path string) (ReadSeekCloser, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
uint8contentWrapper := js.Global().Get("Uint8Array").New(content)
|
||||
data := make([]byte, uint8contentWrapper.Get("byteLength").Int())
|
||||
arr := js.TypedArrayOf(data)
|
||||
arr.Call("set", uint8contentWrapper)
|
||||
arr.Release()
|
||||
f := &file{bytes.NewReader(data)}
|
||||
f := &file{bytes.NewReader(jsutil.ArrayBufferToSlice(content))}
|
||||
return f, nil
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import (
|
||||
"syscall/js"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||
"github.com/hajimehoshi/ebiten/internal/jsutil"
|
||||
"github.com/hajimehoshi/ebiten/internal/web"
|
||||
)
|
||||
|
||||
@ -189,11 +190,10 @@ func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte,
|
||||
|
||||
c.bindFramebuffer(f.native)
|
||||
|
||||
pixels := make([]byte, 4*width*height)
|
||||
p := js.TypedArrayOf(pixels)
|
||||
p := js.Global().Get("Uint8Array").New(4 * width * height)
|
||||
gl.Call("readPixels", 0, 0, width, height, rgba, unsignedByte, p)
|
||||
p.Release()
|
||||
return pixels, nil
|
||||
|
||||
return jsutil.Uint8ArrayToSlice(p), nil
|
||||
}
|
||||
|
||||
func (c *context) bindTextureImpl(t textureNative) {
|
||||
@ -227,9 +227,9 @@ func (c *context) texSubImage2D(t textureNative, pixels []byte, x, y, width, hei
|
||||
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
|
||||
// GLsizei width, GLsizei height,
|
||||
// GLenum format, GLenum type, ArrayBufferView? pixels);
|
||||
p := js.TypedArrayOf(pixels)
|
||||
p, free := jsutil.SliceToTypedArray(pixels)
|
||||
gl.Call("texSubImage2D", texture2d, 0, x, y, width, height, rgba, unsignedByte, p)
|
||||
p.Release()
|
||||
free()
|
||||
}
|
||||
|
||||
func (c *context) newFramebuffer(t textureNative) (framebufferNative, error) {
|
||||
@ -367,9 +367,9 @@ func (c *context) uniformFloats(p program, location string, v []float32) {
|
||||
case 4:
|
||||
gl.Call("uniform4f", js.Value(l), v[0], v[1], v[2], v[3])
|
||||
case 16:
|
||||
arr := js.TypedArrayOf(v)
|
||||
arr, free := jsutil.SliceToTypedArray(v)
|
||||
gl.Call("uniformMatrix4fv", js.Value(l), false, arr)
|
||||
arr.Release()
|
||||
free()
|
||||
default:
|
||||
panic(fmt.Sprintf("opengl: invalid uniform floats num: %d", len(v)))
|
||||
}
|
||||
@ -420,17 +420,17 @@ func (c *context) bindBuffer(bufferType bufferType, b buffer) {
|
||||
func (c *context) arrayBufferSubData(data []float32) {
|
||||
c.ensureGL()
|
||||
gl := c.gl
|
||||
arr := js.TypedArrayOf(data)
|
||||
arr, free := jsutil.SliceToTypedArray(data)
|
||||
gl.Call("bufferSubData", int(arrayBuffer), 0, arr)
|
||||
arr.Release()
|
||||
free()
|
||||
}
|
||||
|
||||
func (c *context) elementArrayBufferSubData(data []uint16) {
|
||||
c.ensureGL()
|
||||
gl := c.gl
|
||||
arr := js.TypedArrayOf(data)
|
||||
arr, free := jsutil.SliceToTypedArray(data)
|
||||
gl.Call("bufferSubData", int(elementArrayBuffer), 0, arr)
|
||||
arr.Release()
|
||||
free()
|
||||
}
|
||||
|
||||
func (c *context) deleteBuffer(b buffer) {
|
||||
|
44
internal/jsutil/go112_js.go
Normal file
44
internal/jsutil/go112_js.go
Normal file
@ -0,0 +1,44 @@
|
||||
// Copyright 2019 The Ebiten Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// +build !go1.13 !wasm
|
||||
|
||||
package jsutil
|
||||
|
||||
import (
|
||||
"syscall/js"
|
||||
)
|
||||
|
||||
func Uint8ArrayToSlice(value js.Value) []byte {
|
||||
// Note that TypedArrayOf cannot work correcly on Wasm.
|
||||
// See https://github.com/golang/go/issues/31980
|
||||
|
||||
s := make([]byte, value.Get("byteLength").Int())
|
||||
a := js.TypedArrayOf(s)
|
||||
a.Call("set", value)
|
||||
a.Release()
|
||||
return s
|
||||
}
|
||||
|
||||
func ArrayBufferToSlice(value js.Value) []byte {
|
||||
return Uint8ArrayToSlice(js.Global().Get("Uint8Array").New(value))
|
||||
}
|
||||
|
||||
func SliceToTypedArray(s interface{}) (js.Value, func()) {
|
||||
// Note that TypedArrayOf cannot work correcly on Wasm.
|
||||
// See https://github.com/golang/go/issues/31980
|
||||
|
||||
a := js.TypedArrayOf(s)
|
||||
return a.Value, func() { a.Release() }
|
||||
}
|
141
internal/jsutil/go113_js.go
Normal file
141
internal/jsutil/go113_js.go
Normal file
@ -0,0 +1,141 @@
|
||||
// Copyright 2019 The Ebiten Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// +build go1.13
|
||||
|
||||
package jsutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"syscall/js"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func Uint8ArrayToSlice(value js.Value) []byte {
|
||||
s := make([]byte, value.Get("byteLength").Int())
|
||||
js.CopyBytesToGo(s, value)
|
||||
return s
|
||||
}
|
||||
|
||||
func ArrayBufferToSlice(value js.Value) []byte {
|
||||
return Uint8ArrayToSlice(js.Global().Get("Uint8Array").New(value))
|
||||
}
|
||||
|
||||
func sliceToByteSlice(s interface{}) []byte {
|
||||
switch s := s.(type) {
|
||||
case []int8:
|
||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||
return *(*[]byte)(unsafe.Pointer(h))
|
||||
case []int16:
|
||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||
h.Len *= 2
|
||||
h.Cap *= 2
|
||||
return *(*[]byte)(unsafe.Pointer(h))
|
||||
case []int32:
|
||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||
h.Len *= 4
|
||||
h.Cap *= 4
|
||||
return *(*[]byte)(unsafe.Pointer(h))
|
||||
case []int64:
|
||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||
h.Len *= 8
|
||||
h.Cap *= 8
|
||||
return *(*[]byte)(unsafe.Pointer(h))
|
||||
case []uint8:
|
||||
return s
|
||||
case []uint16:
|
||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||
h.Len *= 2
|
||||
h.Cap *= 2
|
||||
return *(*[]byte)(unsafe.Pointer(h))
|
||||
case []uint32:
|
||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||
h.Len *= 4
|
||||
h.Cap *= 4
|
||||
return *(*[]byte)(unsafe.Pointer(h))
|
||||
case []uint64:
|
||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||
h.Len *= 8
|
||||
h.Cap *= 8
|
||||
return *(*[]byte)(unsafe.Pointer(h))
|
||||
case []float32:
|
||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||
h.Len *= 4
|
||||
h.Cap *= 4
|
||||
return *(*[]byte)(unsafe.Pointer(h))
|
||||
case []float64:
|
||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||
h.Len *= 8
|
||||
h.Cap *= 8
|
||||
return *(*[]byte)(unsafe.Pointer(h))
|
||||
default:
|
||||
panic(fmt.Sprintf("jsutil: unexpected value at sliceToBytesSlice: %T", s))
|
||||
}
|
||||
}
|
||||
|
||||
func SliceToTypedArray(s interface{}) (js.Value, func()) {
|
||||
switch s := s.(type) {
|
||||
case []int8:
|
||||
a := js.Global().Get("Uint8Array").New(len(s))
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
runtime.KeepAlive(s)
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Int8Array").New(buf, a.Get("byteOffset"), a.Get("byteLength")), func() {}
|
||||
case []int16:
|
||||
a := js.Global().Get("Uint8Array").New(len(s) * 2)
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
runtime.KeepAlive(s)
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Int16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
|
||||
case []int32:
|
||||
a := js.Global().Get("Uint8Array").New(len(s) * 4)
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
runtime.KeepAlive(s)
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Int32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
||||
case []uint8:
|
||||
a := js.Global().Get("Uint8Array").New(len(s))
|
||||
js.CopyBytesToJS(a, s)
|
||||
runtime.KeepAlive(s)
|
||||
return a, func() {}
|
||||
case []uint16:
|
||||
a := js.Global().Get("Uint8Array").New(len(s) * 2)
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
runtime.KeepAlive(s)
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Uint16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
|
||||
case []uint32:
|
||||
a := js.Global().Get("Uint8Array").New(len(s) * 4)
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
runtime.KeepAlive(s)
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Uint32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
||||
case []float32:
|
||||
a := js.Global().Get("Uint8Array").New(len(s) * 4)
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
runtime.KeepAlive(s)
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
||||
case []float64:
|
||||
a := js.Global().Get("Uint8Array").New(len(s) * 8)
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
runtime.KeepAlive(s)
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Float64Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/8), func() {}
|
||||
default:
|
||||
panic(fmt.Sprintf("jsutil: unexpected value at SliceToTypedArray: %T", s))
|
||||
}
|
||||
}
|
94
internal/jsutil/jsutil_test.go
Normal file
94
internal/jsutil/jsutil_test.go
Normal file
@ -0,0 +1,94 @@
|
||||
// Copyright 2019 The Ebiten Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// +build js
|
||||
|
||||
package jsutil_test
|
||||
|
||||
import (
|
||||
"syscall/js"
|
||||
"testing"
|
||||
|
||||
. "github.com/hajimehoshi/ebiten/internal/jsutil"
|
||||
)
|
||||
|
||||
func jsArray(values []float64) js.Value {
|
||||
a := js.Global().Get("Array").New()
|
||||
for _, v := range values {
|
||||
a.Call("push", v)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func TestArrayBufferToSlice(t *testing.T) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
func TestSliceToTypedArray(t *testing.T) {
|
||||
tests := []struct {
|
||||
in interface{}
|
||||
out js.Value
|
||||
}{
|
||||
{
|
||||
in: []int8{1, 2, 3},
|
||||
out: js.Global().Get("Int8Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []int16{1, 2, 3},
|
||||
out: js.Global().Get("Int16Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []int32{1, 2, 3},
|
||||
out: js.Global().Get("Int32Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []uint8{1, 2, 3},
|
||||
out: js.Global().Get("Uint8Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []uint16{1, 2, 3},
|
||||
out: js.Global().Get("Uint16Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []uint32{1, 2, 3},
|
||||
out: js.Global().Get("Uint32Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []float32{1, 2, 3},
|
||||
out: js.Global().Get("Float32Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []float64{1, 2, 3},
|
||||
out: js.Global().Get("Float64Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
got, free := SliceToTypedArray(test.in)
|
||||
defer free()
|
||||
want := test.out
|
||||
if got.Get("constructor") != want.Get("constructor") {
|
||||
t.Errorf("class: got: %s, want: %s", got.Get("constructor").Get("name"), want.Get("constructor").Get("name"))
|
||||
}
|
||||
if got.Length() != want.Length() {
|
||||
t.Errorf("length: got: %d, want: %d", got.Length(), want.Length())
|
||||
}
|
||||
for i := 0; i < got.Length(); i++ {
|
||||
gotv := got.Index(i).Float()
|
||||
wantv := want.Index(i).Float()
|
||||
if gotv != wantv {
|
||||
t.Errorf("[%d]: got: %v, want: %v", i, gotv, wantv)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user