From 7ad3343e9cab099c179408e1492170bae5bcca72 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 22 Apr 2020 01:46:07 +0900 Subject: [PATCH] graphicsdriver/monogame: Implement Pixels Updates #1078 --- internal/graphicsdriver/monogame/graphics.go | 4 ++++ internal/graphicsdriver/monogame/image.go | 6 +++--- internal/monogame/monogame.go | 9 ++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/internal/graphicsdriver/monogame/graphics.go b/internal/graphicsdriver/monogame/graphics.go index 035246448..ffdc3c340 100644 --- a/internal/graphicsdriver/monogame/graphics.go +++ b/internal/graphicsdriver/monogame/graphics.go @@ -124,6 +124,10 @@ func (s *screen) SetAsSource() { panic("monogame: SetAsSource on screen is forbidden") } +func (s *screen) Pixels(width, height int) ([]byte, error) { + panic("monogame: Pixels on screen is forbidden") +} + func (s *screen) ReplacePixels(args []*driver.ReplacePixelsArgs) { panic("monogame: ReplacePixels on screen is forbidden") } diff --git a/internal/graphicsdriver/monogame/image.go b/internal/graphicsdriver/monogame/image.go index cab5530df..9cb4939ab 100644 --- a/internal/graphicsdriver/monogame/image.go +++ b/internal/graphicsdriver/monogame/image.go @@ -24,6 +24,7 @@ import ( type RenderTarget2D interface { SetAsDestination(viewportWidth, viewportHeight int) SetAsSource() + Pixels(width, height int) ([]byte, error) ReplacePixels(args []*driver.ReplacePixelsArgs) Dispose() IsScreen() bool @@ -44,9 +45,8 @@ func (*Image) IsInvalidated() bool { return false } -func (*Image) Pixels() ([]byte, error) { - panic("monogame: Pixels is not implemented yet") - return nil, nil +func (i *Image) Pixels() ([]byte, error) { + return i.v.Pixels(i.width, i.height) } func (i *Image) SetAsDestination() { diff --git a/internal/monogame/monogame.go b/internal/monogame/monogame.go index a1c4c9f00..c80cd7de6 100644 --- a/internal/monogame/monogame.go +++ b/internal/monogame/monogame.go @@ -126,7 +126,14 @@ type RenderTarget2D struct { func (r *RenderTarget2D) Dispose() { runtime.SetFinalizer(r, nil) - r.v.Call("Dispose") + 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 } func (r *RenderTarget2D) ReplacePixels(args []*driver.ReplacePixelsArgs) {