2020-04-04 11:15:39 +02:00
|
|
|
// Copyright 2020 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 monogame
|
|
|
|
|
|
|
|
import (
|
2020-04-05 16:26:41 +02:00
|
|
|
"reflect"
|
2020-04-04 11:15:39 +02:00
|
|
|
"runtime"
|
|
|
|
"syscall/js"
|
2020-04-05 16:26:41 +02:00
|
|
|
"unsafe"
|
2020-04-04 21:34:47 +02:00
|
|
|
|
2020-04-05 17:13:04 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
2020-04-04 21:34:47 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2020-04-04 11:15:39 +02:00
|
|
|
)
|
|
|
|
|
2020-04-04 15:58:43 +02:00
|
|
|
// TODO: This implementation depends on some C# files that are not uploaded yet.
|
|
|
|
// Create 'ebitenmonogame' command to generate C# project for the MonoGame.
|
|
|
|
|
2020-04-04 11:15:39 +02:00
|
|
|
// TODO: Update this
|
2020-04-14 17:58:14 +02:00
|
|
|
const temporaryNamespace = "Go2DotNet.Example.Ebiten"
|
2020-04-04 11:15:39 +02:00
|
|
|
|
|
|
|
type UpdateDrawer interface {
|
|
|
|
Update() error
|
|
|
|
Draw() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Game struct {
|
2020-04-04 15:58:43 +02:00
|
|
|
binding js.Value
|
|
|
|
update js.Func
|
|
|
|
draw js.Func
|
2020-04-04 11:15:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGame(ud UpdateDrawer) *Game {
|
|
|
|
update := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
|
|
return ud.Update()
|
|
|
|
})
|
|
|
|
|
|
|
|
draw := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
|
|
return ud.Draw()
|
|
|
|
})
|
|
|
|
|
2020-04-04 21:34:47 +02:00
|
|
|
v := js.Global().Get(".net").Get(temporaryNamespace+".GameGoBinding").New(update, draw)
|
2020-04-04 11:15:39 +02:00
|
|
|
g := &Game{
|
2020-04-04 15:58:43 +02:00
|
|
|
binding: v,
|
|
|
|
update: update,
|
|
|
|
draw: draw,
|
2020-04-04 11:15:39 +02:00
|
|
|
}
|
|
|
|
runtime.SetFinalizer(g, (*Game).Dispose)
|
|
|
|
return g
|
|
|
|
}
|
2020-04-04 15:58:43 +02:00
|
|
|
|
|
|
|
func (g *Game) Dispose() {
|
|
|
|
runtime.SetFinalizer(g, nil)
|
|
|
|
g.update.Release()
|
|
|
|
g.draw.Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Run() {
|
|
|
|
g.binding.Call("Run")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) NewRenderTarget2D(width, height int) *RenderTarget2D {
|
2020-04-04 21:34:47 +02:00
|
|
|
v := g.binding.Call("NewRenderTarget2D", width, height)
|
2020-04-04 15:58:43 +02:00
|
|
|
r := &RenderTarget2D{
|
2020-04-05 12:19:28 +02:00
|
|
|
v: v,
|
|
|
|
binding: g.binding,
|
2020-04-04 15:58:43 +02:00
|
|
|
}
|
|
|
|
runtime.SetFinalizer(r, (*RenderTarget2D).Dispose)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-04-05 12:19:28 +02:00
|
|
|
func (g *Game) SetVertices(vertices []float32, indices []uint16) {
|
2020-04-05 16:26:41 +02:00
|
|
|
var vs, is js.Value
|
|
|
|
{
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&vertices))
|
|
|
|
h.Len *= 4
|
|
|
|
h.Cap *= 4
|
|
|
|
bs := *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(vertices)
|
|
|
|
vs = js.Global().Get("Uint8Array").New(len(bs))
|
|
|
|
js.CopyBytesToJS(vs, bs)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&indices))
|
|
|
|
h.Len *= 2
|
|
|
|
h.Cap *= 2
|
|
|
|
bs := *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(indices)
|
|
|
|
is = js.Global().Get("Uint8Array").New(len(bs))
|
|
|
|
js.CopyBytesToJS(is, bs)
|
|
|
|
}
|
|
|
|
g.binding.Call("SetVertices", vs, is)
|
2020-04-05 12:19:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 17:13:04 +02:00
|
|
|
func (g *Game) Draw(indexLen int, indexOffset int, mode driver.CompositeMode, colorM *affine.ColorM, filter driver.Filter, address driver.Address) {
|
2020-04-12 06:32:08 +02:00
|
|
|
src, dst := mode.Operations()
|
|
|
|
g.binding.Call("Draw", indexLen, indexOffset, int(src), int(dst))
|
2020-04-05 17:13:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 19:17:49 +02:00
|
|
|
func (g *Game) ResetDestination(viewportWidth, viewportHeight int) {
|
|
|
|
g.binding.Call("SetDestination", nil, viewportWidth, viewportHeight)
|
2020-04-05 18:51:02 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 15:33:56 +02:00
|
|
|
func (g *Game) IsKeyPressed(key driver.Key) bool {
|
|
|
|
// Pass a string of the key since both driver.Key value and XNA's key value are not reliable.
|
|
|
|
return g.binding.Call("IsKeyPressed", key.String()).Bool()
|
|
|
|
}
|
|
|
|
|
2020-04-04 15:58:43 +02:00
|
|
|
type RenderTarget2D struct {
|
2020-04-05 12:19:28 +02:00
|
|
|
v js.Value
|
|
|
|
binding js.Value
|
2020-04-04 15:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RenderTarget2D) Dispose() {
|
|
|
|
runtime.SetFinalizer(r, nil)
|
2020-04-21 18:46:07 +02:00
|
|
|
r.binding.Call("Dispose", r.v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RenderTarget2D) Pixels(width, height int) ([]byte, error) {
|
|
|
|
v := r.binding.Call("Pixels", r.v, width, height)
|
|
|
|
bs := make([]byte, v.Length())
|
|
|
|
js.CopyBytesToGo(bs, v)
|
|
|
|
return bs, nil
|
2020-04-04 15:58:43 +02:00
|
|
|
}
|
2020-04-04 21:34:47 +02:00
|
|
|
|
|
|
|
func (r *RenderTarget2D) ReplacePixels(args []*driver.ReplacePixelsArgs) {
|
|
|
|
for _, a := range args {
|
|
|
|
arr := js.Global().Get("Uint8Array").New(len(a.Pixels))
|
|
|
|
js.CopyBytesToJS(arr, a.Pixels)
|
2020-04-05 12:19:28 +02:00
|
|
|
r.binding.Call("ReplacePixels", r.v, arr, a.X, a.Y, a.Width, a.Height)
|
2020-04-04 21:34:47 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 12:19:28 +02:00
|
|
|
|
2020-04-05 19:17:49 +02:00
|
|
|
func (r *RenderTarget2D) SetAsDestination(viewportWidth, viewportHeight int) {
|
|
|
|
r.binding.Call("SetDestination", r.v, viewportWidth, viewportHeight)
|
2020-04-05 12:19:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RenderTarget2D) SetAsSource() {
|
2020-04-05 19:17:49 +02:00
|
|
|
r.binding.Call("SetSource", r.v)
|
2020-04-05 12:19:28 +02:00
|
|
|
}
|
2020-04-12 06:32:08 +02:00
|
|
|
|
|
|
|
func (r *RenderTarget2D) IsScreen() bool {
|
|
|
|
return false
|
|
|
|
}
|