monogame: Refactoring

This commit is contained in:
Hajime Hoshi 2020-04-05 19:19:28 +09:00
parent 3a5f0a7a95
commit 08ac91fb50
4 changed files with 27 additions and 20 deletions

View File

@ -24,10 +24,7 @@ import (
) )
type Graphics struct { type Graphics struct {
dst *Image game *monogame.Game
src *Image
vertices []float32
indices []uint16
} }
var theGraphics Graphics var theGraphics Graphics
@ -36,6 +33,10 @@ func Get() *Graphics {
return &theGraphics return &theGraphics
} }
func (g *Graphics) SetGame(game *monogame.Game) {
g.game = game
}
func (g *Graphics) SetThread(thread *thread.Thread) { func (g *Graphics) SetThread(thread *thread.Thread) {
panic("monogame: SetThread is not implemented") panic("monogame: SetThread is not implemented")
} }
@ -53,12 +54,11 @@ func (g *Graphics) SetTransparent(transparent bool) {
} }
func (g *Graphics) SetVertices(vertices []float32, indices []uint16) { func (g *Graphics) SetVertices(vertices []float32, indices []uint16) {
g.vertices = vertices g.game.SetVertices(vertices, indices)
g.indices = indices
} }
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) v := g.game.NewRenderTarget2D(width, height)
return &Image{ return &Image{
v: v, v: v,
g: g, g: g,

View File

@ -42,11 +42,11 @@ func (*Image) Pixels() ([]byte, error) {
} }
func (i *Image) SetAsDestination() { func (i *Image) SetAsDestination() {
i.g.dst = i i.v.SetAsDestination()
} }
func (i *Image) SetAsSource() { func (i *Image) SetAsSource() {
i.g.src = i i.v.SetAsSource()
} }
func (i *Image) ReplacePixels(args []*driver.ReplacePixelsArgs) { func (i *Image) ReplacePixels(args []*driver.ReplacePixelsArgs) {

View File

@ -40,12 +40,6 @@ type Game struct {
draw js.Func draw js.Func
} }
var currentGame *Game
func CurrentGame() *Game {
return currentGame
}
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()
@ -62,7 +56,6 @@ func NewGame(ud UpdateDrawer) *Game {
draw: draw, draw: draw,
} }
runtime.SetFinalizer(g, (*Game).Dispose) runtime.SetFinalizer(g, (*Game).Dispose)
currentGame = g
return g return g
} }
@ -70,7 +63,6 @@ func (g *Game) Dispose() {
runtime.SetFinalizer(g, nil) runtime.SetFinalizer(g, nil)
g.update.Release() g.update.Release()
g.draw.Release() g.draw.Release()
currentGame = nil
} }
func (g *Game) Run() { func (g *Game) Run() {
@ -80,14 +72,20 @@ func (g *Game) Run() {
func (g *Game) NewRenderTarget2D(width, height int) *RenderTarget2D { func (g *Game) NewRenderTarget2D(width, height int) *RenderTarget2D {
v := g.binding.Call("NewRenderTarget2D", width, height) v := g.binding.Call("NewRenderTarget2D", width, height)
r := &RenderTarget2D{ r := &RenderTarget2D{
v: v, v: v,
binding: g.binding,
} }
runtime.SetFinalizer(r, (*RenderTarget2D).Dispose) runtime.SetFinalizer(r, (*RenderTarget2D).Dispose)
return r return r
} }
func (g *Game) SetVertices(vertices []float32, indices []uint16) {
// TODO: Implement this
}
type RenderTarget2D struct { type RenderTarget2D struct {
v js.Value v js.Value
binding js.Value
} }
func (r *RenderTarget2D) Dispose() { func (r *RenderTarget2D) Dispose() {
@ -99,6 +97,14 @@ func (r *RenderTarget2D) ReplacePixels(args []*driver.ReplacePixelsArgs) {
for _, a := range args { for _, a := range args {
arr := js.Global().Get("Uint8Array").New(len(a.Pixels)) arr := js.Global().Get("Uint8Array").New(len(a.Pixels))
js.CopyBytesToJS(arr, a.Pixels) js.CopyBytesToJS(arr, a.Pixels)
r.v.Call("ReplacePixels", arr, a.X, a.Y, a.Width, a.Height) r.binding.Call("ReplacePixels", r.v, arr, a.X, a.Y, a.Width, a.Height)
} }
} }
func (r *RenderTarget2D) SetAsDestination() {
r.binding.Set("Dst", r.v)
}
func (r *RenderTarget2D) SetAsSource() {
r.binding.Set("Src", r.v)
}

View File

@ -37,6 +37,7 @@ func (*UI) Run(context driver.UIContext) error {
defer g.Dispose() defer g.Dispose()
theUI.game = g theUI.game = g
theUI.Graphics().(*graphics.Graphics).SetGame(g)
g.Run() g.Run()
return nil return nil
} }