From 9db042ae9e420f88e8668a186f8170586081dcae Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 28 Feb 2018 22:46:57 +0900 Subject: [PATCH] graphics: Refactoring: easier way to clear framebuffer --- graphicscontext.go | 4 ++-- image.go | 4 ++-- internal/graphics/image.go | 4 ++++ internal/restorable/image.go | 24 +++++++----------------- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/graphicscontext.go b/graphicscontext.go index 7799237ee..5597a41df 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -57,8 +57,8 @@ func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale flo w := int(float64(screenWidth) * screenScale) h := int(float64(screenHeight) * screenScale) - px0, py0, px1, py1 := ui.ScreenPadding() - c.screen = newImageWithScreenFramebuffer(w, h, px0, py0, px1, py1) + px0, py0, _, _ := ui.ScreenPadding() + c.screen = newImageWithScreenFramebuffer(w, h) _ = c.screen.Clear() c.offscreen = offscreen diff --git a/image.go b/image.go index 169374722..76a884d46 100644 --- a/image.go +++ b/image.go @@ -421,9 +421,9 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) { return i, nil } -func newImageWithScreenFramebuffer(width, height int, paddingX0, paddingY0, paddingX1, paddingY1 float64) *Image { +func newImageWithScreenFramebuffer(width, height int) *Image { checkSize(width, height) - r := restorable.NewScreenFramebufferImage(width, height, paddingX0, paddingY0, paddingX1, paddingY1) + r := restorable.NewScreenFramebufferImage(width, height) i := &Image{ restorable: r, filter: FilterDefault, diff --git a/internal/graphics/image.go b/internal/graphics/image.go index 7add0f7d2..e6bb9a72f 100644 --- a/internal/graphics/image.go +++ b/internal/graphics/image.go @@ -111,3 +111,7 @@ func (i *Image) createFramebufferIfNeeded() (*framebuffer, error) { i.framebuffer = f return i.framebuffer, nil } + +func (i *Image) ViewportSize() (int, int) { + return i.framebuffer.viewportSize() +} diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 9c67b2574..38ae12e4a 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -17,7 +17,6 @@ package restorable import ( "errors" "image/color" - "math" "runtime" "github.com/hajimehoshi/ebiten/internal/affine" @@ -74,11 +73,6 @@ type Image struct { // screen indicates whether the image is used as an actual screen. screen bool - - paddingX0 float64 - paddingY0 float64 - paddingX1 float64 - paddingY1 float64 } // NewImage creates an empty image with the given size. @@ -93,15 +87,11 @@ func NewImage(width, height int, volatile bool) *Image { } // NewScreenFramebufferImage creates a special image that framebuffer is one for the screen. -func NewScreenFramebufferImage(width, height int, paddingX0, paddingY0, paddingX1, paddingY1 float64) *Image { +func NewScreenFramebufferImage(width, height int) *Image { i := &Image{ - image: graphics.NewScreenFramebufferImage(width, height), - volatile: true, - screen: true, - paddingX0: paddingX0, - paddingY0: paddingY0, - paddingX1: paddingX1, - paddingY1: paddingY1, + image: graphics.NewScreenFramebufferImage(width, height), + volatile: true, + screen: true, } theImages.add(i) runtime.SetFinalizer(i, (*Image).Dispose) @@ -146,11 +136,11 @@ func (i *Image) clearIfVolatile() { panic("not reached") } - w, h := i.image.Size() + w, h := i.image.ViewportSize() x0 := float32(0) y0 := float32(0) - x1 := float32(w + int(math.Ceil(i.paddingX0+i.paddingX1))) - y1 := float32(h + int(math.Ceil(i.paddingY0+i.paddingY1))) + x1 := float32(w) + y1 := float32(h) // For the rule of values, see vertices.go. clearVertices := []float32{ x0, y0, 0, 0, 1, 1,