graphicsdriver/monogame: Create a RenderTarget2D for an image

Updates #1078
This commit is contained in:
Hajime Hoshi 2020-04-04 22:58:43 +09:00
parent 6236ba1f00
commit 8809076682
3 changed files with 50 additions and 20 deletions

View File

@ -19,11 +19,11 @@ package monogame
import ( import (
"github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/monogame"
"github.com/hajimehoshi/ebiten/internal/thread" "github.com/hajimehoshi/ebiten/internal/thread"
) )
type Graphics struct { type Graphics struct {
t *thread.Thread
} }
var theGraphics Graphics var theGraphics Graphics
@ -33,7 +33,7 @@ func Get() *Graphics {
} }
func (g *Graphics) SetThread(thread *thread.Thread) { func (g *Graphics) SetThread(thread *thread.Thread) {
g.t = thread panic("monogame: SetThread is not implemented yet")
} }
func (g *Graphics) Begin() { func (g *Graphics) Begin() {
@ -49,7 +49,9 @@ func (g *Graphics) SetVertices(vertices []float32, indices []uint16) {
} }
func (g *Graphics) NewImage(width, height int) (driver.Image, error) { func (g *Graphics) NewImage(width, height int) (driver.Image, error) {
v := monogame.CurrentGame().NewRenderTarget2D(width, height)
return &Image{ return &Image{
v: v,
width: width, width: width,
height: height, height: height,
}, nil }, nil

View File

@ -18,14 +18,17 @@ package monogame
import ( import (
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/monogame"
) )
type Image struct { type Image struct {
v *monogame.RenderTarget2D
width int width int
height int height int
} }
func (*Image) Dispose() { func (i *Image) Dispose() {
i.v.Dispose()
} }
func (*Image) IsInvalidated() bool { func (*Image) IsInvalidated() bool {

View File

@ -21,6 +21,9 @@ import (
"syscall/js" "syscall/js"
) )
// TODO: This implementation depends on some C# files that are not uploaded yet.
// Create 'ebitenmonogame' command to generate C# project for the MonoGame.
// TODO: Update this // TODO: Update this
const temporaryNamespace = "Go2DotNet.Example.Rotate" const temporaryNamespace = "Go2DotNet.Example.Rotate"
@ -30,24 +33,17 @@ type UpdateDrawer interface {
} }
type Game struct { type Game struct {
v js.Value binding js.Value
update js.Func update js.Func
draw js.Func draw js.Func
} }
func (g *Game) Run() { var currentGame *Game
// Methods named *FromGo is defined to avoid ambiguous matches of methods.
g.v.Call("RunFromGo")
}
func (g *Game) Dispose() { func CurrentGame() *Game {
runtime.SetFinalizer(g, nil) return currentGame
g.update.Release()
g.draw.Release()
} }
type RenderTarget2D js.Value
func NewGame(ud UpdateDrawer) *Game { func NewGame(ud UpdateDrawer) *Game {
update := js.FuncOf(func(this js.Value, args []js.Value) interface{} { update := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return ud.Update() return ud.Update()
@ -57,12 +53,41 @@ func NewGame(ud UpdateDrawer) *Game {
return ud.Draw() return ud.Draw()
}) })
v := js.Global().Get(".net").Get(temporaryNamespace+".GoGame").New(update, draw) v := js.Global().Get(".net").Get(temporaryNamespace+".GoBinding").New(update, draw)
g := &Game{ g := &Game{
v: v, binding: v,
update: update, update: update,
draw: draw, draw: draw,
} }
runtime.SetFinalizer(g, (*Game).Dispose) runtime.SetFinalizer(g, (*Game).Dispose)
currentGame = g
return g return g
} }
func (g *Game) Dispose() {
runtime.SetFinalizer(g, nil)
g.update.Release()
g.draw.Release()
currentGame = nil
}
func (g *Game) Run() {
g.binding.Call("Run")
}
func (g *Game) NewRenderTarget2D(width, height int) *RenderTarget2D {
r := &RenderTarget2D{
v: g.binding.Call("NewRenderTarget2D", width, height),
}
runtime.SetFinalizer(r, (*RenderTarget2D).Dispose)
return r
}
type RenderTarget2D struct {
v js.Value
}
func (r *RenderTarget2D) Dispose() {
runtime.SetFinalizer(r, nil)
r.v.Call("Dispose")
}