mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-02 14:04:28 +01:00
parent
eabe4152a7
commit
6236ba1f00
68
internal/monogame/monogame.go
Normal file
68
internal/monogame/monogame.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
// 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 (
|
||||||
|
"runtime"
|
||||||
|
"syscall/js"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: Update this
|
||||||
|
const temporaryNamespace = "Go2DotNet.Example.Rotate"
|
||||||
|
|
||||||
|
type UpdateDrawer interface {
|
||||||
|
Update() error
|
||||||
|
Draw() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type Game struct {
|
||||||
|
v js.Value
|
||||||
|
update js.Func
|
||||||
|
draw js.Func
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Run() {
|
||||||
|
// Methods named *FromGo is defined to avoid ambiguous matches of methods.
|
||||||
|
g.v.Call("RunFromGo")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Dispose() {
|
||||||
|
runtime.SetFinalizer(g, nil)
|
||||||
|
g.update.Release()
|
||||||
|
g.draw.Release()
|
||||||
|
}
|
||||||
|
|
||||||
|
type RenderTarget2D js.Value
|
||||||
|
|
||||||
|
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()
|
||||||
|
})
|
||||||
|
|
||||||
|
v := js.Global().Get(".net").Get(temporaryNamespace+".GoGame").New(update, draw)
|
||||||
|
g := &Game{
|
||||||
|
v: v,
|
||||||
|
update: update,
|
||||||
|
draw: draw,
|
||||||
|
}
|
||||||
|
runtime.SetFinalizer(g, (*Game).Dispose)
|
||||||
|
return g
|
||||||
|
}
|
@ -17,16 +17,13 @@
|
|||||||
package monogame
|
package monogame
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"syscall/js"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/driver"
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/monogame"
|
graphics "github.com/hajimehoshi/ebiten/internal/graphicsdriver/monogame"
|
||||||
|
"github.com/hajimehoshi/ebiten/internal/monogame"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: Update this
|
|
||||||
const temporaryNamespace = "Go2DotNet.Example.Rotate"
|
|
||||||
|
|
||||||
type UI struct {
|
type UI struct {
|
||||||
|
game *monogame.Game
|
||||||
}
|
}
|
||||||
|
|
||||||
var theUI = &UI{}
|
var theUI = &UI{}
|
||||||
@ -36,17 +33,11 @@ func Get() *UI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (*UI) Run(context driver.UIContext) error {
|
func (*UI) Run(context driver.UIContext) error {
|
||||||
update := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
g := monogame.NewGame(context)
|
||||||
return context.Update()
|
defer g.Dispose()
|
||||||
})
|
|
||||||
defer update.Release()
|
|
||||||
|
|
||||||
draw := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
theUI.game = g
|
||||||
return context.Draw()
|
g.Run()
|
||||||
})
|
|
||||||
defer draw.Release()
|
|
||||||
|
|
||||||
js.Global().Get(".net").Get(temporaryNamespace+".GoGameRunner").Call("Run", update, draw)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,5 +113,5 @@ func (*UI) Window() driver.Window {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (*UI) Graphics() driver.Graphics {
|
func (*UI) Graphics() driver.Graphics {
|
||||||
return monogame.Get()
|
return graphics.Get()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user