From 58f5c9c1d08729505b4bd45be9f49f86f973316d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 24 Feb 2018 23:51:05 +0900 Subject: [PATCH] graphics: Refactoring: clearing framebuffer --- graphicscontext.go | 5 ++++- image.go | 4 ++-- internal/restorable/image.go | 25 ++++++++++--------------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/graphicscontext.go b/graphicscontext.go index 7799237ee..e316466a3 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -17,6 +17,7 @@ package ebiten import ( "github.com/hajimehoshi/ebiten/internal/clock" "github.com/hajimehoshi/ebiten/internal/hooks" + "github.com/hajimehoshi/ebiten/internal/math" "github.com/hajimehoshi/ebiten/internal/restorable" "github.com/hajimehoshi/ebiten/internal/ui" "github.com/hajimehoshi/ebiten/internal/web" @@ -58,7 +59,9 @@ 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) + fw := math.NextPowerOf2Int(w + int(px0+px1)) + fh := math.NextPowerOf2Int(h + int(py0+py1)) + c.screen = newImageWithScreenFramebuffer(w, h, fw, fh) _ = c.screen.Clear() c.offscreen = offscreen diff --git a/image.go b/image.go index 43052df3e..ce174669c 100644 --- a/image.go +++ b/image.go @@ -382,9 +382,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, framebufferWidth, framebufferHeight int) *Image { checkSize(width, height) - r := restorable.NewScreenFramebufferImage(width, height, paddingX0, paddingY0, paddingX1, paddingY1) + r := restorable.NewScreenFramebufferImage(width, height, framebufferWidth, framebufferHeight) i := &Image{r, FilterDefault} runtime.SetFinalizer(i, (*Image).Dispose) return i diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 89cd6cb9d..c8f28dd83 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -80,10 +80,8 @@ type Image struct { // screen indicates whether the image is used as an actual screen. screen bool - paddingX0 float64 - paddingY0 float64 - paddingX1 float64 - paddingY1 float64 + framebufferWidth int + framebufferHeight int } // NewImage creates an empty image with the given size. @@ -117,15 +115,13 @@ func NewImageFromImage(source image.Image) *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, framebufferWidth, framebufferHeight 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, + framebufferWidth: framebufferWidth, + framebufferHeight: framebufferHeight, } theImages.add(i) runtime.SetFinalizer(i, (*Image).Dispose) @@ -170,11 +166,10 @@ func (i *Image) clearIfVolatile() { panic("not reached") } - w, h := i.image.Size() x0 := float32(0) y0 := float32(0) - x1 := float32(w) + float32(i.paddingX0+i.paddingX1) - y1 := float32(h) + float32(i.paddingY0+i.paddingY1) + x1 := float32(i.framebufferWidth) + y1 := float32(i.framebufferHeight) // For the rule of values, see vertices.go. clearVertices := []float32{ x0, y0, 0, 0, 1, 1,